if(q){where.push("p.content LIKE ?");args.push(`%${q}%`);}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 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.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[];
constsources=db.prepare("SELECT id,name FROM sources WHERE is_enabled=1 ORDER BY name").all()as{id: number;name: string}[];