constftsQuery=q.split(/\s+/).filter(Boolean).map((term)=>`"${term.replaceAll('"','""')}"`).join(" AND ");
if(q){where.push("posts_fts MATCH ?");args.push(ftsQuery);}if(tag){where.push("p.tags_json LIKE ?");args.push(`%${JSON.stringify(tag).slice(1,-1)}%`);}if(author){where.push("u.username LIKE ?");args.push(`%${author}%`);}if(sourceId){where.push("s.id=?");args.push(sourceId);}if(from){where.push("date(COALESCE(p.remote_created_at,p.created_at)) >= date(?)");args.push(from);}if(to){where.push("date(COALESCE(p.remote_created_at,p.created_at)) <= date(?)");args.push(to);}if(attachments)where.push("p.attachments_json <> '[]'");
constjoins=` FROM posts p ${q?"JOIN posts_fts ON posts_fts.rowid=p.id":""} JOIN users u ON u.id=p.author_id LEFT JOIN sources s ON s.id=p.source_id `;constpredicate=` WHERE ${where.join(" AND ")}`;
constposts=db.prepare(`SELECT p.*,u.username,s.name,s.remote_display_name,s.base_url AS source_base_url,(SELECT count(*) FROM comments c WHERE c.post_id=p.id AND c.hidden=0) comment_count,(SELECT count(*) FROM reactions r WHERE r.post_id=p.id) reaction_count${joins}${predicate} ORDER BY COALESCE(p.remote_created_at,p.created_at) DESC LIMIT ? OFFSET ?`).all(...args,pageSize,(safePage-1)*pageSize)asPublicPost[];