Initial commit

This commit is contained in:
Romain committed 2026-10-02 18:50:49 +02:00
commit 459e93d314
100 files changed
+26594

No files matched your search

+11
View File
@@ -0,0 +1,11 @@
project_id = "azuucine"
# Réglages des fonctions serveur (utilisés par `supabase functions deploy`).
# Le jeton de connexion est vérifié dans le code de chaque fonction (compatible avec
# les anciennes et les nouvelles clés d'API Supabase).
[functions.tmdb]
verify_jwt = false
[functions.delete-account]
verify_jwt = false
+59
View File
@@ -0,0 +1,59 @@
// Outils partagés par les fonctions serveur d'AzuuCine (Deno / Supabase Edge Functions).
const SUPABASE_URL = Deno.env.get('SUPABASE_URL') ?? ''
const ANON_KEY = Deno.env.get('SUPABASE_ANON_KEY') ?? ''
/** Origines autorisées à appeler les fonctions (ton site), séparées par des virgules. */
const ALLOWED = (Deno.env.get('ALLOWED_ORIGINS') ?? '')
.split(',')
.map((s) => s.trim())
.filter(Boolean)
export function corsHeaders(req: Request): Record<string, string> {
const origin = req.headers.get('Origin') ?? ''
const ok = ALLOWED.length === 0 ? /^http:\/\/(localhost|127\.0\.0\.1)(:\d+)?$/.test(origin) : ALLOWED.includes(origin)
return {
...(ok ? { 'Access-Control-Allow-Origin': origin } : {}),
'Access-Control-Allow-Headers': 'authorization, apikey, content-type',
'Access-Control-Allow-Methods': 'GET, POST, OPTIONS',
'Access-Control-Max-Age': '600',
Vary: 'Origin',
}
}
export function json(req: Request, status: number, body: unknown, extra: Record<string, string> = {}): Response {
return new Response(JSON.stringify(body), {
status,
headers: { ...corsHeaders(req), 'Content-Type': 'application/json; charset=utf-8', 'X-Content-Type-Options': 'nosniff', ...extra },
})
}
// Petit cache jeton → utilisateur (évite de revérifier le même jeton à chaque requête)
const verified = new Map<string, { id: string; until: number }>()
/** Vérifie le jeton de connexion auprès de Supabase Auth. Renvoie l'id de l'utilisateur, ou null. */
export async function getUserId(req: Request): Promise<string | null> {
const auth = req.headers.get('Authorization') ?? ''
const token = auth.startsWith('Bearer ') ? auth.slice(7).trim() : ''
if (!token || token.length > 4096 || token === ANON_KEY) return null
const cached = verified.get(token)
if (cached && cached.until > Date.now()) return cached.id
const res = await fetch(`${SUPABASE_URL}/auth/v1/user`, { headers: { apikey: ANON_KEY, Authorization: `Bearer ${token}` } })
if (!res.ok) return null
const user = (await res.json()) as { id?: string }
if (typeof user.id !== 'string') return null
if (verified.size > 5000) verified.clear()
verified.set(token, { id: user.id, until: Date.now() + 60_000 })
return user.id
}
// Limite de requêtes par compte (fenêtre glissante, par instance)
const hits = new Map<string, number[]>()
export function rateLimited(userId: string, max: number, windowMs: number): boolean {
const now = Date.now()
const list = (hits.get(userId) ?? []).filter((t) => now - t < windowMs)
list.push(now)
hits.set(userId, list)
if (hits.size > 10000) hits.clear()
return list.length > max
}
@@ -0,0 +1,48 @@
// Suppression définitive du compte connecté (et de toutes ses données, par cascade).
// Utilise la clé « service role », disponible uniquement côté serveur.
import { corsHeaders, getUserId, json, rateLimited } from '../_shared/auth.ts'
const SUPABASE_URL = Deno.env.get('SUPABASE_URL') ?? ''
const SERVICE_KEY = Deno.env.get('SUPABASE_SERVICE_ROLE_KEY') ?? ''
Deno.serve(async (req) => {
if (req.method === 'OPTIONS') return new Response(null, { status: 204, headers: corsHeaders(req) })
if (req.method !== 'POST') return json(req, 405, { error: 'method' })
const userId = await getUserId(req)
if (!userId) return json(req, 401, { error: 'auth' })
if (rateLimited('delete:' + userId, 3, 60_000)) return json(req, 429, { error: 'rate' })
let body: { confirm?: unknown } = {}
try {
body = await req.json()
} catch {
/* corps vide */
}
if (body.confirm !== 'SUPPRIMER') return json(req, 400, { error: 'confirm' })
// Photos de profil et bannières (le stockage n'est pas effacé automatiquement avec le compte)
try {
const admin = { apikey: SERVICE_KEY, Authorization: `Bearer ${SERVICE_KEY}`, 'Content-Type': 'application/json' }
const list = await fetch(`${SUPABASE_URL}/storage/v1/object/list/profile-media`, {
method: 'POST',
headers: admin,
body: JSON.stringify({ prefix: userId, limit: 1000 }),
})
const files = list.ok ? ((await list.json()) as { name?: string }[]) : []
const prefixes = files.filter((f) => typeof f.name === 'string').map((f) => `${userId}/${f.name}`)
if (prefixes.length) {
await fetch(`${SUPABASE_URL}/storage/v1/object/profile-media`, { method: 'DELETE', headers: admin, body: JSON.stringify({ prefixes }) })
}
} catch {
/* on supprime le compte quand même */
}
const res = await fetch(`${SUPABASE_URL}/auth/v1/admin/users/${encodeURIComponent(userId)}`, {
method: 'DELETE',
headers: { apikey: SERVICE_KEY, Authorization: `Bearer ${SERVICE_KEY}` },
})
if (!res.ok) return json(req, 500, { error: 'delete' })
return json(req, 200, { ok: true })
})
+68
View File
@@ -0,0 +1,68 @@
// Proxy TMDB : l'app appelle cette fonction, qui ajoute TA clé TMDB côté serveur.
// La clé est un « secret » Supabase (supabase secrets set TMDB_API_KEY=...) : elle n'est
// jamais dans le code, jamais envoyée au navigateur, et seuls les comptes connectés peuvent s'en servir.
import { corsHeaders, getUserId, json, rateLimited } from '../_shared/auth.ts'
const TMDB_KEY = Deno.env.get('TMDB_API_KEY') ?? ''
// Mêmes adresses que dans l'app — tout le reste est refusé
const PATHS = /^\/(search\/multi|configuration|discover\/(movie|tv)|(movie|tv)\/\d{1,10}(\/recommendations|\/season\/\d{1,3})?)$/
// Paramètres autorisés et forme de leur valeur
const PARAMS: Record<string, RegExp> = {
language: /^(fr-FR|en-US|es-ES|it-IT|de-DE|pt-BR|zh-CN|ja-JP|ko-KR)$/,
query: /^[^\u0000-\u001f]{1,100}$/,
include_adult: /^false$/,
page: /^[1-9]\d?$/,
append_to_response: /^(watch\/providers|credits|release_dates)$/,
region: /^[A-Z]{2}$/,
'release_date.gte': /^\d{4}-\d{2}-\d{2}$/,
'release_date.lte': /^\d{4}-\d{2}-\d{2}$/,
'air_date.gte': /^\d{4}-\d{2}-\d{2}$/,
'air_date.lte': /^\d{4}-\d{2}-\d{2}$/,
with_release_type: /^[1-6](\|[1-6]){0,5}$/,
sort_by: /^popularity\.desc$/,
with_origin_country: /^[A-Z]{2}(\|[A-Z]{2}){0,20}$/,
without_genres: /^\d{1,6}(\|\d{1,6}){0,10}$/,
}
Deno.serve(async (req) => {
if (req.method === 'OPTIONS') return new Response(null, { status: 204, headers: corsHeaders(req) })
if (req.method !== 'GET') return json(req, 405, { error: 'method' })
if (!TMDB_KEY) return json(req, 500, { error: 'TMDB_API_KEY manquante' })
const userId = await getUserId(req)
if (!userId) return json(req, 401, { error: 'auth' })
if (rateLimited(userId, 600, 10 * 60_000)) return json(req, 429, { error: 'rate' })
const url = new URL(req.url)
const path = url.searchParams.get('path') ?? ''
if (!PATHS.test(path)) return json(req, 400, { error: 'path' })
const target = new URL('https://api.themoviedb.org/3' + path)
for (const [k, v] of url.searchParams) {
if (k === 'path') continue
const rule = PARAMS[k]
if (!rule || !rule.test(v)) return json(req, 400, { error: 'param', name: k.slice(0, 40) })
target.searchParams.set(k, v)
}
// Clé v3 (32 caractères) en paramètre, jeton v4 en en-tête
const isBearer = TMDB_KEY.length > 40
if (!isBearer) target.searchParams.set('api_key', TMDB_KEY)
const res = await fetch(target, { headers: isBearer ? { Authorization: `Bearer ${TMDB_KEY}` } : {} })
if (!res.ok) return json(req, res.status === 404 ? 404 : 502, { error: 'tmdb', status: res.status })
const body = await res.text()
if (body.length > 2_000_000) return json(req, 502, { error: 'size' })
return new Response(body, {
status: 200,
headers: {
...corsHeaders(req),
'Content-Type': 'application/json; charset=utf-8',
'X-Content-Type-Options': 'nosniff',
// Le navigateur garde la réponse 1 h (moins de requêtes vers TMDB)
'Cache-Control': 'private, max-age=3600',
},
})
})
@@ -0,0 +1,130 @@
-- ─────────────────────────────────────────────────────────────────────────────
-- AzuuCine — base de données (Supabase / Postgres)
--
-- Chaque utilisateur ne voit et ne modifie QUE ses propres lignes :
-- c'est garanti par la base elle-même (Row Level Security), pas par l'app.
-- Même avec la clé publique de l'app, personne ne peut lire les données d'un autre compte.
--
-- À coller une fois dans Supabase → SQL Editor → Run (ou `supabase db push`).
-- ─────────────────────────────────────────────────────────────────────────────
-- Fiches (films, séries, animes…) : le contenu est stocké tel quel (JSON), l'app le valide à la lecture.
create table if not exists public.items (
user_id uuid not null default auth.uid() references auth.users (id) on delete cascade,
id text not null check (id ~ '^[A-Za-z0-9_-]{1,64}$'),
data jsonb check (data is null or (jsonb_typeof(data) = 'object' and octet_length(data::text) <= 400000)),
deleted boolean not null default false,
updated_at timestamptz not null,
server_updated_at timestamptz not null default clock_timestamp(),
primary key (user_id, id),
check (deleted or data is not null)
);
-- Listes perso
create table if not exists public.lists (
user_id uuid not null default auth.uid() references auth.users (id) on delete cascade,
id text not null check (id ~ '^[A-Za-z0-9_-]{1,64}$'),
data jsonb check (data is null or (jsonb_typeof(data) = 'object' and octet_length(data::text) <= 4000)),
deleted boolean not null default false,
updated_at timestamptz not null,
server_updated_at timestamptz not null default clock_timestamp(),
primary key (user_id, id),
check (deleted or data is not null)
);
-- Réglages (note sur 5 ou 10, thème, Top 5…) — une ligne par compte
create table if not exists public.settings (
user_id uuid primary key default auth.uid() references auth.users (id) on delete cascade,
data jsonb not null check (jsonb_typeof(data) = 'object' and octet_length(data::text) <= 4000),
updated_at timestamptz not null,
server_updated_at timestamptz not null default clock_timestamp()
);
create index if not exists items_sync_idx on public.items (user_id, server_updated_at);
create index if not exists lists_sync_idx on public.lists (user_id, server_updated_at);
-- ─── Écritures : « la modification la plus récente gagne » + horodatage serveur ───
create or replace function public.azuu_before_write()
returns trigger
language plpgsql
set search_path = ''
as $$
begin
-- Un appareil resté hors-ligne ne peut pas écraser une version plus récente
if tg_op = 'UPDATE' and new.updated_at < old.updated_at then
return null;
end if;
-- Personne ne peut déplacer une ligne vers un autre compte
if tg_op = 'UPDATE' and new.user_id <> old.user_id then
raise exception 'user_id is immutable';
end if;
-- Date venant du client bornée (pas de date dans le futur lointain qui bloquerait les mises à jour)
if new.updated_at > now() + interval '1 day' then
new.updated_at := now();
end if;
new.server_updated_at := clock_timestamp();
return new;
end;
$$;
drop trigger if exists items_before_write on public.items;
create trigger items_before_write before insert or update on public.items
for each row execute function public.azuu_before_write();
drop trigger if exists lists_before_write on public.lists;
create trigger lists_before_write before insert or update on public.lists
for each row execute function public.azuu_before_write();
drop trigger if exists settings_before_write on public.settings;
create trigger settings_before_write before insert or update on public.settings
for each row execute function public.azuu_before_write();
-- ─── Quotas par compte (évite qu'un compte remplisse la base) ───
create or replace function public.azuu_quota()
returns trigger
language plpgsql
security definer
set search_path = ''
as $$
declare
n integer;
max_rows integer := case tg_table_name when 'items' then 20000 else 300 end;
begin
execute format('select count(*) from public.%I where user_id = $1', tg_table_name) into n using new.user_id;
if n >= max_rows then
raise exception 'quota exceeded for %', tg_table_name;
end if;
return new;
end;
$$;
drop trigger if exists items_quota on public.items;
create trigger items_quota before insert on public.items for each row execute function public.azuu_quota();
drop trigger if exists lists_quota on public.lists;
create trigger lists_quota before insert on public.lists for each row execute function public.azuu_quota();
-- ─── Sécurité au niveau des lignes (RLS) ───
alter table public.items enable row level security;
alter table public.lists enable row level security;
alter table public.settings enable row level security;
alter table public.items force row level security;
alter table public.lists force row level security;
alter table public.settings force row level security;
-- Les visiteurs non connectés n'ont accès à rien
revoke all on public.items, public.lists, public.settings from anon;
grant select, insert, update, delete on public.items, public.lists, public.settings to authenticated;
drop policy if exists "own items" on public.items;
create policy "own items" on public.items for all to authenticated
using ((select auth.uid()) = user_id) with check ((select auth.uid()) = user_id);
drop policy if exists "own lists" on public.lists;
create policy "own lists" on public.lists for all to authenticated
using ((select auth.uid()) = user_id) with check ((select auth.uid()) = user_id);
drop policy if exists "own settings" on public.settings;
create policy "own settings" on public.settings for all to authenticated
using ((select auth.uid()) = user_id) with check ((select auth.uid()) = user_id);
-- Les fonctions internes ne sont pas appelables depuis l'API
revoke all on function public.azuu_before_write() from public, anon, authenticated;
revoke all on function public.azuu_quota() from public, anon, authenticated;
@@ -0,0 +1,580 @@
-- ─────────────────────────────────────────────────────────────────────────────
-- AzuuCine — profils, abonnements et fil d'activité
--
-- À coller dans Supabase → SQL Editor → Run, APRÈS le premier fichier (…_azuucine.sql).
-- Peut être relancé sans risque.
--
-- Principes de confidentialité :
-- - Les tables de données (items, lists, settings) restent visibles uniquement par leur propriétaire.
-- - Les autres ne voient un profil QUE via les fonctions ci-dessous (get_profile, get_feed…),
-- qui vérifient l'accès (profil public, ou abonné accepté pour un compte privé) et retirent
-- les infos privées : avis écrits (sauf ceux marqués publics), listes perso.
-- - Les pseudos sont choisis par chacun ; aucun email n'est jamais exposé.
-- ─────────────────────────────────────────────────────────────────────────────
-- ═════════════════════════════ Profils ═════════════════════════════
create table if not exists public.profiles (
id uuid primary key references auth.users (id) on delete cascade,
username text not null unique check (username ~ '^[a-z0-9_]{3,20}$'),
display_name text not null check (char_length(display_name) between 1 and 40),
bio text not null default '' check (char_length(bio) <= 300),
avatar_url text check (avatar_url is null or char_length(avatar_url) <= 500),
banner_url text check (banner_url is null or char_length(banner_url) <= 500),
is_private boolean not null default false,
created_at timestamptz not null default now(),
updated_at timestamptz not null default now()
);
create index if not exists profiles_display_name_idx on public.profiles (lower(display_name));
-- Pseudo de départ aléatoire (l'email n'est jamais utilisé : il resterait visible de tous)
create or replace function public.azuu_random_username()
returns text
language plpgsql
set search_path = ''
as $$
declare
candidate text;
begin
loop
candidate := 'cinephile_' || substr(md5(random()::text || clock_timestamp()::text), 1, 6);
exit when not exists (select 1 from public.profiles where username = candidate);
end loop;
return candidate;
end;
$$;
-- Création automatique du profil à l'inscription
create or replace function public.azuu_new_user()
returns trigger
language plpgsql
security definer
set search_path = ''
as $$
declare
u text := public.azuu_random_username();
begin
insert into public.profiles (id, username, display_name) values (new.id, u, u) on conflict (id) do nothing;
return new;
end;
$$;
drop trigger if exists azuu_on_auth_user_created on auth.users;
create trigger azuu_on_auth_user_created after insert on auth.users
for each row execute function public.azuu_new_user();
-- Profils des comptes déjà existants
insert into public.profiles (id, username, display_name)
select u.id, x.name, x.name
from auth.users u
cross join lateral (select public.azuu_random_username() as name) x
where not exists (select 1 from public.profiles p where p.id = u.id);
-- Contrôles à chaque modification de profil
create or replace function public.azuu_profile_guard()
returns trigger
language plpgsql
set search_path = ''
as $$
declare
media text := '^https?://[^/?#]+/storage/v1/object/public/profile-media/' || new.id::text || '/[A-Za-z0-9_.-]{1,80}$';
begin
if tg_op = 'UPDATE' and new.id <> old.id then
raise exception 'id is immutable';
end if;
new.username := lower(trim(new.username));
new.display_name := trim(regexp_replace(new.display_name, '[[:cntrl:]]', '', 'g'));
new.bio := trim(regexp_replace(new.bio, '[\x00-\x08\x0B\x0C\x0E-\x1F\x7F]', '', 'g'));
if new.username in ('admin', 'administrator', 'azuucine', 'support', 'moderator', 'root', 'system', 'null', 'undefined', 'me', 'settings') then
raise exception 'username reserved';
end if;
-- Les images ne peuvent venir que du dossier de l'utilisateur dans le stockage de l'app
if new.avatar_url is not null and new.avatar_url !~ media then
raise exception 'invalid avatar url';
end if;
if new.banner_url is not null and new.banner_url !~ media then
raise exception 'invalid banner url';
end if;
new.updated_at := now();
return new;
end;
$$;
drop trigger if exists profiles_guard on public.profiles;
create trigger profiles_guard before insert or update on public.profiles
for each row execute function public.azuu_profile_guard();
-- ═════════════════════════════ Abonnements ═════════════════════════════
create table if not exists public.follows (
follower_id uuid not null default auth.uid() references auth.users (id) on delete cascade,
following_id uuid not null references auth.users (id) on delete cascade,
status text not null default 'pending' check (status in ('pending', 'accepted')),
created_at timestamptz not null default now(),
primary key (follower_id, following_id),
check (follower_id <> following_id)
);
create index if not exists follows_following_idx on public.follows (following_id, status);
create or replace function public.azuu_follow_guard()
returns trigger
language plpgsql
security definer
set search_path = ''
as $$
declare
target_private boolean;
begin
if tg_op = 'INSERT' then
-- On ne peut s'abonner qu'en son propre nom ; l'acceptation dépend du compte suivi
new.follower_id := auth.uid();
select is_private into target_private from public.profiles where id = new.following_id;
if not found then
raise exception 'profile not found';
end if;
new.status := case when target_private then 'pending' else 'accepted' end;
new.created_at := now();
if (select count(*) from public.follows where follower_id = new.follower_id) >= 2000 then
raise exception 'too many follows';
end if;
return new;
end if;
-- UPDATE : seul le compte suivi peut accepter une demande, et rien d'autre ne peut changer
if new.follower_id <> old.follower_id or new.following_id <> old.following_id then
raise exception 'immutable';
end if;
if auth.uid() is distinct from old.following_id then
raise exception 'only the followed account can accept';
end if;
new.status := 'accepted';
new.created_at := old.created_at;
return new;
end;
$$;
drop trigger if exists follows_guard on public.follows;
create trigger follows_guard before insert or update on public.follows
for each row execute function public.azuu_follow_guard();
-- Passer son compte en public accepte les demandes en attente
create or replace function public.azuu_profile_public()
returns trigger
language plpgsql
security definer
set search_path = ''
as $$
begin
if old.is_private and not new.is_private then
update public.follows set status = 'accepted' where following_id = new.id and status = 'pending';
end if;
return new;
end;
$$;
drop trigger if exists profiles_public on public.profiles;
create trigger profiles_public after update of is_private on public.profiles
for each row execute function public.azuu_profile_public();
-- ═════════════════════════════ Activité ═════════════════════════════
-- Seulement « qui a fait quoi et quand » : le contenu (titre, affiche, note) est relu sur la fiche
-- au moment de l'affichage, donc une fiche supprimée disparaît aussi du fil.
create table if not exists public.activity (
id bigint generated always as identity primary key,
user_id uuid not null references auth.users (id) on delete cascade,
item_id text not null,
kind text not null check (kind in ('finished', 'started', 'rewatched', 'added')),
created_at timestamptz not null default now()
);
create index if not exists activity_user_idx on public.activity (user_id, created_at desc);
create or replace function public.azuu_day(v text)
returns date
language plpgsql
immutable
set search_path = ''
as $$
begin
if v ~ '^\d{4}-\d{2}-\d{2}$' then
return v::date;
end if;
return null;
exception when others then
return null;
end;
$$;
create or replace function public.azuu_log(p_user uuid, p_item text, p_kind text)
returns void
language plpgsql
security definer
set search_path = ''
as $$
begin
-- Pas de doublon pour la même fiche dans la journée
delete from public.activity
where user_id = p_user and item_id = p_item and kind = p_kind and created_at > now() - interval '1 day';
insert into public.activity (user_id, item_id, kind) values (p_user, p_item, p_kind);
-- On garde les 500 dernières activités par compte
delete from public.activity
where user_id = p_user and id in (
select id from public.activity where user_id = p_user order by created_at desc offset 500
);
end;
$$;
-- Déduit l'activité des modifications de fiches (les vieux titres importés ne remplissent pas le fil)
create or replace function public.azuu_item_activity()
returns trigger
language plpgsql
security definer
set search_path = ''
as $$
declare
st text;
old_st text;
recent date := current_date - 7;
rw_new int := 0;
rw_old int := 0;
begin
if new.deleted or new.data is null then
return new;
end if;
st := new.data->>'status';
if jsonb_typeof(new.data->'rewatchDates') = 'array' then
rw_new := jsonb_array_length(new.data->'rewatchDates');
end if;
if tg_op = 'UPDATE' then
if not old.deleted and old.data is not null then
old_st := old.data->>'status';
if jsonb_typeof(old.data->'rewatchDates') = 'array' then
rw_old := jsonb_array_length(old.data->'rewatchDates');
end if;
end if;
end if;
if st = 'termine' and old_st is distinct from 'termine'
and coalesce(public.azuu_day(new.data->>'endDate'), current_date) >= recent then
perform public.azuu_log(new.user_id, new.id, 'finished');
elsif st = 'en_cours' and old_st is distinct from 'en_cours'
and coalesce(public.azuu_day(new.data->>'startDate'), current_date) >= recent then
perform public.azuu_log(new.user_id, new.id, 'started');
elsif st = 'a_voir' and tg_op = 'INSERT' and new.updated_at > now() - interval '2 days' then
perform public.azuu_log(new.user_id, new.id, 'added');
end if;
if tg_op = 'UPDATE' and rw_new > rw_old then
perform public.azuu_log(new.user_id, new.id, 'rewatched');
end if;
return new;
end;
$$;
drop trigger if exists items_activity on public.items;
create trigger items_activity after insert or update on public.items
for each row execute function public.azuu_item_activity();
-- ═════════════════════════════ Accès (RLS) ═════════════════════════════
alter table public.profiles enable row level security;
alter table public.follows enable row level security;
alter table public.activity enable row level security;
alter table public.profiles force row level security;
alter table public.follows force row level security;
alter table public.activity force row level security;
revoke all on public.profiles, public.follows, public.activity from anon;
grant select, update on public.profiles to authenticated;
grant select, insert, update, delete on public.follows to authenticated;
grant select on public.activity to authenticated;
-- Pseudo, nom, photo, bannière et bio sont visibles des comptes connectés (comme sur Letterboxd,
-- même pour un compte privé) ; seul le propriétaire peut modifier son profil.
drop policy if exists "profiles readable" on public.profiles;
create policy "profiles readable" on public.profiles for select to authenticated using (true);
drop policy if exists "own profile update" on public.profiles;
create policy "own profile update" on public.profiles for update to authenticated
using ((select auth.uid()) = id) with check ((select auth.uid()) = id);
-- Abonnements : chacun voit ceux qui le concernent ; les listes publiques passent par les fonctions
drop policy if exists "own follows read" on public.follows;
create policy "own follows read" on public.follows for select to authenticated
using ((select auth.uid()) in (follower_id, following_id));
drop policy if exists "follow someone" on public.follows;
create policy "follow someone" on public.follows for insert to authenticated
with check ((select auth.uid()) = follower_id);
drop policy if exists "accept request" on public.follows;
create policy "accept request" on public.follows for update to authenticated
using ((select auth.uid()) = following_id) with check ((select auth.uid()) = following_id);
drop policy if exists "unfollow or remove follower" on public.follows;
create policy "unfollow or remove follower" on public.follows for delete to authenticated
using ((select auth.uid()) in (follower_id, following_id));
drop policy if exists "own activity read" on public.activity;
create policy "own activity read" on public.activity for select to authenticated
using ((select auth.uid()) = user_id);
-- ═════════════════════════════ Fonctions de lecture ═════════════════════════════
-- Ai-je le droit de voir le contenu de ce profil ?
create or replace function public.can_view(owner uuid)
returns boolean
language sql
stable
security definer
set search_path = ''
as $$
select auth.uid() is not null and (
owner = auth.uid()
or exists (select 1 from public.profiles p where p.id = owner and not p.is_private)
or exists (select 1 from public.follows f where f.follower_id = auth.uid() and f.following_id = owner and f.status = 'accepted')
)
$$;
-- Version publique d'une fiche : sans avis privé, sans listes perso, sans affiche trop lourde
create or replace function public.public_item(d jsonb)
returns jsonb
language sql
immutable
set search_path = ''
as $$
select case when length(coalesce(x->>'poster', '')) > 150000 then x - 'poster' else x end
from (select (case when d->>'notesPublic' = 'true' then d else d - 'notes' end) - 'listIds' as x) s
$$;
create or replace function public.profile_card(p public.profiles)
returns jsonb
language sql
stable
security definer
set search_path = ''
as $$
select jsonb_build_object(
'id', p.id, 'username', p.username, 'display_name', p.display_name,
'avatar_url', p.avatar_url, 'is_private', p.is_private,
'relation', coalesce((select f.status from public.follows f where f.follower_id = auth.uid() and f.following_id = p.id), 'none')
)
$$;
-- Profil complet par pseudo
create or replace function public.get_profile(p_username text)
returns jsonb
language plpgsql
stable
security definer
set search_path = ''
as $$
declare
p public.profiles;
me uuid := auth.uid();
res jsonb;
begin
if me is null then
return null;
end if;
select * into p from public.profiles where username = lower(trim(p_username));
if not found then
return null;
end if;
res := public.profile_card(p) || jsonb_build_object(
'bio', p.bio,
'banner_url', p.banner_url,
'is_me', p.id = me,
'follows_me', exists (select 1 from public.follows where follower_id = p.id and following_id = me and status = 'accepted'),
'followers', (select count(*) from public.follows where following_id = p.id and status = 'accepted'),
'following', (select count(*) from public.follows where follower_id = p.id and status = 'accepted'),
'visible', public.can_view(p.id)
);
if public.can_view(p.id) then
res := res || jsonb_build_object(
'stats', (
select jsonb_build_object(
'total', count(*),
'finished', count(*) filter (where data->>'status' = 'termine'),
'finished_year', count(*) filter (where data->>'status' = 'termine' and left(data->>'endDate', 4) = to_char(now(), 'YYYY')),
'films', count(*) filter (where data->>'status' = 'termine' and data->>'type' = 'film'),
'series', count(*) filter (where data->>'status' = 'termine' and data->>'type' <> 'film'),
'episodes', coalesce(sum(case when data->>'episodesWatched' ~ '^\d{1,6}$' then (data->>'episodesWatched')::int else 0 end), 0),
'rated', count(*) filter (where data ? 'rating')
)
from public.items where user_id = p.id and not deleted
),
'top', (
select coalesce(jsonb_agg(public.public_item(data) order by data->'top'->>'category', (data->'top'->>'rank')), '[]'::jsonb)
from public.items where user_id = p.id and not deleted and jsonb_typeof(data->'top') = 'object'
),
'recent', (
select coalesce(jsonb_agg(public.public_item(s.data) order by s.k desc), '[]'::jsonb)
from (
select data, coalesce(data->>'endDate', left(data->>'updatedAt', 10)) as k
from public.items
where user_id = p.id and not deleted and data->>'status' = 'termine'
order by k desc nulls last
limit 12
) s
)
);
end if;
return res;
end;
$$;
-- Bibliothèque d'un profil (par pages)
create or replace function public.get_profile_items(p_user uuid, p_status text default null, p_offset int default 0, p_limit int default 48)
returns jsonb
language plpgsql
stable
security definer
set search_path = ''
as $$
begin
if not public.can_view(p_user) then
return '[]'::jsonb;
end if;
return (
select coalesce(jsonb_agg(public.public_item(s.data) order by s.k desc nulls last, s.id), '[]'::jsonb)
from (
select id, data, coalesce(data->>'endDate', data->>'startDate', left(data->>'updatedAt', 10)) as k
from public.items
where user_id = p_user and not deleted
and (p_status is null or data->>'status' = p_status)
order by k desc nulls last, id
offset greatest(p_offset, 0)
limit least(greatest(p_limit, 1), 60)
) s
);
end;
$$;
-- Fil d'activité de mes abonnements
create or replace function public.get_feed(p_before timestamptz default null, p_limit int default 30)
returns jsonb
language sql
stable
security definer
set search_path = ''
as $$
select coalesce(jsonb_agg(entry order by created_at desc), '[]'::jsonb)
from (
select a.created_at,
jsonb_build_object(
'id', a.id, 'kind', a.kind, 'created_at', a.created_at,
'user', jsonb_build_object('username', p.username, 'display_name', p.display_name, 'avatar_url', p.avatar_url),
'item', public.public_item(i.data)
) as entry
from public.activity a
join public.follows f on f.following_id = a.user_id and f.follower_id = auth.uid() and f.status = 'accepted'
join public.profiles p on p.id = a.user_id
join public.items i on i.user_id = a.user_id and i.id = a.item_id and not i.deleted
where p_before is null or a.created_at < p_before
order by a.created_at desc
limit least(greatest(p_limit, 1), 50)
) s
$$;
-- Recherche de profils (pseudo ou nom)
create or replace function public.search_profiles(q text)
returns jsonb
language plpgsql
stable
security definer
set search_path = ''
as $$
declare
term text := lower(trim(coalesce(q, '')));
pattern text;
begin
if auth.uid() is null or char_length(term) < 2 or char_length(term) > 40 then
return '[]'::jsonb;
end if;
pattern := replace(replace(replace(ltrim(term, '@'), '\', '\\'), '%', '\%'), '_', '\_');
return (
select coalesce(jsonb_agg(public.profile_card(p) order by (p.username = ltrim(term, '@')) desc, p.username), '[]'::jsonb)
from (
select * from public.profiles
where id <> auth.uid() and (username like pattern || '%' or lower(display_name) like '%' || pattern || '%')
order by username
limit 20
) p
);
end;
$$;
-- Abonnés / abonnements d'un profil
create or replace function public.get_follow_list(p_user uuid, p_kind text, p_offset int default 0)
returns jsonb
language plpgsql
stable
security definer
set search_path = ''
as $$
begin
if not public.can_view(p_user) or p_kind not in ('followers', 'following') then
return '[]'::jsonb;
end if;
return (
select coalesce(jsonb_agg(public.profile_card(p) order by s.created_at desc), '[]'::jsonb)
from (
select case when p_kind = 'followers' then f.follower_id else f.following_id end as uid, f.created_at
from public.follows f
where f.status = 'accepted' and (case when p_kind = 'followers' then f.following_id else f.follower_id end) = p_user
order by f.created_at desc
offset greatest(p_offset, 0)
limit 50
) s
join public.profiles p on p.id = s.uid
);
end;
$$;
-- Demandes d'abonnement reçues (compte privé)
create or replace function public.get_follow_requests()
returns jsonb
language sql
stable
security definer
set search_path = ''
as $$
select coalesce(jsonb_agg(public.profile_card(p) order by f.created_at desc), '[]'::jsonb)
from public.follows f
join public.profiles p on p.id = f.follower_id
where f.following_id = auth.uid() and f.status = 'pending'
$$;
-- Les fonctions de lecture ne sont utilisables que par les comptes connectés
revoke all on function public.can_view(uuid), public.public_item(jsonb), public.profile_card(public.profiles),
public.get_profile(text), public.get_profile_items(uuid, text, int, int), public.get_feed(timestamptz, int),
public.search_profiles(text), public.get_follow_list(uuid, text, int), public.get_follow_requests()
from public, anon;
grant execute on function public.get_profile(text), public.get_profile_items(uuid, text, int, int), public.get_feed(timestamptz, int),
public.search_profiles(text), public.get_follow_list(uuid, text, int), public.get_follow_requests()
to authenticated;
-- Outils internes des fonctions ci-dessus : jamais appelables directement depuis l'API
revoke all on function public.can_view(uuid), public.public_item(jsonb), public.profile_card(public.profiles)
from public, anon, authenticated;
-- Fonctions internes : jamais appelables depuis l'API
revoke all on function public.azuu_random_username(), public.azuu_new_user(), public.azuu_profile_guard(),
public.azuu_follow_guard(), public.azuu_profile_public(), public.azuu_day(text), public.azuu_log(uuid, text, text),
public.azuu_item_activity()
from public, anon, authenticated;
-- ═════════════════════════════ Photos de profil et bannières ═════════════════════════════
insert into storage.buckets (id, name, public, file_size_limit, allowed_mime_types)
values ('profile-media', 'profile-media', true, 2097152, array['image/jpeg', 'image/webp', 'image/png'])
on conflict (id) do update
set public = true, file_size_limit = excluded.file_size_limit, allowed_mime_types = excluded.allowed_mime_types;
-- Chacun ne peut écrire que dans son propre dossier « <id>/… » ; la lecture passe par l'adresse publique
drop policy if exists "profile media read own" on storage.objects;
create policy "profile media read own" on storage.objects for select to authenticated
using (bucket_id = 'profile-media' and (storage.foldername(name))[1] = (select auth.uid())::text);
drop policy if exists "profile media upload own" on storage.objects;
create policy "profile media upload own" on storage.objects for insert to authenticated
with check (
bucket_id = 'profile-media'
and (storage.foldername(name))[1] = (select auth.uid())::text
and name ~ '^[0-9a-f-]{36}/(avatar|banner)-[0-9]{1,15}\.(jpg|webp|png)$'
);
drop policy if exists "profile media delete own" on storage.objects;
create policy "profile media delete own" on storage.objects for delete to authenticated
using (bucket_id = 'profile-media' and (storage.foldername(name))[1] = (select auth.uid())::text);