-- Choix des notifications (Réglages → Notifications, 01/12/2026). Idempotent. -- Les types désactivés sont dans les réglages synchronisés du compte : settings.data->'notifOff' (liste de types). -- Une notification d'un type désactivé n'est jamais créée (trigger), et celles déjà reçues sont masquées -- de la liste et du compteur. create or replace function public.azuu_notif_wanted(p_user uuid, p_kind text) returns boolean language sql stable security definer set search_path = '' as $$ select not coalesce(( select jsonb_typeof(s.data->'notifOff') = 'array' and (s.data->'notifOff') ? p_kind from public.settings s where s.user_id = p_user ), false); $$; revoke all on function public.azuu_notif_wanted(uuid, text) from public, anon, authenticated; comment on function public.azuu_notif_wanted(uuid, text) is 'Règle : ce compte veut-il ce type de notification (Réglages → Notifications) ?'; create or replace function public.azuu_notif_filter() returns trigger language plpgsql security definer set search_path = '' as $$ begin if not public.azuu_notif_wanted(new.user_id, new.kind) then return null; end if; return new; end; $$; revoke all on function public.azuu_notif_filter() from public, anon, authenticated; comment on function public.azuu_notif_filter() is 'Trigger (notifications) : une notification d''un type désactivé par son destinataire n''est pas créée.'; drop trigger if exists notifications_wanted on public.notifications; create trigger notifications_wanted before insert on public.notifications for each row execute function public.azuu_notif_filter(); create or replace function public.get_notifications() returns jsonb language sql stable security definer set search_path = '' as $$ with off as ( select coalesce(case when jsonb_typeof(s.data->'notifOff') = 'array' then s.data->'notifOff' end, '[]'::jsonb) as kinds from public.settings s where s.user_id = auth.uid() ) 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 nn where nn.user_id = auth.uid() and public.azuu_mfa_ok() and not exists (select 1 from off where off.kinds ? nn.kind) order by nn.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) $$; comment on function public.get_notifications() is 'App : mes notifications, avec le titre lié (sans les types que j''ai désactivés).'; 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 public.azuu_notif_wanted(auth.uid(), n.kind) 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())) $$; comment on function public.unread_notifications() is 'App : nombre de notifications non lues (sans les types que j''ai désactivés).';