| 1 | package chat |
| 2 | |
| 3 | import ( |
| 4 | "encoding/json" |
| 5 | "slices" |
| 6 | |
| 7 | "amadan.net/oficina/equipo/internal/equipo/push" |
| 8 | "amadan.net/oficina/equipo/internal/wire" |
| 9 | ) |
| 10 | |
| 11 | // notificationIntent enriches only an opted-in device's existing alert. Any |
| 12 | // unknown or incomplete state remains open-only. The receiver must recheck all |
| 13 | // authority and current state; this payload is not a capability. |
| 14 | func (t *txn) notificationIntent(in push.Intent, device push.Registration) push.Intent { |
| 15 | if !t.s.NotificationActions() || device.ActionsVersion != 1 || !push.ValidActionBinding(device.BindingID) || |
| 16 | (in.Kind != push.KindAlert && in.Kind != push.KindPassive) { |
| 17 | return in |
| 18 | } |
| 19 | a := &wire.PushActions{Version: 1, BindingID: device.BindingID, NotificationID: push.NewID()} |
| 20 | switch in.Data.Kind { |
| 21 | case wire.PushCard: |
| 22 | var c cardRecord |
| 23 | if t.db.Where("id = ? AND person = ?", in.Data.CardID, device.Person).Take(&c).Error != nil || |
| 24 | c.State != wire.CardOpen || c.Revision <= 0 || c.Held { |
| 25 | return in |
| 26 | } |
| 27 | var options []string |
| 28 | if c.Options != "" && json.Unmarshal([]byte(c.Options), &options) != nil { |
| 29 | return in |
| 30 | } |
| 31 | switch c.Kind { |
| 32 | case wire.CardQuestion: |
| 33 | // A placement question (options or by name) resolves a project |
| 34 | // in the app, never from the alert. |
| 35 | if c.Placement != "" { |
| 36 | return in |
| 37 | } |
| 38 | in.Category = "equipo.question.choices.v1" |
| 39 | if len(options) == 0 && c.Question != "" && in.Body == c.Question && in.BodyKey == "" { |
| 40 | in.Category = "equipo.question.text.v1" |
| 41 | } |
| 42 | case wire.CardMerge: |
| 43 | if c.TaskID == "" || c.TaskID != in.Data.TaskID || !slices.Contains(options, "merge") { |
| 44 | return in |
| 45 | } |
| 46 | for _, option := range options { |
| 47 | if !slices.Contains([]string{wire.ChoiceMerge, wire.ChoiceWait, wire.ChoiceWaitMac, wire.ChoiceNotNow}, option) { |
| 48 | return in |
| 49 | } |
| 50 | } |
| 51 | in.Category = "equipo.merge.v1" |
| 52 | default: |
| 53 | return in |
| 54 | } |
| 55 | a.CardKind, a.CardRevision = c.Kind, c.Revision |
| 56 | case wire.PushTask: |
| 57 | k, err := t.loadTask(in.Data.TaskID) |
| 58 | if err != nil || k.Creator != device.Person || wire.FinalStage(k.Stage) || k.CancelRequested || k.CopyHold || k.MoveProject != "" { |
| 59 | return in |
| 60 | } |
| 61 | st, found, err := t.latestStep(k.ID) |
| 62 | if err != nil || !found || st.State != wire.StepFailed || !st.Terminated || st.Revision <= 0 || st.Attempt <= 0 || |
| 63 | st.Specialist == wire.SpecialistMerge || st.Specialist == wire.SpecialistPublish { |
| 64 | return in |
| 65 | } |
| 66 | // Offered exactly when the person's Retry would accept this step; |
| 67 | // the bound write still goes through personRetry. |
| 68 | if reason, err := t.retryReason(k, st, device.Person); err != nil || reason != "" { |
| 69 | return in |
| 70 | } |
| 71 | a.TaskRevision, a.StepID, a.StepRevision, a.Attempt = k.Revision, st.ID, st.Revision, st.Attempt |
| 72 | in.Category = "equipo.stopped.v1" |
| 73 | default: |
| 74 | return in |
| 75 | } |
| 76 | in.Data.Actions = a |
| 77 | return in |
| 78 | } |
| 79 | |