hub (unreleased): review fix 1 — „told” is told at an address: a changed notification address is told again and waits again (the hole D84 exists for)
Claude-Session: https://claude.ai/code/session_0159rPz1ZhFKsS53msqPYxtS
This commit is contained in:
1 parent
cbfeeb626e
commit
a6f4aa0b64
10 files changed
+412
-44
No files matched your search
@@ -1,9 +1,12 @@
|
||||
package store
|
||||
|
||||
import (
|
||||
"crypto/sha256"
|
||||
"database/sql"
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
"sort"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
@@ -56,9 +59,53 @@ func (s *Store) migrateOffsiteEndNotices() error {
|
||||
);`); err != nil {
|
||||
return fmt.Errorf("offsite_end_notices: %w", err)
|
||||
}
|
||||
// told_addr: a key of the household's notification address the notice was accepted for (NoticeAddressKey) —
|
||||
// never the address itself. „Told to the household” counts only while it is the address the household has NOW.
|
||||
s.db.Exec("ALTER TABLE offsite_end_notices ADD COLUMN told_addr TEXT NOT NULL DEFAULT ''")
|
||||
return nil
|
||||
}
|
||||
|
||||
// „TOLD” IS TOLD AT AN ADDRESS (review of 2026-11-15, finding 1). Somebody who holds the household's login could
|
||||
// set the notification address to his own, press „delete backup data”, get the notice and put the real address
|
||||
// back: the row said told, the real address had got nothing, and 30 days later the hub agreed. So a row that is
|
||||
// told to the household carries a key of the address the mail was accepted for, and it counts as told only while
|
||||
// that is the household's CURRENT address. Two mechanisms, on purpose:
|
||||
//
|
||||
// 1. THE READER (EndNotices) compares the row's key with the address on record at every read: a row told at another
|
||||
// address reads as NOT told. This is the rule; it holds whoever changed the address and however.
|
||||
// 2. THE WRITER of the address (SaveNotificationPrefsAs) un-tells the household's rows in the same transaction when
|
||||
// the address changes, is removed or is added — so the row is clean, its „first named” starts again (the
|
||||
// operator's 3-day line counts from the change) and the log says what happened.
|
||||
//
|
||||
// The same address in other capitals or with spaces around it is the same address. An operator-told row is not
|
||||
// touched: the operator was told, and a household that gets an address is mailed by the walk anyway.
|
||||
|
||||
// reasonAddressChanged is the hub's own reason on a row whose household must be told again.
|
||||
const reasonAddressChanged = "the household's notification address changed after it was told — it is told again at the address it has now, and the 30 days start again"
|
||||
|
||||
// NoticeAddressKey is the key of a notification address for the notice record: a hash of the address in lower case
|
||||
// without the spaces around it; "" for no address. The address itself is not stored a second time.
|
||||
func NoticeAddressKey(email string) string {
|
||||
norm := strings.ToLower(strings.TrimSpace(email))
|
||||
if norm == "" {
|
||||
return ""
|
||||
}
|
||||
sum := sha256.Sum256([]byte("felhom notice address\n" + norm))
|
||||
return hex.EncodeToString(sum[:])
|
||||
}
|
||||
|
||||
// untellHouseholdNotices clears „told to the household” on every row of a household, inside the caller's
|
||||
// transaction. Returns how many rows it changed.
|
||||
func untellHouseholdNotices(tx *sql.Tx, customerID string, now time.Time) (int64, error) {
|
||||
res, err := tx.Exec(`UPDATE offsite_end_notices SET told_at = NULL, told_to = '', told_addr = '', tries = 0, last_try = NULL,
|
||||
last_reason = ?, first_named = ?, stuck_told = NULL WHERE customer_id = ? AND told_to = ?`,
|
||||
reasonAddressChanged, now.UTC().Format(time.RFC3339), customerID, EndNoticeHousehold)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return res.RowsAffected()
|
||||
}
|
||||
|
||||
// EndClaim is one pending end record of a report, as far as the notice needs it.
|
||||
type EndClaim struct {
|
||||
App string
|
||||
@@ -211,7 +258,13 @@ func (s *Store) noteEndNoticesFull(customerID string, notStored int) {
|
||||
// EndNotices returns the hub's record for one household, by app. A date that cannot be read is the zero time — and
|
||||
// a zero ToldAt is „not told”.
|
||||
func (s *Store) EndNotices(customerID string) (map[string]EndNotice, error) {
|
||||
rows, err := s.db.Query(`SELECT app, press, first_named, told_at, told_to, tries, last_try, last_reason, stuck_told
|
||||
// The address the household has NOW (no row, or an empty one: no address).
|
||||
var email sql.NullString
|
||||
if err := s.db.QueryRow(`SELECT email FROM customer_notifications WHERE customer_id = ?`, customerID).Scan(&email); err != nil && err != sql.ErrNoRows {
|
||||
return nil, err
|
||||
}
|
||||
current := NoticeAddressKey(email.String)
|
||||
rows, err := s.db.Query(`SELECT app, press, first_named, told_at, told_to, tries, last_try, last_reason, stuck_told, told_addr
|
||||
FROM offsite_end_notices WHERE customer_id = ?`, customerID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -220,11 +273,16 @@ func (s *Store) EndNotices(customerID string) (map[string]EndNotice, error) {
|
||||
out := map[string]EndNotice{}
|
||||
for rows.Next() {
|
||||
var n EndNotice
|
||||
var first string
|
||||
var first, toldAddr string
|
||||
var told, lastTry, stuck sql.NullString
|
||||
if err := rows.Scan(&n.App, &n.Press, &first, &told, &n.ToldTo, &n.Tries, &lastTry, &n.LastReason, &stuck); err != nil {
|
||||
if err := rows.Scan(&n.App, &n.Press, &first, &told, &n.ToldTo, &n.Tries, &lastTry, &n.LastReason, &stuck, &toldAddr); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if n.ToldTo == EndNoticeHousehold && (toldAddr == "" || toldAddr != current) {
|
||||
// told at an address the household does not have now (or at none the hub can show): not told
|
||||
n.ToldTo, told = "", sql.NullString{}
|
||||
n.LastReason = reasonAddressChanged
|
||||
}
|
||||
n.FirstNamed = parseNoticeTime(first)
|
||||
n.ToldAt = parseNoticeTime(told.String)
|
||||
n.LastTry = parseNoticeTime(lastTry.String)
|
||||
@@ -251,13 +309,23 @@ func parseNoticeTime(v string) time.Time {
|
||||
// MarkEndNoticeTold writes that the notice of THIS press was accepted by the mail service, at the hub's time `now`,
|
||||
// to `to` (EndNoticeHousehold or EndNoticeOperator). It matches the press: a row that was voided or reset while the
|
||||
// mail was being sent is not marked (ok=false) — that press was never told.
|
||||
func (s *Store) MarkEndNoticeTold(customerID, app, press, to string, now time.Time) (ok bool, err error) {
|
||||
//
|
||||
// addrKey is NoticeAddressKey of the address the household's mail was accepted for (the address at the SEND, which
|
||||
// the sender returns) — required for EndNoticeHousehold, ignored for EndNoticeOperator. If the household's address
|
||||
// has changed since the send, the row is written and reads as not told (EndNotices).
|
||||
func (s *Store) MarkEndNoticeTold(customerID, app, press, to, addrKey string, now time.Time) (ok bool, err error) {
|
||||
if to != EndNoticeHousehold && to != EndNoticeOperator {
|
||||
return false, fmt.Errorf("MarkEndNoticeTold: unknown recipient %q", to)
|
||||
}
|
||||
if to == EndNoticeHousehold && addrKey == "" {
|
||||
return false, fmt.Errorf("MarkEndNoticeTold: a household notice needs the key of the address it went to")
|
||||
}
|
||||
if to == EndNoticeOperator {
|
||||
addrKey = ""
|
||||
}
|
||||
stamp := now.UTC().Format(time.RFC3339)
|
||||
res, err := s.db.Exec(`UPDATE offsite_end_notices SET told_at = ?, told_to = ?, tries = tries + 1, last_try = ?, last_reason = ''
|
||||
WHERE customer_id = ? AND app = ? AND press = ?`, stamp, to, stamp, customerID, app, press)
|
||||
res, err := s.db.Exec(`UPDATE offsite_end_notices SET told_at = ?, told_to = ?, told_addr = ?, tries = tries + 1, last_try = ?, last_reason = ''
|
||||
WHERE customer_id = ? AND app = ? AND press = ?`, stamp, to, addrKey, stamp, customerID, app, press)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
@@ -286,9 +354,23 @@ func (s *Store) SetEndNoticeForTest(customerID, app, press string, firstNamed, t
|
||||
if !toldAt.IsZero() {
|
||||
told = sql.NullString{String: toldAt.UTC().Format(time.RFC3339), Valid: true}
|
||||
}
|
||||
_, err := s.db.Exec(`INSERT INTO offsite_end_notices (customer_id, app, press, first_named, told_at, told_to) VALUES (?, ?, ?, ?, ?, ?)
|
||||
ON CONFLICT(customer_id, app) DO UPDATE SET press = excluded.press, first_named = excluded.first_named, told_at = excluded.told_at, told_to = excluded.told_to`,
|
||||
customerID, app, press, firstNamed.UTC().Format(time.RFC3339), told, to)
|
||||
// A household-told row is told at the address the household has now; a fixture household with none gets one.
|
||||
addr := ""
|
||||
if to == EndNoticeHousehold {
|
||||
var email sql.NullString
|
||||
_ = s.db.QueryRow(`SELECT email FROM customer_notifications WHERE customer_id = ?`, customerID).Scan(&email)
|
||||
if strings.TrimSpace(email.String) == "" {
|
||||
email.String = "fixture@example.com"
|
||||
if _, err := s.db.Exec(`INSERT INTO customer_notifications (customer_id, email, enabled_events, cooldown_hours) VALUES (?, ?, '[]', 6)
|
||||
ON CONFLICT(customer_id) DO UPDATE SET email = excluded.email`, customerID, email.String); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
addr = NoticeAddressKey(email.String)
|
||||
}
|
||||
_, err := s.db.Exec(`INSERT INTO offsite_end_notices (customer_id, app, press, first_named, told_at, told_to, told_addr) VALUES (?, ?, ?, ?, ?, ?, ?)
|
||||
ON CONFLICT(customer_id, app) DO UPDATE SET press = excluded.press, first_named = excluded.first_named, told_at = excluded.told_at, told_to = excluded.told_to, told_addr = excluded.told_addr`,
|
||||
customerID, app, press, firstNamed.UTC().Format(time.RFC3339), told, to, addr)
|
||||
return err
|
||||
}
|
||||
|
||||
|
||||
@@ -26,6 +26,10 @@ func TestNotice_Record_OnePressOneRow(t *testing.T) {
|
||||
s := newTestStore(t)
|
||||
t0 := time.Date(2026, 11, 15, 10, 0, 0, 0, time.UTC)
|
||||
press := "2026-11-15T09:59:00Z"
|
||||
if err := s.SaveNotificationPrefs("c1", "customer@example.com", nil, 6); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
addr := NoticeAddressKey("customer@example.com")
|
||||
note := func(at time.Time, deployed []string, tierOn bool, claims ...EndClaim) {
|
||||
t.Helper()
|
||||
if err := s.NoteEndNotices("c1", deployed, claims, tierOn, at); err != nil {
|
||||
@@ -43,17 +47,17 @@ func TestNotice_Record_OnePressOneRow(t *testing.T) {
|
||||
t.Fatalf("a later report of the same press moved the row: %+v", n)
|
||||
}
|
||||
// told: only with the press the hub mailed about
|
||||
if ok, err := s.MarkEndNoticeTold("c1", "vikunja", "2026-11-01T00:00:00Z", EndNoticeHousehold, t0); err != nil || ok {
|
||||
if ok, err := s.MarkEndNoticeTold("c1", "vikunja", "2026-11-01T00:00:00Z", EndNoticeHousehold, addr, t0); err != nil || ok {
|
||||
t.Fatalf("a told mark for ANOTHER press was written (ok=%v err=%v)", ok, err)
|
||||
}
|
||||
if n, _ := endNotice(t, s, "c1", "vikunja"); n.Told() {
|
||||
t.Fatalf("told by a mark for another press: %+v", n)
|
||||
}
|
||||
if _, err := s.MarkEndNoticeTold("c1", "vikunja", press, "the box", t0); err == nil {
|
||||
if _, err := s.MarkEndNoticeTold("c1", "vikunja", press, "the box", addr, t0); err == nil {
|
||||
t.Fatal("an unknown recipient was accepted")
|
||||
}
|
||||
told := t0.Add(time.Hour)
|
||||
if ok, err := s.MarkEndNoticeTold("c1", "vikunja", press, EndNoticeHousehold, told); err != nil || !ok {
|
||||
if ok, err := s.MarkEndNoticeTold("c1", "vikunja", press, EndNoticeHousehold, addr, told); err != nil || !ok {
|
||||
t.Fatalf("MarkEndNoticeTold: ok=%v err=%v", ok, err)
|
||||
}
|
||||
n, _ = endNotice(t, s, "c1", "vikunja")
|
||||
@@ -184,3 +188,58 @@ func TestNotice_Record_GoesWithTheHousehold(t *testing.T) {
|
||||
t.Fatalf("orphan sweep: %+v %v", n, err)
|
||||
}
|
||||
}
|
||||
|
||||
// Review finding 1 — „told” is told AT AN ADDRESS. The reader's rule alone: a row told to the household counts only
|
||||
// while the household's address on record is the one the mail was accepted for — however the address came to differ
|
||||
// (here: changed behind the writer's back). An operator-told row does not depend on the address.
|
||||
func TestReview1_ToldCountsOnlyAtTheAddressOnRecord(t *testing.T) {
|
||||
s := newTestStore(t)
|
||||
at := time.Date(2026, 10, 1, 10, 0, 0, 0, time.UTC)
|
||||
if err := s.SaveNotificationPrefs("c1", "Real@Example.com", nil, 6); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := s.NoteEndNotices("c1", nil, []EndClaim{{App: "vikunja"}, {App: "kimai"}}, true, at); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if ok, err := s.MarkEndNoticeTold("c1", "vikunja", "", EndNoticeHousehold, NoticeAddressKey(" real@example.COM "), at); err != nil || !ok {
|
||||
t.Fatalf("mark: %v %v", ok, err)
|
||||
}
|
||||
if ok, err := s.MarkEndNoticeTold("c1", "kimai", "", EndNoticeOperator, "", at); err != nil || !ok {
|
||||
t.Fatalf("mark: %v %v", ok, err)
|
||||
}
|
||||
if _, err := s.MarkEndNoticeTold("c1", "vikunja", "", EndNoticeHousehold, "", at); err == nil {
|
||||
t.Fatal("a household notice was marked told with no address key")
|
||||
}
|
||||
if n, _ := endNotice(t, s, "c1", "vikunja"); !n.Told() {
|
||||
t.Fatalf("the same address in other capitals must be the same address: %+v", n)
|
||||
}
|
||||
for _, other := range []string{"intruder@example.net", ""} {
|
||||
if _, err := s.db.Exec(`UPDATE customer_notifications SET email = ? WHERE customer_id = 'c1'`, other); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
n, _ := endNotice(t, s, "c1", "vikunja")
|
||||
if n.Told() || !n.ToldAt.IsZero() || !strings.Contains(n.LastReason, "address changed") {
|
||||
t.Fatalf("the address on record is %q and the row still reads told: %+v", other, n)
|
||||
}
|
||||
if k, _ := endNotice(t, s, "c1", "kimai"); !k.Told() || k.ToldTo != EndNoticeOperator {
|
||||
t.Fatalf("an operator-told row must not depend on the household's address: %+v", k)
|
||||
}
|
||||
}
|
||||
// the address is put back by hand: the row reads told again — it WAS told at this address
|
||||
if _, err := s.db.Exec(`UPDATE customer_notifications SET email = 'real@example.com' WHERE customer_id = 'c1'`); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if n, _ := endNotice(t, s, "c1", "vikunja"); !n.Told() {
|
||||
t.Fatalf("told at the address the household has: %+v", n)
|
||||
}
|
||||
// … but through the WRITER a change is final: the row is un-told, and changing back does not bring it back
|
||||
if err := s.SaveNotificationPrefs("c1", "intruder@example.net", nil, 6); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := s.SaveNotificationPrefs("c1", "real@example.com", nil, 6); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if n, _ := endNotice(t, s, "c1", "vikunja"); n.Told() {
|
||||
t.Fatalf("away and back through the save: the old „told” came back: %+v", n)
|
||||
}
|
||||
}
|
||||
@@ -1049,7 +1049,27 @@ func (s *Store) SaveNotificationPrefsAs(customerID, email string, enabledEvents
|
||||
if householdSaved {
|
||||
hs = 1
|
||||
}
|
||||
_, err := s.db.Exec(`
|
||||
// The notice lock (review of 2026-11-15, finding 1): a save that CHANGES the address — another one, none, or a
|
||||
// first one — un-tells the household's off-site end notices in the same transaction (offsite_end_notice.go).
|
||||
tx, err := s.db.Begin()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer tx.Rollback()
|
||||
var old sql.NullString
|
||||
if qerr := tx.QueryRow(`SELECT email FROM customer_notifications WHERE customer_id = ?`, customerID).Scan(&old); qerr != nil && qerr != sql.ErrNoRows {
|
||||
return qerr
|
||||
}
|
||||
if NoticeAddressKey(old.String) != NoticeAddressKey(email) {
|
||||
n, uerr := untellHouseholdNotices(tx, customerID, time.Now())
|
||||
if uerr != nil {
|
||||
return uerr
|
||||
}
|
||||
if n > 0 && s.logger != nil {
|
||||
s.logger.Printf("[WARN] notification address of %s changed: %d off-site end notice(s) that were told at the old address are NOT told any more — the household is mailed again at its new address and the 30 days start again", customerID, n)
|
||||
}
|
||||
}
|
||||
_, err = tx.Exec(`
|
||||
INSERT INTO customer_notifications (customer_id, email, enabled_events, cooldown_hours, household_saved, updated_at)
|
||||
VALUES (?, ?, ?, ?, ?, datetime('now'))
|
||||
ON CONFLICT(customer_id) DO UPDATE SET
|
||||
@@ -1060,7 +1080,10 @@ func (s *Store) SaveNotificationPrefsAs(customerID, email string, enabledEvents
|
||||
updated_at = datetime('now')`,
|
||||
customerID, email, string(eventsJSON), cooldownHours, hs,
|
||||
)
|
||||
return err
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return tx.Commit()
|
||||
}
|
||||
|
||||
// LogNotification records a notification attempt.
|
||||
|
||||
Reference in new issue
Block a user