-- Profil public : le lien bobinoo.org/@pseudo s'ouvre sans compte, seulement si la personne l'a activé. -- -- Par défaut, rien ne change : un profil n'est visible que des membres connectés (et de ses abonnés s'il est privé). -- Avec « Profil visible sans compte » activé (et un profil non privé, non suspendu), public_profile() renvoie -- la carte du profil, ses Top 5, ce qu'il regarde et ses derniers titres vus, réduits au strict nécessaire -- (titre, affiche, type, année, note) : ni avis, ni dates, ni notes perso. La bibliothèque complète reste -- réservée aux membres (get_profile_items passe toujours par can_view). -- -- Pas de contrôle de double authentification dans public_profile : elle ne renvoie que ce que la personne a choisi -- de montrer à tout le monde, et rien de plus à un compte connecté. Un profil non public, privé, suspendu ou -- inexistant donne la même réponse (null) : impossible de savoir si un pseudo existe. Idempotent. alter table public.profiles add column if not exists public_link boolean not null default false; comment on column public.profiles.public_link is 'Lien bobinoo.org/@pseudo ouvert sans compte (choix de la personne, désactivé par défaut)'; grant select (public_link) on public.profiles to authenticated; grant update (public_link) on public.profiles to authenticated; -- Fiche réduite pour les visiteurs sans compte create or replace function public.azuu_public_item_min(d jsonb) returns jsonb language sql immutable set search_path = '' as $$ select jsonb_strip_nulls(jsonb_build_object( 'id', d->'id', 'title', d->'title', 'type', d->'type', 'subtype', d->'subtype', 'year', d->'year', 'status', d->'status', 'rating', d->'rating', 'poster', case when length(coalesce(d->>'poster', '')) <= 150000 then d->'poster' end, 'externalId', d->'externalId', 'genres', d->'genres', 'top', d->'top' )) $$; revoke all on function public.azuu_public_item_min(jsonb) from public, anon, authenticated; create or replace function public.public_profile(p_username text) returns jsonb language plpgsql stable security definer set search_path = '' as $$ declare name text := lower(trim(coalesce(p_username, ''))); p public.profiles; begin if name !~ '^[a-z0-9_]{3,20}$' then return null; end if; select * into p from public.profiles where username = name and public_link and not is_private and not suspended; if not found then return null; end if; return jsonb_build_object( 'id', p.id, 'username', p.username, 'display_name', p.display_name, 'avatar_url', p.avatar_url, 'banner_url', p.banner_url, 'bio', p.bio, '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'), 'stats', jsonb_build_object( 'finished', (select count(*) from public.items where user_id = p.id and not deleted and data->>'status' = 'termine') ), 'top', ( select coalesce(jsonb_agg(public.azuu_public_item_min(data) order by data->'top'->>'category', (data->'top'->>'rank')), '[]'::jsonb) from public.items i where i.user_id = p.id and not i.deleted and jsonb_typeof(i.data->'top') = 'object' and coalesce( (select s.data->'topCategories' ? (i.data->'top'->>'category') from public.settings s where s.user_id = p.id and jsonb_typeof(s.data->'topCategories') = 'array'), true) ), 'recent', ( select coalesce(jsonb_agg(public.azuu_public_item_min(s.data) order by s.k desc nulls last), '[]'::jsonb) from ( select data, coalesce(data->>'endDate', data->>'startDate') as k from public.items where user_id = p.id and not deleted and data->>'status' = 'termine' order by k desc nulls last limit 5 ) s ), 'watching', ( select coalesce(jsonb_agg(public.azuu_public_item_min(s.data) order by s.k desc nulls last), '[]'::jsonb) from ( select data, coalesce(data->>'startDate', left(data->>'updatedAt', 10)) as k from public.items where user_id = p.id and not deleted and data->>'status' = 'en_cours' order by k desc nulls last limit 8 ) s ) ); end; $$; revoke all on function public.public_profile(text) from public; grant execute on function public.public_profile(text) to anon, authenticated; -- get_profile : renvoie aussi le réglage du lien public, seulement à la personne elle-même (redéfinie à l'identique sinon) 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 or not public.azuu_mfa_ok() 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; if p.suspended and p.id <> me then return null; end if; if p.id <> me and exists (select 1 from public.blocks b where b.blocker_id = p.id and b.blocked_id = me) 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 p.id = me then res := res || jsonb_build_object('public_link', p.public_link); end if; if public.can_view(p.id) then res := res || jsonb_build_object( 'stats', jsonb_build_object( 'finished', (select count(*) from public.items where user_id = p.id and not deleted and data->>'status' = 'termine') ), 'top', ( select coalesce(jsonb_agg(public.public_item(data) order by data->'top'->>'category', (data->'top'->>'rank')), '[]'::jsonb) from public.items i where i.user_id = p.id and not i.deleted and jsonb_typeof(i.data->'top') = 'object' and coalesce( (select s.data->'topCategories' ? (i.data->'top'->>'category') from public.settings s where s.user_id = p.id and jsonb_typeof(s.data->'topCategories') = 'array'), true) ), 'recent', ( select coalesce(jsonb_agg(public.public_item(s.data) order by s.k desc nulls last), '[]'::jsonb) from ( select data, coalesce(data->>'endDate', data->>'startDate') as k from public.items where user_id = p.id and not deleted and data->>'status' = 'termine' order by k desc nulls last limit 5 ) s ), 'watching', ( select coalesce(jsonb_agg(public.public_item(s.data) order by s.k desc nulls last), '[]'::jsonb) from ( select data, coalesce(data->>'startDate', left(data->>'updatedAt', 10)) as k from public.items where user_id = p.id and not deleted and data->>'status' = 'en_cours' order by k desc nulls last limit 8 ) s ) ); end if; return res; end; $$;