Merge branch 'worktree-agent-ab18ecf78a0bb887b' into claude/relaxed-maxwell-c10wof
# Conflicts: # src/i18n/locales/ar.ts # src/i18n/locales/de.ts # src/i18n/locales/en.ts # src/i18n/locales/es.ts # src/i18n/locales/fr.ts # src/i18n/locales/hi.ts # src/i18n/locales/id.ts # src/i18n/locales/it.ts # src/i18n/locales/ja.ts # src/i18n/locales/ko.ts # src/i18n/locales/nl.ts # src/i18n/locales/pl.ts # src/i18n/locales/pt.ts # src/i18n/locales/ru.ts # src/i18n/locales/th.ts # src/i18n/locales/tr.ts # src/i18n/locales/vi.ts # src/i18n/locales/zh.ts # src/pages/SettingsPage.tsx
This commit is contained in:
42 files changed
+4983
-32
No files matched your search
@@ -0,0 +1,428 @@
|
||||
-- ════════════════════════════════════════════════════════════════════════════
|
||||
-- AzuuCine — Modération (9 octobre 2026)
|
||||
-- 1. Signalements (avis, profils ; commentaires et listes partagées prêts à brancher).
|
||||
-- 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
|
||||
-- sont masqués côté app.
|
||||
--
|
||||
-- À coller dans Supabase → SQL Editor → Run. Idempotent (réexécutable sans risque).
|
||||
-- Dépend de : 20261002 (profiles, follows, can_view), 20261005 (admins, is_admin,
|
||||
-- suspended, admin_clear_review), 20261007 (review_reactions).
|
||||
-- ════════════════════════════════════════════════════════════════════════════
|
||||
|
||||
-- ═════════════════════════════ Journal de modération ═════════════════════════════
|
||||
-- Qui a fait quoi (suspensions, suppressions, signalements classés). Lisible seulement
|
||||
-- via les fonctions admin (aucun accès direct).
|
||||
create table if not exists public.moderation_log (
|
||||
id bigint generated always as identity primary key,
|
||||
admin_id uuid references auth.users (id) on delete set null,
|
||||
action text not null check (char_length(action) <= 40),
|
||||
target_type text check (target_type is null or char_length(target_type) <= 20),
|
||||
target_id text check (target_id is null or char_length(target_id) <= 200),
|
||||
target_user_id uuid references auth.users (id) on delete cascade,
|
||||
created_at timestamptz not null default now()
|
||||
);
|
||||
create index if not exists moderation_log_target_idx on public.moderation_log (target_user_id, created_at desc);
|
||||
alter table public.moderation_log enable row level security;
|
||||
alter table public.moderation_log force row level security;
|
||||
revoke all on public.moderation_log from anon, authenticated;
|
||||
|
||||
-- ── « suspended » : seuls un admin (ou le SQL Editor / service_role) peuvent la changer ──
|
||||
-- Défense en profondeur : même si une permission de colonne venait à manquer, un membre ne
|
||||
-- peut pas lever sa propre suspension en modifiant son profil.
|
||||
create or replace function public.azuu_suspended_guard()
|
||||
returns trigger language plpgsql security definer set search_path = '' as $$
|
||||
begin
|
||||
if new.suspended is distinct from old.suspended
|
||||
and auth.uid() is not null and not public.is_admin() then
|
||||
raise exception 'forbidden';
|
||||
end if;
|
||||
return new;
|
||||
end;
|
||||
$$;
|
||||
drop trigger if exists profiles_suspended_guard on public.profiles;
|
||||
create trigger profiles_suspended_guard before update of suspended on public.profiles
|
||||
for each row execute function public.azuu_suspended_guard();
|
||||
|
||||
-- Chaque suspension / réactivation est journalisée (quel que soit le chemin : admin_set_suspended,
|
||||
-- admin_report_action, SQL Editor…).
|
||||
create or replace function public.azuu_suspended_log()
|
||||
returns trigger language plpgsql security definer set search_path = '' as $$
|
||||
begin
|
||||
if new.suspended is distinct from old.suspended then
|
||||
insert into public.moderation_log (admin_id, action, target_type, target_id, target_user_id)
|
||||
values (auth.uid(), case when new.suspended then 'suspend' else 'unsuspend' end, 'profile', new.id::text, new.id);
|
||||
end if;
|
||||
return null;
|
||||
end;
|
||||
$$;
|
||||
drop trigger if exists profiles_suspended_log on public.profiles;
|
||||
create trigger profiles_suspended_log after update of suspended on public.profiles
|
||||
for each row execute function public.azuu_suspended_log();
|
||||
|
||||
-- ═════════════════════════════ Signalements ═════════════════════════════
|
||||
create table if not exists public.reports (
|
||||
id bigint generated always as identity primary key,
|
||||
reporter_id uuid not null references auth.users (id) on delete cascade,
|
||||
target_type text not null check (target_type in ('review', 'profile', 'comment', 'list')),
|
||||
target_id text not null check (target_id ~ '^[A-Za-z0-9_:.-]{1,200}$'),
|
||||
target_user_id uuid not null references auth.users (id) on delete cascade, -- auteur du contenu
|
||||
reason text not null check (reason in ('spoiler', 'harassment', 'hate', 'sexual', 'spam', 'impersonation', 'other')),
|
||||
details text check (details is null or char_length(details) <= 500),
|
||||
-- Copie du contenu au moment du signalement (preuve si l'auteur le modifie ou l'efface)
|
||||
snapshot jsonb check (snapshot is null or (jsonb_typeof(snapshot) = 'object' and octet_length(snapshot::text) <= 6000)),
|
||||
status text not null default 'open' check (status in ('open', 'resolved', 'dismissed')),
|
||||
resolved_by uuid references auth.users (id) on delete set null,
|
||||
resolved_at timestamptz,
|
||||
created_at timestamptz not null default now(),
|
||||
-- Pas deux fois la même cible par la même personne
|
||||
unique (reporter_id, target_type, target_id, target_user_id)
|
||||
);
|
||||
create index if not exists reports_target_idx on public.reports (target_type, target_id, target_user_id);
|
||||
create index if not exists reports_status_idx on public.reports (status, created_at);
|
||||
create index if not exists reports_reporter_idx on public.reports (reporter_id, created_at desc);
|
||||
alter table public.reports enable row level security;
|
||||
alter table public.reports force row level security;
|
||||
-- Aucun accès direct : insertion via report_content(), lecture/màj via les fonctions admin.
|
||||
revoke all on public.reports from anon, authenticated;
|
||||
|
||||
-- Signaler un contenu. Renvoie { ok: true, duplicate: bool }.
|
||||
create or replace function public.report_content(
|
||||
p_type text, p_target_id text, p_target_user uuid, p_reason text, p_details text default null
|
||||
)
|
||||
returns jsonb language plpgsql security definer set search_path = '' as $$
|
||||
declare
|
||||
me uuid := auth.uid();
|
||||
d text;
|
||||
snap jsonb;
|
||||
n integer;
|
||||
begin
|
||||
if me is null then raise exception 'auth'; end if;
|
||||
if p_type is null or p_type not in ('review', 'profile', 'comment', 'list') then raise exception 'bad type'; end if;
|
||||
if p_reason is null or p_reason not in ('spoiler', 'harassment', 'hate', 'sexual', 'spam', 'impersonation', 'other') then
|
||||
raise exception 'bad reason';
|
||||
end if;
|
||||
if p_target_id is null or p_target_id !~ '^[A-Za-z0-9_:.-]{1,200}$' then raise exception 'bad target'; end if;
|
||||
if p_target_user is null then raise exception 'bad target'; end if;
|
||||
if p_target_user = me then raise exception 'cannot report yourself'; end if;
|
||||
-- Un compte suspendu ne signale pas
|
||||
if exists (select 1 from public.profiles p where p.id = me and p.suspended) then raise exception 'forbidden'; end if;
|
||||
|
||||
-- Précision facultative : sans caractères de contrôle, 500 caractères max
|
||||
d := nullif(trim(regexp_replace(coalesce(p_details, ''), '[\x00-\x08\x0B\x0C\x0E-\x1F\x7F]', '', 'g')), '');
|
||||
if d is not null and char_length(d) > 500 then raise exception 'details too long'; end if;
|
||||
|
||||
-- La cible doit exister ET être visible de la personne qui signale
|
||||
if p_type = 'review' then
|
||||
select jsonb_build_object('title', left(i.data->>'title', 200), 'notes', left(i.data->>'notes', 3000),
|
||||
'rating', i.data->'rating', 'externalId', left(i.data->>'externalId', 60))
|
||||
into snap
|
||||
from public.items i
|
||||
where i.user_id = p_target_user and i.id = p_target_id and not i.deleted
|
||||
and coalesce(i.data->>'notes', '') <> '';
|
||||
if snap is null or not public.can_view(p_target_user) then raise exception 'not found'; end if;
|
||||
elsif p_type = 'profile' then
|
||||
if p_target_id <> p_target_user::text then raise exception 'bad target'; end if;
|
||||
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)
|
||||
into snap
|
||||
from public.profiles p where p.id = p_target_user;
|
||||
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;
|
||||
end if;
|
||||
|
||||
-- Anti-abus : 20 signalements par 24 h, 5 par minute
|
||||
select count(*) into n from public.reports r where r.reporter_id = me and r.created_at > now() - interval '1 day';
|
||||
if n >= 20 then raise exception 'rate limited'; end if;
|
||||
select count(*) into n from public.reports r where r.reporter_id = me and r.created_at > now() - interval '1 minute';
|
||||
if n >= 5 then raise exception 'rate limited'; end if;
|
||||
|
||||
insert into public.reports (reporter_id, target_type, target_id, target_user_id, reason, details, snapshot)
|
||||
values (me, p_type, p_target_id, p_target_user, p_reason, d, snap)
|
||||
on conflict (reporter_id, target_type, target_id, target_user_id) do nothing;
|
||||
return jsonb_build_object('ok', true, 'duplicate', not found);
|
||||
end;
|
||||
$$;
|
||||
|
||||
-- ═════════════════════════════ Blocage ═════════════════════════════
|
||||
create table if not exists public.blocks (
|
||||
blocker_id uuid not null references auth.users (id) on delete cascade,
|
||||
blocked_id uuid not null references auth.users (id) on delete cascade,
|
||||
created_at timestamptz not null default now(),
|
||||
primary key (blocker_id, blocked_id),
|
||||
check (blocker_id <> blocked_id)
|
||||
);
|
||||
create index if not exists blocks_blocked_idx on public.blocks (blocked_id);
|
||||
alter table public.blocks enable row level security;
|
||||
alter table public.blocks force row level security;
|
||||
revoke all on public.blocks from anon, authenticated;
|
||||
|
||||
-- Bloquer : retire aussi les abonnements dans les deux sens.
|
||||
create or replace function public.block_user(p_user 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 p_user is null or p_user = me then raise exception 'bad target'; end if;
|
||||
if not exists (select 1 from public.profiles p where p.id = p_user) then raise exception 'not found'; end if;
|
||||
if (select count(*) from public.blocks b where b.blocker_id = me) >= 1000 then raise exception 'too many blocks'; end if;
|
||||
insert into public.blocks (blocker_id, blocked_id) values (me, p_user) on conflict do nothing;
|
||||
delete from public.follows f
|
||||
where (f.follower_id = me and f.following_id = p_user) or (f.follower_id = p_user and f.following_id = me);
|
||||
return jsonb_build_object('ok', true);
|
||||
end;
|
||||
$$;
|
||||
|
||||
create or replace function public.unblock_user(p_user uuid)
|
||||
returns jsonb language plpgsql security definer set search_path = '' as $$
|
||||
begin
|
||||
if auth.uid() is null then raise exception 'auth'; end if;
|
||||
delete from public.blocks b where b.blocker_id = auth.uid() and b.blocked_id = p_user;
|
||||
return jsonb_build_object('ok', true);
|
||||
end;
|
||||
$$;
|
||||
|
||||
-- Mes blocages (pour masquer les contenus côté app)
|
||||
create or replace function public.my_blocks()
|
||||
returns jsonb language sql stable security definer set search_path = '' as $$
|
||||
select coalesce(jsonb_agg(jsonb_build_object(
|
||||
'id', b.blocked_id, 'username', p.username, 'display_name', p.display_name,
|
||||
'avatar_url', p.avatar_url, 'created_at', b.created_at) order by b.created_at desc), '[]'::jsonb)
|
||||
from public.blocks b
|
||||
left join public.profiles p on p.id = b.blocked_id
|
||||
where b.blocker_id = auth.uid()
|
||||
$$;
|
||||
|
||||
-- Un compte bloqué ne peut plus s'abonner (ni dans l'autre sens).
|
||||
-- Nom choisi pour passer APRÈS « follows_guard » (ordre alphabétique) qui fixe follower_id.
|
||||
create or replace function public.azuu_follow_block_guard()
|
||||
returns trigger language plpgsql security definer set search_path = '' as $$
|
||||
begin
|
||||
if exists (select 1 from public.blocks b
|
||||
where (b.blocker_id = new.following_id and b.blocked_id = new.follower_id)
|
||||
or (b.blocker_id = new.follower_id and b.blocked_id = new.following_id)) then
|
||||
raise exception 'blocked';
|
||||
end if;
|
||||
return new;
|
||||
end;
|
||||
$$;
|
||||
drop trigger if exists follows_zblock_guard on public.follows;
|
||||
create trigger follows_zblock_guard before insert on public.follows
|
||||
for each row execute function public.azuu_follow_block_guard();
|
||||
|
||||
-- … ni réagir à mes avis (pas de notification non plus, donc).
|
||||
create or replace function public.azuu_reaction_block_guard()
|
||||
returns trigger language plpgsql security definer set search_path = '' as $$
|
||||
begin
|
||||
if exists (select 1 from public.blocks b where b.blocker_id = new.author_id and b.blocked_id = new.user_id) then
|
||||
raise exception 'blocked';
|
||||
end if;
|
||||
return new;
|
||||
end;
|
||||
$$;
|
||||
do $$ begin
|
||||
if to_regclass('public.review_reactions') is not null then
|
||||
execute 'drop trigger if exists review_reactions_block_guard on public.review_reactions';
|
||||
execute 'create trigger review_reactions_block_guard before insert or update on public.review_reactions
|
||||
for each row execute function public.azuu_reaction_block_guard()';
|
||||
end if;
|
||||
end $$;
|
||||
|
||||
-- ═════════════════════════════ Administration ═════════════════════════════
|
||||
|
||||
-- Petite carte « compte » pour l'admin
|
||||
create or replace function public.azuu_admin_card(p_user uuid)
|
||||
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, 'suspended', p.suspended,
|
||||
'is_admin', exists (select 1 from public.admins a where a.user_id = p.id)) end
|
||||
from (select 1) x left join public.profiles p on p.id = p_user
|
||||
$$;
|
||||
|
||||
-- File des signalements, regroupés par cible.
|
||||
-- p_status = 'open' : cibles ayant au moins un signalement ouvert (les plus signalées d'abord, puis les plus anciennes)
|
||||
-- = 'closed' : cibles entièrement traitées (les plus récentes d'abord)
|
||||
create or replace function public.admin_reports(p_status text default 'open', p_offset integer default 0)
|
||||
returns jsonb language plpgsql stable security definer set search_path = '' as $$
|
||||
declare
|
||||
res jsonb;
|
||||
open_q boolean := coalesce(p_status, 'open') = 'open';
|
||||
begin
|
||||
if not public.is_admin() then raise exception 'forbidden'; end if;
|
||||
with g as (
|
||||
select r.target_type, r.target_id, r.target_user_id,
|
||||
count(*)::int as total,
|
||||
(count(*) filter (where r.status = 'open'))::int as open_count,
|
||||
min(r.created_at) as first_at,
|
||||
max(r.created_at) as last_at,
|
||||
(array_agg(r.status order by r.resolved_at desc nulls last, r.created_at desc))[1] as last_status
|
||||
from public.reports r
|
||||
group by r.target_type, r.target_id, r.target_user_id
|
||||
),
|
||||
page as (
|
||||
select g.* from g
|
||||
where (open_q and g.open_count > 0) or (not open_q and g.open_count = 0)
|
||||
order by case when open_q then g.total end desc nulls last,
|
||||
case when open_q then g.first_at end asc nulls last,
|
||||
g.last_at desc
|
||||
offset greatest(coalesce(p_offset, 0), 0) limit 30
|
||||
)
|
||||
select coalesce(jsonb_agg(jsonb_build_object(
|
||||
'target_type', pg.target_type,
|
||||
'target_id', pg.target_id,
|
||||
'target_user', public.azuu_admin_card(pg.target_user_id),
|
||||
'total', pg.total,
|
||||
'open_count', pg.open_count,
|
||||
'first_at', pg.first_at,
|
||||
'last_at', pg.last_at,
|
||||
'status', case when pg.open_count > 0 then 'open' else pg.last_status end,
|
||||
-- motif -> nombre
|
||||
'reasons', (select jsonb_object_agg(x.reason, x.c) from (
|
||||
select r.reason, count(*)::int c from public.reports r
|
||||
where r.target_type = pg.target_type and r.target_id = pg.target_id and r.target_user_id = pg.target_user_id
|
||||
group by r.reason) x),
|
||||
-- 10 derniers signalements (avec précision)
|
||||
'reports', (select coalesce(jsonb_agg(y.e order by y.created_at desc), '[]'::jsonb) from (
|
||||
select jsonb_build_object('reason', r.reason, 'details', r.details, 'created_at', r.created_at, 'status', r.status,
|
||||
'reporter', public.azuu_admin_card(r.reporter_id)) e, r.created_at
|
||||
from public.reports r
|
||||
where r.target_type = pg.target_type and r.target_id = pg.target_id and r.target_user_id = pg.target_user_id
|
||||
order by r.created_at desc limit 10) y),
|
||||
-- Contenu au moment du (dernier) signalement
|
||||
'snapshot', (select r.snapshot from public.reports r
|
||||
where r.target_type = pg.target_type and r.target_id = pg.target_id and r.target_user_id = pg.target_user_id
|
||||
and r.snapshot is not null order by r.created_at desc limit 1),
|
||||
-- Contenu actuel (null s'il a été effacé)
|
||||
'current', case pg.target_type
|
||||
when 'review' then (select jsonb_build_object('title', left(i.data->>'title', 200), 'notes', left(i.data->>'notes', 3000),
|
||||
'rating', i.data->'rating')
|
||||
from public.items i where i.user_id = pg.target_user_id and i.id = pg.target_id
|
||||
and not i.deleted and coalesce(i.data->>'notes', '') <> '')
|
||||
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)
|
||||
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
|
||||
where r.target_user_id = pg.target_user_id)
|
||||
) order by case when open_q then pg.total end desc nulls last,
|
||||
case when open_q then pg.first_at end asc nulls last,
|
||||
pg.last_at desc), '[]'::jsonb)
|
||||
into res
|
||||
from page pg;
|
||||
return res;
|
||||
end;
|
||||
$$;
|
||||
|
||||
-- Action sur une cible signalée.
|
||||
-- p_action : 'delete' (efface le contenu + résout), 'suspend' (suspend l'auteur + résout),
|
||||
-- 'unsuspend', 'dismiss' (classe sans suite), 'resolve' (marque résolu)
|
||||
create or replace function public.admin_report_action(p_type text, p_target_id text, p_target_user uuid, p_action text)
|
||||
returns jsonb language plpgsql security definer set search_path = '' as $$
|
||||
declare
|
||||
me uuid := auth.uid();
|
||||
n integer := 0;
|
||||
begin
|
||||
if not public.is_admin() then raise exception 'forbidden'; end if;
|
||||
if p_type is null or p_type not in ('review', 'profile', 'comment', 'list') then raise exception 'bad type'; end if;
|
||||
if p_action is null or p_action not in ('delete', 'suspend', 'unsuspend', 'dismiss', 'resolve') then raise exception 'bad action'; end if;
|
||||
if p_target_user is null then raise exception 'bad target'; end if;
|
||||
|
||||
if p_action = 'delete' then
|
||||
if p_type = 'review' then
|
||||
-- Même chemin que l'onglet « Avis » (renvoyé à l'appareil du membre à la synchro)
|
||||
perform public.admin_clear_review(p_target_user, p_target_id);
|
||||
elsif p_type = 'profile' then
|
||||
if p_target_id <> p_target_user::text then raise exception 'bad target'; end if;
|
||||
-- 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;
|
||||
else
|
||||
-- 'comment' / 'list' : à brancher quand ces tables existent (fusion des fonctionnalités)
|
||||
raise exception 'unsupported target';
|
||||
end if;
|
||||
elsif p_action in ('suspend', 'unsuspend') then
|
||||
if p_target_user = me then raise exception 'cannot suspend yourself'; end if;
|
||||
if p_action = 'suspend' and exists (select 1 from public.admins a where a.user_id = p_target_user) then
|
||||
raise exception 'cannot suspend an admin';
|
||||
end if;
|
||||
update public.profiles set suspended = (p_action = 'suspend') where id = p_target_user;
|
||||
end if;
|
||||
|
||||
if p_action in ('delete', 'suspend', 'resolve', 'dismiss') then
|
||||
update public.reports r
|
||||
set status = case when p_action = 'dismiss' then 'dismissed' else 'resolved' end,
|
||||
resolved_by = me, resolved_at = now()
|
||||
where r.target_type = p_type and r.target_id = p_target_id and r.target_user_id = p_target_user and r.status = 'open';
|
||||
get diagnostics n = row_count;
|
||||
end if;
|
||||
|
||||
-- (suspensions déjà journalisées par le déclencheur)
|
||||
if p_action not in ('suspend', 'unsuspend') then
|
||||
insert into public.moderation_log (admin_id, action, target_type, target_id, target_user_id)
|
||||
values (me, 'report_' || p_action, p_type, p_target_id, p_target_user);
|
||||
end if;
|
||||
return jsonb_build_object('ok', true, 'reports', n);
|
||||
end;
|
||||
$$;
|
||||
|
||||
-- Comptes suspendus (les plus récemment suspendus d'abord)
|
||||
create or replace function public.admin_suspended_users(p_offset integer default 0)
|
||||
returns jsonb language plpgsql stable security definer set search_path = '' as $$
|
||||
declare res jsonb;
|
||||
begin
|
||||
if not public.is_admin() then raise exception 'forbidden'; end if;
|
||||
select coalesce(jsonb_agg(x.e order by x.since desc nulls last, x.username), '[]'::jsonb) into res
|
||||
from (
|
||||
select p.username,
|
||||
l.created_at as since,
|
||||
jsonb_build_object(
|
||||
'id', p.id, 'username', p.username, 'display_name', p.display_name, 'avatar_url', p.avatar_url,
|
||||
'suspended_at', l.created_at,
|
||||
'suspended_by', case when l.admin_id is null then null else
|
||||
(select a.username from public.profiles a where a.id = l.admin_id) end,
|
||||
'reports', (select count(*)::int from public.reports r where r.target_user_id = p.id),
|
||||
'created_at', p.created_at) e
|
||||
from public.profiles p
|
||||
left join lateral (
|
||||
select ml.created_at, ml.admin_id from public.moderation_log ml
|
||||
where ml.target_user_id = p.id and ml.action = 'suspend'
|
||||
order by ml.created_at desc limit 1
|
||||
) l on true
|
||||
where p.suspended
|
||||
order by l.created_at desc nulls last, p.username
|
||||
offset greatest(coalesce(p_offset, 0), 0) limit 50
|
||||
) x;
|
||||
return res;
|
||||
end;
|
||||
$$;
|
||||
|
||||
-- ═════════════════════════════ Droits ═════════════════════════════
|
||||
revoke all on function public.azuu_suspended_guard() from public, anon, authenticated;
|
||||
revoke all on function public.azuu_suspended_log() from public, anon, authenticated;
|
||||
revoke all on function public.azuu_follow_block_guard() from public, anon, authenticated;
|
||||
revoke all on function public.azuu_reaction_block_guard() from public, anon, authenticated;
|
||||
revoke all on function public.azuu_admin_card(uuid) from public, anon, authenticated;
|
||||
|
||||
revoke all on function public.report_content(text, text, uuid, text, text) from public, anon;
|
||||
revoke all on function public.block_user(uuid) from public, anon;
|
||||
revoke all on function public.unblock_user(uuid) from public, anon;
|
||||
revoke all on function public.my_blocks() from public, anon;
|
||||
revoke all on function public.admin_reports(text, integer) from public, anon;
|
||||
revoke all on function public.admin_report_action(text, text, uuid, text) from public, anon;
|
||||
revoke all on function public.admin_suspended_users(integer) from public, anon;
|
||||
|
||||
grant execute on function public.report_content(text, text, uuid, text, text) to authenticated;
|
||||
grant execute on function public.block_user(uuid) to authenticated;
|
||||
grant execute on function public.unblock_user(uuid) to authenticated;
|
||||
grant execute on function public.my_blocks() to authenticated;
|
||||
grant execute on function public.admin_reports(text, integer) to authenticated;
|
||||
grant execute on function public.admin_report_action(text, text, uuid, text) to authenticated;
|
||||
grant execute on function public.admin_suspended_users(integer) to authenticated;
|
||||
@@ -0,0 +1,268 @@
|
||||
-- ════════════════════════════════════════════════════════════════════════════
|
||||
-- AzuuCine — « Signaler un bug » + remontée d'erreurs (9 octobre 2026)
|
||||
-- 1. bug_reports : formulaire des Réglages (comptes connectés, 5 par jour).
|
||||
-- 2. client_errors : erreurs JavaScript de l'app, regroupées par empreinte, sans donnée perso
|
||||
-- (équivalent Sentry minimal, sans service tiers). Écriture ouverte à anon
|
||||
-- mais bornée (taille, débit par empreinte et par heure, taille de la table).
|
||||
-- 3. admin_alerts : compteurs pour le badge de l'administration.
|
||||
--
|
||||
-- À coller dans Supabase → SQL Editor → Run, APRÈS 20261009160000_moderation.sql.
|
||||
-- Idempotent (réexécutable sans risque).
|
||||
-- ════════════════════════════════════════════════════════════════════════════
|
||||
|
||||
-- ═════════════════════════════ Nettoyage des textes ═════════════════════════════
|
||||
-- Retire ce qui pourrait identifier quelqu'un : e-mails, jetons (JWT, clés), UUID,
|
||||
-- paramètres d'URL (?…, #…), longues suites de chiffres.
|
||||
create or replace function public.azuu_scrub(p text, p_max integer)
|
||||
returns text language sql immutable set search_path = '' as $$
|
||||
select left(
|
||||
regexp_replace(
|
||||
regexp_replace(
|
||||
regexp_replace(
|
||||
regexp_replace(
|
||||
regexp_replace(
|
||||
regexp_replace(
|
||||
regexp_replace(coalesce(p, ''), '[\x00-\x08\x0B\x0C\x0E-\x1F\x7F]', '', 'g'),
|
||||
'[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}', '[email]', 'g'),
|
||||
'eyJ[A-Za-z0-9_-]+\.[A-Za-z0-9_-]+(\.[A-Za-z0-9_-]*)?', '[token]', 'g'),
|
||||
'(sb_(publishable|secret)_|sk_|pk_)[A-Za-z0-9_-]+', '[key]', 'g'),
|
||||
'[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}', '[id]', 'g'),
|
||||
'(https?://[^\s?#''"()]+)[?#][^\s''"()]*', '\1', 'g'),
|
||||
'[A-Za-z0-9_-]{40,}', '[token]', 'g'),
|
||||
greatest(p_max, 0))
|
||||
$$;
|
||||
revoke all on function public.azuu_scrub(text, integer) from public, anon, authenticated;
|
||||
|
||||
-- ═════════════════════════════ Signaler un bug ═════════════════════════════
|
||||
create table if not exists public.bug_reports (
|
||||
id bigint generated always as identity primary key,
|
||||
user_id uuid not null references auth.users (id) on delete cascade,
|
||||
description text not null check (char_length(description) between 10 and 2000),
|
||||
-- Infos techniques (version, navigateur, langue, thème, écran, page) — liste blanche de clés
|
||||
context jsonb not null default '{}'::jsonb check (jsonb_typeof(context) = 'object' and octet_length(context::text) <= 2000),
|
||||
status text not null default 'new' check (status in ('new', 'in_progress', 'fixed')),
|
||||
created_at timestamptz not null default now(),
|
||||
updated_at timestamptz not null default now()
|
||||
);
|
||||
create index if not exists bug_reports_status_idx on public.bug_reports (status, created_at desc);
|
||||
create index if not exists bug_reports_user_idx on public.bug_reports (user_id, created_at desc);
|
||||
alter table public.bug_reports enable row level security;
|
||||
alter table public.bug_reports force row level security;
|
||||
revoke all on public.bug_reports from anon, authenticated;
|
||||
|
||||
create or replace function public.submit_bug_report(p_description text, p_context jsonb default '{}'::jsonb)
|
||||
returns jsonb language plpgsql security definer set search_path = '' as $$
|
||||
declare
|
||||
me uuid := auth.uid();
|
||||
d text;
|
||||
ctx jsonb := '{}'::jsonb;
|
||||
k text;
|
||||
new_id bigint;
|
||||
begin
|
||||
if me is null then raise exception 'auth'; end if;
|
||||
d := trim(regexp_replace(coalesce(p_description, ''), '[\x00-\x08\x0B\x0C\x0E-\x1F\x7F]', '', 'g'));
|
||||
if char_length(d) < 10 then raise exception 'too short'; end if;
|
||||
if char_length(d) > 2000 then raise exception 'too long'; end if;
|
||||
if (select count(*) from public.bug_reports b where b.user_id = me and b.created_at > now() - interval '1 day') >= 5 then
|
||||
raise exception 'rate limited';
|
||||
end if;
|
||||
-- Seules ces clés sont gardées, en texte court nettoyé
|
||||
if jsonb_typeof(p_context) = 'object' then
|
||||
foreach k in array array['version', 'browser', 'os', 'lang', 'theme', 'screen', 'viewport', 'route', 'online', 'standalone'] loop
|
||||
if p_context ? k and jsonb_typeof(p_context->k) in ('string', 'number', 'boolean') then
|
||||
ctx := ctx || jsonb_build_object(k, public.azuu_scrub(p_context->>k, 80));
|
||||
end if;
|
||||
end loop;
|
||||
end if;
|
||||
insert into public.bug_reports (user_id, description, context) values (me, d, ctx) returning id into new_id;
|
||||
return jsonb_build_object('ok', true, 'id', new_id);
|
||||
end;
|
||||
$$;
|
||||
|
||||
create or replace function public.admin_bug_reports(p_status text default null, p_offset integer default 0)
|
||||
returns jsonb language plpgsql stable security definer set search_path = '' as $$
|
||||
declare res jsonb;
|
||||
begin
|
||||
if not public.is_admin() then raise exception 'forbidden'; end if;
|
||||
select coalesce(jsonb_agg(x.e order by x.rank, x.created_at desc), '[]'::jsonb) into res
|
||||
from (
|
||||
select b.created_at,
|
||||
case b.status when 'new' then 0 when 'in_progress' then 1 else 2 end as rank,
|
||||
jsonb_build_object('id', b.id, 'description', b.description, 'context', b.context, 'status', b.status,
|
||||
'created_at', b.created_at, 'updated_at', b.updated_at,
|
||||
'reporter', public.azuu_admin_card(b.user_id)) e
|
||||
from public.bug_reports b
|
||||
where p_status is null or b.status = p_status
|
||||
order by case b.status when 'new' then 0 when 'in_progress' then 1 else 2 end, b.created_at desc
|
||||
offset greatest(coalesce(p_offset, 0), 0) limit 30
|
||||
) x;
|
||||
return res;
|
||||
end;
|
||||
$$;
|
||||
|
||||
create or replace function public.admin_set_bug_status(p_id bigint, p_status text)
|
||||
returns jsonb language plpgsql security definer set search_path = '' as $$
|
||||
begin
|
||||
if not public.is_admin() then raise exception 'forbidden'; end if;
|
||||
if p_status is null or p_status not in ('new', 'in_progress', 'fixed') then raise exception 'bad status'; end if;
|
||||
update public.bug_reports set status = p_status, updated_at = now() where id = p_id;
|
||||
return jsonb_build_object('ok', found);
|
||||
end;
|
||||
$$;
|
||||
|
||||
-- ═════════════════════════════ Remontée d'erreurs ═════════════════════════════
|
||||
create table if not exists public.client_errors (
|
||||
fingerprint text primary key check (fingerprint ~ '^[0-9a-f]{32}$'),
|
||||
source text not null default 'error' check (source in ('error', 'rejection', 'render')),
|
||||
message text not null check (char_length(message) between 1 and 500),
|
||||
stack text check (stack is null or char_length(stack) <= 4000),
|
||||
url text check (url is null or char_length(url) <= 300),
|
||||
app_version text check (app_version is null or char_length(app_version) <= 40),
|
||||
user_agent text check (user_agent is null or char_length(user_agent) <= 300),
|
||||
count integer not null default 1,
|
||||
-- Débit : nombre d'occurrences dans l'heure en cours (au-delà de 100, on ne compte plus)
|
||||
hour_start timestamptz not null default date_trunc('hour', now()),
|
||||
hour_count integer not null default 1,
|
||||
status text not null default 'new' check (status in ('new', 'seen', 'fixed')),
|
||||
first_seen timestamptz not null default now(),
|
||||
last_seen timestamptz not null default now()
|
||||
);
|
||||
create index if not exists client_errors_last_idx on public.client_errors (last_seen desc);
|
||||
create index if not exists client_errors_first_idx on public.client_errors (first_seen desc);
|
||||
alter table public.client_errors enable row level security;
|
||||
alter table public.client_errors force row level security;
|
||||
revoke all on public.client_errors from anon, authenticated;
|
||||
|
||||
-- Appelée par l'app (avec ou sans compte). Ne renvoie rien d'utile à un attaquant.
|
||||
create or replace function public.log_client_error(
|
||||
p_message text, p_stack text default null, p_url text default null,
|
||||
p_version text default null, p_ua text default null, p_source text default 'error'
|
||||
)
|
||||
returns void language plpgsql security definer set search_path = '' as $$
|
||||
declare
|
||||
msg text;
|
||||
st text;
|
||||
u text;
|
||||
v text;
|
||||
ua text;
|
||||
src text;
|
||||
first_line text;
|
||||
fp text;
|
||||
begin
|
||||
-- Corps trop gros : on ignore sans rien analyser
|
||||
if p_message is null or octet_length(p_message) > 4000 or octet_length(coalesce(p_stack, '')) > 20000
|
||||
or octet_length(coalesce(p_url, '')) > 2000 or octet_length(coalesce(p_ua, '')) > 1000 then
|
||||
return;
|
||||
end if;
|
||||
msg := nullif(trim(public.azuu_scrub(p_message, 500)), '');
|
||||
if msg is null then return; end if;
|
||||
st := nullif(trim(public.azuu_scrub(p_stack, 4000)), '');
|
||||
-- Adresse : origine + chemin seulement (ni paramètres ni ancre)
|
||||
u := nullif(left(regexp_replace(coalesce(p_url, ''), '[?#].*$', ''), 300), '');
|
||||
if u is not null and u !~ '^https?://[^\s]+$' then u := null; end if;
|
||||
if u is not null then u := public.azuu_scrub(u, 300); end if;
|
||||
v := case when coalesce(p_version, '') ~ '^[0-9A-Za-z._+-]{1,40}$' then p_version end;
|
||||
ua := nullif(public.azuu_scrub(p_ua, 300), '');
|
||||
src := case when p_source in ('error', 'rejection', 'render') then p_source else 'error' end;
|
||||
|
||||
-- Empreinte calculée ici (pas par le client) : message sans nombres + 1re ligne de pile sans n° de ligne
|
||||
first_line := coalesce((regexp_match(coalesce(st, ''), '([^\n]*\S[^\n]*)'))[1], '');
|
||||
-- La 1re ligne de pile répète souvent le message (Chrome : « TypeError: … ») : on prend la 1re ligne « at … »
|
||||
first_line := coalesce((regexp_match(coalesce(st, ''), '\n\s*(at [^\n]+|[^\n]*@[^\n]+)'))[1], first_line);
|
||||
fp := md5(src || '|' || regexp_replace(lower(msg), '[0-9]+', '0', 'g') || '|'
|
||||
|| regexp_replace(regexp_replace(first_line, ':[0-9]+(:[0-9]+)?', '', 'g'), '-[A-Za-z0-9_-]{8}\.js', '.js', 'g'));
|
||||
|
||||
if not exists (select 1 from public.client_errors e where e.fingerprint = fp) then
|
||||
-- Nouvelles empreintes : 30 par heure maximum (inondation d'erreurs inventées)
|
||||
if (select count(*) from public.client_errors e where e.first_seen > now() - interval '1 hour') >= 30 then
|
||||
return;
|
||||
end if;
|
||||
-- Table bornée : au-delà de 2000 empreintes, on oublie les plus anciennes
|
||||
delete from public.client_errors e where e.fingerprint in (
|
||||
select e2.fingerprint from public.client_errors e2 order by e2.last_seen desc offset 1999
|
||||
);
|
||||
end if;
|
||||
|
||||
insert into public.client_errors as e (fingerprint, source, message, stack, url, app_version, user_agent)
|
||||
values (fp, src, msg, st, u, v, ua)
|
||||
on conflict (fingerprint) do update set
|
||||
count = e.count + case when e.hour_start < date_trunc('hour', now()) or e.hour_count < 100 then 1 else 0 end,
|
||||
hour_count = case when e.hour_start < date_trunc('hour', now()) then 1 else least(e.hour_count + 1, 100) end,
|
||||
hour_start = date_trunc('hour', now()),
|
||||
last_seen = now(),
|
||||
url = coalesce(excluded.url, e.url),
|
||||
app_version = coalesce(excluded.app_version, e.app_version),
|
||||
user_agent = coalesce(excluded.user_agent, e.user_agent),
|
||||
-- Une erreur marquée « corrigée » qui revient redevient « nouvelle » (régression)
|
||||
status = case when e.status = 'fixed' then 'new' else e.status end;
|
||||
end;
|
||||
$$;
|
||||
|
||||
create or replace function public.admin_client_errors(p_status text default null, p_offset integer default 0)
|
||||
returns jsonb language plpgsql stable security definer set search_path = '' as $$
|
||||
declare res jsonb;
|
||||
begin
|
||||
if not public.is_admin() then raise exception 'forbidden'; end if;
|
||||
select coalesce(jsonb_agg(to_jsonb(x) - 'hour_start' - 'hour_count' order by x.last_seen desc), '[]'::jsonb) into res
|
||||
from (
|
||||
select * from public.client_errors e
|
||||
where p_status is null or e.status = p_status
|
||||
order by e.last_seen desc
|
||||
offset greatest(coalesce(p_offset, 0), 0) limit 50
|
||||
) x;
|
||||
return res;
|
||||
end;
|
||||
$$;
|
||||
|
||||
-- p_fingerprint null + 'seen' : tout marquer comme vu (à l'ouverture de l'onglet)
|
||||
create or replace function public.admin_set_error_status(p_fingerprint text, p_status text)
|
||||
returns jsonb language plpgsql security definer set search_path = '' as $$
|
||||
declare n integer;
|
||||
begin
|
||||
if not public.is_admin() then raise exception 'forbidden'; end if;
|
||||
if p_status is null or p_status not in ('new', 'seen', 'fixed', 'delete') then raise exception 'bad status'; end if;
|
||||
if p_fingerprint is null then
|
||||
if p_status <> 'seen' then raise exception 'bad status'; end if;
|
||||
update public.client_errors set status = 'seen' where status = 'new';
|
||||
elsif p_status = 'delete' then
|
||||
delete from public.client_errors where fingerprint = p_fingerprint;
|
||||
else
|
||||
update public.client_errors set status = p_status where fingerprint = p_fingerprint;
|
||||
end if;
|
||||
get diagnostics n = row_count;
|
||||
return jsonb_build_object('ok', true, 'count', n);
|
||||
end;
|
||||
$$;
|
||||
|
||||
-- Compteurs du badge « Administration »
|
||||
create or replace function public.admin_alerts()
|
||||
returns jsonb language plpgsql stable security definer set search_path = '' as $$
|
||||
begin
|
||||
if not public.is_admin() then raise exception 'forbidden'; end if;
|
||||
return jsonb_build_object(
|
||||
'reports', (select count(*)::int from (select 1 from public.reports r where r.status = 'open'
|
||||
group by r.target_type, r.target_id, r.target_user_id) t),
|
||||
'bugs', (select count(*)::int from public.bug_reports b where b.status = 'new'),
|
||||
'errors', (select count(*)::int from public.client_errors e where e.status = 'new'),
|
||||
'suspended', (select count(*)::int from public.profiles p where p.suspended)
|
||||
);
|
||||
end;
|
||||
$$;
|
||||
|
||||
-- ═════════════════════════════ Droits ═════════════════════════════
|
||||
revoke all on function public.submit_bug_report(text, jsonb) from public, anon;
|
||||
revoke all on function public.admin_bug_reports(text, integer) from public, anon;
|
||||
revoke all on function public.admin_set_bug_status(bigint, text) from public, anon;
|
||||
revoke all on function public.log_client_error(text, text, text, text, text, text) from public;
|
||||
revoke all on function public.admin_client_errors(text, integer) from public, anon;
|
||||
revoke all on function public.admin_set_error_status(text, text) from public, anon;
|
||||
revoke all on function public.admin_alerts() from public, anon;
|
||||
|
||||
grant execute on function public.submit_bug_report(text, jsonb) to authenticated;
|
||||
grant execute on function public.admin_bug_reports(text, integer) to authenticated;
|
||||
grant execute on function public.admin_set_bug_status(bigint, text) to authenticated;
|
||||
-- Seule fonction ouverte aux visiteurs non connectés (l'écran de connexion peut planter aussi)
|
||||
grant execute on function public.log_client_error(text, text, text, text, text, text) to anon, authenticated;
|
||||
grant execute on function public.admin_client_errors(text, integer) to authenticated;
|
||||
grant execute on function public.admin_set_error_status(text, text) to authenticated;
|
||||
grant execute on function public.admin_alerts() to authenticated;
|
||||
Reference in new issue
Block a user