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
5 changes: 3 additions & 2 deletions src/routes/posts.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,14 @@ 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 } = req.query;

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

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.authorName - 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, authorName = null }) {
let orderBy;

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

if (authorName) {
whereClause += ` AND a.name = $${paramIndex}`;
params.push(authorName);
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