Skip to content

Commit ca3c86f

Browse files
authored
feat(notifications): add per-channel preferences (#116)
Add one persisted per-channel notification preference across browser, desktop, Pushover, API, and SDK surfaces, with resolved mention metadata and authorized thread-message hydration. Co-authored-by: Jacqueline Henriksen <jjjhenriksen@gmail.com>
1 parent 187131c commit ca3c86f

66 files changed

Lines changed: 2435 additions & 218 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎CHANGELOG.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
## 0.3.0 - Unreleased
44

5-
- Added coherent conversation organization and attention tools, beginning with topic selection and filtered timelines across the web app, API, and SDK. Thanks @PollyBot13.
5+
- Added coherent conversation organization and attention tools: topic selection and filtered timelines plus per-channel all, mentions-only, and muted notification preferences across web, desktop, Pushover, API, and SDK surfaces. Thanks @PollyBot13 and @jjjhenriksen.
66

77
## 0.2.2 - 2026-08-01
88

‎apps/api/internal/httpapi/authz_test.go‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ func TestHTTPUnauthorizedRoutes(t *testing.T) {
5353
{http.MethodPost, "/api/messages/msg_missing/thread/replies", `{"body":"x"}`},
5454
{http.MethodPost, "/api/messages/msg_missing/reactions", `{"emoji":"x"}`},
5555
{http.MethodDelete, "/api/messages/msg_missing/reactions/x", ""},
56+
{http.MethodGet, "/api/channels/chn_missing/notification-settings", ""},
57+
{http.MethodPatch, "/api/channels/chn_missing/notification-settings", `{"preference":"muted"}`},
5658
{http.MethodGet, "/api/realtime/events?workspace_id=wsp_missing", ""},
5759
{http.MethodGet, "/api/realtime/ws?workspace_id=wsp_missing", ""},
5860
{http.MethodGet, "/api/search?workspace_id=wsp_missing&q=x", ""},
@@ -229,6 +231,8 @@ func TestHTTPBotTokenWorkspaceIsolation(t *testing.T) {
229231
expectStatusWithBearer(t, token.Token, http.MethodGet, server.URL+"/api/routes/"+otherWorkspace.RouteID+"/"+otherChannel.RouteID, nil, http.StatusForbidden)
230232
expectStatusWithBearer(t, token.Token, http.MethodGet, server.URL+"/api/channels/"+otherChannel.ID+"/messages", nil, http.StatusForbidden)
231233
expectStatusWithBearer(t, token.Token, http.MethodPost, server.URL+"/api/channels/"+otherChannel.ID+"/messages", strings.NewReader(`{"body":"nope"}`), http.StatusForbidden)
234+
expectStatusWithBearer(t, token.Token, http.MethodGet, server.URL+"/api/channels/"+otherChannel.ID+"/notification-settings", nil, http.StatusForbidden)
235+
expectStatusWithBearer(t, token.Token, http.MethodPatch, server.URL+"/api/channels/"+otherChannel.ID+"/notification-settings", strings.NewReader(`{"preference":"muted"}`), http.StatusForbidden)
232236
expectStatusWithBearer(t, token.Token, http.MethodPost, server.URL+"/api/channels/"+otherChannel.ID+"/read", strings.NewReader(`{"seq":1}`), http.StatusForbidden)
233237
expectStatusWithBearer(t, token.Token, http.MethodPost, server.URL+"/api/hooks/mattermost/"+otherChannel.ID, strings.NewReader(`{"text":"nope"}`), http.StatusForbidden)
234238
expectStatusWithBearer(t, token.Token, http.MethodPost, server.URL+"/api/hooks/slash/"+otherChannel.ID, strings.NewReader(`command=/nope`), http.StatusForbidden)
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package httpapi
2+
3+
import (
4+
"errors"
5+
"net/http"
6+
7+
"github.com/go-chi/chi/v5"
8+
"github.com/openclaw/clickclack/apps/api/internal/store"
9+
)
10+
11+
func (s *Server) getChannelNotificationSettings(w http.ResponseWriter, r *http.Request) {
12+
actor, err := s.currentActor(r)
13+
if err != nil {
14+
writeError(w, http.StatusUnauthorized, err)
15+
return
16+
}
17+
if err := actor.requireScope("messages:read"); err != nil {
18+
writeError(w, http.StatusForbidden, err)
19+
return
20+
}
21+
channelID := chi.URLParam(r, "channel_id")
22+
if !s.requireBotChannelWorkspace(w, r, actor, channelID) {
23+
return
24+
}
25+
preference, err := s.store.GetChannelNotificationPreference(
26+
r.Context(),
27+
channelID,
28+
actor.user.ID,
29+
)
30+
if err != nil {
31+
writeStoreError(w, err)
32+
return
33+
}
34+
writeJSON(w, http.StatusOK, map[string]string{"preference": preference})
35+
}
36+
37+
func (s *Server) updateChannelNotificationSettings(w http.ResponseWriter, r *http.Request) {
38+
actor, err := s.currentActor(r)
39+
if err != nil {
40+
writeError(w, http.StatusUnauthorized, err)
41+
return
42+
}
43+
if err := actor.requireScope("messages:write"); err != nil {
44+
writeError(w, http.StatusForbidden, err)
45+
return
46+
}
47+
channelID := chi.URLParam(r, "channel_id")
48+
if !s.requireBotChannelWorkspace(w, r, actor, channelID) {
49+
return
50+
}
51+
var body struct {
52+
Preference string `json:"preference"`
53+
}
54+
if err := readJSON(w, r, &body); err != nil {
55+
writeError(w, http.StatusBadRequest, err)
56+
return
57+
}
58+
if body.Preference != store.ChannelNotifyAll &&
59+
body.Preference != store.ChannelNotifyMentions &&
60+
body.Preference != store.ChannelNotifyMuted {
61+
writeError(w, http.StatusBadRequest, errors.New("preference must be all, mentions, or muted"))
62+
return
63+
}
64+
if err := s.store.UpsertChannelNotificationSettings(r.Context(), store.ChannelNotificationInput{
65+
ChannelID: channelID,
66+
UserID: actor.user.ID,
67+
Preference: body.Preference,
68+
}); err != nil {
69+
writeStoreError(w, err)
70+
return
71+
}
72+
writeJSON(w, http.StatusOK, map[string]string{"preference": body.Preference})
73+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package httpapi
2+
3+
import (
4+
"context"
5+
"net/http"
6+
"net/http/httptest"
7+
"strings"
8+
"testing"
9+
10+
"github.com/openclaw/clickclack/apps/api/internal/realtime"
11+
"github.com/openclaw/clickclack/apps/api/internal/store"
12+
)
13+
14+
func TestChannelNotificationSettingsHTTP(t *testing.T) {
15+
t.Parallel()
16+
ctx := context.Background()
17+
st := newEmptyHTTPStore(t)
18+
owner, err := st.EnsureBootstrap(ctx, "Owner", "channel-notifications@example.com")
19+
if err != nil {
20+
t.Fatal(err)
21+
}
22+
workspaces, err := st.ListWorkspaces(ctx, owner.ID)
23+
if err != nil {
24+
t.Fatal(err)
25+
}
26+
channels, err := st.ListChannels(ctx, workspaces[0].ID, owner.ID)
27+
if err != nil {
28+
t.Fatal(err)
29+
}
30+
server := httptest.NewServer(New(st, realtime.NewHub(), Options{}).Handler())
31+
t.Cleanup(server.Close)
32+
endpoint := server.URL + "/api/channels/" + channels[0].ID + "/notification-settings"
33+
34+
initial := getJSON[map[string]string](t, endpoint)
35+
if initial["preference"] != store.ChannelNotifyAll {
36+
t.Fatalf("expected all as the default preference, got %#v", initial)
37+
}
38+
updated := patchJSON[map[string]string](t, endpoint, map[string]string{"preference": store.ChannelNotifyMentions})
39+
if updated["preference"] != store.ChannelNotifyMentions {
40+
t.Fatalf("expected mentions preference, got %#v", updated)
41+
}
42+
loaded := getJSON[map[string]string](t, endpoint)
43+
if loaded["preference"] != store.ChannelNotifyMentions {
44+
t.Fatalf("expected persisted mentions preference, got %#v", loaded)
45+
}
46+
expectStatus(t, http.MethodPatch, endpoint, strings.NewReader(`{"preference":"invalid"}`), http.StatusBadRequest)
47+
expectStatus(t, http.MethodPatch, endpoint, strings.NewReader(`{"preference":`), http.StatusBadRequest)
48+
}

‎apps/api/internal/httpapi/correlation_events_test.go‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ func TestMessageEventCorrelationSurvivesResponseRealtimeAndRetrieval(t *testing.
8484
replyEvent := eventByType(t, replyResult.Events, "thread.reply_created")
8585
stateEvent := eventByType(t, replyResult.Events, "thread.state_updated")
8686
assertAPIEventPayloadValue(t, replyEvent, "correlation_id", "corr-api-reply")
87+
assertAPIEventPayloadMissing(t, replyEvent, "author_id")
8788
assertAPIEventPayloadMissing(t, replyEvent, "body")
8889
assertAPIEventPayloadMissing(t, stateEvent, "correlation_id")
8990
liveReply := readEventType(t, conn, "thread.reply_created")

‎apps/api/internal/httpapi/features.go‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1517,7 +1517,7 @@ func (s *Server) createDirectMessage(w http.ResponseWriter, r *http.Request) {
15171517
if err == nil && event.ID != "" {
15181518
s.publishEvent(r.Context(), event)
15191519
if !store.IsActivityMessageKind(message.Kind) {
1520-
s.notifyMessageCreated(r.Context(), message)
1520+
s.notifyMessageCreated(r.Context(), message, event.MentionedUserIDs)
15211521
}
15221522
}
15231523
writeMessageCreateResult(w, message, event, err)
@@ -1546,7 +1546,7 @@ func (s *Server) mattermostWebhook(w http.ResponseWriter, r *http.Request) {
15461546
message, event, err := s.store.CreateMessage(r.Context(), store.CreateMessageInput{ChannelID: chi.URLParam(r, "channel_id"), AuthorID: act.user.ID, Body: body.Text})
15471547
if err == nil {
15481548
s.publishEvent(r.Context(), event)
1549-
s.notifyMessageCreated(r.Context(), message)
1549+
s.notifyMessageCreated(r.Context(), message, event.MentionedUserIDs)
15501550
}
15511551
writeResultStatus(w, http.StatusCreated, map[string]any{"message": message, "event": event}, err)
15521552
}
@@ -1587,7 +1587,7 @@ func (s *Server) slashCommand(w http.ResponseWriter, r *http.Request) {
15871587
message, event, err := s.store.CreateMessage(r.Context(), store.CreateMessageInput{ChannelID: chi.URLParam(r, "channel_id"), AuthorID: act.user.ID, Body: body})
15881588
if err == nil {
15891589
s.publishEvent(r.Context(), event)
1590-
s.notifyMessageCreated(r.Context(), message)
1590+
s.notifyMessageCreated(r.Context(), message, event.MentionedUserIDs)
15911591
}
15921592
writeResultStatus(w, http.StatusCreated, map[string]any{
15931593
"response_type": "in_channel",
@@ -1661,7 +1661,7 @@ func (s *Server) invokeRegisteredSlashCommand(w http.ResponseWriter, r *http.Req
16611661
return
16621662
}
16631663
s.publishEvent(r.Context(), event)
1664-
s.notifyMessageCreated(r.Context(), message)
1664+
s.notifyMessageCreated(r.Context(), message, event.MentionedUserIDs)
16651665
}
16661666
writeJSON(w, http.StatusOK, map[string]any{
16671667
"response_type": callback.ResponseType,

‎apps/api/internal/httpapi/mutations_test.go‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -659,15 +659,15 @@ func TestPushNotificationsRespectModerationVisibility(t *testing.T) {
659659
if err != nil {
660660
t.Fatal(err)
661661
}
662-
server.notifyMessageCreated(ctx, hidden)
662+
server.notifyMessageCreated(ctx, hidden, nil)
663663
if len(notifier.notifications) != 0 {
664664
t.Fatalf("guest should not receive hidden channel push notifications: %#v", notifier.notifications)
665665
}
666666
visible, _, err := st.CreateMessage(ctx, store.CreateMessageInput{ChannelID: guestChannelID, AuthorID: moderator.ID, Body: "visible"})
667667
if err != nil {
668668
t.Fatal(err)
669669
}
670-
server.notifyMessageCreated(ctx, visible)
670+
server.notifyMessageCreated(ctx, visible, nil)
671671
if len(notifier.notifications) != 1 || notifier.notifications[0].RecipientKey != "g12345678901234567890123456789" {
672672
t.Fatalf("guest should receive guest channel push notification, got %#v", notifier.notifications)
673673
}
@@ -698,7 +698,7 @@ func TestPushNotificationsRespectModerationVisibility(t *testing.T) {
698698
if err != nil {
699699
t.Fatal(err)
700700
}
701-
server.notifyMessageCreated(ctx, dmMessage)
701+
server.notifyMessageCreated(ctx, dmMessage, nil)
702702
if len(notifier.notifications) != 0 {
703703
t.Fatalf("demoted guest should not receive DM push notifications: %#v", notifier.notifications)
704704
}

‎apps/api/internal/httpapi/notifications.go‎

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@ type PushNotifier interface {
1919
Notify(ctx context.Context, notification PushNotification) error
2020
}
2121

22-
func (s *Server) notifyMessageCreated(ctx context.Context, message store.Message) {
22+
func (s *Server) notifyMessageCreated(ctx context.Context, message store.Message, mentionedUserIDs []string) {
2323
if s.pushNotifier == nil {
2424
return
2525
}
26-
recipients, err := s.store.ListPushNotificationRecipients(ctx, message.ID)
26+
recipients, err := s.store.ListPushNotificationRecipients(ctx, message.ID, mentionedUserIDs)
2727
if err != nil {
2828
log.Printf("push notification recipient lookup failed: %v", err)
2929
return
@@ -43,6 +43,15 @@ func (s *Server) notifyMessageCreated(ctx context.Context, message store.Message
4343
}
4444
}
4545

46+
func messageEventMentionedUserIDs(events []store.Event) []string {
47+
for _, event := range events {
48+
if event.Type == "message.created" || event.Type == "thread.reply_created" {
49+
return event.MentionedUserIDs
50+
}
51+
}
52+
return nil
53+
}
54+
4655
func (s *Server) canNotifyMessageRecipient(ctx context.Context, message store.Message, userID string) bool {
4756
_, err := s.store.GetMessage(ctx, message.ID, userID)
4857
return err == nil

‎apps/api/internal/httpapi/server.go‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,8 @@ func (s *Server) Handler() http.Handler {
203203
r.Patch("/channels/{channel_id}", s.updateChannel)
204204
r.Get("/channels/{channel_id}/messages", s.listMessages)
205205
r.Post("/channels/{channel_id}/messages", s.createMessage)
206+
r.Get("/channels/{channel_id}/notification-settings", s.getChannelNotificationSettings)
207+
r.Patch("/channels/{channel_id}/notification-settings", s.updateChannelNotificationSettings)
206208
r.Post("/channels/{channel_id}/read", s.markChannelRead)
207209
r.Get("/messages/by-nonce", s.getMessageByNonce)
208210
r.Get("/messages/{message_id}", s.getMessage)
@@ -999,7 +1001,7 @@ func (s *Server) createMessage(w http.ResponseWriter, r *http.Request) {
9991001
if err == nil && event.ID != "" {
10001002
s.publishEvent(r.Context(), event)
10011003
if !store.IsActivityMessageKind(message.Kind) {
1002-
s.notifyMessageCreated(r.Context(), message)
1004+
s.notifyMessageCreated(r.Context(), message, event.MentionedUserIDs)
10031005
}
10041006
}
10051007
writeMessageCreateResult(w, message, event, err)
@@ -1164,7 +1166,7 @@ func (s *Server) createThreadReply(w http.ResponseWriter, r *http.Request) {
11641166
message, state, events, err := s.store.CreateThreadReply(r.Context(), store.CreateThreadReplyInput{RootMessageID: chi.URLParam(r, "message_id"), AuthorID: act.user.ID, Body: body.Body, QuotedMessageID: optionalString(body.QuotedMessageID), Nonce: body.Nonce})
11651167
if err == nil && len(events) > 0 {
11661168
s.publishEvents(r.Context(), events)
1167-
s.notifyMessageCreated(r.Context(), message)
1169+
s.notifyMessageCreated(r.Context(), message, messageEventMentionedUserIDs(events))
11681170
}
11691171
writeThreadReplyCreateResult(w, message, state, events, err)
11701172
}

‎apps/api/internal/store/postgres/export.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ func (s *Store) ExportJSON(ctx context.Context, writer io.Writer) error {
2121

2222
out := map[string]any{}
2323
tables := []string{
24-
"users", "user_notification_settings", "identities", "workspaces", "workspace_members", "channels",
24+
"users", "user_notification_settings", "channel_notification_settings", "identities", "workspaces", "workspace_members", "channels",
2525
"messages", "thread_state", "reactions", "events", "event_recipients", "uploads",
2626
"channel_reads", "direct_reads",
2727
"message_attachments", "direct_conversations", "direct_conversation_members",

0 commit comments

Comments
 (0)