From 2b4e476e2fc99182f57cc6bbe8064459df02947a Mon Sep 17 00:00:00 2001 From: mamad Date: Fri, 28 Aug 2026 22:16:18 +0330 Subject: [PATCH] fix: robust jsonb target post matching in get_recent_target_posts --- db/repository.py | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/db/repository.py b/db/repository.py index b6cfb50..59cbbd1 100644 --- a/db/repository.py +++ b/db/repository.py @@ -220,13 +220,19 @@ class Repository: rows = await conn.fetch( """ SELECT * FROM posts - WHERE ( - status = 'published' - OR target_channel_id = $1 - OR published_to::text LIKE '%"target_id": ' || $1 || '%' - ) - AND ($2::BIGINT IS NULL OR id != $2) - AND is_deleted = FALSE + WHERE is_deleted = FALSE + AND ($2::BIGINT IS NULL OR id != $2) + AND ( + target_channel_id = $1 + OR ( + published_to IS NOT NULL + AND jsonb_typeof(published_to) = 'array' + AND EXISTS ( + SELECT 1 FROM jsonb_array_elements(published_to) elem + WHERE (elem->>'target_id')::bigint = $1 + ) + ) + ) ORDER BY COALESCE(published_at, created_at) DESC, id DESC LIMIT $3; """,