@marktiderman/genesis-chat 0.12.0 → 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.
- package/README.md +46 -1
- package/dist/{chunk-2HNQWDLF.js → chunk-3UNYTK62.js} +3 -3
- package/dist/{chunk-2HNQWDLF.js.map → chunk-3UNYTK62.js.map} +1 -1
- package/dist/{chunk-VBIYDXL2.js → chunk-GEEJMKQF.js} +22 -3
- package/dist/chunk-GEEJMKQF.js.map +1 -0
- package/dist/{chunk-K5GYNHNE.js → chunk-H4BRVJJ7.js} +136 -23
- package/dist/chunk-H4BRVJJ7.js.map +1 -0
- package/dist/{chunk-3HQN2FZ5.js → chunk-SEMN3S6Q.js} +3 -3
- package/dist/{chunk-3HQN2FZ5.js.map → chunk-SEMN3S6Q.js.map} +1 -1
- package/dist/{client-DJs3ltZc.d.ts → client-DXWyDJ2u.d.ts} +14 -0
- package/dist/index.d.ts +4 -4
- package/dist/index.js +3 -3
- package/dist/native.d.ts +20 -12
- package/dist/native.js +40 -32
- package/dist/native.js.map +1 -1
- package/dist/{outbox-YFTbGIAl.d.ts → outbox-Ctngv6Yz.d.ts} +1 -1
- package/dist/push.d.ts +2 -2
- package/dist/react.d.ts +73 -4
- package/dist/react.js +3 -3
- package/dist/server.d.ts +1 -1
- package/dist/server.js +1 -1
- package/dist/{thread-CgHP00Jw.d.ts → thread-mZsRiIGz.d.ts} +1 -1
- package/dist/web.d.ts +24 -7
- package/dist/web.js +140 -67
- package/dist/web.js.map +1 -1
- package/migrations/0007_genesis_chat_unread_and_delete_for_me.sql +455 -0
- package/migrations/tests/0007_genesis_chat_unread_and_delete_for_me.test.sql +178 -0
- package/package.json +13 -9
- package/src/__tests__/inbox-row-actions.test.tsx +251 -0
- package/src/__tests__/unread-delete-for-me.test.ts +84 -0
- package/src/__tests__/web.test.tsx +5 -2
- package/src/core/client.ts +21 -1
- package/src/native/__tests__/inbox-screen-actions.test.tsx +131 -0
- package/src/native/__tests__/inbox-screen.test.tsx +29 -1
- package/src/native/__tests__/thread-layout.test.ts +2 -2
- package/src/native/copy.ts +7 -10
- package/src/native/inbox-screen.tsx +52 -27
- package/src/native/index.ts +1 -1
- package/src/react/inbox-actions.ts +121 -0
- package/src/react/index.tsx +99 -17
- package/src/web/inbox.tsx +120 -11
- package/src/web/index.ts +1 -1
- package/src/web/memory-client.ts +40 -7
- package/dist/chunk-K5GYNHNE.js.map +0 -1
- package/dist/chunk-VBIYDXL2.js.map +0 -1
|
@@ -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;
|
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
-- @feat:chat
|
|
2
|
+
-- pgTAP test for 0007_genesis_chat_unread_and_delete_for_me.sql.
|
|
3
|
+
-- Copy into your app's `supabase/tests/database/` and run `supabase test db`.
|
|
4
|
+
-- It writes only its own users, kinds and conversations, and rolls everything back.
|
|
5
|
+
--
|
|
6
|
+
-- People: a1 and b2 talk directly; c3 is someone else.
|
|
7
|
+
begin;
|
|
8
|
+
create extension if not exists pgtap with schema extensions;
|
|
9
|
+
select plan(49);
|
|
10
|
+
|
|
11
|
+
insert into auth.users (id, aud, role, email, instance_id)
|
|
12
|
+
select ('00000000-0000-4000-8000-00000000c7' || n)::uuid, 'authenticated', 'authenticated', 'genesis-chat-7-' || n || '@example.test', '00000000-0000-0000-0000-000000000000'
|
|
13
|
+
from unnest(array['a1', 'b2', 'c3']) n;
|
|
14
|
+
|
|
15
|
+
create schema if not exists private;
|
|
16
|
+
create or replace function private.genesis_viewer_people(viewer uuid)
|
|
17
|
+
returns table (tenant_id uuid, person_id uuid) language sql stable set search_path = '' as $$
|
|
18
|
+
select '00000000-0000-4000-8000-0000000000f7'::uuid, viewer
|
|
19
|
+
$$;
|
|
20
|
+
|
|
21
|
+
create function pg_temp.person(n text) returns uuid language sql immutable as $$ select ('00000000-0000-4000-8000-00000000c7' || n)::uuid $$;
|
|
22
|
+
create function pg_temp.sign_in(n text) returns void language plpgsql as $$
|
|
23
|
+
begin
|
|
24
|
+
perform set_config('request.jwt.claims', json_build_object('sub', pg_temp.person(n), 'role', 'authenticated')::text, true);
|
|
25
|
+
perform set_config('request.jwt.claim.sub', pg_temp.person(n)::text, true);
|
|
26
|
+
perform set_config('role', 'authenticated', true);
|
|
27
|
+
end $$;
|
|
28
|
+
grant execute on function pg_temp.person(text), pg_temp.sign_in(text) to authenticated;
|
|
29
|
+
|
|
30
|
+
create temp table t (k text primary key, v uuid);
|
|
31
|
+
grant select, insert, update on t to authenticated;
|
|
32
|
+
create function pg_temp.v(k_ text) returns uuid language sql stable as $$ select v from t where k = k_ $$;
|
|
33
|
+
create function pg_temp.unread() returns integer language sql stable as $$
|
|
34
|
+
select unread from genesis_chat.inbox() where conversation_id = pg_temp.v('ab')
|
|
35
|
+
$$;
|
|
36
|
+
create function pg_temp.listed() returns integer language sql stable as $$
|
|
37
|
+
select count(*)::integer from genesis_chat.inbox() where conversation_id = pg_temp.v('ab')
|
|
38
|
+
$$;
|
|
39
|
+
create function pg_temp.visible() returns integer language sql stable as $$
|
|
40
|
+
select count(*)::integer from genesis_chat.messages where conversation_id = pg_temp.v('ab') and thread_root_id is null
|
|
41
|
+
$$;
|
|
42
|
+
grant execute on function pg_temp.v(text), pg_temp.unread(), pg_temp.listed(), pg_temp.visible() to authenticated;
|
|
43
|
+
|
|
44
|
+
-- Capture the hints genesis_chat.broadcast sends (the stub realtime.send drops them).
|
|
45
|
+
create table realtime.test7_sent (topic text, event text, payload jsonb);
|
|
46
|
+
create or replace function realtime.send(payload jsonb, event text, topic text, private boolean default true)
|
|
47
|
+
returns void language sql as $$ insert into realtime.test7_sent values (topic, event, payload) $$;
|
|
48
|
+
create function pg_temp.hinted(n text) returns integer language sql stable security definer as $$
|
|
49
|
+
select count(*)::integer from realtime.test7_sent
|
|
50
|
+
where topic = 'genesis-chat:user:' || pg_temp.person(n) and event = 'inbox' and payload->>'conversation_id' = pg_temp.v('ab')::text
|
|
51
|
+
$$;
|
|
52
|
+
grant execute on function pg_temp.hinted(text) to authenticated;
|
|
53
|
+
|
|
54
|
+
create function public.genesis_chat_test7_may_start(member_ids uuid[], subject_type text, subject_id text, user_id uuid)
|
|
55
|
+
returns boolean language sql stable set search_path = '' as $$ select true $$;
|
|
56
|
+
select genesis_chat.register_kind('direct', 'Direct message', p_max_members => 2,
|
|
57
|
+
p_start_check => 'public.genesis_chat_test7_may_start(uuid[], text, text, uuid)');
|
|
58
|
+
|
|
59
|
+
select pg_temp.sign_in('a1');
|
|
60
|
+
insert into t select 'ab', genesis_chat.start_conversation('direct', array[pg_temp.person('b2')]);
|
|
61
|
+
select genesis_chat.send_message(pg_temp.v('ab'), gen_random_uuid(), 'hello');
|
|
62
|
+
select genesis_chat.send_message(pg_temp.v('ab'), gen_random_uuid(), 'how was the week?');
|
|
63
|
+
|
|
64
|
+
-- ── Mark as Unread ──────────────────────────────────────────────────────────────
|
|
65
|
+
select is(pg_temp.unread(), 0, 'a1''s own messages are not unread for a1');
|
|
66
|
+
select lives_ok($$ select genesis_chat.set_my_settings(pg_temp.v('ab'), p_marked_unread => true) $$, 'a1 marks it unread');
|
|
67
|
+
select is(pg_temp.unread(), 1, 'it counts as one unread, though the last message is a1''s own');
|
|
68
|
+
select is((select count(*)::int from genesis_chat.member_state where user_id = pg_temp.person('a1') and marked_unread), 1, 'a1 reads their own mark');
|
|
69
|
+
select is(genesis_chat.unread_total(), 1, 'and in the badge');
|
|
70
|
+
select pg_temp.sign_in('b2');
|
|
71
|
+
select is(pg_temp.unread(), 2, 'b2''s unread is untouched by a1''s mark (b2 hasn''t read a1''s two)');
|
|
72
|
+
select is((select count(*)::int from genesis_chat.member_state where user_id = pg_temp.person('a1')), 0, 'b2 can''t see that a1 marked it unread');
|
|
73
|
+
select hasnt_column('genesis_chat', 'members', 'marked_unread', 'members, which everyone in it reads, doesn''t hold the mark');
|
|
74
|
+
select hasnt_column('genesis_chat', 'members', 'cleared_seq', 'or what a member deleted');
|
|
75
|
+
select ok(pg_temp.hinted('a1') > 0, 'marking it unread sends a1''s own devices an inbox hint');
|
|
76
|
+
select pg_temp.sign_in('a1');
|
|
77
|
+
select is((select last_read_seq from genesis_chat.members where conversation_id = pg_temp.v('ab') and user_id = pg_temp.person('a1')),
|
|
78
|
+
(select last_seq from genesis_chat.conversations where id = pg_temp.v('ab')), 'the read place doesn''t move back');
|
|
79
|
+
|
|
80
|
+
select genesis_chat.mark_read(pg_temp.v('ab'));
|
|
81
|
+
select is(pg_temp.unread(), 0, 'reading it clears the mark');
|
|
82
|
+
select genesis_chat.set_my_settings(pg_temp.v('ab'), p_marked_unread => true);
|
|
83
|
+
select genesis_chat.set_my_settings(pg_temp.v('ab'), p_marked_unread => false);
|
|
84
|
+
select is(pg_temp.unread(), 0, 'and so does unmarking it');
|
|
85
|
+
|
|
86
|
+
-- ── Delete for me ───────────────────────────────────────────────────────────────
|
|
87
|
+
-- A photo and a link in the history too: their file and preview go with it.
|
|
88
|
+
reset role;
|
|
89
|
+
insert into storage.objects (bucket_id, name, owner_id)
|
|
90
|
+
values ('genesis-chat', pg_temp.v('ab') || '/' || pg_temp.person('a1') || '/photo.jpg', pg_temp.person('a1')::text);
|
|
91
|
+
select genesis_chat.save_link_preview('https://example.com/page', 'ok', 'Example page');
|
|
92
|
+
select pg_temp.sign_in('a1');
|
|
93
|
+
select genesis_chat.send_message(pg_temp.v('ab'), gen_random_uuid(), '',
|
|
94
|
+
jsonb_build_array(jsonb_build_object('kind', 'image', 'path', pg_temp.v('ab') || '/' || pg_temp.person('a1') || '/photo.jpg')));
|
|
95
|
+
insert into t select 'linked', (genesis_chat.send_message(pg_temp.v('ab'), gen_random_uuid(), 'look at https://example.com/page')).id;
|
|
96
|
+
|
|
97
|
+
select genesis_chat.pin_message(pg_temp.v('linked'));
|
|
98
|
+
reset role;
|
|
99
|
+
update genesis_chat.members set archived = true where conversation_id = pg_temp.v('ab') and user_id = pg_temp.person('b2');
|
|
100
|
+
select pg_temp.sign_in('b2');
|
|
101
|
+
select is(pg_temp.visible(), 4, 'b2 reads all four messages');
|
|
102
|
+
select is((select count(*)::int from genesis_chat.pins where conversation_id = pg_temp.v('ab')), 1, 'and the pin');
|
|
103
|
+
select ok(genesis_chat.can_read_object(pg_temp.v('ab') || '/' || pg_temp.person('a1') || '/photo.jpg', pg_temp.person('a1')::text, now() - interval '2 days'), 'and the photo');
|
|
104
|
+
select is((select count(*)::int from genesis_chat.link_previews_for(pg_temp.v('linked'))), 1, 'and the link''s preview');
|
|
105
|
+
select lives_ok($$ select genesis_chat.delete_conversation_for_me(pg_temp.v('ab')) $$, 'b2 deletes the conversation for themselves');
|
|
106
|
+
select is(pg_temp.listed(), 0, 'it leaves b2''s inbox');
|
|
107
|
+
select ok(pg_temp.hinted('b2') > 0, 'and sends b2''s own devices an inbox hint');
|
|
108
|
+
select is((select count(*)::int from genesis_chat.pins where conversation_id = pg_temp.v('ab')), 0, 'its pins leave b2''s pinned bar');
|
|
109
|
+
select is((select archived from genesis_chat.members where conversation_id = pg_temp.v('ab') and user_id = pg_temp.person('b2')), false, 'and it''s no longer archived, so a new message brings it to the inbox');
|
|
110
|
+
select is(pg_temp.visible(), 0, 'and its history leaves b2''s thread');
|
|
111
|
+
select ok(not genesis_chat.can_read_object(pg_temp.v('ab') || '/' || pg_temp.person('a1') || '/photo.jpg', pg_temp.person('a1')::text, now() - interval '2 days'), 'its files can''t be fetched by b2');
|
|
112
|
+
select is((select count(*)::int from genesis_chat.link_previews_for(pg_temp.v('linked'))), 0, 'nor its link previews');
|
|
113
|
+
select is((select count(*)::int from genesis_chat.search_messages('week')), 0, 'search doesn''t find what b2 deleted');
|
|
114
|
+
select is(genesis_chat.unread_total(), 0, 'nor does the badge count it');
|
|
115
|
+
|
|
116
|
+
select pg_temp.sign_in('a1');
|
|
117
|
+
select is(pg_temp.visible(), 4, 'a1 still reads everything');
|
|
118
|
+
select ok(genesis_chat.can_read_object(pg_temp.v('ab') || '/' || pg_temp.person('a1') || '/photo.jpg', pg_temp.person('a1')::text, now() - interval '2 days'), 'and still fetches the photo');
|
|
119
|
+
select is(pg_temp.listed(), 1, 'and still has it in the inbox');
|
|
120
|
+
select is((select count(*)::int from genesis_chat.pins where conversation_id = pg_temp.v('ab')), 1, 'and the pin');
|
|
121
|
+
|
|
122
|
+
-- What b2 can't read on the main timeline doesn't bring it back: a thread reply, an observers-only
|
|
123
|
+
-- note, or a message moderation hid.
|
|
124
|
+
insert into t select 'reply', (genesis_chat.send_message(pg_temp.v('ab'), gen_random_uuid(), 'a reply in the old thread', p_thread_root_id => pg_temp.v('linked'))).id;
|
|
125
|
+
insert into t select 'hidden', (genesis_chat.send_message(pg_temp.v('ab'), gen_random_uuid(), 'something rude')).id;
|
|
126
|
+
reset role;
|
|
127
|
+
update genesis_chat.messages set moderation = 'hidden' where id = pg_temp.v('hidden');
|
|
128
|
+
insert into genesis_chat.messages (conversation_id, seq, sender_id, sender_role, body, visibility)
|
|
129
|
+
select pg_temp.v('ab'), c.last_seq + 1, pg_temp.person('a1'), 'member', 'a note only observers read', 'observers'
|
|
130
|
+
from genesis_chat.conversations c where c.id = pg_temp.v('ab');
|
|
131
|
+
update genesis_chat.conversations set last_seq = last_seq + 1 where id = pg_temp.v('ab');
|
|
132
|
+
select pg_temp.sign_in('b2');
|
|
133
|
+
select is(pg_temp.listed(), 0, 'a thread reply, an observers-only note or a hidden message doesn''t bring it back to b2');
|
|
134
|
+
select is(genesis_chat.unread_total(), 0, 'nor into b2''s badge');
|
|
135
|
+
reset role;
|
|
136
|
+
select is((select count(*)::int from genesis_chat.push_targets(pg_temp.v('reply')) where user_id = pg_temp.person('b2')), 0,
|
|
137
|
+
'nor is b2 notified of the reply in the thread they deleted');
|
|
138
|
+
select pg_temp.sign_in('a1');
|
|
139
|
+
select genesis_chat.send_message(pg_temp.v('ab'), gen_random_uuid(), 'are you there?');
|
|
140
|
+
|
|
141
|
+
select pg_temp.sign_in('b2');
|
|
142
|
+
select is(pg_temp.listed(), 1, 'a new message brings it back to b2''s inbox');
|
|
143
|
+
select is(pg_temp.visible(), 1, 'with only what came after');
|
|
144
|
+
select is(pg_temp.unread(), 1, 'and only that unread');
|
|
145
|
+
reset role;
|
|
146
|
+
select is((select count(*)::int from genesis_chat.push_targets((select id from genesis_chat.messages where conversation_id = pg_temp.v('ab') and body = 'are you there?'))
|
|
147
|
+
where user_id = pg_temp.person('b2')), 1, 'and the message that brings it back notifies b2');
|
|
148
|
+
|
|
149
|
+
select pg_temp.sign_in('c3');
|
|
150
|
+
select throws_ok($$ select genesis_chat.delete_conversation_for_me(pg_temp.v('ab')) $$, '42501', null, 'someone not in it can''t delete it for anyone');
|
|
151
|
+
select throws_ok($$ select genesis_chat.set_my_settings(pg_temp.v('ab'), p_marked_unread => true) $$, '42501', null, 'or mark it unread');
|
|
152
|
+
|
|
153
|
+
reset role;
|
|
154
|
+
select is((select count(*)::int from genesis_chat.messages where conversation_id = pg_temp.v('ab')), 8, 'Delete for me deleted nothing: every message is still stored');
|
|
155
|
+
select is((select count(*)::int from genesis_chat.messages where conversation_id = pg_temp.v('ab') and deleted_at is not null), 0, 'and none is marked deleted');
|
|
156
|
+
select is((select cleared_seq from genesis_chat.member_state where conversation_id = pg_temp.v('ab') and user_id = pg_temp.person('a1')), null, 'a1''s own view was never cleared');
|
|
157
|
+
|
|
158
|
+
-- A conversation with no messages yet still shows in the inbox (nothing deleted: cleared_seq is null).
|
|
159
|
+
select pg_temp.sign_in('a1');
|
|
160
|
+
insert into t select 'ac', genesis_chat.start_conversation('direct', array[pg_temp.person('c3')]);
|
|
161
|
+
select is((select count(*)::int from genesis_chat.inbox() where conversation_id = pg_temp.v('ac')), 1, 'a new, empty conversation is listed');
|
|
162
|
+
|
|
163
|
+
-- Delete up to what you saw: a message that came in after it isn't deleted unseen.
|
|
164
|
+
insert into t select 'seen', (genesis_chat.send_message(pg_temp.v('ac'), gen_random_uuid(), 'first')).id;
|
|
165
|
+
select genesis_chat.send_message(pg_temp.v('ac'), gen_random_uuid(), 'second, after c3 looked');
|
|
166
|
+
select pg_temp.sign_in('c3');
|
|
167
|
+
select lives_ok($$ select genesis_chat.delete_conversation_for_me(pg_temp.v('ac'), (select seq from genesis_chat.messages where id = pg_temp.v('seen'))) $$,
|
|
168
|
+
'c3 deletes up to the message they saw');
|
|
169
|
+
select is((select count(*)::int from genesis_chat.inbox() where conversation_id = pg_temp.v('ac')), 1, 'the message after it keeps the conversation in c3''s inbox');
|
|
170
|
+
select is((select count(*)::int from genesis_chat.messages where conversation_id = pg_temp.v('ac')), 1, 'and only it is left in c3''s thread');
|
|
171
|
+
select genesis_chat.delete_conversation_for_me(pg_temp.v('ac'), 1);
|
|
172
|
+
select is((select count(*)::int from genesis_chat.messages where conversation_id = pg_temp.v('ac')), 1, 'deleting again with an older place never brings history back');
|
|
173
|
+
select genesis_chat.delete_conversation_for_me(pg_temp.v('ac'), 1000000);
|
|
174
|
+
select is((select cleared_seq from genesis_chat.member_state where conversation_id = pg_temp.v('ac') and user_id = pg_temp.person('c3')),
|
|
175
|
+
(select last_seq from genesis_chat.conversations where id = pg_temp.v('ac')), 'a place past the latest message stops at it');
|
|
176
|
+
|
|
177
|
+
select * from finish();
|
|
178
|
+
rollback;
|