| 1 | package chat |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "crypto/sha256" |
| 6 | "encoding/hex" |
| 7 | "encoding/json" |
| 8 | "errors" |
| 9 | "time" |
| 10 | |
| 11 | "amadan.net/oficina/equipo/internal/wire" |
| 12 | "gorm.io/gorm" |
| 13 | ) |
| 14 | |
| 15 | // Notification actions remain off until the operator completes device checks. |
| 16 | func (s *Store) SetNotificationActions(enabled bool) { s.notificationActions.Store(enabled) } |
| 17 | func (s *Store) NotificationActions() bool { return s.notificationActions.Load() } |
| 18 | |
| 19 | type notificationReceipt struct { |
| 20 | Person string `gorm:"primaryKey"` |
| 21 | Credential string `gorm:"primaryKey"` |
| 22 | Operation string `gorm:"primaryKey"` |
| 23 | Target string `gorm:"primaryKey"` |
| 24 | Key string `gorm:"primaryKey"` |
| 25 | Digest string |
| 26 | CardID string |
| 27 | MessageID string |
| 28 | TurnID string |
| 29 | Attempt int64 |
| 30 | ExpiresAt time.Time `gorm:"index:notification_receipt_expiry"` |
| 31 | } |
| 32 | |
| 33 | func (notificationReceipt) TableName() string { return "chat_notification_receipts" } |
| 34 | |
| 35 | func (t *txn) actionReceipt(person, credential, operation, target, key string, input any) (*notificationReceipt, bool, error) { |
| 36 | if !t.s.NotificationActions() || credential == "" { |
| 37 | return nil, false, ErrForbidden |
| 38 | } |
| 39 | if !validKey(key) { |
| 40 | return nil, false, ErrInvalid |
| 41 | } |
| 42 | if err := t.s.pushCurrent(t.db, person, credential); err != nil { |
| 43 | return nil, false, ErrForbidden |
| 44 | } |
| 45 | raw, err := json.Marshal(input) |
| 46 | if err != nil { |
| 47 | return nil, false, err |
| 48 | } |
| 49 | digest := sha256.Sum256(raw) |
| 50 | r := notificationReceipt{Person: person, Credential: credential, Operation: operation, Target: target, Key: key, Digest: hex.EncodeToString(digest[:]), ExpiresAt: t.now().Add(7 * 24 * time.Hour)} |
| 51 | // A bounded batch, plus this exact expired key, avoids unbounded cleanup. |
| 52 | if err = t.db.Exec("DELETE FROM chat_notification_receipts WHERE rowid IN (SELECT rowid FROM chat_notification_receipts WHERE expires_at <= ? LIMIT 100)", t.now()).Error; err != nil { |
| 53 | return nil, false, err |
| 54 | } |
| 55 | query := t.db.Where("person = ? AND credential = ? AND operation = ? AND target = ? AND key = ?", person, credential, operation, target, key) |
| 56 | if err = query.Where("expires_at <= ?", t.now()).Delete(¬ificationReceipt{}).Error; err != nil { |
| 57 | return nil, false, err |
| 58 | } |
| 59 | var prior notificationReceipt |
| 60 | err = t.db.Where("person = ? AND credential = ? AND operation = ? AND target = ? AND key = ?", person, credential, operation, target, key).Take(&prior).Error |
| 61 | if errors.Is(err, gorm.ErrRecordNotFound) { |
| 62 | return &r, false, nil |
| 63 | } |
| 64 | if err != nil { |
| 65 | return nil, false, err |
| 66 | } |
| 67 | if prior.Digest != r.Digest { |
| 68 | return nil, false, ErrConflict |
| 69 | } |
| 70 | return &prior, true, nil |
| 71 | } |
| 72 | |
| 73 | func (t *txn) receiptAnswer(r notificationReceipt) (out wire.Answered, err error) { |
| 74 | var c cardRecord |
| 75 | if err = t.db.Where("id = ? AND person = ?", r.CardID, r.Person).Take(&c).Error; err != nil { |
| 76 | return out, err |
| 77 | } |
| 78 | if out.Card, err = t.cardView(c); err != nil { |
| 79 | return out, err |
| 80 | } |
| 81 | if r.MessageID != "" { |
| 82 | var m messageRecord |
| 83 | if err = t.db.Where("id = ? AND person = ?", r.MessageID, r.Person).Take(&m).Error; err != nil { |
| 84 | return out, err |
| 85 | } |
| 86 | out.Message = messageView(m) |
| 87 | } |
| 88 | if r.TurnID != "" { |
| 89 | var turn turnRecord |
| 90 | if err = t.db.Where("id = ? AND person = ?", r.TurnID, r.Person).Take(&turn).Error; err != nil { |
| 91 | return out, err |
| 92 | } |
| 93 | out.Turn, err = t.turnView(turn) |
| 94 | } |
| 95 | return out, err |
| 96 | } |
| 97 | |
| 98 | // RetryTaskBound retries exactly the failure seen in the notification. |
| 99 | func (s *Store) RetryTaskBound(ctx context.Context, person, credential, id string, in wire.RetryTask) (attempt int64, err error) { |
| 100 | if in.Revision <= 0 || in.StepRevision <= 0 || in.Attempt <= 0 || in.StepID == "" || in.IdempotencyKey == "" { |
| 101 | return 0, ErrInvalid |
| 102 | } |
| 103 | err = s.tx(ctx, func(t *txn) error { |
| 104 | if err := s.authority.CurrentPerson(ctx, t.db); err != nil { |
| 105 | return err |
| 106 | } |
| 107 | r, prior, err := t.actionReceipt(person, credential, "retry", id, in.IdempotencyKey, in) |
| 108 | if err != nil { |
| 109 | return err |
| 110 | } |
| 111 | if prior { |
| 112 | attempt = r.Attempt |
| 113 | return nil |
| 114 | } |
| 115 | k, err := t.loadTask(id) |
| 116 | if err != nil { |
| 117 | return err |
| 118 | } |
| 119 | st, found, err := t.latestStep(id) |
| 120 | if err != nil { |
| 121 | return err |
| 122 | } |
| 123 | if k.Revision != in.Revision || !found || st.ID != in.StepID || st.Revision != in.StepRevision || st.Attempt != in.Attempt { |
| 124 | return ErrNotRetryable |
| 125 | } |
| 126 | attempt, err = t.personRetry(person, id, in.StepID, false) |
| 127 | if err != nil { |
| 128 | return err |
| 129 | } |
| 130 | r.Attempt = attempt |
| 131 | return t.db.Create(r).Error |
| 132 | }) |
| 133 | return attempt, err |
| 134 | } |
| 135 | |