diff --git a/README.md b/README.md index 348ae61..c0a3664 100644 --- a/README.md +++ b/README.md @@ -747,7 +747,9 @@ such a message carries (`""` for a clearing reaction) and `null` for every other message, so clients can hide reaction messages from message lists. `reactions` lists the effective reactions on this message: each participant's latest reaction, grouped by emoji in order of first reaction, with the -reactors of each. It is `[]` when there are none. +reactors of each. It is `[]` when there are none. An add-to batch is a message +of its own (its own `sha256` and participants), so its reactions are listed on +it, as `add_to[].reactions` (omitted when there are none), not on the message. `edit` and `edited` implement [FMSG-008 Edits](https://github.com/markmnl/fmsg/blob/main/standards/fmsg-008-edits.md). An edit is an ordinary message: a reply from the @@ -999,13 +1001,21 @@ recipients isn't counted against the caller's send limits (see Sets or clears the authenticated identity's reaction on a message, per [FMSG-005 Reactions](https://github.com/markmnl/fmsg/blob/main/standards/fmsg-005-reactions.md). -The identity must be a participant of the message — the sender or a recipient -in `to` or `add_to` — and the message must be sent and not terminal. - -The reaction is sent immediately as a reaction message: a reply to `:id` with -`no_reply` and `terminal` set, type `text/plain;charset=UTF-8`, body the emoji, -addressed to every other participant of the message, or to the caller alone -for a message they sent only to themselves. Delivery then proceeds +`:id` names the message reacted to: a message id or `sha256`, or the `sha256` +of an add-to batch. An add-to batch is a message of its own — the original's +contents with a wider audience — so a reaction to it is to that batch, not to +the original. Named by id, a message the identity was only added to (in a +batch) is that batch, the only one of them they have. The identity must be a +participant of the message reacted to — its sender or a recipient in `to`, and +for a batch also its adder or a recipient it added — and the message must be +sent and not terminal. + +The reaction is sent immediately as a reaction message: a reply to that +message (`psha256` its hash) with `no_reply` and `terminal` set, type +`text/plain;charset=UTF-8`, body the emoji, addressed to every other +participant of that message — for the original not those only added in +batches, for a batch also its adder and the recipients it added — or to the +caller alone for a message they sent only to themselves. Delivery then proceeds exactly as for any sent message, and like any message it counts against the reactor's send limits and each local recipient's receive limits (see [Quotas](#quotas)). A reaction message cannot be replied to, reacted to, or @@ -1017,7 +1027,8 @@ have recipients added. | ------- | -------- | -------- | ----------- | | `emoji` | `string` | no | A single emoji (a Unicode `RGI_Emoji` sequence, or any single well-formed emoji sequence). `""` or `null` clears the caller's reaction. | -Setting the same reaction the caller already has is idempotent: nothing is +Each message — the original and each batch — has its own reactions. +Setting the same reaction the caller already has on it is idempotent: nothing is sent and the existing reaction message is returned with `200 OK`. Clearing when the caller has no reaction returns `200 OK` with `null` values. diff --git a/internal/handlers/messages.go b/internal/handlers/messages.go index 61023eb..ad7f2b2 100644 --- a/internal/handlers/messages.go +++ b/internal/handlers/messages.go @@ -699,10 +699,7 @@ func (h *MessageHandler) Get(c *gin.Context) { // Compute ShortText only after authorization has been confirmed. msg.ShortText = h.extractShortText(dataPath, msg.Type) msg.Reaction = h.reactionOf(msg.HasPid, msg.NoReply, msg.Terminal, msg.Important, msg.HasAddTo, msg.Type, msg.Size, len(msg.Attachments), dataPath) - msg.Reactions = h.loadReactions(ctx, []int64{msgID})[msgID] - if msg.Reactions == nil { - msg.Reactions = []models.Reaction{} - } + msg.Reactions = applyReactions(h.loadReactions(ctx, []int64{msgID}), msgID, msg.AddTo) if editShape(msg.HasPid, msg.NoReply, msg.Terminal, msg.Important, msg.HasAddTo, msg.Type, len(msg.Attachments)) { _, msg.Edit = h.editSubject(ctx, msgID) } @@ -1579,10 +1576,7 @@ func (h *MessageHandler) messageItemFor(ctx context.Context, msgID int64, recipi dataPath: dataPath, } item.Reaction = h.reactionOf(item.HasPid, item.NoReply, item.Terminal, item.Important, item.HasAddTo, item.Type, item.Size, len(item.Attachments), dataPath) - item.Reactions = h.loadReactions(ctx, []int64{msgID})[msgID] - if item.Reactions == nil { - item.Reactions = []models.Reaction{} - } + item.Reactions = applyReactions(h.loadReactions(ctx, []int64{msgID}), msgID, item.AddTo) if editShape(item.HasPid, item.NoReply, item.Terminal, item.Important, item.HasAddTo, item.Type, len(item.Attachments)) { _, item.Edit = h.editSubject(ctx, msgID) } diff --git a/internal/handlers/quota_integration_test.go b/internal/handlers/quota_integration_test.go index 35bdf91..7563b31 100644 --- a/internal/handlers/quota_integration_test.go +++ b/internal/handlers/quota_integration_test.go @@ -178,7 +178,7 @@ func TestLocalDeliveryChecksAndRecordsReceiveQuota(t *testing.T) { // usage recorded; adding recipients sends no new message. erin, frank := "@erin@example.com", "@frank@example.com" id.set(frank, map[string]any{"limitRecvSizePer1d": 10}) - jsonResponse(t, a.request("@alice@example.com", "POST", "/fmsg/"+msg+"/add-to", map[string]any{"add_to": []string{erin, frank}}), 200) + batch := jsonResponse(t, a.request("@alice@example.com", "POST", "/fmsg/"+msg+"/add-to", map[string]any{"add_to": []string{erin, frank}}), 200) rows = a.recipientRows(t, "msg_add_to", msg) wantRow(t, rows, erin, true, responseCodeAccept) wantRow(t, rows, frank, false, responseCodeUserFull) @@ -195,17 +195,26 @@ func TestLocalDeliveryChecksAndRecordsReceiveQuota(t *testing.T) { if len(send) != 2 || send[1].Address != bob || send[1].Size != int64(len("👍")) { t.Fatalf("reaction send usage: %+v", send) } - // carol is still full, and nobody unknown; alice, dave, erin and frank - // each have room for the reaction. - recv = id.posted("recv")[2:] - gotRecv := map[string]int64{} - for _, u := range recv { - gotRecv[u.Address] = u.Size - } - want := map[string]int64{"@alice@example.com": 4, dave: 4, erin: 4, frank: 4} - if fmt.Sprint(gotRecv) != fmt.Sprint(want) || len(recv) != len(want) { - t.Fatalf("reaction recv usage: %+v, want %v", recv, want) + // The reaction goes to the message's participants: carol is still full, + // nobody unknown, and alice and dave each have room. erin and frank were + // added in a batch, a message of its own: they don't get it. + wantRecv := func(from int, want map[string]int64) { + t.Helper() + recv := id.posted("recv")[from:] + gotRecv := map[string]int64{} + for _, u := range recv { + gotRecv[u.Address] = u.Size + } + if fmt.Sprint(gotRecv) != fmt.Sprint(want) || len(recv) != len(want) { + t.Fatalf("reaction recv usage: %+v, want %v", recv, want) + } } + wantRecv(2, map[string]int64{"@alice@example.com": 4, dave: 4}) + // A reaction on the batch goes to its participants: the message's and + // those it added. + n := len(id.posted("recv")) + jsonResponse(t, a.request(bob, "POST", "/fmsg/"+batch["sha256"].(string)+"/react", map[string]any{"emoji": "👍"}), 201) + wantRecv(n, map[string]int64{"@alice@example.com": 4, dave: 4, erin: 4, frank: 4}) } func TestSendRefusedOverSendQuota(t *testing.T) { diff --git a/internal/handlers/reactions.go b/internal/handlers/reactions.go index a410fcb..d1f521c 100644 --- a/internal/handlers/reactions.go +++ b/internal/handlers/reactions.go @@ -118,9 +118,18 @@ func (h *MessageHandler) reactionSubject(ctx context.Context, msgID int64) (int6 return *pid, true } +// reactionKey names a reaction's subject: a message, or one add-to batch of +// it (batch is that batch's hex sha256). A batch is a message of its own, so +// its reactions are its own, not the original's. +type reactionKey struct { + msgID int64 + batch string +} + // reactionRow is one stored reaction message on a subject. type reactionRow struct { subjectID int64 + batch string // hex sha256 of the add-to batch the reaction names; "" for the message itself msgID int64 from string time float64 @@ -141,13 +150,14 @@ func newerReaction(a, b reactionRow) bool { // reactions: one per reactor, grouped by emoji in order of first reaction, // reactors within a group in time order. A reactor whose effective reaction is // empty has none and is omitted. -func effectiveReactions(rows []reactionRow) map[int64][]models.Reaction { - bySubject := make(map[int64]map[string]reactionRow) +func effectiveReactions(rows []reactionRow) map[reactionKey][]models.Reaction { + bySubject := make(map[reactionKey]map[string]reactionRow) for _, r := range rows { - reactors := bySubject[r.subjectID] + k := reactionKey{r.subjectID, r.batch} + reactors := bySubject[k] if reactors == nil { reactors = make(map[string]reactionRow) - bySubject[r.subjectID] = reactors + bySubject[k] = reactors } key := strings.ToLower(r.from) if cur, ok := reactors[key]; !ok || newerReaction(r, cur) { @@ -155,7 +165,7 @@ func effectiveReactions(rows []reactionRow) map[int64][]models.Reaction { } } - result := make(map[int64][]models.Reaction, len(bySubject)) + result := make(map[reactionKey][]models.Reaction, len(bySubject)) for subject, reactors := range bySubject { var eff []reactionRow for _, r := range reactors { @@ -195,8 +205,9 @@ func (h *MessageHandler) loadReactionRows(ctx context.Context, subjectIDs []int6 return nil, nil } rows, err := h.q().Query(ctx, ` - SELECT r.pid, r.id, r.from_addr, r.time_sent, r.sha256, r.size, r.filepath + SELECT r.pid, COALESCE(encode(sb.sha256, 'hex'), ''), r.id, r.from_addr, r.time_sent, r.sha256, r.size, r.filepath FROM msg r + LEFT JOIN msg_add_to_batch sb ON sb.msg_id = r.pid AND sb.sha256 = r.psha256 WHERE r.pid = ANY($1) AND r.time_sent IS NOT NULL AND r.is_terminal AND r.no_reply AND NOT r.is_important @@ -217,7 +228,7 @@ func (h *MessageHandler) loadReactionRows(ctx context.Context, subjectIDs []int6 var r reactionRow var size int var dataPath string - if err := rows.Scan(&r.subjectID, &r.msgID, &r.from, &r.time, &r.hash, &size, &dataPath); err != nil { + if err := rows.Scan(&r.subjectID, &r.batch, &r.msgID, &r.from, &r.time, &r.hash, &size, &dataPath); err != nil { return nil, err } e, ok := h.readReactionData(dataPath, size) @@ -230,8 +241,9 @@ func (h *MessageHandler) loadReactionRows(ctx context.Context, subjectIDs []int6 return out, rows.Err() } -// loadReactions returns the effective reactions on each of the given subjects. -func (h *MessageHandler) loadReactions(ctx context.Context, subjectIDs []int64) map[int64][]models.Reaction { +// loadReactions returns the effective reactions on each of the given messages +// and on each of their add-to batches. +func (h *MessageHandler) loadReactions(ctx context.Context, subjectIDs []int64) map[reactionKey][]models.Reaction { rows, err := h.loadReactionRows(ctx, subjectIDs, "") if err != nil { log.Printf("load reactions: %v", err) @@ -240,6 +252,20 @@ func (h *MessageHandler) loadReactions(ctx context.Context, subjectIDs []int64) return effectiveReactions(rows) } +// applyReactions sets each add-to batch's own reactions and returns the +// message's (never nil). +func applyReactions(set map[reactionKey][]models.Reaction, msgID int64, addTo []models.AddToBatch) []models.Reaction { + for i := range addTo { + if addTo[i].SHA256 != nil { + addTo[i].Reactions = set[reactionKey{msgID, strings.ToLower(*addTo[i].SHA256)}] + } + } + if r := set[reactionKey{msgID, ""}]; r != nil { + return r + } + return []models.Reaction{} +} + // populateListReactions fills Reaction and Reactions on every list item. func (h *MessageHandler) populateListReactions(ctx context.Context, items []messageListItem) { ids := make([]int64, len(items)) @@ -250,17 +276,17 @@ func (h *MessageHandler) populateListReactions(ctx context.Context, items []mess } reactions := h.loadReactions(ctx, ids) for i := range items { - items[i].Reactions = reactions[items[i].ID] - if items[i].Reactions == nil { - items[i].Reactions = []models.Reaction{} - } + items[i].Reactions = applyReactions(reactions, items[i].ID, items[i].AddTo) } } // reactionRecipients returns a subject's participants other than the reactor, // in message order and deduplicated case-insensitively: the to list of a -// reaction (FMSG-005 Recipients). -func reactionRecipients(subject *models.Message, reactor string) []string { +// reaction (FMSG-005 Recipients). The subject is the message, or, when batch +// is set, that add-to batch of it: a message of its own, whose participants +// are also its adder and the recipients it added. Other batches' recipients +// are not participants of the subject. +func reactionRecipients(subject *models.Message, batch *models.AddToBatch, reactor string) []string { seen := map[string]bool{strings.ToLower(reactor): true} var out []string add := func(addr string) { @@ -275,15 +301,37 @@ func reactionRecipients(subject *models.Message, reactor string) []string { for _, a := range subject.To { add(a) } - for _, b := range subject.AddTo { - add(b.AddToFrom) - for _, a := range b.To { + if batch != nil { + add(batch.AddToFrom) + for _, a := range batch.To { add(a) } } return out } +// reactSubject resolves the :id of POST /fmsg/:id/react: a message id or +// sha256, or the sha256 of an add-to batch, which is a message of its own. It +// returns the message id the batch is stored with and the batch hash ("" for +// the message itself). +func reactSubject(c *gin.Context, q queries) (int64, string, bool) { + if s := strings.ToLower(c.Param("id")); len(s) == 64 { + if hash, err := decodeHash(s); err == nil { + var id int64 + err := q.QueryRow(c.Request.Context(), `SELECT msg_id FROM msg_add_to_batch WHERE sha256 = $1 LIMIT 1`, hash).Scan(&id) + if err == nil { + return id, s, true + } + if !errors.Is(err, pgx.ErrNoRows) { + c.JSON(http.StatusInternalServerError, gin.H{"error": "failed to resolve message"}) + return 0, "", false + } + } + } + id, ok := resolveID(c, q) + return id, "", ok +} + // reactInput is the request body of POST /fmsg/:id/react. type reactInput struct { Emoji *string `json:"emoji"` // a single emoji; null or "" clears the caller's reaction @@ -295,7 +343,7 @@ type reactInput struct { // idempotent and sends nothing. func (h *MessageHandler) React(c *gin.Context) { identity := middleware.GetIdentity(c) - msgID, ok := resolveID(c, h.q()) + msgID, batchHash, ok := reactSubject(c, h.q()) if !ok { return } @@ -330,12 +378,31 @@ func (h *MessageHandler) React(c *gin.Context) { c.JSON(http.StatusNotFound, gin.H{"error": "message not found"}) return } + // The subject: the message, or the add-to batch named by its hash. + // Named by id, a caller who was only added has just their batch, so that + // is what they react to. + onOriginal := strings.EqualFold(subject.From, identity) || isRecipient(subject.To, identity) + var batch *models.AddToBatch + for i := range subject.AddTo { + b := &subject.AddTo[i] + if b.SHA256 == nil { + continue + } + if batchHash != "" && strings.EqualFold(*b.SHA256, batchHash) || batchHash == "" && !onOriginal && isRecipient(b.To, identity) { + batch = b + break + } + } // Only a participant of the subject may react to it (the host enforces // the same rule on the wire). - if !strings.EqualFold(subject.From, identity) && !isRecipient(subject.To, identity) && !addToContains(subject.AddTo, identity) { + if !onOriginal && (batch == nil || !isRecipient(batch.To, identity)) { c.JSON(http.StatusForbidden, gin.H{"error": "only participants may react to a message"}) return } + subjectBatch := "" + if batch != nil { + subjectBatch = strings.ToLower(*batch.SHA256) + } if subject.Time == nil { c.JSON(http.StatusConflict, gin.H{"error": "cannot react to a draft"}) return @@ -345,7 +412,7 @@ func (h *MessageHandler) React(c *gin.Context) { return } - recipients := reactionRecipients(subject, identity) + recipients := reactionRecipients(subject, batch, identity) if len(recipients) == 0 { // A message the caller sent only to themselves: the reaction is // addressed to the caller (FMSG-005 v0.1.1). @@ -361,6 +428,9 @@ func (h *MessageHandler) React(c *gin.Context) { } var current *reactionRow for i := range mine { + if mine[i].batch != subjectBatch { + continue // a reaction on another message: the original or another batch + } if current == nil || newerReaction(mine[i], *current) { current = &mine[i] } @@ -390,14 +460,8 @@ func (h *MessageHandler) React(c *gin.Context) { } parentHash := subject.SHA256 - if !sameAddr(subject.From, identity) && !isRecipient(subject.To, identity) { - parentHash = nil - for _, b := range subject.AddTo { - if isRecipient(b.To, identity) { - parentHash = b.SHA256 - break - } - } + if batch != nil { + parentHash = batch.SHA256 } if parentHash == nil { c.JSON(500, gin.H{"error": "reaction parent has no finalized identity"}) diff --git a/internal/handlers/reactions_integration_test.go b/internal/handlers/reactions_integration_test.go new file mode 100644 index 0000000..2c56688 --- /dev/null +++ b/internal/handlers/reactions_integration_test.go @@ -0,0 +1,98 @@ +package handlers + +import ( + "context" + "encoding/json" + "fmt" + "sort" + "testing" +) + +// An add-to batch is a message of its own: a reaction names the message or +// the batch it was made on, goes to that message's participants, and is shown +// on it alone. +func TestReactionsPerMessage(t *testing.T) { + a := newFinalizationAPI(t) + alice, bob, carol := "@alice@example.com", "@bob@example.com", "@carol@example.com" + orig := a.sendAs(t, alice, []string{bob}, "hello", nil) + batch := jsonResponse(t, a.request(alice, "POST", "/fmsg/"+orig+"/add-to", map[string]any{"add_to": []string{carol}}), 200) + batchHash := batch["sha256"].(string) + + react := func(who, id, emoji string, code int) string { + t.Helper() + w := a.request(who, "POST", "/fmsg/"+id+"/react", map[string]any{"emoji": emoji}) + if w.Code != code { + t.Fatalf("%s reacts %s on %s: HTTP %d, want %d: %s", who, emoji, id, w.Code, code, w.Body) + } + var v map[string]any + _ = json.Unmarshal(w.Body.Bytes(), &v) + return fmt.Sprintf("%.0f", v["id"]) + } + to := func(id string) []string { + t.Helper() + var out []string + rows, err := a.pool.Query(context.Background(), `SELECT addr FROM msg_to WHERE msg_id = $1`, mustID(t, id)) + if err != nil { + t.Fatal(err) + } + defer rows.Close() + for rows.Next() { + var s string + _ = rows.Scan(&s) + out = append(out, s) + } + sort.Strings(out) + return out + } + + // On the message: its participants only. + if got := to(react(bob, orig, "👍", 201)); fmt.Sprint(got) != fmt.Sprint([]string{alice}) { + t.Errorf("reaction on the message went to %v", got) + } + // On the batch: its participants, the message's and those it added. The + // same emoji on another message is a reaction of its own, not a repeat. + if got := to(react(bob, batchHash, "👍", 201)); fmt.Sprint(got) != fmt.Sprint([]string{alice, carol}) { + t.Errorf("reaction on the batch went to %v", got) + } + react(bob, batchHash, "👍", 200) // the same again on the batch: nothing sent + // Someone only added holds the batch: named by id, that is what they react to. + if got := to(react(carol, orig, "🎉", 201)); fmt.Sprint(got) != fmt.Sprint([]string{alice, bob}) { + t.Errorf("added recipient's reaction went to %v", got) + } + + var got struct { + Reactions []struct { + Emoji string `json:"emoji"` + From []string `json:"from"` + } `json:"reactions"` + AddTo []struct { + SHA256 string `json:"sha256"` + Reactions []struct { + Emoji string `json:"emoji"` + From []string `json:"from"` + } `json:"reactions"` + } `json:"add_to"` + } + w := a.request(alice, "GET", "/fmsg/"+orig, nil) + if err := json.Unmarshal(w.Body.Bytes(), &got); err != nil || w.Code != 200 { + t.Fatal(w.Code, w.Body) + } + if fmt.Sprint(got.Reactions) != fmt.Sprint([]any{struct { + Emoji string + From []string + }{"👍", []string{bob}}}) { + t.Errorf("message reactions = %+v", got.Reactions) + } + if len(got.AddTo) != 1 || got.AddTo[0].SHA256 != batchHash || fmt.Sprint(got.AddTo[0].Reactions) != fmt.Sprint([]any{ + struct { + Emoji string + From []string + }{"👍", []string{bob}}, + struct { + Emoji string + From []string + }{"🎉", []string{carol}}, + }) { + t.Errorf("batch reactions = %+v", got.AddTo) + } +} diff --git a/internal/handlers/reactions_test.go b/internal/handlers/reactions_test.go index 8e2478c..50993a4 100644 --- a/internal/handlers/reactions_test.go +++ b/internal/handlers/reactions_test.go @@ -95,14 +95,18 @@ func TestEffectiveReactions(t *testing.T) { {subjectID: 1, msgID: 14, from: "@dave@example.com", time: 104, emoji: ""}, // dave clears {subjectID: 2, msgID: 20, from: "@bob@example.com", time: 100, emoji: "🎉"}, {subjectID: 3, msgID: 30, from: "@bob@example.com", time: 100, emoji: ""}, // only a clear + // A batch of message 1 is a message of its own: bob's reaction there + // is separate from his reaction on the original. + {subjectID: 1, batch: "ab", msgID: 15, from: "@bob@example.com", time: 105, emoji: "😂"}, } got := effectiveReactions(rows) - want := map[int64][]models.Reaction{ - 1: { + want := map[reactionKey][]models.Reaction{ + {1, ""}: { {Emoji: "👍", From: []string{"@carol@example.edu"}}, {Emoji: "❤️", From: []string{"@Bob@example.com"}}, }, - 2: {{Emoji: "🎉", From: []string{"@bob@example.com"}}}, + {1, "ab"}: {{Emoji: "😂", From: []string{"@bob@example.com"}}}, + {2, ""}: {{Emoji: "🎉", From: []string{"@bob@example.com"}}}, } if !reflect.DeepEqual(got, want) { gj, _ := json.Marshal(got) @@ -116,14 +120,15 @@ func TestEffectiveReactionsTieBreaksOnHash(t *testing.T) { {subjectID: 1, msgID: 10, from: "@bob@example.com", time: 100, hash: []byte{0x01}, emoji: "👍"}, {subjectID: 1, msgID: 11, from: "@bob@example.com", time: 100, hash: []byte{0x02}, emoji: "❤️"}, } + k := reactionKey{1, ""} got := effectiveReactions(rows) - if len(got[1]) != 1 || got[1][0].Emoji != "❤️" { - t.Errorf("tie at equal time should pick the greater hash, got %+v", got[1]) + if len(got[k]) != 1 || got[k][0].Emoji != "❤️" { + t.Errorf("tie at equal time should pick the greater hash, got %+v", got[k]) } // Order independence. got = effectiveReactions([]reactionRow{rows[1], rows[0]}) - if len(got[1]) != 1 || got[1][0].Emoji != "❤️" { - t.Errorf("tie-break must not depend on row order, got %+v", got[1]) + if len(got[k]) != 1 || got[k][0].Emoji != "❤️" { + t.Errorf("tie-break must not depend on row order, got %+v", got[k]) } } @@ -135,12 +140,19 @@ func TestReactionRecipients(t *testing.T) { {AddToFrom: "@bob@example.com", To: []string{"@dave@example.org", "@Carol@example.edu"}}, }, } - got := reactionRecipients(subject, "@BOB@example.com") - want := []string{"@alice@example.com", "@carol@example.edu", "@dave@example.org"} + // On the message: its participants only, not the batch's added recipients. + got := reactionRecipients(subject, nil, "@BOB@example.com") + want := []string{"@alice@example.com", "@carol@example.edu"} if !reflect.DeepEqual(got, want) { - t.Errorf("reactionRecipients = %v, want %v", got, want) + t.Errorf("reactionRecipients(message) = %v, want %v", got, want) } - if got := reactionRecipients(&models.Message{From: "@alice@example.com", To: []string{"@alice@example.com"}}, "@alice@example.com"); len(got) != 0 { + // On the batch, a message of its own: also its adder and added recipients. + got = reactionRecipients(subject, &subject.AddTo[0], "@BOB@example.com") + want = []string{"@alice@example.com", "@carol@example.edu", "@dave@example.org"} + if !reflect.DeepEqual(got, want) { + t.Errorf("reactionRecipients(batch) = %v, want %v", got, want) + } + if got := reactionRecipients(&models.Message{From: "@alice@example.com", To: []string{"@alice@example.com"}}, nil, "@alice@example.com"); len(got) != 0 { t.Errorf("self-only message should have no recipients, got %v", got) } } diff --git a/internal/handlers/transaction.go b/internal/handlers/transaction.go index ef5aed1..7ba563e 100644 --- a/internal/handlers/transaction.go +++ b/internal/handlers/transaction.go @@ -86,7 +86,9 @@ func atomic(c *gin.Context, database *db.DB, next func(pgx.Tx)) { } defer tx.Rollback(ctx) if c.Param("id") != "" { - id, ok := resolveID(c, tx) + // An add-to batch's hash locks the message it is stored with; routes + // that take one (react) resolve it again themselves. + id, _, ok := reactSubject(c, tx) if !ok { return } diff --git a/internal/models/models.go b/internal/models/models.go index 9788529..204d00d 100644 --- a/internal/models/models.go +++ b/internal/models/models.go @@ -24,6 +24,10 @@ type AddToBatch struct { To []string `json:"to"` ToDelivery []RecipientDelivery `json:"to_delivery"` Time float64 `json:"time"` + // FMSG-005: effective reactions on this batch. A batch is a message of + // its own (its own hash and audience), reacted to separately from the + // original and from other batches. + Reactions []Reaction `json:"reactions,omitempty"` } // Message represents a fmsg message as exchanged over the HTTP API.