diff --git a/dbTools/profile.js b/dbTools/profile.js index f4f3240..106862c 100644 --- a/dbTools/profile.js +++ b/dbTools/profile.js @@ -352,6 +352,12 @@ userDB = (DB) => { return g ? g.isPrivate : false; } + DB.isGroupNewsOnly = async (groupid) => { + if(userProfileCache[groupid]) return userProfileCache[groupid].newsOnly; + let g = await DB.getGroup(groupid); + return g ? g.newsOnly : false; + } + DB.isOwnerOfGroup = async (profileid, groupid) => { let profile = await DB.getProfileCache(profileid); let group = userProfileCache[groupid] ? userProfileCache[groupid] : await DB.getGroup(groupid); diff --git a/notifications.js b/notifications.js index fffcbbe..3628dbf 100644 --- a/notifications.js +++ b/notifications.js @@ -384,6 +384,7 @@ const Notifications = { }, async yourGroupGotANewPost(groupProfile, senderProfile, message, post) { const DB = await DBGetter.getDB; + if(!groupProfile.subscribed) return 0; let subscribedPromise = Object.keys(groupProfile.subscribed).map((profileid) => { return DB.getProfileCache(profileid); }); @@ -395,6 +396,11 @@ const Notifications = { users.forEach((userEmail, index) => { let userProfile = subscribed[index]; //who is this email sending to if (userProfile._id == senderProfile._id) return 0; //avoid sending self notifications + if(groupProfile._id == senderProfile._id){ + const notifBody = `${groupProfile.profile.firstName} ${groupProfile.profile.lastName} has a new post!`; + sendPushNotification(userProfile.token, notifBody, {}); + return DB.addNotification(userProfile._id, notifBody, post._id, null, senderProfile._id); + } const notifBody = `${senderProfile.profile.firstName} post in the group ${groupProfile.profile.firstName} ${groupProfile.profile.lastName}`; sendPushNotification(userProfile.token, notifBody, {}); DB.addNotification(userProfile._id, notifBody, post._id, null, senderProfile._id); @@ -422,6 +428,15 @@ const Notifications = { DB.addNotification(toProfileId, notifBody, post._id, null, senderProfile._id); return youGotANewPostTemplate(profile, user.username, senderProfile, message); }, + async yourGroupMakeANewPost(post) { + const whoPostedId = post.profileid; + const message = post.content; + const DB = await DBGetter.getDB; + const profile = await DB.getProfileCache(whoPostedId); + if (profile.isGroup) { + return this.yourGroupGotANewPost(profile, profile, message, post); + } + }, youHaveAnInvitation, broadcastNews, async yourGroupHasARequest(requesterProfileId, groupId) { diff --git a/routes/post.js b/routes/post.js index 262fbda..617e57a 100644 --- a/routes/post.js +++ b/routes/post.js @@ -141,7 +141,9 @@ DB.getDB.then((DB) => { profileid: getProfileId(req), ...req.body } - if (post.toProfile && await DB.isGroupPrivate(post.toProfile)) { + const isGroupPrivate = await DB.isGroupPrivate(post.toProfile); + const isGroupNewsOnly = await DB.isGroupNewsOnly(post.toProfile); + if (post.toProfile && isGroupPrivate) { let requestProfile = getProfileId(req) + ""; let group = await DB.getProfileCache(post.toProfile); if (!group.subscribed[requestProfile] && group._id != requestProfile) { @@ -150,6 +152,11 @@ DB.getDB.then((DB) => { }); } } + if (post.toProfile && isGroupNewsOnly) { + return res.json({ + status: "This is a news only group, only the admin can post", + }); + } post.toProfile = post.toProfile ? DB.ObjectID(post.toProfile) : undefined; let postObj = new Post(post); let dbr = await DB.newPost(postObj); @@ -158,6 +165,7 @@ DB.getDB.then((DB) => { if ((post.toProfile && post.toProfile != post.profileid) || post.nonOrganicType == 'News') { Notifications.youGotANewPost(post); } + Notifications.yourGroupGotANewPost(post); return res.json({ status: "ok", ...post