Skip to content

Commit 4f0d766

Browse files
committed
make chat email webhook best effort
1 parent d787c9b commit 4f0d766

2 files changed

Lines changed: 62 additions & 3 deletions

File tree

supabase/functions/send-email-for-new-chat-message/index.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ const handler = async (_request: Request): Promise<Response> => {
3939
}
4040

4141
// Prepare data
42-
let payload: { record?: { id?: unknown; thread_id?: unknown } };
42+
let payload: { record?: { id?: unknown } };
4343
try {
4444
payload = await _request.json();
4545
} catch (_error) {
@@ -234,7 +234,7 @@ const handler = async (_request: Request): Promise<Response> => {
234234
senderName,
235235
recipientName,
236236
// messageContent: record.content,
237-
threadId: record.thread_id,
237+
threadId: threadData.id,
238238
listingSlug,
239239
listingAreaName,
240240
recipientRole,
@@ -254,7 +254,7 @@ const handler = async (_request: Request): Promise<Response> => {
254254
senderName,
255255
recipientName,
256256
// messageContent: record.content,
257-
threadId: record.thread_id,
257+
threadId: threadData.id,
258258
listingSlug,
259259
listingAreaName,
260260
recipientRole,
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
-- Chat notification emails are best-effort. A webhook failure should never
2+
-- abort the original chat message insert.
3+
4+
create or replace function private.notify_new_chat_message()
5+
returns trigger
6+
language plpgsql
7+
security definer
8+
set search_path = ''
9+
as $$
10+
declare
11+
webhook_secret text;
12+
begin
13+
if to_regprocedure('net.http_post(text,jsonb,jsonb,jsonb,integer)') is null then
14+
raise warning 'Skipping chat message email webhook because pg_net is unavailable.';
15+
return new;
16+
end if;
17+
18+
if to_regclass('vault.decrypted_secrets') is not null then
19+
execute
20+
'select decrypted_secret from vault.decrypted_secrets where name = $1 limit 1'
21+
into webhook_secret
22+
using 'PEELS_CHAT_MESSAGE_WEBHOOK_SECRET';
23+
end if;
24+
25+
if webhook_secret is null or webhook_secret = '' then
26+
raise warning 'Skipping chat message email webhook because PEELS_CHAT_MESSAGE_WEBHOOK_SECRET is not set in Vault.';
27+
return new;
28+
end if;
29+
30+
begin
31+
perform net.http_post(
32+
url := 'http://kong:8000/functions/v1/send-email-for-new-chat-message',
33+
body := jsonb_build_object(
34+
'type', tg_op,
35+
'table', tg_table_name,
36+
'schema', tg_table_schema,
37+
'record', to_jsonb(new),
38+
'old_record', null
39+
),
40+
params := '{}'::jsonb,
41+
headers := jsonb_build_object(
42+
'Content-Type', 'application/json',
43+
'x-peels-webhook-secret', webhook_secret
44+
),
45+
timeout_milliseconds := 5000
46+
);
47+
exception
48+
when others then
49+
raise warning 'Skipping chat message email webhook because the request failed: %', sqlerrm;
50+
end;
51+
52+
return new;
53+
end;
54+
$$;
55+
56+
alter function private.notify_new_chat_message() owner to postgres;
57+
58+
revoke all privileges on function private.notify_new_chat_message()
59+
from anon, authenticated, public;

0 commit comments

Comments
 (0)