Bunch small things: Notifs for react, subsplash...

This commit is contained in:
aeroreyna
2022-12-28 22:54:19 -05:00
parent 6b6f263821
commit 1f3cc06470
10 changed files with 686 additions and 295 deletions

View File

@@ -15,13 +15,19 @@ const sendPushNotification = async (profileToken, body, data) => {
if (!profileToken) return 0;
let expo = new Expo();
if(!Array.isArray(profileToken)) return 0;
let pushTokens = [];
profileToken.forEach((token) => {
if (!Expo.isExpoPushToken(token)) {
console.error(`Push token ${token} is not a valid Expo push token`);
return 0;
}
pushTokens.push(token);
})
// Create the messages that you want to send to clients
let messages = [];
let pushToken = profileToken;
if (!Expo.isExpoPushToken(pushToken)) {
console.error(`Push token ${pushToken} is not a valid Expo push token`);
return 0;
}
// Construct a message (see https://docs.expo.io/push-notifications/sending-notifications/)
messages.push({
to: pushToken,
@@ -348,6 +354,34 @@ const Notifications = {
DB.addNotification(post.profileid, notifBody, postId, post.comments.length - 1, senderProfile._id);
return youGotANewPostCommentTemplate(post, userEmail, postProfile, senderProfile, message);
},
async yourBookmarkedPostGotAReactiom(post, postProfile, senderProfile) {
const DB = await DBGetter.getDB;
const subscribedPromise = post.bookmarks.map((profileid) => {
return DB.getProfileCache(profileid);
});
const subscribed = await Promise.all(subscribedPromise);
subscribed.forEach((bookedProfile) => {
if (bookedProfile._id == senderProfile._id) return 0;
const notifBody = `${senderProfile.profile.firstName} liked a post you follow`;
sendPushNotification(bookedProfile.token, notifBody, {});
DB.addNotification(bookedProfile._id, notifBody, post._id, null, senderProfile._id);
});
},
async youGotANewReaction(postId, whoReactedId, reactionType){
const DB = await DBGetter.getDB;
const post = await DB.getPost(postId);
const postProfile = await DB.getProfileCache(post.profileid);
const senderProfile = await DB.getProfileCache(whoReactedId);
const userEmail = await DB.getUsernameByIdCache(postProfile.userid);
if (post.bookmarks) {
this.yourBookmarkedPostGotAReactiom(post, postProfile, senderProfile)
}
if (postProfile.isCourse || senderProfile._id == postProfile._id) return 0; //Course owners do not need to receive notifs
const notifBody = `${senderProfile.profile.firstName} liked your post`;
sendPushNotification(postProfile.token, notifBody, {});
DB.addNotification(post.profileid, notifBody, postId, null, senderProfile._id);
return 0;
},
async yourGroupGotANewPost(groupProfile, senderProfile, message, post) {
const DB = await DBGetter.getDB;
let subscribedPromise = Object.keys(groupProfile.subscribed).map((profileid) => {
@@ -384,7 +418,7 @@ const Notifications = {
}
const notifBody = `${senderProfile.profile.firstName} post in your profile`;
sendPushNotification(profile.token, notifBody, {});
sendWebNotification(profile.webSubscription, notifBody, message)
sendWebNotification(profile.webSubscription, notifBody, message);
DB.addNotification(toProfileId, notifBody, post._id, null, senderProfile._id);
return youGotANewPostTemplate(profile, user.username, senderProfile, message);
},
@@ -396,14 +430,22 @@ const Notifications = {
const groupProfile = await DB.getProfileCache(groupId);
const user = await DB.getUserById(groupProfile.userid);
yourGroupHasARequestTemplate(groupProfile, user.username, requesterProfile)
const notifBody = `${requesterProfile.profile.firstName} wants to join your group ${groupProfile.profile.firstName} ${groupProfile.profile.lastName}`;
//sendPushNotification(profile.token, notifBody, {});
//sendWebNotification(profile.webSubscription, notifBody, message);
},
async yourGroupRequestAccepted(requesterProfileId, groupId) {
const DB = await DBGetter.getDB;
const requesterProfile = await DB.getProfileCache(requesterProfileId);
const groupProfile = await DB.getProfileCache(groupId);
const user = await DB.getUserById(requesterProfile.userid);
yourGroupRequestAcceptedTemplate(groupProfile, user.username, requesterProfile)
}
yourGroupRequestAcceptedTemplate(groupProfile, user.username, requesterProfile);
const notifBody = `You were added to the group ${groupProfile.profile.firstName} ${groupProfile.profile.lastName}`;
sendPushNotification(requesterProfile.token, notifBody, {});
sendWebNotification(requesterProfile.webSubscription, notifBody);
DB.addNotification(requesterProfile, notifBody, null, null, groupProfile._id);
},
}
module.exports = Notifications