-- Bobinoo — notifications sociales de base (28 nov. 2026). Idempotent. -- En plus des abonnements : réaction / commentaire sur mon avis, et activité des comptes que je suis -- (avis donné, série commencée, titre ajouté à « À voir »). Toujours dans l'app, pas de push. -- Les listes perso restent privées : pas de notification quand quelqu'un en crée une. alter table public.notifications add column if not exists item_id text; alter table public.notifications add column if not exists emoji text; alter table public.notifications drop constraint if exists notifications_kind_check; alter table public.notifications add constraint notifications_kind_check check (kind in ('new_follower', 'follow_request', 'unfollow', 'follow_accepted', 'reaction', 'review_comment', 'friend_review', 'friend_started', 'friend_added')); create index if not exists notifications_user_actor_kind_idx on public.notifications (user_id, actor_id, kind); -- Garde commune : jamais entre comptes bloqués, ni d'un compte suspendu, ni à soi-même create or replace function public.azuu_can_notify(p_target uuid, p_actor uuid) returns boolean language sql stable security definer set search_path = '' as $$ select p_target is not null and p_actor is not null and p_target <> p_actor and not exists (select 1 from public.blocks b where (b.blocker_id = p_target and b.blocked_id = p_actor) or (b.blocker_id = p_actor and b.blocked_id = p_target)) and not exists (select 1 from public.profiles p where p.id = p_actor and p.suspended) and exists (select 1 from auth.users u where u.id = p_target) and exists (select 1 from auth.users u where u.id = p_actor) $$; revoke execute on function public.azuu_can_notify(uuid, uuid) from public, anon, authenticated; -- On garde les 200 notifications les plus récentes par compte create or replace function public.azuu_notif_prune(p_user uuid) returns void language sql security definer set search_path = '' as $$ delete from public.notifications n where n.user_id = p_user and n.id < (select n2.id from public.notifications n2 where n2.user_id = p_user order by n2.id desc offset 199 limit 1) $$; revoke execute on function public.azuu_notif_prune(uuid) from public, anon, authenticated; -- ─── Réactions à mon avis ─── create or replace function public.azuu_reaction_notify() returns trigger language plpgsql security definer set search_path = '' as $$ begin if tg_op = 'DELETE' then delete from public.notifications where user_id = old.author_id and actor_id = old.user_id and kind = 'reaction' and item_id = old.item_id; return null; end if; if not public.azuu_can_notify(new.author_id, new.user_id) then return null; end if; -- Une seule ligne par personne et par avis (un changement d'emoji remplace l'ancienne) delete from public.notifications where user_id = new.author_id and actor_id = new.user_id and kind = 'reaction' and item_id = new.item_id; insert into public.notifications (user_id, kind, actor_id, item_id, emoji) values (new.author_id, 'reaction', new.user_id, new.item_id, left(new.emoji, 16)); perform public.azuu_notif_prune(new.author_id); return null; end $$; revoke execute on function public.azuu_reaction_notify() from public, anon, authenticated; drop trigger if exists review_reactions_notify on public.review_reactions; create trigger review_reactions_notify after insert or update or delete on public.review_reactions for each row execute function public.azuu_reaction_notify(); -- ─── Commentaires sur mon avis ─── create or replace function public.azuu_comment_notify() returns trigger language plpgsql security definer set search_path = '' as $$ begin if not public.azuu_can_notify(new.author_id, new.user_id) then return null; end if; -- Plusieurs commentaires de la même personne sur le même avis : une seule ligne (la plus récente) delete from public.notifications where user_id = new.author_id and actor_id = new.user_id and kind = 'review_comment' and item_id = new.item_id and read_at is null; insert into public.notifications (user_id, kind, actor_id, item_id) values (new.author_id, 'review_comment', new.user_id, new.item_id); perform public.azuu_notif_prune(new.author_id); return null; end $$; revoke execute on function public.azuu_comment_notify() from public, anon, authenticated; drop trigger if exists review_comments_notify on public.review_comments; create trigger review_comments_notify after insert on public.review_comments for each row execute function public.azuu_comment_notify(); -- ─── Activité des comptes que je suis ─── -- Envoi à tous les abonnés acceptés. Rafales (import, ajout en série) : une seule ligne non lue -- par abonné, par personne et par type sur 3 heures, qui montre le titre le plus récent. 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 f record; begin if exists (select 1 from public.profiles p where p.id = p_actor and p.suspended) then return; end if; for f in select follower_id from public.follows where following_id = p_actor and status = 'accepted' limit 2000 loop if not public.azuu_can_notify(f.follower_id, p_actor) then continue; end if; delete from public.notifications where user_id = f.follower_id and actor_id = p_actor and kind = p_kind and (item_id = p_item or (read_at is null and created_at > now() - interval '3 hours')); insert into public.notifications (user_id, kind, actor_id, item_id) values (f.follower_id, p_kind, p_actor, p_item); perform public.azuu_notif_prune(f.follower_id); end loop; end $$; revoke execute on function public.azuu_notify_followers(uuid, text, text) from public, anon, authenticated; create or replace function public.azuu_item_notify() returns trigger language plpgsql security definer set search_path = '' as $$ declare st text; old_st text; old_notes text := ''; recent date := current_date - 7; begin if new.deleted or new.data is null then return null; end if; st := new.data->>'status'; if tg_op = 'UPDATE' then -- Fiche restaurée (après suppression) : ce n'est pas une nouvelle activité if old.deleted or old.data is null then return null; end if; old_st := old.data->>'status'; old_notes := coalesce(old.data->>'notes', ''); end if; -- Avis : quand un avis apparaît sur une fiche (pas à chaque retouche) if old_notes = '' and coalesce(new.data->>'notes', '') <> '' then perform public.azuu_notify_followers(new.user_id, 'friend_review', new.id); end if; -- Série / anime / drama commencé (récemment) if st = 'en_cours' and old_st is distinct from 'en_cours' and coalesce(new.data->>'type', '') <> 'film' and coalesce(public.azuu_day(new.data->>'startDate'), current_date) >= recent then perform public.azuu_notify_followers(new.user_id, 'friend_started', new.id); end if; -- Ajouté à « À voir » if tg_op = 'INSERT' and st = 'a_voir' and new.updated_at > now() - interval '2 days' then perform public.azuu_notify_followers(new.user_id, 'friend_added', new.id); end if; return null; end $$; revoke execute on function public.azuu_item_notify() from public, anon, authenticated; drop trigger if exists items_notify on public.items; create trigger items_notify after insert or update on public.items for each row execute function public.azuu_item_notify(); -- ─── Lecture ─── -- Le titre concerné est joint (fiche publique, sans les champs privés) ; une fiche que je ne peux plus voir est omise. create or replace function public.get_notifications() returns jsonb language sql stable security definer set search_path = '' as $$ select coalesce(jsonb_agg(jsonb_build_object( 'id', n.id, 'kind', n.kind, 'created_at', n.created_at, 'read', n.read_at is not null, 'emoji', n.emoji, 'actor', public.profile_card(p), 'item', it.item) order by n.id desc), '[]'::jsonb) from ( select * from public.notifications where user_id = auth.uid() and public.azuu_mfa_ok() order by id desc limit 100 ) n join public.profiles p on p.id = n.actor_id and not p.suspended left join lateral ( select public.public_item(i.data) as item from public.items i where n.item_id is not null and not i.deleted and i.id = n.item_id and i.user_id = case when n.kind in ('reaction', 'review_comment') then n.user_id else n.actor_id end and (n.kind in ('reaction', 'review_comment') or public.can_view(n.actor_id)) ) it on true where not exists (select 1 from public.blocks b where (b.blocker_id = auth.uid() and b.blocked_id = n.actor_id) or (b.blocker_id = n.actor_id and b.blocked_id = auth.uid())) and (n.item_id is null or it.item is not null) $$; create or replace function public.unread_notifications() returns integer language sql stable security definer set search_path = '' as $$ select count(*)::int from public.notifications n join public.profiles p on p.id = n.actor_id and not p.suspended where n.user_id = auth.uid() and public.azuu_mfa_ok() and n.read_at is null and not exists (select 1 from public.blocks b where (b.blocker_id = auth.uid() and b.blocked_id = n.actor_id) or (b.blocker_id = n.actor_id and b.blocked_id = auth.uid())) $$; revoke execute on function public.get_notifications() from public, anon; revoke execute on function public.unread_notifications() from public, anon; grant execute on function public.get_notifications() to authenticated; grant execute on function public.unread_notifications() to authenticated;