diff --git a/dbTools/profile.js b/dbTools/profile.js index a3a01c8..5fd205c 100644 --- a/dbTools/profile.js +++ b/dbTools/profile.js @@ -198,6 +198,16 @@ userDB = (DB) => { }); } + DB.getFollowingTheProfile = async (profileId) => { + //const profile_id = DB.ObjectID(profileId); + let r = await DB.profileCols.find({ following: (profileId+'') }) + .toArray().catch((err) => { + console.log(err); + return []; + }); + return r; + } + DB.getData = async (profileid, key) => { let profile = await DB.getProfile(profileid); return profile.data[key]; diff --git a/notifications.js b/notifications.js index 7fedd3f..c9e131b 100644 --- a/notifications.js +++ b/notifications.js @@ -445,6 +445,21 @@ const Notifications = { return this.yourGroupGotANewPost(profile, profile, message, post); } }, + async yourSubscriptionProfileHasNewPost(post) { + const whoPostedId = post.profileid; + const message = post.content; + const DB = await DBGetter.getDB; + const profile = await DB.getProfileCache(whoPostedId); + const subscribed_profiles = await DB.getFollowingTheProfile(whoPostedId); + subscribed_profiles.forEach((subscribed_id, index) => { + const userProfile = subscribed_profiles[index]; + if (userProfile._id == whoPostedId._id) return 0; + const notifBody = `${profile.profile.firstName} posted: ${message.substring(0, 50)}...`; + sendPushNotification(userProfile.token, notifBody, {}); + //sendWebNotification(userProfile.webSubscription, notifBody, message); + DB.addNotification(userProfile._id, notifBody, post._id, null, profile._id); + }); + }, youHaveAnInvitation, broadcastNews, async yourGroupHasARequest(requesterProfileId, groupId) { diff --git a/routes/post.js b/routes/post.js index 617e57a..bbc8e17 100644 --- a/routes/post.js +++ b/routes/post.js @@ -163,9 +163,12 @@ DB.getDB.then((DB) => { post = postObj.toObj(); post._id = dbr.insertedId; if ((post.toProfile && post.toProfile != post.profileid) || post.nonOrganicType == 'News') { - Notifications.youGotANewPost(post); + await Notifications.youGotANewPost(post); + } + await Notifications.yourGroupMakeANewPost(post); + if(!isGroupPrivate){ + await Notifications.yourSubscriptionProfileHasNewPost(post); } - Notifications.yourGroupGotANewPost(post); return res.json({ status: "ok", ...post