-- Bobinoo — « Mes abonnements » : ce que mes abonnements ont noté (28 nov. 2026). Idempotent. -- Avant : tout (commencé, veut voir, vu…). Maintenant : les titres notés, une ligne par titre (la plus récente), -- avec l'avis s'il y en a un. Une note donnée ou changée plus tard remonte aussi (« rated »). alter table public.activity drop constraint if exists activity_kind_check; delete from public.activity where kind in ('started', 'added'); alter table public.activity add constraint activity_kind_check check (kind in ('finished', 'rewatched', 'rated')); create or replace function public.azuu_item_activity() returns trigger language plpgsql security definer set search_path = '' as $$ declare st text; old_st text; old_rating 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' and not old.deleted and old.data is not null then old_st := old.data->>'status'; old_rating := old.data->>'rating'; if jsonb_typeof(old.data->'rewatchDates') = 'array' then rw_old := jsonb_array_length(old.data->'rewatchDates'); end if; end if; if st = 'termine' and old_st is distinct from 'termine' and coalesce(public.azuu_day(new.data->>'endDate'), case when tg_op = 'UPDATE' then current_date end) >= recent then perform public.azuu_log(new.user_id, new.id, 'finished'); elsif tg_op = 'UPDATE' and new.data ? 'rating' and (new.data->>'rating') is distinct from old_rating then -- Note donnée (ou changée) sur un titre déjà là : la ligne remonte en haut du fil perform public.azuu_log(new.user_id, new.id, 'rated'); 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; $$; -- Fil : titres notés de mes abonnements, une ligne par titre (l'évènement le plus récent) create or replace function public.get_feed(p_before timestamptz default null, p_limit integer 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 l.created_at, jsonb_build_object( 'id', l.id, 'kind', l.kind, 'created_at', l.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 ( select distinct on (a.user_id, a.item_id) a.id, a.user_id, a.item_id, a.kind, a.created_at 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' order by a.user_id, a.item_id, a.created_at desc ) l join public.profiles p on p.id = l.user_id and not p.suspended join public.items i on i.user_id = l.user_id and i.id = l.item_id and not i.deleted where (p_before is null or l.created_at < p_before) and (i.data->>'rating') ~ '^\d+(\.\d+)?$' and (i.data->>'rating')::numeric > 0 order by l.created_at desc limit least(greatest(p_limit, 1), 50) ) s $$; revoke execute on function public.get_feed(timestamptz, integer) from public, anon; grant execute on function public.get_feed(timestamptz, integer) to authenticated;