Fusion social (spoilers, commentaires, listes partagées) + raccordement à la modération
- Signalement des commentaires et des listes partagées : copie du contenu, aperçu admin et suppression branchés dans report_content / admin_reports / admin_report_action. - Un compte bloqué ne peut plus commenter mes avis (déclencheur), et ses commentaires sont masqués côté client. - La migration défis retire à la fois tags et episodeLog des fiches publiques. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01YVo1qxKoBQCaMd7Ue8PGbe
This commit is contained in:
42 files changed
+3535
-35
No files matched your search
@@ -0,0 +1,233 @@
|
||||
-- ════════════════════════════════════════════════════════════════════════════
|
||||
-- AzuuCine — Avis : spoilers et commentaires (9 octobre 2026)
|
||||
-- 1. Spoilers : l'avis peut être marqué « contient des spoilers ». Le drapeau vit dans la
|
||||
-- fiche synchronisée (items.data->'notesSpoiler', comme l'avis lui-même) ; title_reviews
|
||||
-- le renvoie pour que l'app masque le texte jusqu'au tap.
|
||||
-- 2. Commentaires sous les avis (≤ 500 caractères) : visibles par ceux qui voient l'avis
|
||||
-- (can_view sur l'auteur de l'avis, comptes suspendus exclus), anti-spam (10/minute),
|
||||
-- suppression par l'auteur du commentaire, l'auteur de l'avis ou un admin.
|
||||
-- Notification « review_comment » à l'auteur de l'avis (préférence notifPrefs.comments).
|
||||
-- 3. Nouveaux types de notification : review_comment, shared_list_invite (listes partagées,
|
||||
-- voir …130100_shared_lists.sql) + préférences notifPrefs.comments / notifPrefs.lists.
|
||||
--
|
||||
-- À coller dans Supabase → SQL Editor → Run, APRÈS …20261007120000_social_notifications.sql.
|
||||
-- Idempotent (réexécutable sans risque).
|
||||
-- ════════════════════════════════════════════════════════════════════════════
|
||||
|
||||
-- ── Nouveaux types de notification ──
|
||||
alter table public.notifications drop constraint if exists notifications_kind_check;
|
||||
alter table public.notifications add constraint notifications_kind_check
|
||||
check (kind in ('follow_request','follow_accepted','new_follower','reaction','new_episode','new_season',
|
||||
'review_comment','shared_list_invite'));
|
||||
|
||||
-- Préférences (oui par défaut) : settings.data.notifPrefs = { episodes, follows, accepted, reactions, comments, lists }
|
||||
create or replace function public.azuu_wants(p_user uuid, p_kind text)
|
||||
returns boolean language sql stable security definer set search_path = '' as $$
|
||||
select coalesce((
|
||||
select (s.data->'notifPrefs'->>(case p_kind
|
||||
when 'follow_request' then 'follows'
|
||||
when 'new_follower' then 'follows'
|
||||
when 'follow_accepted' then 'accepted'
|
||||
when 'reaction' then 'reactions'
|
||||
when 'review_comment' then 'comments'
|
||||
when 'shared_list_invite' then 'lists'
|
||||
else 'episodes' end)) is distinct from 'false'
|
||||
from public.settings s where s.user_id = p_user
|
||||
), true)
|
||||
$$;
|
||||
revoke all on function public.azuu_wants(uuid, text) from public, anon, authenticated;
|
||||
|
||||
-- ── Commentaires ──
|
||||
create table if not exists public.review_comments (
|
||||
id bigint generated always as identity primary key,
|
||||
author_id uuid not null references auth.users(id) on delete cascade, -- auteur de l'avis
|
||||
item_id text not null check (item_id ~ '^[A-Za-z0-9_-]{1,64}$'), -- fiche de l'auteur
|
||||
user_id uuid not null default auth.uid() references auth.users(id) on delete cascade, -- qui commente
|
||||
body text not null check (char_length(body) between 1 and 500),
|
||||
created_at timestamptz not null default now()
|
||||
);
|
||||
create index if not exists review_comments_target_idx on public.review_comments (author_id, item_id, created_at);
|
||||
create index if not exists review_comments_user_idx on public.review_comments (user_id, created_at desc);
|
||||
alter table public.review_comments enable row level security;
|
||||
alter table public.review_comments force row level security;
|
||||
-- Aucun accès direct : tout passe par les fonctions ci-dessous (qui vérifient les droits)
|
||||
revoke all on public.review_comments from public, anon, authenticated;
|
||||
|
||||
-- L'avis (auteur, fiche) existe-t-il et suis-je autorisé à le voir ?
|
||||
create or replace function public.azuu_review_visible(p_author uuid, p_item text)
|
||||
returns boolean language sql stable security definer set search_path = '' as $$
|
||||
select auth.uid() is not null
|
||||
and p_author is not null and p_item is not null
|
||||
and public.can_view(p_author)
|
||||
and exists (
|
||||
select 1 from public.items i join public.profiles pr on pr.id = i.user_id
|
||||
where i.user_id = p_author and i.id = p_item and not i.deleted
|
||||
and coalesce(i.data->>'notes', '') <> ''
|
||||
and (not pr.suspended or p_author = auth.uid())
|
||||
)
|
||||
$$;
|
||||
revoke all on function public.azuu_review_visible(uuid, text) from public, anon, authenticated;
|
||||
|
||||
-- Fil de commentaires d'un avis (200 derniers, du plus ancien au plus récent)
|
||||
create or replace function public.get_review_comments(p_author uuid, p_item text)
|
||||
returns jsonb language plpgsql stable security definer set search_path = '' as $$
|
||||
declare
|
||||
me uuid := auth.uid();
|
||||
moderator boolean;
|
||||
begin
|
||||
if not public.azuu_review_visible(p_author, p_item) then
|
||||
return jsonb_build_object('visible', false, 'can_moderate', false, 'comments', '[]'::jsonb);
|
||||
end if;
|
||||
moderator := p_author = me or public.is_admin();
|
||||
return jsonb_build_object(
|
||||
'visible', true,
|
||||
'can_moderate', moderator,
|
||||
'comments', coalesce((
|
||||
select jsonb_agg(jsonb_build_object(
|
||||
'id', c.id, 'body', c.body, 'created_at', c.created_at,
|
||||
'user', jsonb_build_object('id', p.id, 'username', p.username, 'display_name', p.display_name, 'avatar_url', p.avatar_url),
|
||||
'mine', c.user_id = me,
|
||||
'can_delete', moderator or c.user_id = me
|
||||
) order by c.created_at, c.id)
|
||||
from (
|
||||
select * from public.review_comments rc
|
||||
where rc.author_id = p_author and rc.item_id = p_item
|
||||
order by rc.created_at desc, rc.id desc
|
||||
limit 200
|
||||
) c
|
||||
join public.profiles p on p.id = c.user_id
|
||||
where not p.suspended or c.user_id = me
|
||||
), '[]'::jsonb)
|
||||
);
|
||||
end;
|
||||
$$;
|
||||
|
||||
-- Écrire un commentaire (renvoie le commentaire créé)
|
||||
create or replace function public.add_review_comment(p_author uuid, p_item text, p_body text)
|
||||
returns jsonb language plpgsql security definer set search_path = '' as $$
|
||||
declare
|
||||
me uuid := auth.uid();
|
||||
b text;
|
||||
c public.review_comments;
|
||||
p public.profiles;
|
||||
begin
|
||||
if me is null then raise exception 'auth'; end if;
|
||||
if not public.azuu_review_visible(p_author, p_item) then raise exception 'review not found'; end if;
|
||||
select * into p from public.profiles where id = me;
|
||||
if not found or p.suspended then raise exception 'forbidden'; end if;
|
||||
-- Caractères de contrôle retirés (sauf retours à la ligne), pas plus de 2 lignes vides d'affilée
|
||||
b := regexp_replace(coalesce(p_body, ''), '\r\n?', E'\n', 'g');
|
||||
b := regexp_replace(b, '[\x00-\x08\x0B\x0C\x0E-\x1F\x7F]', '', 'g');
|
||||
b := regexp_replace(b, '\n{3,}', E'\n\n', 'g');
|
||||
b := regexp_replace(b, '^\s+|\s+$', '', 'g'); -- espaces ET retours à la ligne en début / fin
|
||||
if char_length(b) < 1 or char_length(b) > 500 then raise exception 'invalid comment'; end if;
|
||||
-- Anti-spam : 10 commentaires par minute, 300 par jour
|
||||
if (select count(*) from public.review_comments where user_id = me and created_at > now() - interval '1 minute') >= 10
|
||||
or (select count(*) from public.review_comments where user_id = me and created_at > now() - interval '1 day') >= 300 then
|
||||
raise exception 'rate limit';
|
||||
end if;
|
||||
if (select count(*) from public.review_comments where author_id = p_author and item_id = p_item) >= 1000 then
|
||||
raise exception 'too many comments';
|
||||
end if;
|
||||
insert into public.review_comments (author_id, item_id, user_id, body)
|
||||
values (p_author, p_item, me, b)
|
||||
returning * into c;
|
||||
-- Notifie l'auteur de l'avis (jamais soi-même ; selon ses préférences)
|
||||
perform public.azuu_notify(p_author, 'review_comment', me, p_item, null);
|
||||
return jsonb_build_object(
|
||||
'id', c.id, 'body', c.body, 'created_at', c.created_at,
|
||||
'user', jsonb_build_object('id', p.id, 'username', p.username, 'display_name', p.display_name, 'avatar_url', p.avatar_url),
|
||||
'mine', true, 'can_delete', true
|
||||
);
|
||||
end;
|
||||
$$;
|
||||
|
||||
-- Supprimer un commentaire : le sien, ceux sous son avis, ou tout (admin)
|
||||
create or replace function public.delete_review_comment(p_id bigint)
|
||||
returns jsonb language plpgsql security definer set search_path = '' as $$
|
||||
declare
|
||||
me uuid := auth.uid();
|
||||
n integer;
|
||||
begin
|
||||
if me is null then raise exception 'auth'; end if;
|
||||
delete from public.review_comments c
|
||||
where c.id = p_id and (c.user_id = me or c.author_id = me or public.is_admin());
|
||||
get diagnostics n = row_count;
|
||||
return jsonb_build_object('ok', n > 0);
|
||||
end;
|
||||
$$;
|
||||
|
||||
-- Avis effacé (texte vidé, fiche supprimée, modération) : ses commentaires partent avec lui
|
||||
create or replace function public.azuu_review_cleanup()
|
||||
returns trigger language plpgsql security definer set search_path = '' as $$
|
||||
begin
|
||||
delete from public.review_comments where author_id = new.user_id and item_id = new.id;
|
||||
return null;
|
||||
end;
|
||||
$$;
|
||||
drop trigger if exists items_review_cleanup on public.items;
|
||||
create trigger items_review_cleanup after update on public.items
|
||||
for each row
|
||||
when (coalesce(old.data->>'notes', '') <> '' and (new.deleted or coalesce(new.data->>'notes', '') = ''))
|
||||
execute function public.azuu_review_cleanup();
|
||||
|
||||
-- ── Tous les avis sur un titre : + drapeau spoiler et nombre de commentaires ──
|
||||
create or replace function public.title_reviews(p_external_id text, p_offset integer default 0)
|
||||
returns jsonb language plpgsql stable security definer set search_path = '' as $$
|
||||
declare me uuid := auth.uid(); res jsonb;
|
||||
begin
|
||||
if me is null or p_external_id !~ '^(tmdb:(movie|tv):[0-9]{1,10}|anilist:[0-9]{1,10})$' then
|
||||
return jsonb_build_object('friends', '[]'::jsonb, 'others', '[]'::jsonb);
|
||||
end if;
|
||||
with revs as (
|
||||
select i.user_id, i.id as item_id, i.updated_at,
|
||||
nullif(i.data->>'rating','')::numeric as rating,
|
||||
i.data->>'notes' as notes,
|
||||
coalesce(i.data->>'notesSpoiler', '') = 'true' as spoiler,
|
||||
pr,
|
||||
exists(select 1 from public.follows f where f.follower_id = me and f.following_id = i.user_id and f.status = 'accepted') as friend
|
||||
from public.items i
|
||||
join public.profiles pr on pr.id = i.user_id
|
||||
where i.data->>'externalId' = p_external_id
|
||||
and not i.deleted and coalesce(i.data->>'notes','') <> ''
|
||||
and i.user_id <> me and not pr.suspended
|
||||
and (not pr.is_private
|
||||
or exists(select 1 from public.follows f where f.follower_id = me and f.following_id = i.user_id and f.status = 'accepted'))
|
||||
),
|
||||
enriched as (
|
||||
select r.*,
|
||||
(select coalesce(jsonb_object_agg(e.emoji, e.c), '{}'::jsonb)
|
||||
from (select emoji, count(*) c from public.review_reactions rr
|
||||
where rr.author_id = r.user_id and rr.item_id = r.item_id group by emoji) e) as reactions,
|
||||
(select count(*) from public.review_reactions rr where rr.author_id = r.user_id and rr.item_id = r.item_id) as total,
|
||||
(select emoji from public.review_reactions rr where rr.author_id = r.user_id and rr.item_id = r.item_id and rr.user_id = me) as mine,
|
||||
(select count(*) from public.review_comments rc join public.profiles cp on cp.id = rc.user_id
|
||||
where rc.author_id = r.user_id and rc.item_id = r.item_id and (not cp.suspended or rc.user_id = me)) as comments
|
||||
from revs r
|
||||
)
|
||||
select jsonb_build_object(
|
||||
'friends', coalesce((select jsonb_agg(entry order by total desc, updated_at desc) from (
|
||||
select jsonb_build_object('user', public.profile_card(pr), 'user_id', user_id, 'item_id', item_id,
|
||||
'updated_at', updated_at, 'rating', rating, 'notes', notes, 'spoiler', spoiler,
|
||||
'reactions', reactions, 'total', total, 'mine', mine, 'comments', comments) entry, total, updated_at
|
||||
from enriched where friend offset greatest(p_offset, 0) limit 40) f), '[]'::jsonb),
|
||||
'others', coalesce((select jsonb_agg(entry order by total desc, updated_at desc) from (
|
||||
select jsonb_build_object('user', public.profile_card(pr), 'user_id', user_id, 'item_id', item_id,
|
||||
'updated_at', updated_at, 'rating', rating, 'notes', notes, 'spoiler', spoiler,
|
||||
'reactions', reactions, 'total', total, 'mine', mine, 'comments', comments) entry, total, updated_at
|
||||
from enriched where not friend offset greatest(p_offset, 0) limit 40) o), '[]'::jsonb)
|
||||
) into res;
|
||||
return res;
|
||||
end;
|
||||
$$;
|
||||
|
||||
-- ── Droits (gate interne : auth.uid() / can_view / is_admin) ──
|
||||
revoke all on function public.azuu_review_cleanup() from public, anon, authenticated;
|
||||
revoke all on function public.get_review_comments(uuid, text) from public, anon;
|
||||
revoke all on function public.add_review_comment(uuid, text, text) from public, anon;
|
||||
revoke all on function public.delete_review_comment(bigint) from public, anon;
|
||||
revoke all on function public.title_reviews(text, integer) from public, anon;
|
||||
grant execute on function public.get_review_comments(uuid, text) to authenticated;
|
||||
grant execute on function public.add_review_comment(uuid, text, text) to authenticated;
|
||||
grant execute on function public.delete_review_comment(bigint) to authenticated;
|
||||
grant execute on function public.title_reviews(text, integer) to authenticated;
|
||||
@@ -0,0 +1,418 @@
|
||||
-- ════════════════════════════════════════════════════════════════════════════
|
||||
-- AzuuCine — Listes partagées / collaboratives (9 octobre 2026)
|
||||
-- - Le propriétaire crée une liste et invite des membres (parmi ceux qu'il suit ou qui le suivent).
|
||||
-- - Invitation = notification « shared_list_invite » ; le membre doit accepter.
|
||||
-- - Chaque membre ajoute / retire des titres (instantané minimal : référence TMDB/AniList, titre,
|
||||
-- affiche, type, année) et voit qui a ajouté quoi. On peut quitter une liste ; le propriétaire
|
||||
-- peut retirer un membre, renommer ou supprimer la liste.
|
||||
-- - Les listes perso (public.lists) ne changent pas : elles restent privées.
|
||||
--
|
||||
-- Sécurité : RLS activé, AUCUN accès direct aux tables ; tout passe par des fonctions
|
||||
-- security definer qui vérifient l'appartenance (seuls les membres lisent / écrivent).
|
||||
--
|
||||
-- À coller dans Supabase → SQL Editor → Run, APRÈS …20261009130000_review_spoilers_comments.sql.
|
||||
-- Idempotent (réexécutable sans risque).
|
||||
-- ════════════════════════════════════════════════════════════════════════════
|
||||
|
||||
-- ── Tables ──
|
||||
create table if not exists public.shared_lists (
|
||||
id uuid primary key default gen_random_uuid(),
|
||||
owner_id uuid not null default auth.uid() references auth.users(id) on delete cascade,
|
||||
name text not null check (char_length(name) between 1 and 80),
|
||||
created_at timestamptz not null default now(),
|
||||
updated_at timestamptz not null default now()
|
||||
);
|
||||
create index if not exists shared_lists_owner_idx on public.shared_lists (owner_id, created_at desc);
|
||||
|
||||
-- Le propriétaire est aussi membre (status 'accepted') : une seule règle d'appartenance.
|
||||
create table if not exists public.shared_list_members (
|
||||
list_id uuid not null references public.shared_lists(id) on delete cascade,
|
||||
user_id uuid not null references auth.users(id) on delete cascade,
|
||||
status text not null default 'pending' check (status in ('pending', 'accepted')),
|
||||
-- Qui a invité : gardé en « set null » pour que la liste survive à la suppression d'un compte
|
||||
invited_by uuid references auth.users(id) on delete set null,
|
||||
created_at timestamptz not null default now(),
|
||||
primary key (list_id, user_id)
|
||||
);
|
||||
create index if not exists shared_list_members_user_idx on public.shared_list_members (user_id, status);
|
||||
create index if not exists shared_list_members_inviter_idx on public.shared_list_members (invited_by, created_at desc);
|
||||
|
||||
create table if not exists public.shared_list_items (
|
||||
list_id uuid not null references public.shared_lists(id) on delete cascade,
|
||||
external_id text not null check (external_id ~ '^(tmdb:(movie|tv):[0-9]{1,10}|anilist:[0-9]{1,10})$'),
|
||||
title text not null check (char_length(title) between 1 and 300),
|
||||
poster text check (poster is null or (char_length(poster) <= 300
|
||||
and poster ~ '^https://(image\.tmdb\.org|s4\.anilist\.co)/[A-Za-z0-9/_.-]+$')),
|
||||
type text not null check (type in ('film', 'serie', 'anime', 'kdrama', 'cdrama', 'autre')),
|
||||
year integer check (year is null or year between 1870 and 2200),
|
||||
-- Qui l'a ajouté : « set null » à la suppression du compte (le titre reste pour les autres membres)
|
||||
added_by uuid references auth.users(id) on delete set null,
|
||||
added_at timestamptz not null default now(),
|
||||
primary key (list_id, external_id)
|
||||
);
|
||||
create index if not exists shared_list_items_list_idx on public.shared_list_items (list_id, added_at desc);
|
||||
create index if not exists shared_list_items_adder_idx on public.shared_list_items (added_by, added_at desc);
|
||||
|
||||
alter table public.shared_lists enable row level security;
|
||||
alter table public.shared_lists force row level security;
|
||||
alter table public.shared_list_members enable row level security;
|
||||
alter table public.shared_list_members force row level security;
|
||||
alter table public.shared_list_items enable row level security;
|
||||
alter table public.shared_list_items force row level security;
|
||||
revoke all on public.shared_lists, public.shared_list_members, public.shared_list_items from public, anon, authenticated;
|
||||
|
||||
-- ── Outils internes ──
|
||||
-- Suis-je membre (accepté) de cette liste ?
|
||||
create or replace function public.azuu_sl_member(p_list uuid)
|
||||
returns boolean language sql stable security definer set search_path = '' as $$
|
||||
select auth.uid() is not null and exists (
|
||||
select 1 from public.shared_list_members m
|
||||
where m.list_id = p_list and m.user_id = auth.uid() and m.status = 'accepted'
|
||||
)
|
||||
$$;
|
||||
|
||||
create or replace function public.azuu_sl_owner(p_list uuid)
|
||||
returns boolean language sql stable security definer set search_path = '' as $$
|
||||
select auth.uid() is not null and exists (select 1 from public.shared_lists l where l.id = p_list and l.owner_id = auth.uid())
|
||||
$$;
|
||||
|
||||
-- Nom propre : sans caractères de contrôle, espaces superflus retirés
|
||||
create or replace function public.azuu_sl_name(p_name text)
|
||||
returns text language sql immutable set search_path = '' as $$
|
||||
select trim(regexp_replace(regexp_replace(coalesce(p_name, ''), '\s+', ' ', 'g'), '[[:cntrl:]]', '', 'g'))
|
||||
$$;
|
||||
|
||||
create or replace function public.azuu_mini_card(p public.profiles)
|
||||
returns jsonb language sql stable security definer set search_path = '' as $$
|
||||
select case when p.id is null then null else
|
||||
jsonb_build_object('id', p.id, 'username', p.username, 'display_name', p.display_name, 'avatar_url', p.avatar_url) end
|
||||
$$;
|
||||
|
||||
create or replace function public.azuu_me_active()
|
||||
returns boolean language sql stable security definer set search_path = '' as $$
|
||||
select auth.uid() is not null and exists (select 1 from public.profiles p where p.id = auth.uid() and not p.suspended)
|
||||
$$;
|
||||
|
||||
-- ── Lecture ──
|
||||
-- Mes listes partagées (+ invitations en attente). p_external_id : indique si ce titre est déjà dans chaque liste.
|
||||
create or replace function public.get_shared_lists(p_external_id text default null)
|
||||
returns jsonb language plpgsql stable security definer set search_path = '' as $$
|
||||
declare me uuid := auth.uid();
|
||||
begin
|
||||
if me is null then
|
||||
return jsonb_build_object('lists', '[]'::jsonb, 'invites', '[]'::jsonb);
|
||||
end if;
|
||||
return jsonb_build_object(
|
||||
'lists', coalesce((
|
||||
select jsonb_agg(s.entry order by s.updated_at desc)
|
||||
from (
|
||||
select l.updated_at, jsonb_build_object(
|
||||
'id', l.id, 'name', l.name, 'is_owner', l.owner_id = me,
|
||||
'owner', public.azuu_mini_card(op),
|
||||
'count', (select count(*) from public.shared_list_items i where i.list_id = l.id),
|
||||
'members', (select count(*) from public.shared_list_members mm where mm.list_id = l.id and mm.status = 'accepted'),
|
||||
'posters', coalesce((select jsonb_agg(x.poster) from (
|
||||
select i.poster from public.shared_list_items i
|
||||
where i.list_id = l.id and i.poster is not null order by i.added_at desc limit 4) x), '[]'::jsonb),
|
||||
'has', p_external_id is not null and exists (
|
||||
select 1 from public.shared_list_items i where i.list_id = l.id and i.external_id = p_external_id),
|
||||
'updated_at', l.updated_at
|
||||
) as entry
|
||||
from public.shared_list_members m
|
||||
join public.shared_lists l on l.id = m.list_id
|
||||
join public.profiles op on op.id = l.owner_id
|
||||
where m.user_id = me and m.status = 'accepted'
|
||||
limit 200
|
||||
) s
|
||||
), '[]'::jsonb),
|
||||
'invites', coalesce((
|
||||
select jsonb_agg(s.entry order by s.created_at desc)
|
||||
from (
|
||||
select m.created_at, jsonb_build_object(
|
||||
'id', l.id, 'name', l.name,
|
||||
'owner', public.azuu_mini_card(op),
|
||||
'members', (select count(*) from public.shared_list_members mm where mm.list_id = l.id and mm.status = 'accepted'),
|
||||
'created_at', m.created_at
|
||||
) as entry
|
||||
from public.shared_list_members m
|
||||
join public.shared_lists l on l.id = m.list_id
|
||||
join public.profiles op on op.id = l.owner_id
|
||||
where m.user_id = me and m.status = 'pending' and not op.suspended
|
||||
limit 100
|
||||
) s
|
||||
), '[]'::jsonb)
|
||||
);
|
||||
end;
|
||||
$$;
|
||||
|
||||
-- Une liste : membres et titres (membres acceptés uniquement)
|
||||
create or replace function public.get_shared_list(p_list uuid)
|
||||
returns jsonb language plpgsql stable security definer set search_path = '' as $$
|
||||
declare
|
||||
me uuid := auth.uid();
|
||||
l public.shared_lists;
|
||||
begin
|
||||
if not public.azuu_sl_member(p_list) then return null; end if;
|
||||
select * into l from public.shared_lists where id = p_list;
|
||||
return jsonb_build_object(
|
||||
'id', l.id, 'name', l.name, 'is_owner', l.owner_id = me, 'owner_id', l.owner_id,
|
||||
'created_at', l.created_at, 'updated_at', l.updated_at,
|
||||
'members', coalesce((
|
||||
select jsonb_agg(public.azuu_mini_card(p) || jsonb_build_object('status', m.status, 'is_owner', m.user_id = l.owner_id)
|
||||
order by (m.user_id = l.owner_id) desc, m.status, m.created_at)
|
||||
from public.shared_list_members m join public.profiles p on p.id = m.user_id
|
||||
where m.list_id = l.id
|
||||
), '[]'::jsonb),
|
||||
'items', coalesce((
|
||||
select jsonb_agg(jsonb_build_object(
|
||||
'external_id', s.external_id, 'title', s.title, 'poster', s.poster, 'type', s.type, 'year', s.year,
|
||||
'added_at', s.added_at, 'added_by', public.azuu_mini_card(p)
|
||||
) order by s.added_at desc)
|
||||
from (select * from public.shared_list_items i where i.list_id = l.id order by i.added_at desc limit 500) s
|
||||
left join public.profiles p on p.id = s.added_by
|
||||
), '[]'::jsonb)
|
||||
);
|
||||
end;
|
||||
$$;
|
||||
|
||||
-- Personnes que je peux inviter : abonnés / abonnements acceptés, pas déjà dans la liste
|
||||
create or replace function public.shared_list_candidates(p_list uuid, p_q text default '')
|
||||
returns jsonb language plpgsql stable security definer set search_path = '' as $$
|
||||
declare
|
||||
me uuid := auth.uid();
|
||||
term text := lower(trim(coalesce(p_q, '')));
|
||||
pattern text;
|
||||
begin
|
||||
if not public.azuu_sl_owner(p_list) or char_length(term) > 40 then return '[]'::jsonb; end if;
|
||||
pattern := replace(replace(replace(ltrim(term, '@'), '\', '\\'), '%', '\%'), '_', '\_');
|
||||
return coalesce((
|
||||
select jsonb_agg(public.profile_card(p) order by p.username)
|
||||
from (
|
||||
select pr.* from public.profiles pr
|
||||
where pr.id <> me and not pr.suspended
|
||||
and exists (
|
||||
select 1 from public.follows f where f.status = 'accepted'
|
||||
and ((f.follower_id = me and f.following_id = pr.id) or (f.follower_id = pr.id and f.following_id = me))
|
||||
)
|
||||
and not exists (select 1 from public.shared_list_members m where m.list_id = p_list and m.user_id = pr.id)
|
||||
and (pattern = '' or pr.username like pattern || '%' or lower(pr.display_name) like '%' || pattern || '%')
|
||||
order by pr.username
|
||||
limit 30
|
||||
) p
|
||||
), '[]'::jsonb);
|
||||
end;
|
||||
$$;
|
||||
|
||||
-- ── Écriture ──
|
||||
create or replace function public.create_shared_list(p_name text)
|
||||
returns jsonb language plpgsql security definer set search_path = '' as $$
|
||||
declare
|
||||
me uuid := auth.uid();
|
||||
n text := public.azuu_sl_name(p_name);
|
||||
new_id uuid;
|
||||
begin
|
||||
if not public.azuu_me_active() then raise exception 'forbidden'; end if;
|
||||
if char_length(n) < 1 or char_length(n) > 80 then raise exception 'invalid name'; end if;
|
||||
if (select count(*) from public.shared_lists where owner_id = me) >= 50 then raise exception 'too many lists'; end if;
|
||||
if (select count(*) from public.shared_lists where owner_id = me and created_at > now() - interval '1 minute') >= 5 then
|
||||
raise exception 'rate limit';
|
||||
end if;
|
||||
if (select count(*) from public.shared_list_members where user_id = me and status = 'accepted') >= 100 then
|
||||
raise exception 'too many lists';
|
||||
end if;
|
||||
insert into public.shared_lists (owner_id, name) values (me, n) returning id into new_id;
|
||||
insert into public.shared_list_members (list_id, user_id, status, invited_by) values (new_id, me, 'accepted', me);
|
||||
return jsonb_build_object('id', new_id);
|
||||
end;
|
||||
$$;
|
||||
|
||||
create or replace function public.rename_shared_list(p_list uuid, p_name text)
|
||||
returns jsonb language plpgsql security definer set search_path = '' as $$
|
||||
declare n text := public.azuu_sl_name(p_name);
|
||||
begin
|
||||
if not public.azuu_sl_owner(p_list) or not public.azuu_me_active() then raise exception 'forbidden'; end if;
|
||||
if char_length(n) < 1 or char_length(n) > 80 then raise exception 'invalid name'; end if;
|
||||
update public.shared_lists set name = n, updated_at = now() where id = p_list;
|
||||
return jsonb_build_object('ok', true);
|
||||
end;
|
||||
$$;
|
||||
|
||||
create or replace function public.delete_shared_list(p_list uuid)
|
||||
returns jsonb language plpgsql security definer set search_path = '' as $$
|
||||
begin
|
||||
if not (public.azuu_sl_owner(p_list) or public.is_admin()) then raise exception 'forbidden'; end if;
|
||||
delete from public.notifications where kind = 'shared_list_invite' and item_id = p_list::text;
|
||||
delete from public.shared_lists where id = p_list; -- membres et titres : en cascade
|
||||
return jsonb_build_object('ok', true);
|
||||
end;
|
||||
$$;
|
||||
|
||||
-- Inviter quelqu'un (propriétaire seulement, parmi ses abonnés / abonnements)
|
||||
create or replace function public.invite_to_shared_list(p_list uuid, p_user uuid)
|
||||
returns jsonb language plpgsql security definer set search_path = '' as $$
|
||||
declare
|
||||
me uuid := auth.uid();
|
||||
n integer;
|
||||
begin
|
||||
if not public.azuu_sl_owner(p_list) or not public.azuu_me_active() then raise exception 'forbidden'; end if;
|
||||
if p_user is null or p_user = me then raise exception 'invalid user'; end if;
|
||||
if not exists (select 1 from public.profiles p where p.id = p_user and not p.suspended) then raise exception 'profile not found'; end if;
|
||||
if not exists (
|
||||
select 1 from public.follows f where f.status = 'accepted'
|
||||
and ((f.follower_id = me and f.following_id = p_user) or (f.follower_id = p_user and f.following_id = me))
|
||||
) then
|
||||
raise exception 'not connected';
|
||||
end if;
|
||||
if (select count(*) from public.shared_list_members where list_id = p_list) >= 30 then raise exception 'too many members'; end if;
|
||||
if (select count(*) from public.shared_list_members where invited_by = me and user_id <> me and created_at > now() - interval '1 minute') >= 10 then
|
||||
raise exception 'rate limit';
|
||||
end if;
|
||||
insert into public.shared_list_members (list_id, user_id, status, invited_by)
|
||||
values (p_list, p_user, 'pending', me)
|
||||
on conflict (list_id, user_id) do nothing;
|
||||
get diagnostics n = row_count;
|
||||
if n > 0 then
|
||||
perform public.azuu_notify(p_user, 'shared_list_invite', me, p_list::text, null);
|
||||
end if;
|
||||
return jsonb_build_object('ok', true, 'invited', n > 0);
|
||||
end;
|
||||
$$;
|
||||
|
||||
-- Accepter / refuser une invitation
|
||||
create or replace function public.respond_shared_list_invite(p_list uuid, p_accept boolean)
|
||||
returns jsonb language plpgsql security definer set search_path = '' as $$
|
||||
declare me uuid := auth.uid();
|
||||
begin
|
||||
if me is null then raise exception 'auth'; end if;
|
||||
if not exists (select 1 from public.shared_list_members where list_id = p_list and user_id = me and status = 'pending') then
|
||||
raise exception 'invite not found';
|
||||
end if;
|
||||
if p_accept then
|
||||
if not public.azuu_me_active() then raise exception 'forbidden'; end if;
|
||||
if (select count(*) from public.shared_list_members where user_id = me and status = 'accepted') >= 100 then
|
||||
raise exception 'too many lists';
|
||||
end if;
|
||||
update public.shared_list_members set status = 'accepted' where list_id = p_list and user_id = me;
|
||||
else
|
||||
delete from public.shared_list_members where list_id = p_list and user_id = me;
|
||||
delete from public.notifications where user_id = me and kind = 'shared_list_invite' and item_id = p_list::text;
|
||||
end if;
|
||||
return jsonb_build_object('ok', true);
|
||||
end;
|
||||
$$;
|
||||
|
||||
-- Quitter une liste (le propriétaire, lui, la supprime)
|
||||
create or replace function public.leave_shared_list(p_list uuid)
|
||||
returns jsonb language plpgsql security definer set search_path = '' as $$
|
||||
declare me uuid := auth.uid();
|
||||
begin
|
||||
if me is null then raise exception 'auth'; end if;
|
||||
if public.azuu_sl_owner(p_list) then raise exception 'owner cannot leave'; end if;
|
||||
delete from public.shared_list_members where list_id = p_list and user_id = me;
|
||||
delete from public.notifications where user_id = me and kind = 'shared_list_invite' and item_id = p_list::text;
|
||||
return jsonb_build_object('ok', true);
|
||||
end;
|
||||
$$;
|
||||
|
||||
-- Retirer un membre (ou annuler une invitation) : propriétaire seulement
|
||||
create or replace function public.remove_shared_list_member(p_list uuid, p_user uuid)
|
||||
returns jsonb language plpgsql security definer set search_path = '' as $$
|
||||
begin
|
||||
if not public.azuu_sl_owner(p_list) then raise exception 'forbidden'; end if;
|
||||
if p_user = auth.uid() then raise exception 'owner cannot leave'; end if;
|
||||
delete from public.shared_list_members where list_id = p_list and user_id = p_user;
|
||||
delete from public.notifications where user_id = p_user and kind = 'shared_list_invite' and item_id = p_list::text;
|
||||
return jsonb_build_object('ok', true);
|
||||
end;
|
||||
$$;
|
||||
|
||||
-- Ajouter un titre : p_item = { external_id, title, poster?, type, year? }
|
||||
create or replace function public.add_shared_list_item(p_list uuid, p_item jsonb)
|
||||
returns jsonb language plpgsql security definer set search_path = '' as $$
|
||||
declare
|
||||
me uuid := auth.uid();
|
||||
ext text := p_item->>'external_id';
|
||||
ttl text := left(public.azuu_sl_name(p_item->>'title'), 300);
|
||||
pst text := nullif(p_item->>'poster', '');
|
||||
typ text := coalesce(p_item->>'type', 'autre');
|
||||
yr integer;
|
||||
n integer;
|
||||
begin
|
||||
if not public.azuu_sl_member(p_list) or not public.azuu_me_active() then raise exception 'forbidden'; end if;
|
||||
if jsonb_typeof(p_item) is distinct from 'object' then raise exception 'invalid item'; end if;
|
||||
if ext is null or ext !~ '^(tmdb:(movie|tv):[0-9]{1,10}|anilist:[0-9]{1,10})$' or char_length(ttl) < 1 then
|
||||
raise exception 'invalid item';
|
||||
end if;
|
||||
if typ not in ('film', 'serie', 'anime', 'kdrama', 'cdrama', 'autre') then typ := 'autre'; end if;
|
||||
if pst is not null and (char_length(pst) > 300 or pst !~ '^https://(image\.tmdb\.org|s4\.anilist\.co)/[A-Za-z0-9/_.-]+$') then
|
||||
pst := null;
|
||||
end if;
|
||||
if coalesce(p_item->>'year', '') ~ '^[0-9]{4}$' and (p_item->>'year')::int between 1870 and 2200 then
|
||||
yr := (p_item->>'year')::int;
|
||||
end if;
|
||||
if (select count(*) from public.shared_list_items where list_id = p_list) >= 500 then raise exception 'list full'; end if;
|
||||
if (select count(*) from public.shared_list_items where added_by = me and added_at > now() - interval '1 minute') >= 30 then
|
||||
raise exception 'rate limit';
|
||||
end if;
|
||||
insert into public.shared_list_items (list_id, external_id, title, poster, type, year, added_by)
|
||||
values (p_list, ext, ttl, pst, typ, yr, me)
|
||||
on conflict (list_id, external_id) do nothing;
|
||||
get diagnostics n = row_count;
|
||||
if n > 0 then update public.shared_lists set updated_at = now() where id = p_list; end if;
|
||||
return jsonb_build_object('ok', true, 'added', n > 0);
|
||||
end;
|
||||
$$;
|
||||
|
||||
create or replace function public.remove_shared_list_item(p_list uuid, p_external_id text)
|
||||
returns jsonb language plpgsql security definer set search_path = '' as $$
|
||||
declare n integer;
|
||||
begin
|
||||
if not public.azuu_sl_member(p_list) or not public.azuu_me_active() then raise exception 'forbidden'; end if;
|
||||
delete from public.shared_list_items where list_id = p_list and external_id = p_external_id;
|
||||
get diagnostics n = row_count;
|
||||
if n > 0 then update public.shared_lists set updated_at = now() where id = p_list; end if;
|
||||
return jsonb_build_object('ok', true, 'removed', n > 0);
|
||||
end;
|
||||
$$;
|
||||
|
||||
-- ── Notifications : titres des nouveaux types + état de l'invitation ──
|
||||
create or replace function public.get_notifications(p_offset integer default 0)
|
||||
returns jsonb language sql stable security definer set search_path = '' as $$
|
||||
select coalesce(jsonb_agg(x order by created_at desc), '[]'::jsonb)
|
||||
from (
|
||||
select n.id, n.kind, n.created_at, n.read_at, n.emoji, n.item_id, n.episode, n.ep_count,
|
||||
case when p.id is null then null else
|
||||
jsonb_build_object('username', p.username, 'display_name', p.display_name, 'avatar_url', p.avatar_url) end as actor,
|
||||
case
|
||||
when n.kind in ('reaction', 'review_comment', 'new_episode', 'new_season')
|
||||
then (select it.data->>'title' from public.items it where it.user_id = n.user_id and it.id = n.item_id)
|
||||
when n.kind = 'shared_list_invite'
|
||||
then (select sl.name from public.shared_lists sl where sl.id::text = n.item_id)
|
||||
end as title,
|
||||
case when n.kind = 'shared_list_invite'
|
||||
then (select m.status from public.shared_list_members m where m.list_id::text = n.item_id and m.user_id = n.user_id)
|
||||
end as invite_status
|
||||
from public.notifications n
|
||||
left join public.profiles p on p.id = n.actor_id
|
||||
where n.user_id = auth.uid() and public.azuu_wants(n.user_id, n.kind)
|
||||
order by n.created_at desc
|
||||
offset greatest(p_offset, 0) limit 30
|
||||
) x
|
||||
$$;
|
||||
|
||||
-- ── Droits ──
|
||||
revoke all on function public.azuu_sl_member(uuid), public.azuu_sl_owner(uuid), public.azuu_sl_name(text),
|
||||
public.azuu_mini_card(public.profiles), public.azuu_me_active()
|
||||
from public, anon, authenticated;
|
||||
revoke all on function public.get_shared_lists(text), public.get_shared_list(uuid), public.shared_list_candidates(uuid, text),
|
||||
public.create_shared_list(text), public.rename_shared_list(uuid, text), public.delete_shared_list(uuid),
|
||||
public.invite_to_shared_list(uuid, uuid), public.respond_shared_list_invite(uuid, boolean), public.leave_shared_list(uuid),
|
||||
public.remove_shared_list_member(uuid, uuid), public.add_shared_list_item(uuid, jsonb), public.remove_shared_list_item(uuid, text),
|
||||
public.get_notifications(integer)
|
||||
from public, anon;
|
||||
grant execute on function public.get_shared_lists(text), public.get_shared_list(uuid), public.shared_list_candidates(uuid, text),
|
||||
public.create_shared_list(text), public.rename_shared_list(uuid, text), public.delete_shared_list(uuid),
|
||||
public.invite_to_shared_list(uuid, uuid), public.respond_shared_list_invite(uuid, boolean), public.leave_shared_list(uuid),
|
||||
public.remove_shared_list_member(uuid, uuid), public.add_shared_list_item(uuid, jsonb), public.remove_shared_list_item(uuid, text),
|
||||
public.get_notifications(integer)
|
||||
to authenticated;
|
||||
@@ -1,6 +1,6 @@
|
||||
-- ════════════════════════════════════════════════════════════════════════════
|
||||
-- AzuuCine — Modération (9 octobre 2026)
|
||||
-- 1. Signalements (avis, profils ; commentaires et listes partagées prêts à brancher).
|
||||
-- 1. Signalements (avis, profils, commentaires, listes partagées).
|
||||
-- 2. Tableau de modération (admin) : file des signalements regroupés par cible, actions.
|
||||
-- 3. Comptes suspendus : journal des suspensions + garde-fou sur la colonne « suspended ».
|
||||
-- 4. Blocage d'un membre : il ne peut plus te suivre ni réagir à tes avis ; ses avis
|
||||
@@ -129,12 +129,27 @@ begin
|
||||
into snap
|
||||
from public.profiles p where p.id = p_target_user;
|
||||
if snap is null then raise exception 'not found'; end if;
|
||||
elsif p_type = 'comment' then
|
||||
-- Commentaire sous un avis : il doit exister et l'avis doit m'être visible
|
||||
if p_target_id !~ '^[0-9]{1,18}$' then raise exception 'bad target'; end if;
|
||||
select jsonb_build_object('body', left(c.body, 500), 'review_author', c.author_id, 'item_id', c.item_id,
|
||||
'title', left((select i.data->>'title' from public.items i where i.user_id = c.author_id and i.id = c.item_id), 200))
|
||||
into snap
|
||||
from public.review_comments c
|
||||
where c.id = p_target_id::bigint and c.user_id = p_target_user
|
||||
and public.azuu_review_visible(c.author_id, c.item_id);
|
||||
if snap is null then raise exception 'not found'; end if;
|
||||
else
|
||||
-- 'comment' / 'list' : tables ajoutées par d'autres migrations. On vérifie au minimum que
|
||||
-- l'auteur existe et que son contenu m'est visible ; la copie du contenu sera ajoutée à la fusion.
|
||||
if not exists (select 1 from public.profiles p where p.id = p_target_user) or not public.can_view(p_target_user) then
|
||||
raise exception 'not found';
|
||||
end if;
|
||||
-- Liste partagée : seuls ses membres la voient, donc seuls eux peuvent la signaler
|
||||
if p_target_id !~ '^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$' then raise exception 'bad target'; end if;
|
||||
select jsonb_build_object('name', l.name,
|
||||
'titles', (select coalesce(jsonb_agg(left(it.title, 120) order by it.added_at desc), '[]'::jsonb)
|
||||
from (select title, added_at from public.shared_list_items x where x.list_id = l.id order by x.added_at desc limit 20) it))
|
||||
into snap
|
||||
from public.shared_lists l
|
||||
where l.id = p_target_id::uuid and l.owner_id = p_target_user
|
||||
and exists (select 1 from public.shared_list_members m where m.list_id = l.id and m.user_id = me and m.status = 'accepted');
|
||||
if snap is null then raise exception 'not found'; end if;
|
||||
end if;
|
||||
|
||||
-- Anti-abus : 20 signalements par 24 h, 5 par minute
|
||||
@@ -234,6 +249,15 @@ do $$ begin
|
||||
end if;
|
||||
end $$;
|
||||
|
||||
-- … ni commenter mes avis.
|
||||
do $$ begin
|
||||
if to_regclass('public.review_comments') is not null then
|
||||
execute 'drop trigger if exists review_comments_block_guard on public.review_comments';
|
||||
execute 'create trigger review_comments_block_guard before insert on public.review_comments
|
||||
for each row execute function public.azuu_reaction_block_guard()';
|
||||
end if;
|
||||
end $$;
|
||||
|
||||
-- ═════════════════════════════ Administration ═════════════════════════════
|
||||
|
||||
-- Petite carte « compte » pour l'admin
|
||||
@@ -308,6 +332,12 @@ begin
|
||||
when 'profile' then (select jsonb_build_object('username', p.username, 'display_name', p.display_name, 'bio', p.bio,
|
||||
'avatar_url', p.avatar_url, 'banner_url', p.banner_url)
|
||||
from public.profiles p where p.id = pg.target_user_id)
|
||||
when 'comment' then (select jsonb_build_object('body', left(c.body, 500))
|
||||
from public.review_comments c
|
||||
where pg.target_id ~ '^[0-9]{1,18}$' and c.id = pg.target_id::bigint and c.user_id = pg.target_user_id)
|
||||
when 'list' then (select jsonb_build_object('name', l.name)
|
||||
from public.shared_lists l
|
||||
where pg.target_id ~ '^[0-9a-f-]{36}$' and l.id = pg.target_id::uuid and l.owner_id = pg.target_user_id)
|
||||
else null end,
|
||||
-- Antécédents : nombre de contenus différents de ce compte déjà signalés
|
||||
'user_reported_targets', (select count(distinct (r.target_type, r.target_id))::int from public.reports r
|
||||
@@ -344,9 +374,13 @@ begin
|
||||
-- Remet le profil à zéro : bio, photo et bannière effacées, nom affiché = pseudo
|
||||
update public.profiles p set bio = '', avatar_url = null, banner_url = null, display_name = p.username
|
||||
where p.id = p_target_user;
|
||||
elsif p_type = 'comment' then
|
||||
if p_target_id !~ '^[0-9]{1,18}$' then raise exception 'bad target'; end if;
|
||||
delete from public.review_comments c where c.id = p_target_id::bigint and c.user_id = p_target_user;
|
||||
else
|
||||
-- 'comment' / 'list' : à brancher quand ces tables existent (fusion des fonctionnalités)
|
||||
raise exception 'unsupported target';
|
||||
if p_target_id !~ '^[0-9a-f-]{36}$' then raise exception 'bad target'; end if;
|
||||
-- Supprime la liste (membres et titres partent en cascade)
|
||||
delete from public.shared_lists l where l.id = p_target_id::uuid and l.owner_id = p_target_user;
|
||||
end if;
|
||||
elsif p_action in ('suspend', 'unsuspend') then
|
||||
if p_target_user = me then raise exception 'cannot suspend yourself'; end if;
|
||||
|
||||
Reference in new issue
Block a user