|
| 1 | +-- Follow-up hardening and performance fixes from PR review. |
| 2 | + |
| 3 | +alter table public.public_listings |
| 4 | + add column latitude double precision |
| 5 | + generated always as ((coordinates->>'latitude')::double precision) stored, |
| 6 | + add column longitude double precision |
| 7 | + generated always as ((coordinates->>'longitude')::double precision) stored; |
| 8 | + |
| 9 | +create index public_listings_longitude_latitude_idx |
| 10 | + on public.public_listings (longitude, latitude); |
| 11 | + |
| 12 | +create index if not exists chat_messages_thread_id_created_at_id_idx |
| 13 | + on public.chat_messages (thread_id, created_at desc, id desc); |
| 14 | + |
| 15 | +create or replace function public.listings_in_view( |
| 16 | + min_lat double precision, |
| 17 | + min_long double precision, |
| 18 | + max_lat double precision, |
| 19 | + max_long double precision |
| 20 | +) returns table(id bigint, slug text, type text, coordinates jsonb) |
| 21 | +language sql |
| 22 | +security invoker |
| 23 | +stable |
| 24 | +set search_path = '' |
| 25 | +as $$ |
| 26 | + select |
| 27 | + public_listings.id, |
| 28 | + public_listings.slug, |
| 29 | + public_listings.type, |
| 30 | + public_listings.coordinates |
| 31 | + from public.public_listings |
| 32 | + where public_listings.latitude between min_lat and max_lat |
| 33 | + and public_listings.longitude between min_long and max_long |
| 34 | +$$; |
| 35 | + |
| 36 | +alter function public.listings_in_view( |
| 37 | + double precision, |
| 38 | + double precision, |
| 39 | + double precision, |
| 40 | + double precision |
| 41 | +) owner to postgres; |
| 42 | + |
| 43 | +revoke all privileges on function public.listings_in_view( |
| 44 | + double precision, |
| 45 | + double precision, |
| 46 | + double precision, |
| 47 | + double precision |
| 48 | +) from anon, authenticated, public; |
| 49 | + |
| 50 | +grant execute on function public.listings_in_view( |
| 51 | + double precision, |
| 52 | + double precision, |
| 53 | + double precision, |
| 54 | + double precision |
| 55 | +) to anon, authenticated, service_role; |
| 56 | + |
| 57 | +create or replace function public.latest_chat_messages_for_threads(thread_ids uuid[]) |
| 58 | +returns table( |
| 59 | + id uuid, |
| 60 | + content text, |
| 61 | + created_at timestamp with time zone, |
| 62 | + read_at timestamp with time zone, |
| 63 | + sender_id uuid, |
| 64 | + thread_id uuid |
| 65 | +) |
| 66 | +language sql |
| 67 | +security invoker |
| 68 | +stable |
| 69 | +set search_path = '' |
| 70 | +as $$ |
| 71 | + select distinct on (chat_messages.thread_id) |
| 72 | + chat_messages.id, |
| 73 | + chat_messages.content, |
| 74 | + chat_messages.created_at, |
| 75 | + chat_messages.read_at, |
| 76 | + chat_messages.sender_id, |
| 77 | + chat_messages.thread_id |
| 78 | + from public.chat_messages |
| 79 | + where auth.uid() is not null |
| 80 | + and chat_messages.thread_id = any(thread_ids) |
| 81 | + order by chat_messages.thread_id, chat_messages.created_at desc, chat_messages.id desc |
| 82 | +$$; |
| 83 | + |
| 84 | +alter function public.latest_chat_messages_for_threads(uuid[]) owner to postgres; |
| 85 | + |
| 86 | +revoke all privileges on function public.latest_chat_messages_for_threads(uuid[]) |
| 87 | + from anon, authenticated, public; |
| 88 | + |
| 89 | +grant execute on function public.latest_chat_messages_for_threads(uuid[]) |
| 90 | + to authenticated, service_role; |
| 91 | + |
| 92 | +create or replace function private.enforce_chat_thread_insert() |
| 93 | +returns trigger |
| 94 | +language plpgsql |
| 95 | +security definer |
| 96 | +set search_path = '' |
| 97 | +as $$ |
| 98 | +declare |
| 99 | + jwt_role text; |
| 100 | +begin |
| 101 | + jwt_role := nullif(current_setting('request.jwt.claim.role', true), ''); |
| 102 | + |
| 103 | + if jwt_role = 'service_role' |
| 104 | + or ((select auth.uid()) is null and jwt_role is null) |
| 105 | + then |
| 106 | + return new; |
| 107 | + end if; |
| 108 | + |
| 109 | + if (select auth.uid()) is null or new.initiator_id is distinct from (select auth.uid()) then |
| 110 | + raise exception 'Users can only start chat threads as themselves.' |
| 111 | + using errcode = '42501'; |
| 112 | + end if; |
| 113 | + |
| 114 | + if ( |
| 115 | + select count(*) |
| 116 | + from public.chat_messages |
| 117 | + where sender_id = new.initiator_id |
| 118 | + and created_at >= now() - interval '1 hour' |
| 119 | + ) >= 10 then |
| 120 | + raise exception 'Too many messages sent recently to start a new chat thread.' |
| 121 | + using errcode = '42501'; |
| 122 | + end if; |
| 123 | + |
| 124 | + if ( |
| 125 | + select count(*) |
| 126 | + from public.chat_threads |
| 127 | + where initiator_id = new.initiator_id |
| 128 | + and created_at >= now() - interval '1 hour' |
| 129 | + ) >= 6 then |
| 130 | + raise exception 'Too many chat threads started recently.' |
| 131 | + using errcode = '42501'; |
| 132 | + end if; |
| 133 | + |
| 134 | + return new; |
| 135 | +end; |
| 136 | +$$; |
0 commit comments