-- Exclude banned users' likes and comments from the feed's scalar counts (H11).
--
-- `v_feed` already excludes banned UPLOADERS (`usr.is_banned = FALSE` on the join), but the two
-- correlated subqueries added by migration 024 counted every like and every non-deleted comment
-- regardless of who wrote it. So after a ban:
--
--   * the banned guest's own photos disappear from the feed (correct), but
--   * their likes still inflate the counter on everyone else's photos, and
--   * their comments still contribute to `comment_count` — and, until the change to
--     `Comment::list_for_upload` that ships with this migration, were still RENDERED in the
--     lightbox on the most-viewed photo of the evening.
--
-- The host's mental model of "ban" is "this person's contributions are gone". Photos honoured it;
-- likes and comments did not. Migration 021 already applied exactly this reasoning to hashtag
-- counts, and the export query filters `is_banned` too — this brings the last read path in line.
--
-- Derived at read time, so `unban_user` restores the counts with no extra work, exactly as it
-- already restores the photos.

CREATE OR REPLACE VIEW v_feed AS
SELECT
    u.id,
    u.event_id,
    u.user_id,
    usr.display_name AS uploader_name,
    usr.is_banned,
    usr.uploads_hidden,
    u.preview_path,
    u.thumbnail_path,
    u.display_path,
    u.mime_type,
    u.caption,
    u.created_at,
    (SELECT count(*) FROM "like" l
       JOIN "user" lu ON lu.id = l.user_id
      WHERE l.upload_id = u.id AND NOT lu.is_banned)                                   AS like_count,
    (SELECT count(*) FROM comment c
       JOIN "user" cu ON cu.id = c.user_id
      WHERE c.upload_id = u.id AND c.deleted_at IS NULL AND NOT cu.is_banned)          AS comment_count
FROM upload u
JOIN "user" usr ON u.user_id = usr.id
WHERE u.deleted_at IS NULL
  AND usr.uploads_hidden = FALSE
  AND usr.is_banned = FALSE;
