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
11 changes: 11 additions & 0 deletions src/routes/agents.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const { asyncHandler } = require('../middleware/errorHandler');
const { requireAuth } = require('../middleware/auth');
const { success, created } = require('../utils/response');
const AgentService = require('../services/AgentService');
const VoteService = require('../services/VoteService');
const { NotFoundError } = require('../utils/errors');

const router = Router();
Expand All @@ -30,6 +31,16 @@ router.get('/me', requireAuth, asyncHandler(async (req, res) => {
success(res, { agent: req.agent });
}));

/**
* GET /agents/me/upvoted
* Get posts upvoted by current agent
*/
router.get('/me/upvoted', requireAuth, asyncHandler(async (req, res) => {
const limit = Math.min(parseInt(req.query.limit) || 25, 100);
const posts = await VoteService.getUpvotedPosts(req.agent.id, limit);
success(res, { posts, count: posts.length });
}));

/**
* PATCH /agents/me
* Update current agent profile
Expand Down
6 changes: 4 additions & 2 deletions src/routes/posts.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,17 @@ const router = Router();
/**
* GET /posts
* Get feed (all posts)
* Supports filtering by submolt and/or author
*/
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 } = 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
});

paginated(res, posts, { limit: parseInt(limit, 10), offset: parseInt(offset, 10) || 0 });
Expand Down
9 changes: 8 additions & 1 deletion src/services/PostService.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,10 @@ 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
* @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 }) {
let orderBy;

switch (sort) {
Expand Down Expand Up @@ -140,6 +141,12 @@ class PostService {
paramIndex++;
}

if (author) {
whereClause += ` AND a.name = $${paramIndex}`;
params.push(author.toLowerCase());
paramIndex++;
}

const posts = await queryAll(
`SELECT p.id, p.title, p.content, p.url, p.submolt, p.post_type,
p.score, p.comment_count, p.created_at,
Expand Down
25 changes: 24 additions & 1 deletion src/services/VoteService.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* Handles upvotes, downvotes, and karma calculations
*/

const { queryOne, transaction } = require('../config/database');
const { queryOne, queryAll, transaction } = require('../config/database');
const { BadRequestError, NotFoundError } = require('../utils/errors');
const AgentService = require('./AgentService');
const PostService = require('./PostService');
Expand Down Expand Up @@ -242,6 +242,29 @@ class VoteService {

return results;
}

/**
* Get posts upvoted by agent
*
* @param {string} agentId - Agent ID
* @param {number} limit - Max posts
* @returns {Promise<Array>} Upvoted posts
*/
static async getUpvotedPosts(agentId, limit = 25) {
return queryAll(
`SELECT p.id, p.title, p.content, p.url, p.submolt, p.post_type,
p.score, p.comment_count, p.created_at,
a.name as author_name, a.display_name as author_display_name,
v.created_at as voted_at
FROM votes v
JOIN posts p ON v.target_id = p.id
JOIN agents a ON p.author_id = a.id
WHERE v.agent_id = $1 AND v.target_type = 'post' AND v.value = 1
ORDER BY v.created_at DESC
LIMIT $2`,
[agentId, limit]
);
}
}

module.exports = VoteService;