This commit is contained in:
Adolfo Reyna
2023-07-08 16:36:46 -04:00
3 changed files with 30 additions and 1 deletions

View File

@@ -352,6 +352,12 @@ userDB = (DB) => {
return g ? g.isPrivate : false; 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) => { DB.isOwnerOfGroup = async (profileid, groupid) => {
let profile = await DB.getProfileCache(profileid); let profile = await DB.getProfileCache(profileid);
let group = userProfileCache[groupid] ? userProfileCache[groupid] : await DB.getGroup(groupid); let group = userProfileCache[groupid] ? userProfileCache[groupid] : await DB.getGroup(groupid);

View File

@@ -384,6 +384,7 @@ const Notifications = {
}, },
async yourGroupGotANewPost(groupProfile, senderProfile, message, post) { async yourGroupGotANewPost(groupProfile, senderProfile, message, post) {
const DB = await DBGetter.getDB; const DB = await DBGetter.getDB;
if(!groupProfile.subscribed) return 0;
let subscribedPromise = Object.keys(groupProfile.subscribed).map((profileid) => { let subscribedPromise = Object.keys(groupProfile.subscribed).map((profileid) => {
return DB.getProfileCache(profileid); return DB.getProfileCache(profileid);
}); });
@@ -395,6 +396,11 @@ const Notifications = {
users.forEach((userEmail, index) => { users.forEach((userEmail, index) => {
let userProfile = subscribed[index]; //who is this email sending to let userProfile = subscribed[index]; //who is this email sending to
if (userProfile._id == senderProfile._id) return 0; //avoid sending self notifications 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}`; const notifBody = `${senderProfile.profile.firstName} post in the group ${groupProfile.profile.firstName} ${groupProfile.profile.lastName}`;
sendPushNotification(userProfile.token, notifBody, {}); sendPushNotification(userProfile.token, notifBody, {});
DB.addNotification(userProfile._id, notifBody, post._id, null, senderProfile._id); 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); DB.addNotification(toProfileId, notifBody, post._id, null, senderProfile._id);
return youGotANewPostTemplate(profile, user.username, senderProfile, message); 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, youHaveAnInvitation,
broadcastNews, broadcastNews,
async yourGroupHasARequest(requesterProfileId, groupId) { async yourGroupHasARequest(requesterProfileId, groupId) {

View File

@@ -141,7 +141,9 @@ DB.getDB.then((DB) => {
profileid: getProfileId(req), profileid: getProfileId(req),
...req.body ...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 requestProfile = getProfileId(req) + "";
let group = await DB.getProfileCache(post.toProfile); let group = await DB.getProfileCache(post.toProfile);
if (!group.subscribed[requestProfile] && group._id != requestProfile) { 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; post.toProfile = post.toProfile ? DB.ObjectID(post.toProfile) : undefined;
let postObj = new Post(post); let postObj = new Post(post);
let dbr = await DB.newPost(postObj); let dbr = await DB.newPost(postObj);
@@ -158,6 +165,7 @@ DB.getDB.then((DB) => {
if ((post.toProfile && post.toProfile != post.profileid) || post.nonOrganicType == 'News') { if ((post.toProfile && post.toProfile != post.profileid) || post.nonOrganicType == 'News') {
Notifications.youGotANewPost(post); Notifications.youGotANewPost(post);
} }
Notifications.yourGroupGotANewPost(post);
return res.json({ return res.json({
status: "ok", status: "ok",
...post ...post