oficina / equipo Public

Clone
git clone https://amadan.net/oficina/equipo

Plain git — no account needed to clone.

Download

Download this file

1package push
2
3import (
4 "errors"
5 "time"
6
7 "gorm.io/gorm"
8 "gorm.io/gorm/clause"
9)
10
11// HoldRecord keeps the platform's retry deadline across passes and sender
12// restarts. The single platform row covers new and already queued pushes.
13type HoldRecord struct {
14 ID string `gorm:"primaryKey"`
15 Until time.Time
16}
17
18func (HoldRecord) TableName() string { return "push_holds" }
19
20func platformHeld(tx *gorm.DB, now time.Time) (bool, error) {
21 var n int64
22 err := tx.Model(&HoldRecord{}).Where("id = ? AND until > ?", "platform", now).Count(&n).Error
23 return n > 0, err
24}
25
26func holdPlatform(tx *gorm.DB, until time.Time) error {
27 return tx.Clauses(clause.OnConflict{
28 Columns: []clause.Column{{Name: "id"}},
29 DoUpdates: clause.Assignments(map[string]any{"until": gorm.Expr("MAX(push_holds.until, excluded.until)")}),
30 }).Create(&HoldRecord{ID: "platform", Until: until}).Error
31}
32
33// Never expose an opener's error: it can include token or payload text.
34var errUnreadableToken = errors.New("push: unreadable token")
35
36// forgetToken only invalidates the token this send used. A renewal after
37// Apple's last-valid timestamp also survives, for every token type.
38// Compare opened tokens in Go; neither tokens nor their hashes enter SQL.
39func (s *Sender) forgetToken(tx *gorm.DB, r OutboxRecord, sent string, apple time.Time) error {
40 var sealed string
41 var registered time.Time
42 if r.Target == TargetActivity {
43 var a ActivityToken
44 err := tx.Where("credential = ? AND task_id = ?", r.Credential, r.TaskID).Take(&a).Error
45 if errors.Is(err, gorm.ErrRecordNotFound) {
46 return nil
47 }
48 if err != nil {
49 return err
50 }
51 sealed, registered = a.Token, a.CreatedAt
52 } else {
53 var reg Registration
54 err := tx.Where("credential = ?", r.Credential).Take(&reg).Error
55 if errors.Is(err, gorm.ErrRecordNotFound) {
56 return nil
57 }
58 if err != nil {
59 return err
60 }
61 sealed, registered = reg.Token, reg.UpdatedAt
62 if r.Target == TargetStart {
63 sealed = reg.StartToken
64 }
65 }
66 current, err := s.Box.open(domainToken, sealed)
67 if err != nil {
68 return errUnreadableToken
69 }
70 if current != sent || !apple.IsZero() && registered.After(apple) {
71 return nil
72 }
73 switch r.Target {
74 case TargetStart:
75 return tx.Model(&Registration{}).Where("credential = ?", r.Credential).Select("start_token").Updates(map[string]any{"start_token": ""}).Error
76 case TargetActivity:
77 return tx.Where("credential = ? AND task_id = ?", r.Credential, r.TaskID).Delete(&ActivityToken{}).Error
78 }
79 return ForgetCredential(tx, r.Credential)
80}
81