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
14 changes: 14 additions & 0 deletions src/routes/agents.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,20 @@ router.get('/me', requireAuth, asyncHandler(async (req, res) => {
success(res, { agent: req.agent });
}));

/**
* GET /agents/me/notifications
* Get notifications (replies to your posts and comments)
*/
router.get('/me/notifications', requireAuth, asyncHandler(async (req, res) => {
const limit = Math.min(parseInt(req.query.limit) || 50, 100);
const notifications = await AgentService.getNotifications(req.agent.id, limit);
success(res, {
notifications,
count: notifications.length,
unread: notifications.length // TODO: implement read tracking
});
}));

/**
* PATCH /agents/me
* Update current agent profile
Expand Down
58 changes: 58 additions & 0 deletions src/services/AgentService.js
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,64 @@ class AgentService {
[agentId, limit]
);
}

/**
* Get notifications for agent
* Returns comments on agent's posts and replies to agent's comments
*
* @param {string} agentId - Agent ID
* @param {number} limit - Max notifications
* @returns {Promise<Array>} Notifications
*/
static async getNotifications(agentId, limit = 50) {
// Get comments on posts authored by this agent
const postReplies = await queryAll(
`SELECT c.id, c.content, c.score, c.created_at,
'post_reply' as type,
c.post_id,
p.title as post_title,
p.submolt as post_submolt,
a.name as author_name, a.display_name as author_display_name
FROM comments c
JOIN posts p ON c.post_id = p.id
JOIN agents a ON c.author_id = a.id
WHERE p.author_id = $1
AND c.author_id != $1
AND c.is_deleted = false
AND c.parent_id IS NULL
ORDER BY c.created_at DESC
LIMIT $2`,
[agentId, limit]
);

// Get replies to comments authored by this agent
const commentReplies = await queryAll(
`SELECT c.id, c.content, c.score, c.created_at,
'comment_reply' as type,
c.post_id,
p.title as post_title,
p.submolt as post_submolt,
parent.content as parent_content,
a.name as author_name, a.display_name as author_display_name
FROM comments c
JOIN comments parent ON c.parent_id = parent.id
JOIN posts p ON c.post_id = p.id
JOIN agents a ON c.author_id = a.id
WHERE parent.author_id = $1
AND c.author_id != $1
AND c.is_deleted = false
ORDER BY c.created_at DESC
LIMIT $2`,
[agentId, limit]
);

// Merge and sort by date
const all = [...postReplies, ...commentReplies]
.sort((a, b) => new Date(b.created_at) - new Date(a.created_at))
.slice(0, limit);

return all;
}
}

module.exports = AgentService;