@marktiderman/genesis-chat 0.11.4 → 0.13.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (72) hide show
  1. package/README.md +162 -4
  2. package/dist/{chunk-ZQFREERR.js → chunk-3UNYTK62.js} +3 -3
  3. package/dist/{chunk-ZQFREERR.js.map → chunk-3UNYTK62.js.map} +1 -1
  4. package/dist/{chunk-5XYDVUPS.js → chunk-GEEJMKQF.js} +29 -4
  5. package/dist/chunk-GEEJMKQF.js.map +1 -0
  6. package/dist/{chunk-D4RPOYB7.js → chunk-H4BRVJJ7.js} +193 -30
  7. package/dist/chunk-H4BRVJJ7.js.map +1 -0
  8. package/dist/{chunk-O2QSBRSH.js → chunk-SEMN3S6Q.js} +3 -3
  9. package/dist/{chunk-O2QSBRSH.js.map → chunk-SEMN3S6Q.js.map} +1 -1
  10. package/dist/{client-DP7rhJ1U.d.ts → client-DXWyDJ2u.d.ts} +29 -0
  11. package/dist/index.d.ts +4 -4
  12. package/dist/index.js +3 -3
  13. package/dist/native.d.ts +355 -145
  14. package/dist/native.js +701 -217
  15. package/dist/native.js.map +1 -1
  16. package/dist/{outbox-C4LnqpKT.d.ts → outbox-Ctngv6Yz.d.ts} +1 -1
  17. package/dist/push.d.ts +2 -2
  18. package/dist/react.d.ts +83 -6
  19. package/dist/react.js +3 -3
  20. package/dist/server.d.ts +1 -1
  21. package/dist/server.js +1 -1
  22. package/dist/{thread-CSzdqNR_.d.ts → thread-mZsRiIGz.d.ts} +1 -1
  23. package/dist/web.d.ts +24 -7
  24. package/dist/web.js +140 -67
  25. package/dist/web.js.map +1 -1
  26. package/migrations/0006_genesis_chat_read_receipts.sql +110 -0
  27. package/migrations/0007_genesis_chat_unread_and_delete_for_me.sql +455 -0
  28. package/migrations/tests/0006_genesis_chat_read_receipts.test.sql +131 -0
  29. package/migrations/tests/0007_genesis_chat_unread_and_delete_for_me.test.sql +178 -0
  30. package/package.json +17 -9
  31. package/src/__tests__/inbox-row-actions.test.tsx +251 -0
  32. package/src/__tests__/unread-delete-for-me.test.ts +84 -0
  33. package/src/__tests__/web.test.tsx +5 -2
  34. package/src/core/client.ts +30 -1
  35. package/src/core/types.ts +7 -0
  36. package/src/native/__tests__/bubble-native-feel.test.tsx +153 -0
  37. package/src/native/__tests__/composer-attach-menu.test.tsx +47 -0
  38. package/src/native/__tests__/inbox-screen-actions.test.tsx +131 -0
  39. package/src/native/__tests__/inbox-screen.test.tsx +29 -1
  40. package/src/native/__tests__/media-cache-key.test.tsx +21 -0
  41. package/src/native/__tests__/organise-screens.test.tsx +2 -0
  42. package/src/native/__tests__/receipts.test.ts +95 -0
  43. package/src/native/__tests__/review-lows.test.tsx +80 -0
  44. package/src/native/__tests__/sheets-fit.test.tsx +45 -0
  45. package/src/native/__tests__/stable-hooks.test.tsx +68 -0
  46. package/src/native/__tests__/thread-layout.test.ts +2 -2
  47. package/src/native/__tests__/thread-screen-audit.test.tsx +248 -0
  48. package/src/native/__tests__/thread-screen.test.tsx +111 -0
  49. package/src/native/__tests__/thread-title.test.tsx +59 -0
  50. package/src/native/composer.tsx +61 -26
  51. package/src/native/copy.ts +47 -10
  52. package/src/native/emoji-picker.tsx +1 -1
  53. package/src/native/inbox-screen.tsx +52 -27
  54. package/src/native/index.ts +7 -2
  55. package/src/native/media.tsx +7 -1
  56. package/src/native/message-actions-sheet.tsx +1 -1
  57. package/src/native/message-bubble.tsx +171 -19
  58. package/src/native/modules.ts +3 -1
  59. package/src/native/receipts.ts +102 -0
  60. package/src/native/schedule-sheet.tsx +8 -3
  61. package/src/native/stack-options.ts +35 -3
  62. package/src/native/thread-screen.tsx +243 -17
  63. package/src/native/thread-times.tsx +89 -0
  64. package/src/native/thread-title.tsx +111 -0
  65. package/src/native/typing-bubble.tsx +85 -0
  66. package/src/react/inbox-actions.ts +121 -0
  67. package/src/react/index.tsx +172 -23
  68. package/src/web/inbox.tsx +120 -11
  69. package/src/web/index.ts +1 -1
  70. package/src/web/memory-client.ts +40 -7
  71. package/dist/chunk-5XYDVUPS.js.map +0 -1
  72. package/dist/chunk-D4RPOYB7.js.map +0 -1
@@ -0,0 +1,110 @@
1
+ -- Genesis Chat 0006: read receipts
2
+ --
3
+ -- Builds on 0001–0005 (never edited once released). Needs nothing from your app, and no other
4
+ -- package's migrations depend on where it runs after chat 0005.
5
+ --
6
+ -- 1. `members.last_read_at`: when the member last read someone else's message, that is, when
7
+ -- their read place (`last_read_seq`) last moved past a message another person sent (any kind
8
+ -- but system: a voice note or video is a message with attachments, and counts). Posting
9
+ -- moves your read place past your own message, which sets nothing. Null until it happens,
10
+ -- on rows written before this migration, and for observers. A sender's "Read 9:41" is this.
11
+ -- 2. Whenever a member's read place moves past someone else's message (even twice in one
12
+ -- transaction, when `now()` and so `last_read_at` don't change), the conversation's live topic
13
+ -- gets a `read` hint ({conversation_id, user_id,
14
+ -- seq, at}), so "Delivered" turns to "Read" while the sender has the thread open. Members
15
+ -- already read each other's `last_read_seq` (members_read); the hint carries nothing more,
16
+ -- and clients treat it only as a cue to read the members again, never as the read place.
17
+ -- An observer's reads are never sent. Conversations of more than 32 members get no hint
18
+ -- (each read would reach every open thread, O(n²) deliveries per message): their "Read by N"
19
+ -- updates when the members are next read (opening the thread, a refresh).
20
+ -- 3. Clients may broadcast only what clients send: `activity` (typing, recording) and presence.
21
+ -- Every other event on a conversation's topic (`message`, `read`, `members`, …) comes from the
22
+ -- database's own triggers through `genesis_chat.broadcast`, so a member can't forge another
23
+ -- member's read receipt (or a message hint) on everyone's screen.
24
+ -- 4. A hidden (moderated) or deleted message doesn't count as read: its sender mustn't see "Read"
25
+ -- on what the other person was never shown.
26
+ --
27
+ -- Privacy: with this migration every member and observer of a conversation can see when each
28
+ -- member last read it (`last_read_at`), as they already see how far (`last_read_seq`).
29
+
30
+ alter table genesis_chat.members add column last_read_at timestamptz;
31
+
32
+ comment on column genesis_chat.members.last_read_at is
33
+ 'When this member''s read place last moved past someone else''s message (read receipts). Genesis Chat.';
34
+
35
+ -- Whether a read place moving from p_from to p_to passed a message someone else sent that this
36
+ -- member was shown: any kind but system, to everyone, not hidden by moderation, not deleted.
37
+ create function genesis_chat.read_passed(p_conversation_id uuid, p_user_id uuid, p_from bigint, p_to bigint)
38
+ returns boolean
39
+ language sql
40
+ stable
41
+ security definer
42
+ set search_path = ''
43
+ as $$
44
+ select p_to > p_from and exists (
45
+ select 1 from genesis_chat.messages m
46
+ where m.conversation_id = p_conversation_id
47
+ and m.seq > p_from and m.seq <= p_to
48
+ and m.kind <> 'system' and m.visibility = 'everyone'
49
+ and m.moderation is distinct from 'hidden' and m.deleted_at is null
50
+ and m.sender_id is distinct from p_user_id
51
+ )
52
+ $$;
53
+ revoke execute on function genesis_chat.read_passed(uuid, uuid, bigint, bigint) from public, anon, authenticated;
54
+
55
+ create function genesis_chat.read_moved()
56
+ returns trigger
57
+ language plpgsql
58
+ security definer
59
+ set search_path = ''
60
+ as $$
61
+ begin
62
+ if new.role <> 'observer' and genesis_chat.read_passed(new.conversation_id, new.user_id, old.last_read_seq, new.last_read_seq) then
63
+ new.last_read_at := now();
64
+ end if;
65
+ return new;
66
+ end;
67
+ $$;
68
+ revoke execute on function genesis_chat.read_moved() from public, anon, authenticated;
69
+ create trigger members_read_moved before update of last_read_seq on genesis_chat.members
70
+ for each row execute function genesis_chat.read_moved();
71
+
72
+ create function genesis_chat.read_hint()
73
+ returns trigger
74
+ language plpgsql
75
+ security definer
76
+ set search_path = ''
77
+ as $$
78
+ begin
79
+ if new.role <> 'observer'
80
+ and genesis_chat.read_passed(new.conversation_id, new.user_id, old.last_read_seq, new.last_read_seq)
81
+ and (
82
+ select count(*) from genesis_chat.members m
83
+ where m.conversation_id = new.conversation_id and m.left_at is null
84
+ ) <= 32 then
85
+ perform genesis_chat.broadcast('genesis-chat:conv:' || new.conversation_id, 'read',
86
+ jsonb_build_object('conversation_id', new.conversation_id, 'user_id', new.user_id,
87
+ 'seq', new.last_read_seq, 'at', new.last_read_at));
88
+ end if;
89
+ return null;
90
+ end;
91
+ $$;
92
+ revoke execute on function genesis_chat.read_hint() from public, anon, authenticated;
93
+ create trigger members_read_hint after update of last_read_seq on genesis_chat.members
94
+ for each row execute function genesis_chat.read_hint();
95
+
96
+ -- Only client events from clients: the server's own hints go out through genesis_chat.broadcast
97
+ -- (SECURITY DEFINER), which this policy doesn't govern.
98
+ do $do$
99
+ begin
100
+ if to_regclass('realtime.messages') is not null then
101
+ execute 'drop policy if exists genesis_chat_signal on realtime.messages';
102
+ execute $p$
103
+ create policy genesis_chat_signal on realtime.messages for insert to authenticated
104
+ with check ((realtime.messages.extension = 'presence'
105
+ or (realtime.messages.extension = 'broadcast' and realtime.messages.event = 'activity'))
106
+ and genesis_chat.can_join_topic(realtime.topic(), true))
107
+ $p$;
108
+ end if;
109
+ end;
110
+ $do$;
@@ -0,0 +1,455 @@
1
+ -- Genesis Chat 0007: Mark as Unread and Delete for me
2
+ --
3
+ -- Builds on 0001–0006 (never edited once released). Needs nothing from your app.
4
+ --
5
+ -- Both live in `member_state`, a member's private row beside `members`: only that member reads it
6
+ -- (`members` is readable by everyone in the conversation, so it can't hold either).
7
+ --
8
+ -- 1. `marked_unread`: a member can mark a conversation unread, as Messages and Mail do. Only they
9
+ -- see it: their inbox counts it as at least one unread, and so do their badges (`unread_total`,
10
+ -- `push_targets`), until they read it again (`mark_read` clears it). It never moves their read
11
+ -- place back, so a sender's "Read" (0006) stays true. Set with
12
+ -- `set_my_settings(p_marked_unread => …)`.
13
+ -- 2. `cleared_seq`: Delete for me. `delete_conversation_for_me` sets it to the latest message they
14
+ -- saw (`p_seq`, or the conversation's latest), which takes the conversation out of that member's
15
+ -- inbox and their history out of their thread: they read only messages after it (a restrictive
16
+ -- read policy, so every read path agrees; search, files, link previews and pins too). The next
17
+ -- message they can read on the main timeline brings the conversation back with only what came
18
+ -- after; an observers-only note, a thread reply or a hidden message doesn't. Nobody else's inbox,
19
+ -- thread or read receipts change, and nothing is deleted.
20
+ -- Both RPCs send the member's own devices an `inbox` hint, so every open inbox refetches.
21
+
22
+ create table genesis_chat.member_state (
23
+ conversation_id uuid not null,
24
+ user_id uuid not null,
25
+ marked_unread boolean not null default false,
26
+ cleared_seq bigint,
27
+ primary key (conversation_id, user_id),
28
+ foreign key (conversation_id, user_id) references genesis_chat.members (conversation_id, user_id) on delete cascade
29
+ );
30
+
31
+ comment on table genesis_chat.member_state is
32
+ 'A member''s private state in a conversation: Mark as Unread and Delete for me. Only that member reads it; written by set_my_settings, mark_read and delete_conversation_for_me. Genesis Chat.';
33
+ comment on column genesis_chat.member_state.marked_unread is
34
+ 'Marked as unread by this member; only they see it, and reading clears it. Genesis Chat.';
35
+ comment on column genesis_chat.member_state.cleared_seq is
36
+ 'Delete for me: this member sees only messages after this seq, and the conversation leaves their inbox until one they can read arrives. Null: never deleted. Genesis Chat.';
37
+
38
+ alter table genesis_chat.member_state enable row level security;
39
+ create policy member_state_read on genesis_chat.member_state for select to authenticated
40
+ using (user_id = auth.uid());
41
+ grant select on genesis_chat.member_state to authenticated;
42
+ grant select, insert, update, delete on genesis_chat.member_state to service_role;
43
+
44
+ -- The history a member deleted for themselves is gone from what they read. Restrictive, so it
45
+ -- narrows 0001's read policy (and 0002's moderation one) without replacing them.
46
+ create policy messages_cleared_for_me on genesis_chat.messages
47
+ as restrictive for select to authenticated
48
+ using (seq > coalesce((select s.cleared_seq from genesis_chat.member_state s
49
+ where s.conversation_id = messages.conversation_id and s.user_id = auth.uid()), 0));
50
+
51
+ -- Whether a member has a message they can read on the main timeline after p_seq: what brings a
52
+ -- conversation they deleted back to their inbox. Not an observers-only note, a thread reply, a
53
+ -- deleted message or one moderation hid (unless they sent it).
54
+ create function genesis_chat.readable_after(p_conversation_id uuid, p_user_id uuid, p_seq bigint)
55
+ returns boolean
56
+ language sql
57
+ stable
58
+ security definer
59
+ set search_path = ''
60
+ as $$
61
+ select exists (
62
+ select 1 from genesis_chat.messages m
63
+ where m.conversation_id = p_conversation_id and m.seq > coalesce(p_seq, 0)
64
+ and m.visibility = 'everyone' and m.thread_root_id is null and m.deleted_at is null
65
+ and (m.moderation is distinct from 'hidden' or m.sender_id = p_user_id))
66
+ $$;
67
+
68
+ revoke execute on function genesis_chat.readable_after(uuid, uuid, bigint) from public, anon, authenticated;
69
+ grant execute on function genesis_chat.readable_after(uuid, uuid, bigint) to service_role;
70
+
71
+ -- Delete for me: the conversation leaves your inbox, and its history your thread, until a new
72
+ -- message. `p_seq` is the latest message you saw (the inbox row you deleted): a message that came
73
+ -- in after it isn't deleted unseen. It never moves back, and it unpins and unarchives the
74
+ -- conversation for you, so a new message brings it back to the inbox, as Messages does.
75
+ create function genesis_chat.delete_conversation_for_me(p_conversation_id uuid, p_seq bigint default null)
76
+ returns void
77
+ language plpgsql
78
+ security definer
79
+ set search_path = ''
80
+ as $$
81
+ declare
82
+ me uuid := auth.uid();
83
+ latest bigint;
84
+ begin
85
+ update genesis_chat.members m
86
+ set pinned = false, archived = false
87
+ where m.conversation_id = p_conversation_id and m.user_id = me and m.left_at is null and m.role <> 'observer';
88
+ if not found then
89
+ raise exception 'You aren''t in this conversation' using errcode = '42501';
90
+ end if;
91
+ select c.last_seq into latest from genesis_chat.conversations c where c.id = p_conversation_id;
92
+ insert into genesis_chat.member_state as s (conversation_id, user_id, cleared_seq, marked_unread)
93
+ values (p_conversation_id, me, greatest(least(coalesce(p_seq, latest), latest), 0), false)
94
+ on conflict (conversation_id, user_id) do update
95
+ set cleared_seq = greatest(coalesce(s.cleared_seq, 0), excluded.cleared_seq), marked_unread = false;
96
+ perform genesis_chat.broadcast('genesis-chat:user:' || me, 'inbox',
97
+ jsonb_build_object('conversation_id', p_conversation_id));
98
+ end;
99
+ $$;
100
+
101
+ -- Your own settings, now with Mark as Unread. Same arguments as 0001's, plus p_marked_unread.
102
+ drop function genesis_chat.set_my_settings(uuid, timestamptz, text, boolean, boolean, boolean);
103
+ create function genesis_chat.set_my_settings(
104
+ p_conversation_id uuid,
105
+ p_muted_until timestamptz default null,
106
+ p_notify text default null,
107
+ p_archived boolean default null,
108
+ p_pinned boolean default null,
109
+ p_clear_mute boolean default false,
110
+ p_marked_unread boolean default null
111
+ )
112
+ returns void
113
+ language plpgsql
114
+ security definer
115
+ set search_path = ''
116
+ as $$
117
+ begin
118
+ if p_notify is not null and p_notify not in ('all', 'mentions', 'none') then
119
+ raise exception 'Notify is all, mentions or none' using errcode = '22023';
120
+ end if;
121
+ update genesis_chat.members
122
+ set muted_until = case when p_clear_mute then null else coalesce(p_muted_until, muted_until) end,
123
+ notify = coalesce(p_notify, notify),
124
+ archived = coalesce(p_archived, archived),
125
+ pinned = coalesce(p_pinned, pinned)
126
+ where conversation_id = p_conversation_id and user_id = auth.uid() and left_at is null;
127
+ if not found then
128
+ raise exception 'You aren''t in this conversation' using errcode = '42501';
129
+ end if;
130
+ if p_marked_unread is not null then
131
+ insert into genesis_chat.member_state as s (conversation_id, user_id, marked_unread)
132
+ values (p_conversation_id, auth.uid(), p_marked_unread)
133
+ on conflict (conversation_id, user_id) do update set marked_unread = excluded.marked_unread;
134
+ end if;
135
+ -- Your other devices' inboxes refetch (pinned, archived, muted and marked unread all show there).
136
+ perform genesis_chat.broadcast('genesis-chat:user:' || auth.uid(), 'inbox',
137
+ jsonb_build_object('conversation_id', p_conversation_id));
138
+ end;
139
+ $$;
140
+
141
+ -- Reading clears Mark as Unread (0001's body, plus that).
142
+ create or replace function genesis_chat.mark_read(p_conversation_id uuid, p_seq bigint default null)
143
+ returns bigint
144
+ language plpgsql
145
+ security definer
146
+ set search_path = ''
147
+ as $$
148
+ declare
149
+ me uuid := auth.uid();
150
+ latest bigint;
151
+ result bigint;
152
+ begin
153
+ select last_seq into latest from genesis_chat.conversations where id = p_conversation_id;
154
+ if latest is null or not genesis_chat.can_view(p_conversation_id) then
155
+ raise exception 'You aren''t in this conversation' using errcode = '42501';
156
+ end if;
157
+ if genesis_chat.is_member(p_conversation_id) then
158
+ update genesis_chat.members set last_read_seq = greatest(last_read_seq, least(coalesce(p_seq, latest), latest))
159
+ where conversation_id = p_conversation_id and user_id = me
160
+ returning last_read_seq into result;
161
+ update genesis_chat.member_state set marked_unread = false
162
+ where conversation_id = p_conversation_id and user_id = me and marked_unread;
163
+ else
164
+ insert into genesis_chat.members as m (conversation_id, user_id, role, last_read_seq)
165
+ values (p_conversation_id, me, 'observer', least(coalesce(p_seq, latest), latest))
166
+ on conflict (conversation_id, user_id) do update
167
+ set last_read_seq = greatest(m.last_read_seq, excluded.last_read_seq)
168
+ returning last_read_seq into result;
169
+ end if;
170
+ return result;
171
+ end;
172
+ $$;
173
+
174
+ -- What a member counts as unread: messages after both where they read and what they deleted,
175
+ -- and at least one when they marked it unread.
176
+ create function genesis_chat.member_unread(p_conversation_id uuid, p_user_id uuid, p_last_read_seq bigint, p_cleared_seq bigint, p_marked_unread boolean)
177
+ returns integer
178
+ language sql
179
+ stable
180
+ security definer
181
+ set search_path = ''
182
+ as $$
183
+ select greatest(genesis_chat.unread_count(p_conversation_id, p_user_id, greatest(p_last_read_seq, coalesce(p_cleared_seq, 0))),
184
+ case when p_marked_unread then 1 else 0 end)
185
+ $$;
186
+
187
+ revoke execute on function genesis_chat.member_unread(uuid, uuid, bigint, bigint, boolean) from public, anon, authenticated;
188
+ grant execute on function genesis_chat.member_unread(uuid, uuid, bigint, bigint, boolean) to service_role;
189
+
190
+ -- The inbox (0002's), counting Mark as Unread and leaving out what you deleted until something newer.
191
+ create or replace function genesis_chat.inbox(
192
+ p_kind text default null,
193
+ p_archived boolean default false,
194
+ p_limit integer default 50,
195
+ p_before timestamptz default null,
196
+ p_kinds text[] default null,
197
+ p_exclude_kinds text[] default null
198
+ )
199
+ returns table (
200
+ conversation_id uuid,
201
+ kind text,
202
+ title text,
203
+ subject_type text,
204
+ subject_id text,
205
+ status text,
206
+ role text,
207
+ other_user_id uuid,
208
+ member_count integer,
209
+ last_seq bigint,
210
+ last_read_seq bigint,
211
+ unread integer,
212
+ mentioned boolean,
213
+ muted boolean,
214
+ pinned boolean,
215
+ archived boolean,
216
+ last_message_at timestamptz,
217
+ last_message jsonb
218
+ )
219
+ language sql
220
+ stable
221
+ security definer
222
+ set search_path = ''
223
+ as $$
224
+ select c.id, c.kind, c.title, c.subject_type, c.subject_id, c.status, me.role,
225
+ (select o.user_id from genesis_chat.members o
226
+ where o.conversation_id = c.id and o.user_id <> me.user_id and o.left_at is null and o.role <> 'observer'
227
+ and k.max_members = 2 limit 1),
228
+ (select count(*)::integer from genesis_chat.members o
229
+ where o.conversation_id = c.id and o.left_at is null and o.role <> 'observer'),
230
+ c.last_seq, me.last_read_seq,
231
+ genesis_chat.member_unread(c.id, me.user_id, me.last_read_seq, ps.cleared_seq, coalesce(ps.marked_unread, false)),
232
+ exists (select 1 from genesis_chat.messages m
233
+ where m.conversation_id = c.id and m.seq > greatest(me.last_read_seq, coalesce(ps.cleared_seq, 0)) and me.user_id = any (m.mentions)
234
+ and m.moderation is distinct from 'hidden'),
235
+ coalesce(me.muted_until > now(), false), me.pinned, me.archived, c.last_message_at,
236
+ (select jsonb_build_object('id', m.id, 'seq', m.seq, 'sender_id', m.sender_id, 'kind', m.kind,
237
+ 'body', left(m.body, 160), 'deleted', m.deleted_at is not null, 'created_at', m.created_at,
238
+ 'attachment', (select a.kind from genesis_chat.attachments a where a.message_id = m.id order by a.position limit 1))
239
+ from genesis_chat.messages m
240
+ where m.conversation_id = c.id and m.visibility = 'everyone' and m.thread_root_id is null and m.seq > coalesce(ps.cleared_seq, 0)
241
+ and (m.moderation is distinct from 'hidden' or m.sender_id = me.user_id)
242
+ order by m.seq desc limit 1)
243
+ from genesis_chat.members me
244
+ join genesis_chat.conversations c on c.id = me.conversation_id
245
+ join genesis_chat.kinds k on k.kind = c.kind
246
+ left join genesis_chat.member_state ps on ps.conversation_id = me.conversation_id and ps.user_id = me.user_id
247
+ where me.user_id = auth.uid() and me.left_at is null and me.role <> 'observer'
248
+ and me.archived = coalesce(p_archived, false)
249
+ and (ps.cleared_seq is null or genesis_chat.readable_after(c.id, me.user_id, ps.cleared_seq))
250
+ and (p_kind is null or c.kind = p_kind)
251
+ and (p_kinds is null or c.kind = any (p_kinds))
252
+ and (p_exclude_kinds is null or not (c.kind = any (p_exclude_kinds)))
253
+ and (p_before is null or coalesce(c.last_message_at, c.created_at) < p_before)
254
+ order by me.pinned desc, coalesce(c.last_message_at, c.created_at) desc, c.id
255
+ limit least(greatest(coalesce(p_limit, 50), 1), 200)
256
+ $$;
257
+
258
+ -- The badge (0002's), counted the same way.
259
+ create or replace function genesis_chat.unread_total(p_user_id uuid default null, p_exclude_kinds text[] default null)
260
+ returns integer
261
+ language sql
262
+ stable
263
+ security definer
264
+ set search_path = ''
265
+ as $$
266
+ select coalesce(sum(genesis_chat.member_unread(c.id, me.user_id, me.last_read_seq, ps.cleared_seq, coalesce(ps.marked_unread, false))), 0)::integer
267
+ from genesis_chat.members me join genesis_chat.conversations c on c.id = me.conversation_id
268
+ left join genesis_chat.member_state ps on ps.conversation_id = me.conversation_id and ps.user_id = me.user_id
269
+ where me.user_id = case when auth.role() = 'service_role' then coalesce(p_user_id, auth.uid()) else auth.uid() end
270
+ and me.left_at is null and me.role <> 'observer' and me.archived = false
271
+ and coalesce(me.muted_until <= now(), true)
272
+ and (ps.cleared_seq is null or genesis_chat.readable_after(c.id, me.user_id, ps.cleared_seq))
273
+ and (c.last_seq > greatest(me.last_read_seq, coalesce(ps.cleared_seq, 0)) or ps.marked_unread)
274
+ and (p_exclude_kinds is null or not (c.kind = any (p_exclude_kinds)))
275
+ $$;
276
+
277
+ -- A push's badge (0002's push_targets), counted the same way, and nobody told of a message in a
278
+ -- conversation they deleted unless it brings it back.
279
+ create or replace function genesis_chat.push_targets(p_message_id uuid, p_badge_exclude_kinds text[] default null)
280
+ returns table (user_id uuid, conversation_id uuid, title text, sender_id uuid, preview text, mentioned boolean, unread_total integer)
281
+ language sql
282
+ stable
283
+ security definer
284
+ set search_path = ''
285
+ as $$
286
+ with msg as (
287
+ select m.*, c.title as conversation_title
288
+ from genesis_chat.messages m join genesis_chat.conversations c on c.id = m.conversation_id
289
+ where m.id = p_message_id and m.deleted_at is null and not m.silent and m.kind = 'text' and m.visibility = 'everyone'
290
+ and m.moderation is distinct from 'hidden'
291
+ ), recipients as (
292
+ select me.user_id from msg join genesis_chat.members me on me.conversation_id = msg.conversation_id
293
+ where me.left_at is null and me.role <> 'observer' and me.user_id is distinct from msg.sender_id
294
+ and coalesce(me.muted_until <= now(), true) and me.notify <> 'none'
295
+ and (me.notify = 'all' or me.user_id = any (msg.mentions))
296
+ union
297
+ select c.assignee_id from msg join genesis_chat.conversations c on c.id = msg.conversation_id
298
+ where c.assignee_id is not null and c.assignee_id is distinct from msg.sender_id
299
+ )
300
+ select r.user_id, msg.conversation_id, msg.conversation_title, msg.sender_id,
301
+ case when msg.body <> '' then left(msg.body, 180)
302
+ else (select case a.kind when 'image' then 'Photo' when 'voice' then 'Voice message' when 'video' then 'Video'
303
+ when 'file' then 'File' else 'Attachment' end
304
+ from genesis_chat.attachments a where a.message_id = msg.id order by a.position limit 1) end,
305
+ r.user_id = any (msg.mentions),
306
+ (select coalesce(sum(genesis_chat.member_unread(c2.id, m2.user_id, m2.last_read_seq, p2.cleared_seq, coalesce(p2.marked_unread, false))), 0)::integer
307
+ from genesis_chat.members m2 join genesis_chat.conversations c2 on c2.id = m2.conversation_id
308
+ left join genesis_chat.member_state p2 on p2.conversation_id = m2.conversation_id and p2.user_id = m2.user_id
309
+ where m2.user_id = r.user_id and m2.left_at is null and m2.role <> 'observer' and not m2.archived
310
+ and coalesce(m2.muted_until <= now(), true)
311
+ and (p2.cleared_seq is null or genesis_chat.readable_after(c2.id, m2.user_id, p2.cleared_seq))
312
+ and (c2.last_seq > greatest(m2.last_read_seq, coalesce(p2.cleared_seq, 0)) or p2.marked_unread)
313
+ and (p_badge_exclude_kinds is null or not (c2.kind = any (p_badge_exclude_kinds))))
314
+ from recipients r cross join msg
315
+ -- Someone who deleted the conversation for themselves hears only of a message that brings it
316
+ -- back to their inbox (not, say, a reply in a thread whose root they deleted).
317
+ where not exists (select 1 from genesis_chat.member_state s
318
+ where s.conversation_id = msg.conversation_id and s.user_id = r.user_id and s.cleared_seq is not null
319
+ and not genesis_chat.readable_after(msg.conversation_id, r.user_id, s.cleared_seq))
320
+ $$;
321
+
322
+ -- Search (0003's search_messages) never finds what you deleted for yourself.
323
+ create or replace function genesis_chat.search_messages(p_query text, p_conversation_id uuid default null, p_limit integer default 30)
324
+ returns table (message_id uuid, conversation_id uuid, seq bigint, sender_id uuid, created_at timestamptz, snippet text, in_transcript boolean, thread_root_id uuid)
325
+ language plpgsql
326
+ stable
327
+ security definer
328
+ set search_path = ''
329
+ as $$
330
+ declare
331
+ q tsquery;
332
+ begin
333
+ if auth.uid() is null or length(btrim(coalesce(p_query, ''))) < 2 then
334
+ return;
335
+ end if;
336
+ q := websearch_to_tsquery('simple', p_query);
337
+ return query
338
+ with mine as (
339
+ select me.conversation_id, coalesce(ps.cleared_seq, 0) as cleared_seq from genesis_chat.members me
340
+ left join genesis_chat.member_state ps on ps.conversation_id = me.conversation_id and ps.user_id = me.user_id
341
+ where me.user_id = auth.uid() and me.left_at is null and me.role <> 'observer'
342
+ and (p_conversation_id is null or me.conversation_id = p_conversation_id)
343
+ union
344
+ select p_conversation_id, 0::bigint where p_conversation_id is not null and genesis_chat.i_observe(p_conversation_id)
345
+ ), hits as (
346
+ select m.id, m.conversation_id, m.seq, m.sender_id, m.created_at,
347
+ ts_headline('simple', translate(m.body, '«»', '‹›'), q, 'MaxWords=18, MinWords=6, StartSel=«, StopSel=»') as snippet, false as in_transcript,
348
+ m.thread_root_id
349
+ from genesis_chat.messages m join mine using (conversation_id)
350
+ where m.search @@ q and m.deleted_at is null and m.seq > mine.cleared_seq
351
+ and (m.visibility = 'everyone' or genesis_chat.i_observe(m.conversation_id))
352
+ and (m.moderation is distinct from 'hidden' or m.sender_id = auth.uid() or genesis_chat.i_observe(m.conversation_id))
353
+ union all
354
+ select m.id, m.conversation_id, m.seq, m.sender_id, m.created_at,
355
+ ts_headline('simple', translate(a.transcript, '«»', '‹›'), q, 'MaxWords=18, MinWords=6, StartSel=«, StopSel=»'), true,
356
+ m.thread_root_id
357
+ from genesis_chat.attachments a
358
+ join genesis_chat.messages m on m.id = a.message_id
359
+ join mine on mine.conversation_id = m.conversation_id
360
+ where a.transcript is not null and a.transcript_search @@ q and m.deleted_at is null and m.seq > mine.cleared_seq
361
+ and (m.visibility = 'everyone' or genesis_chat.i_observe(m.conversation_id))
362
+ and (m.moderation is distinct from 'hidden' or m.sender_id = auth.uid() or genesis_chat.i_observe(m.conversation_id))
363
+ )
364
+ select distinct on (h.created_at, h.id) h.id, h.conversation_id, h.seq, h.sender_id, h.created_at, h.snippet, h.in_transcript, h.thread_root_id
365
+ from hits h
366
+ order by h.created_at desc, h.id
367
+ limit least(greatest(coalesce(p_limit, 30), 1), 100);
368
+ end;
369
+ $$;
370
+
371
+ -- Where the signed-in person's deleted history ends in a conversation (0: nothing deleted).
372
+ create function genesis_chat.my_cleared_seq(p_conversation_id uuid)
373
+ returns bigint
374
+ language sql
375
+ stable
376
+ security definer
377
+ set search_path = ''
378
+ as $$
379
+ select coalesce((select s.cleared_seq from genesis_chat.member_state s
380
+ where s.conversation_id = p_conversation_id and s.user_id = auth.uid()), 0)
381
+ $$;
382
+
383
+ revoke execute on function genesis_chat.my_cleared_seq(uuid) from public, anon;
384
+ grant execute on function genesis_chat.my_cleared_seq(uuid) to authenticated, service_role;
385
+
386
+ -- A file (0005's can_read_object): never one from history you deleted for yourself.
387
+ create or replace function genesis_chat.can_read_object(object_name text, object_owner text, uploaded_at timestamptz)
388
+ returns boolean
389
+ language sql
390
+ stable
391
+ security definer
392
+ set search_path = ''
393
+ as $$
394
+ select auth.uid() is not null and (
395
+ exists (
396
+ select 1 from genesis_chat.attachments a
397
+ join genesis_chat.messages m on m.id = a.message_id
398
+ where ((a.provider = 'storage' and a.path = object_name) or a.poster_path = object_name)
399
+ and m.deleted_at is null
400
+ and genesis_chat.can_view(m.conversation_id)
401
+ and (m.visibility = 'everyone' or genesis_chat.observes(m.conversation_id))
402
+ and m.seq > genesis_chat.my_cleared_seq(m.conversation_id))
403
+ or (object_owner = auth.uid()::text and split_part(object_name, '/', 2) = auth.uid()::text
404
+ and uploaded_at > now() - interval '1 day'))
405
+ $$;
406
+
407
+ -- A message's link previews (0002's link_previews_for): nothing for history you deleted for yourself.
408
+ create or replace function genesis_chat.link_previews_for(p_message_id uuid)
409
+ returns table (url text, status text, title text, description text, image_url text, site_name text, fetched_at timestamptz)
410
+ language sql
411
+ stable
412
+ security definer
413
+ set search_path = ''
414
+ as $$
415
+ select p.url, p.status, p.title, p.description, p.image_url, p.site_name, p.fetched_at
416
+ from genesis_chat.messages m
417
+ cross join lateral genesis_chat.message_links(m.body) l(u)
418
+ join genesis_chat.link_previews p on p.url = l.u
419
+ where m.id = p_message_id and m.deleted_at is null and auth.uid() is not null
420
+ and genesis_chat.can_view(m.conversation_id)
421
+ and (m.visibility = 'everyone' or genesis_chat.i_observe(m.conversation_id))
422
+ and (m.moderation is distinct from 'hidden' or m.sender_id = auth.uid() or genesis_chat.i_observe(m.conversation_id))
423
+ and m.seq > genesis_chat.my_cleared_seq(m.conversation_id)
424
+ $$;
425
+
426
+ -- A pinned message from history you deleted for yourself isn't one of your pins. Restrictive, so
427
+ -- it narrows 0001's pins_read.
428
+ create function genesis_chat.cleared_for_me(p_message_id uuid)
429
+ returns boolean
430
+ language sql
431
+ stable
432
+ security definer
433
+ set search_path = ''
434
+ as $$
435
+ select exists (select 1 from genesis_chat.messages m
436
+ join genesis_chat.member_state s on s.conversation_id = m.conversation_id and s.user_id = auth.uid()
437
+ where m.id = p_message_id and m.seq <= s.cleared_seq)
438
+ $$;
439
+
440
+ revoke execute on function genesis_chat.cleared_for_me(uuid) from public, anon;
441
+ grant execute on function genesis_chat.cleared_for_me(uuid) to authenticated, service_role;
442
+
443
+ create policy pins_cleared_for_me on genesis_chat.pins
444
+ as restrictive for select to authenticated
445
+ using (not genesis_chat.cleared_for_me(message_id));
446
+
447
+ revoke execute on function
448
+ genesis_chat.delete_conversation_for_me(uuid, bigint),
449
+ genesis_chat.set_my_settings(uuid, timestamptz, text, boolean, boolean, boolean, boolean)
450
+ from public, anon;
451
+
452
+ grant execute on function
453
+ genesis_chat.delete_conversation_for_me(uuid, bigint),
454
+ genesis_chat.set_my_settings(uuid, timestamptz, text, boolean, boolean, boolean, boolean)
455
+ to authenticated, service_role;