Files
bobinoo/supabase/migrations/20261201110000_notification_prefs.sql
T
Claude 43e2ecd589
deploy / deploy (push) Successful in 1m12s
Réglages : choisir ses notifications ; journal de la console lisible
- Réglages → Notifications (comptes connectés) : un interrupteur par type, en 3 groupes (abonnements,
  mes avis, activité des comptes suivis), 18 langues. Choix synchronisé (settings.notifOff).
- Serveur (20261201110000, appliquée) : une notification d'un type désactivé n'est pas créée (trigger
  notifications_wanted) ; celles déjà reçues sont masquées de la liste et du compteur.
- Console : les détails des connexions étaient enregistrés comme texte JSON et affichés lettre par
  lettre. Enregistrement corrigé (sql.json), anciennes lignes remises en forme à la lecture
  (20261201100000), appareil affiché en clair (« Chrome · Windows »).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01YVo1qxKoBQCaMd7Ue8PGbe
2026-12-01 10:10:33 +00:00

73 lines
3.9 KiB
PL/PgSQL

-- 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).';