From 66748cec8b234470a225604b7a72313427cbce35 Mon Sep 17 00:00:00 2001 From: Adolfo Reyna Date: Mon, 6 Sep 2021 13:22:35 -0700 Subject: [PATCH] search profiles withot search --- dbTools/profile.js | 30 ++++++++++++++++++++++++++++-- def/profile.js | 2 ++ routes/profile.js | 9 +++++++++ 3 files changed, 39 insertions(+), 2 deletions(-) diff --git a/dbTools/profile.js b/dbTools/profile.js index 429c4ef..6d188a5 100644 --- a/dbTools/profile.js +++ b/dbTools/profile.js @@ -13,7 +13,7 @@ userDB = (DB) => { } DB.getProfile = async (profileId) => { - //if (userProfileCache[profileId]) return userProfileCache[profileId]; + //if (userProfileCache[profileId] && !userProfileCache[profileId].isGroup) return userProfileCache[profileId]; const _id = DB.ObjectID(profileId); let r = await DB.profileCols.findOne({ _id }).catch((err) => { console.log(err); @@ -23,6 +23,16 @@ userDB = (DB) => { return r; } + DB.getProfiles = async (query) => { + let r = await DB.profileCols.find({isGroup: false}) + .sort({ lastUpdate: -1 }).limit(20) + .toArray().catch((err) => { + console.log(err); + return false; + }); + return r; + } + DB.getUserProfiles = async (userId) => { const userid = DB.ObjectID(userId); return await DB.profileCols.find({ userid }).toArray().catch((err) => { @@ -71,6 +81,22 @@ userDB = (DB) => { }); } + DB.addNotification = async (profileid, message) => { + const _id = DB.ObjectID(profileid); + let update = { + $push:{ + notifications: { + ts: new Date(), + body: message + } + } + } + return DB.profileCols.updateOne({_id}, update).catch((err)=>{ + console.log(err); + return false; + }); + } + //Groups DB.getGroups = async () => { let r = await DB.profileCols.find({isGroup: true}) @@ -84,7 +110,7 @@ userDB = (DB) => { DB.getGroup = async (groupid) => { const _id = DB.ObjectID(groupid); - if(userProfileCache[groupid]) return userProfileCache[groupid]; + //if(userProfileCache[groupid]) return userProfileCache[groupid]; let r = await DB.profileCols.findOne({_id, isGroup: true}) .toArray().catch((err) => { console.log(err); diff --git a/def/profile.js b/def/profile.js index 09a7335..1193d03 100644 --- a/def/profile.js +++ b/def/profile.js @@ -19,6 +19,7 @@ class User { this.isGroup = info.isGroup || false; this.isCourse = info.isCourse || false; this.subscribed = info.subscribed || {}; + this.notifications = info.notifications || []; } toObj(){ @@ -34,6 +35,7 @@ class User { r.isGroup = this.isGroup; r.isCourse = this.isCourse; r.subscribed = this.subscribed; + r.notifications = this.notifications; return r; } diff --git a/routes/profile.js b/routes/profile.js index c4b7ac0..ece3d7d 100644 --- a/routes/profile.js +++ b/routes/profile.js @@ -87,6 +87,15 @@ DB.getDB.then((DB)=>{ }); }); + router.get("/search", async (req, res) => { + let query = req.query.query; + let profiles = await DB.getProfiles(query); + return res.json({ + status: "ok", + profiles + }); + }); + router.get("/:id", async (req, res) => { let profileId = req.params.id; let profile = await DB.getProfile(profileId);