Notifications phase 2 (Gitea #10): share triggers, targeted people, Signal, managed-by footer

- share_received trigger: scanner fires when a bookmark, tag or collection
  is shared with the rule owner (same dedupe discipline; sharer name and
  item title in the message)
- config.targetUserIds on rules: same-tenant targets (<=20, validated at
  write time); each targeted Notification row carries recipientId and is
  delivered through the RECIPIENT's own enabled channel of the rule's kind,
  gated by the recipient's tenant plan (no channel => suppressed, visible
  in history); history lists rows you own or were targeted on
- signal channel kind: signal-cli REST bridge (POST {restUrl}/v2/send with
  account query param and recipients array); account encrypted + masked
  like every other credential; netguard-dialed like every webhook
- 'Managed by David's Superbrain.' footer is plan DATA: entitlements
  ManagedFooter (free hosted plan only; managed_footer override key),
  appended at delivery time - paying plans, core and self-hosted never
  carry it
- catalog: signal joins standard/premium/core/selfhosted channel lists
- migration 019 + schema.sql (trigger/kind CHECKs, Notification.recipientId)
- UI: trigger option, same-tenant target picker via /api/users/search,
  signal channel fields, targeted-count badge on rules
- tests: share/target/footer/signal legs against fake webhook, fake SMTP
  and a fake signal-cli bridge; entitlements footer assertions
This commit is contained in:
Andrew S Erwin
2026-11-09 17:42:38 -06:00
parent c2e2f54fbc
commit 3b11ac83fc
10 changed files with 676 additions and 60 deletions
+101 -9
View File
@@ -61,10 +61,67 @@ func channelConfigFields(kind string) []string {
return []string{"homeserver", "roomId", "accessToken"}
case "telegram":
return []string{"botToken", "chatId"}
case "signal":
return []string{"restUrl", "account", "recipient"}
}
return nil
}
// cleanRuleTargets validates configured notification targets: existing
// users in the caller's tenant, deduped, capped at 20. Cross-tenant
// targeting is refused so a rule can never dial another organization's
// channels; delivery uses each target's OWN enabled channel of the rule's
// kind (a target without one is suppressed, visible in history).
func cleanRuleTargets(r *http.Request, b object) ([]string, error) {
c, ok := b["config"].(map[string]any)
if !ok {
return nil, nil
}
raw, present := c["targetUserIds"]
if !present || raw == nil {
return nil, nil
}
list, ok := raw.([]any)
if !ok {
return nil, bad("config.targetUserIds must be an array")
}
seen := map[string]bool{}
out := []string{}
for _, v := range list {
id, _ := v.(string)
if id == "" || seen[id] {
return nil, bad("config.targetUserIds must be unique non-empty ids")
}
seen[id] = true
out = append(out, id)
}
if len(out) > 20 {
return nil, bad("config.targetUserIds may contain at most 20 users")
}
if len(out) == 0 {
return nil, nil
}
var tenantID *string
if e := database(r).QueryRowContext(r.Context(),
`SELECT "tenantId" FROM "User" WHERE id=$1`, user(r)).Scan(&tenantID); e != nil {
return nil, e
}
tid := ""
if tenantID != nil {
tid = *tenantID
}
var n int
if e := database(r).QueryRowContext(r.Context(),
`SELECT count(*) FROM "User" WHERE id = ANY($1) AND COALESCE("tenantId",'') = $2`,
out, tid).Scan(&n); e != nil {
return nil, e
}
if n != len(out) {
return nil, bad("config.targetUserIds must be users in your organization")
}
return out, nil
}
func cleanChannelConfig(kind string, raw any) (map[string]string, error) {
cfg := map[string]string{}
if raw == nil {
@@ -377,8 +434,8 @@ func HandleNotificationRulesCreate(w http.ResponseWriter, r *http.Request) {
return
}
trigger := str(b, "trigger")
if trigger != "signal_created" && trigger != "bookmark_due" {
respond(w, 0, nil, bad("Trigger must be signal_created or bookmark_due"))
if trigger != "signal_created" && trigger != "bookmark_due" && trigger != "share_received" {
respond(w, 0, nil, bad("Trigger must be signal_created, bookmark_due or share_received"))
return
}
channel, e := owned(r, database(r), "NotificationChannel", str(b, "channelId"), "ownerId")
@@ -405,6 +462,14 @@ func HandleNotificationRulesCreate(w http.ResponseWriter, r *http.Request) {
}
cfg["leadHours"] = lead
}
targets, e := cleanRuleTargets(r, b)
if e != nil {
respond(w, 0, nil, e)
return
}
if targets != nil {
cfg["targetUserIds"] = targets
}
raw, _ := json.Marshal(cfg)
cooldown := 60
if v, ok := b["cooldownMinutes"].(float64); ok && v >= 1 && v <= 7*24*60 {
@@ -488,14 +553,40 @@ func HandleNotificationRulesUpdate(w http.ResponseWriter, r *http.Request) {
}
updates["channelId"] = str(b, "channelId")
}
if _, present := b["config"]; present && str(existing, "trigger") == "bookmark_due" {
lead := 24
if c, ok := b["config"].(map[string]any); ok {
if lv, ok := c["leadHours"].(float64); ok && lv >= 0 && lv <= 24*30 {
lead = int(lv)
if _, present := b["config"]; present {
// Start from the stored config so untouched keys survive a patch.
cfg := object{}
switch c := existing["config"].(type) {
case map[string]any:
for k, v := range c {
cfg[k] = v
}
case string:
if c != "" {
_ = json.Unmarshal([]byte(c), &cfg)
}
}
raw, _ := json.Marshal(object{"leadHours": lead})
if str(existing, "trigger") == "bookmark_due" {
lead := 24
if v, ok := cfg["leadHours"].(float64); ok && v >= 0 && v <= 24*30 {
lead = int(v)
}
if c, ok := b["config"].(map[string]any); ok {
if lv, ok := c["leadHours"].(float64); ok && lv >= 0 && lv <= 24*30 {
lead = int(lv)
}
}
cfg["leadHours"] = lead
}
targets, e := cleanRuleTargets(r, b)
if e != nil {
respond(w, 0, nil, e)
return
}
if targets != nil {
cfg["targetUserIds"] = targets
}
raw, _ := json.Marshal(cfg)
updates["config"] = string(raw)
}
if len(updates) == 0 {
@@ -525,7 +616,7 @@ func HandleNotificationsList(w http.ResponseWriter, r *http.Request) {
FROM "Notification" n
JOIN "NotificationRule" r ON r.id = n."ruleId"
JOIN "NotificationChannel" c ON c.id = r."channelId"
WHERE n."ownerId"=$1
WHERE n."ownerId"=$1 OR n."recipientId"=$1
ORDER BY n."createdAt" DESC LIMIT 100`, user(r))
if e != nil {
respond(w, 0, nil, e)
@@ -537,6 +628,7 @@ func HandleNotificationsList(w http.ResponseWriter, r *http.Request) {
"id": row["id"], "title": row["title"], "body": row["body"],
"status": row["status"], "attempts": row["attempts"],
"ruleName": row["ruleName"], "channelKind": row["channelKind"],
"recipientId": row["recipientId"],
"sentAt": row["sentAt"], "createdAt": row["createdAt"],
})
}