From 46a2fc5c2b2f8778c5a5108566f94c67eda02f7f Mon Sep 17 00:00:00 2001 From: Adolfo Reyna Date: Thu, 27 Feb 2025 23:45:17 -0500 Subject: [PATCH] Add endpoint to retrieve posts by tag with validation --- dbTools/post.js | 21 ++++++++++++++++++++- routes/post.js | 12 ++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/dbTools/post.js b/dbTools/post.js index 9c47a2d..fd3f336 100644 --- a/dbTools/post.js +++ b/dbTools/post.js @@ -233,7 +233,7 @@ postDB = (DB)=>{ if(!DB.ObjectID.isValid(profileId)) return []; const profile = await DB.getProfile(profileId); if(!profile) return []; - query = { + const query = { nonOrganicType: null // Exlcude news }; return DB.postCols.find(query).sort({lastUpdated: -1}).limit(50).toArray().then(async (posts)=>{ @@ -244,6 +244,25 @@ postDB = (DB)=>{ }); } + // For all post with tags const query = { content: { $regex: '#\\w+', $options: 'i' } }; + DB.getPostsByTag = async (tag, profileId, limit = 50) => { + if(!DB.ObjectID.isValid(profileId)) return []; + const profile = await DB.getProfile(profileId); + if(!profile) return []; + let query = { + content: { + "$regex": tag + }, + nonOrganicType: null // Exlcude news + }; + return DB.postCols.find(query).sort({lastUpdated: -1}).limit(limit).toArray().then(async (posts)=>{ + return await filterPrivateGroups(posts, profile); + }).catch((err)=>{ + console.log(err); + return false; + }); + } + DB.getNews = async () => { let query = { nonOrganicType: 'News' diff --git a/routes/post.js b/routes/post.js index aa141b0..0ff909f 100644 --- a/routes/post.js +++ b/routes/post.js @@ -95,6 +95,18 @@ DB.getDB.then((DB) => { return res.json(posts); }); + router.get("/tag/:tag", async (req, res) => { + const profileid = getProfileId(req); + const tag = req.params.tag; + if(!tag) { + return res.json({ + status: "Tag is required", + }); + } + let posts = await DB.getPostsByTag(tag, profileid); + return res.json(posts); + }); + router.get("/usr/:id", async (req, res) => { const profileId = req.params.id; const viewerProdileId = getProfileId(req);