-- Écritures plus légères (audit du 29/11) : rien ne change pour l'utilisateur, c'est la vitesse qui change. -- 1. Notifications aux abonnés : une seule requête pour tous les abonnés, au lieu d'une boucle (≈0,7 ms par -- abonné et par événement : un compte suivi par 200 personnes approchait la limite de 8 s à l'import). -- 2. Quota de fiches et de listes : vérifié une fois par envoi, et non à chaque ligne (count(*) répété : le coût -- d'un import grandissait comme le carré du nombre de fiches). -- 3. Une fiche renvoyée à l'identique n'est plus réécrite : les autres appareils ne la retéléchargent plus. -- 4. Ménage : index jamais utilisé. -- Idempotent. -- 1. ─── Notifications ─── create or replace function public.azuu_notify_followers(p_actor uuid, p_kind text, p_item text) returns void language plpgsql security definer set search_path = '' as $$ declare prune uuid[]; begin if p_actor is null or exists (select 1 from public.profiles p where p.id = p_actor and p.suspended) then return; end if; -- Mêmes règles qu'avant (azuu_can_notify) : pas soi-même, personne de bloqué dans un sens ou dans l'autre. -- L'existence des deux comptes est garantie par les clés étrangères de follows. with f as ( select fo.follower_id from public.follows fo where fo.following_id = p_actor and fo.status = 'accepted' and fo.follower_id <> p_actor and not exists ( select 1 from public.blocks b where (b.blocker_id = fo.follower_id and b.blocked_id = p_actor) or (b.blocker_id = p_actor and b.blocked_id = fo.follower_id) ) limit 2000 ), -- Regroupement : la même nouvelle pour le même titre, ou une rafale non lue des 3 dernières heures, est remplacée gone as ( delete from public.notifications n using f where n.user_id = f.follower_id and n.actor_id = p_actor and n.kind = p_kind and (n.item_id = p_item or (n.read_at is null and n.created_at > now() - interval '3 hours')) ), ins as ( insert into public.notifications (user_id, kind, actor_id, item_id) select f.follower_id, p_kind, p_actor, p_item from f returning user_id, id ) -- Au plus 200 notifications par personne : le ménage est fait de temps en temps (≈ 1 ajout sur 32), -- et à chaque lecture (mark_notifications_read), plutôt qu'à chaque ajout pour chaque abonné select coalesce(array_agg(ins.user_id) filter (where ins.id % 32 = 0), '{}') into prune from ins; if cardinality(prune) > 0 then delete from public.notifications n using unnest(prune) as u(uid) where n.user_id = u.uid and n.id < (select n2.id from public.notifications n2 where n2.user_id = u.uid order by n2.id desc offset 199 limit 1); end if; end $$; revoke all on function public.azuu_notify_followers(uuid, text, text) from public, anon, authenticated; create or replace function public.mark_notifications_read() returns void language sql security definer set search_path = '' as $$ update public.notifications set read_at = now() where user_id = auth.uid() and public.azuu_mfa_ok() and read_at is null; select public.azuu_notif_prune(auth.uid()) where auth.uid() is not null and public.azuu_mfa_ok(); $$; -- 2. ─── Quota : une vérification par envoi ─── create or replace function public.azuu_quota_stmt() returns trigger language plpgsql security definer set search_path = '' as $$ declare over boolean; begin -- Lignes réellement ajoutées par cet envoi (une mise à jour d'une fiche existante n'y figure pas) execute format( 'select exists (select 1 from (select distinct user_id from new_rows) u where (select count(*) from public.%I t where t.user_id = u.user_id and not t.deleted) > $1)', tg_table_name ) into over using case tg_table_name when 'items' then 20000 else 300 end; if over then raise exception 'quota exceeded for %', tg_table_name; end if; return null; end $$; revoke all on function public.azuu_quota_stmt() from public, anon, authenticated; drop trigger if exists items_quota on public.items; drop trigger if exists items_quota_stmt on public.items; create trigger items_quota_stmt after insert on public.items referencing new table as new_rows for each statement execute function public.azuu_quota_stmt(); drop trigger if exists lists_quota on public.lists; drop trigger if exists lists_quota_stmt on public.lists; create trigger lists_quota_stmt after insert on public.lists referencing new table as new_rows for each statement execute function public.azuu_quota_stmt(); -- 3. ─── Fiche renvoyée à l'identique : pas de réécriture ─── create or replace function public.azuu_before_write() returns trigger language plpgsql set search_path = '' as $$ begin -- Un appareil resté hors-ligne ne peut pas écraser une version plus récente if tg_op = 'UPDATE' and new.updated_at < old.updated_at then return null; end if; -- Personne ne peut déplacer une ligne vers un autre compte if tg_op = 'UPDATE' and new.user_id <> old.user_id then raise exception 'user_id is immutable'; end if; -- Exactement la même version : rien à écrire (la date serveur ne bouge pas, les autres appareils ne la relisent pas) if tg_op = 'UPDATE' and new.updated_at = old.updated_at and new.deleted = old.deleted and new.data is not distinct from old.data then return null; end if; -- Date venant du client bornée (pas de date dans le futur lointain qui bloquerait les mises à jour) if new.updated_at > now() + interval '1 day' then new.updated_at := now(); end if; new.server_updated_at := clock_timestamp(); return new; end; $$; -- 4. ─── Ménage ─── drop index if exists public.profiles_display_name_idx;