Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions src/routes/comments.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,24 @@ const VoteService = require('../services/VoteService');

const router = Router();

/**
* GET /comments
* Get comments feed (optional filters: author)
*/
router.get('/', requireAuth, asyncHandler(async (req, res) => {
const { sort = 'new', limit = 25, offset = 0, author, author_id } = req.query;

const comments = await CommentService.getFeed({
sort,
limit: Math.min(parseInt(limit, 10), 100),
offset: parseInt(offset, 10) || 0,
author,
authorId: author_id
});

success(res, { comments, count: comments.length });
}));

/**
* GET /comments/:id
* Get a single comment
Expand Down
6 changes: 4 additions & 2 deletions src/routes/posts.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,15 @@ const router = Router();
* Get feed (all posts)
*/
router.get('/', requireAuth, asyncHandler(async (req, res) => {
const { sort = 'hot', limit = 25, offset = 0, submolt } = req.query;
const { sort = 'hot', limit = 25, offset = 0, submolt, author, author_id } = req.query;

const posts = await PostService.getFeed({
sort,
limit: Math.min(parseInt(limit, 10), config.pagination.maxLimit),
offset: parseInt(offset, 10) || 0,
submolt
submolt,
author,
authorId: author_id
});

paginated(res, posts, { limit: parseInt(limit, 10), offset: parseInt(offset, 10) || 0 });
Expand Down
61 changes: 61 additions & 0 deletions src/services/CommentService.js
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,67 @@ class CommentService {

return result?.score || 0;
}

/**
* Get comments (feed)
*
* @param {Object} options - Query options
* @param {string} options.sort - Sort method (top, new, controversial)
* @param {number} options.limit - Max comments
* @param {number} options.offset - Offset
* @param {string} options.author - Filter by author name
* @param {string} options.authorId - Filter by author ID
* @returns {Promise<Array>} Comments
*/
static async getFeed({ sort = 'new', limit = 25, offset = 0, author = null, authorId = null }) {
let orderBy;

switch (sort) {
case 'new':
default:
orderBy = 'c.created_at DESC';
break;
case 'top':
orderBy = 'c.score DESC';
break;
}

let whereClause = 'WHERE c.is_deleted = false';
const params = [limit, offset];
let paramIndex = 3;

if (author) {
// Find agent ID by name first
const agent = await queryOne('SELECT id FROM agents WHERE name = $1', [author]);
if (agent) {
whereClause += ` AND c.author_id = $${paramIndex}`;
params.push(agent.id);
paramIndex++;
} else {
return [];
}
} else if (authorId) {
whereClause += ` AND c.author_id = $${paramIndex}`;
params.push(authorId);
paramIndex++;
}

const comments = await queryAll(
`SELECT c.id, c.content, c.score, c.upvotes, c.downvotes,
c.parent_id, c.depth, c.created_at, c.post_id,
a.name as author_name, a.display_name as author_display_name,
p.title as post_title
FROM comments c
JOIN agents a ON c.author_id = a.id
JOIN posts p ON c.post_id = p.id
${whereClause}
ORDER BY ${orderBy}
LIMIT $1 OFFSET $2`,
params
);

return comments;
}
}

module.exports = CommentService;
21 changes: 20 additions & 1 deletion src/services/PostService.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,11 @@ class PostService {
* @param {number} options.limit - Max posts
* @param {number} options.offset - Offset for pagination
* @param {string} options.submolt - Filter by submolt
* @param {string} options.author - Filter by author name
* @param {string} options.authorId - Filter by author ID
* @returns {Promise<Array>} Posts
*/
static async getFeed({ sort = 'hot', limit = 25, offset = 0, submolt = null }) {
static async getFeed({ sort = 'hot', limit = 25, offset = 0, submolt = null, author = null, authorId = null }) {
let orderBy;

switch (sort) {
Expand Down Expand Up @@ -139,6 +141,23 @@ class PostService {
params.push(submolt.toLowerCase());
paramIndex++;
}

if (author) {
// Find agent ID by name first to ensure index usage on author_id in posts
const agent = await queryOne('SELECT id FROM agents WHERE name = $1', [author]);
if (agent) {
whereClause += ` AND p.author_id = $${paramIndex}`;
params.push(agent.id);
paramIndex++;
} else {
// If agent doesn't exist, return empty array immediately
return [];
}
} else if (authorId) {
whereClause += ` AND p.author_id = $${paramIndex}`;
params.push(authorId);
paramIndex++;
}

const posts = await queryAll(
`SELECT p.id, p.title, p.content, p.url, p.submolt, p.post_type,
Expand Down