Files
bobinoo/supabase/migrations/20261127120000_follow_notifications.sql
Claude c66f0f62a6 Notifications d'abonnement (et rien d'autre)
- Cloche sur l'accueil (avec le nombre non lu) : qui s'abonne, demande à s'abonner,
  se désabonne, ou accepte ma demande ; accepter/refuser une demande directement depuis la liste
- Serveur (migration 20261127120000, déjà appliquée) : déclencheur sur follows, une ligne par
  personne et par sens, 200 max par compte ; rien entre comptes bloqués ou suspendus ;
  retirer un abonné, bloquer ou supprimer un compte ne notifie pas
- Fonctions get_notifications / unread_notifications / mark_notifications_read (comptes connectés seulement)
- Textes dans les 18 langues

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01YVo1qxKoBQCaMd7Ue8PGbe
2026-11-27 16:09:55 +00:00

108 lines
6.0 KiB
PL/PgSQL

-- Bobinoo — notifications d'abonnement uniquement (27 novembre 2026). Idempotent.
-- Dans l'app (pas de push) : quelqu'un s'abonne, demande à s'abonner, se désabonne, ou accepte ma demande.
-- Rien n'est créé entre deux comptes qui se bloquent, ni par un compte suspendu.
-- Nouveau type « s'est désabonné »
alter table public.notifications drop constraint if exists notifications_kind_check;
alter table public.notifications add constraint notifications_kind_check
check (kind = any (array['follow_request', 'follow_accepted', 'new_follower', 'unfollow',
'reaction', 'new_episode', 'new_season', 'review_comment', 'shared_list_invite']));
create or replace function public.azuu_follow_notify()
returns trigger language plpgsql security definer set search_path = '' as $$
declare
target uuid;
actor uuid;
k text;
begin
if tg_op = 'INSERT' then
target := new.following_id; actor := new.follower_id;
k := case when new.status = 'accepted' then 'new_follower' else 'follow_request' end;
elsif tg_op = 'UPDATE' then
if not (old.status = 'pending' and new.status = 'accepted') then return null; end if;
-- Ma demande acceptée : prévenir le demandeur ; la demande devient un abonné chez le compte suivi
perform 1 from public.blocks b
where (b.blocker_id = new.follower_id and b.blocked_id = new.following_id)
or (b.blocker_id = new.following_id and b.blocked_id = new.follower_id);
if found then return null; end if;
update public.notifications set kind = 'new_follower', read_at = coalesce(read_at, now())
where user_id = new.following_id and actor_id = new.follower_id and kind = 'follow_request';
target := new.follower_id; actor := new.following_id; k := 'follow_accepted';
else
-- Désabonnement : seulement quand l'abonné part de lui-même (pas « retirer un abonné », ni blocage, ni compte supprimé)
if auth.uid() is distinct from old.follower_id then return null; end if;
if old.status = 'pending' then
-- Demande annulée : elle disparaît simplement
delete from public.notifications where user_id = old.following_id and actor_id = old.follower_id and kind = 'follow_request';
return null;
end if;
target := old.following_id; actor := old.follower_id; k := 'unfollow';
end if;
if target is null or actor is null or target = actor then return null; end if;
perform 1 from public.blocks b
where (b.blocker_id = target and b.blocked_id = actor) or (b.blocker_id = actor and b.blocked_id = target);
if found then return null; end if;
if exists (select 1 from public.profiles p where p.id = actor and p.suspended) then return null; end if;
-- Compte en cours de suppression (cascade) : rien à notifier, et surtout ne pas bloquer la suppression
if not exists (select 1 from auth.users u where u.id = actor) or not exists (select 1 from auth.users u where u.id = target) then return null; end if;
-- Une seule ligne par personne et par sens (abonné / désabonné) : s'abonner-désabonner en boucle ne remplit pas la liste
delete from public.notifications
where user_id = target and actor_id = actor
and case when k = 'follow_accepted' then kind = 'follow_accepted'
else kind in ('new_follower', 'follow_request', 'unfollow') end;
insert into public.notifications (user_id, kind, actor_id) values (target, k, actor);
-- On garde les 200 plus récentes
delete from public.notifications n
where n.user_id = target
and n.id < (select n2.id from public.notifications n2 where n2.user_id = target order by n2.id desc offset 199 limit 1);
return null;
end $$;
revoke execute on function public.azuu_follow_notify() from public, anon, authenticated;
drop trigger if exists follows_notify on public.follows;
create trigger follows_notify after insert or update or delete on public.follows
for each row execute function public.azuu_follow_notify();
-- Mes notifications (les 100 dernières), avec le profil de la personne
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,
'actor', public.profile_card(p)) order by n.id desc), '[]'::jsonb)
from (
select * from public.notifications
where user_id = auth.uid() and kind in ('new_follower', 'follow_request', 'unfollow', 'follow_accepted')
order by id desc limit 100
) n
join public.profiles p on p.id = n.actor_id and not p.suspended
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()))
$$;
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 n.read_at is null
and n.kind in ('new_follower', 'follow_request', 'unfollow', 'follow_accepted')
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()))
$$;
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 read_at is null
$$;
revoke execute on function public.get_notifications() from public, anon;
revoke execute on function public.unread_notifications() from public, anon;
revoke execute on function public.mark_notifications_read() from public, anon;
grant execute on function public.get_notifications() to authenticated;
grant execute on function public.unread_notifications() to authenticated;
grant execute on function public.mark_notifications_read() to authenticated;