Add endpoint to retrieve posts by tag with validation
This commit is contained in:
@@ -233,7 +233,7 @@ postDB = (DB)=>{
|
|||||||
if(!DB.ObjectID.isValid(profileId)) return [];
|
if(!DB.ObjectID.isValid(profileId)) return [];
|
||||||
const profile = await DB.getProfile(profileId);
|
const profile = await DB.getProfile(profileId);
|
||||||
if(!profile) return [];
|
if(!profile) return [];
|
||||||
query = {
|
const query = {
|
||||||
nonOrganicType: null // Exlcude news
|
nonOrganicType: null // Exlcude news
|
||||||
};
|
};
|
||||||
return DB.postCols.find(query).sort({lastUpdated: -1}).limit(50).toArray().then(async (posts)=>{
|
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 () => {
|
DB.getNews = async () => {
|
||||||
let query = {
|
let query = {
|
||||||
nonOrganicType: 'News'
|
nonOrganicType: 'News'
|
||||||
|
|||||||
@@ -95,6 +95,18 @@ DB.getDB.then((DB) => {
|
|||||||
return res.json(posts);
|
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) => {
|
router.get("/usr/:id", async (req, res) => {
|
||||||
const profileId = req.params.id;
|
const profileId = req.params.id;
|
||||||
const viewerProdileId = getProfileId(req);
|
const viewerProdileId = getProfileId(req);
|
||||||
|
|||||||
Reference in New Issue
Block a user