-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsocket.js
More file actions
190 lines (175 loc) · 7.56 KB
/
Copy pathsocket.js
File metadata and controls
190 lines (175 loc) · 7.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/* This file contains actions related to handle notifications */
const StudyGroup = require("./src/models/StudyGroup");
const User = require("./src/models/User");
const jwt = require("jsonwebtoken");
const jwt_decode = require("jwt-decode");
const { io } = require("./server");
const Forum = require("./src/models/Forum");
const notificationTypes = {
join: "join-request",
accepted: "accepted",
collaborationMsg: "collaboration-msg",
postComment: "post-comment",
};
function handleRequestJoinGroup(data) {
const { id } = jwt_decode(data.jwt);
User.findOne({ _id: id }).then((sender) => {
User.findOne({ _id: data.group.creatorId }).then(async (receiver) => {
let notification = {
timeCreated: Date.now(),
senderId: sender._id,
senderName: sender.firstName + " " + sender.lastName,
type: notificationTypes.join,
group: data.group,
answers: data.answers,
questions: data.questions,
};
await User.updateOne(
{ _id: receiver._id },
{ unseenNotifications: receiver.unseenNotifications.concat(notification) }
);
await StudyGroup.updateOne(
{ _id: data.group._id },
{ pendingUsers: data.group.pendingUsers.concat(id)}
);
if (receiver.socketId) {
io.to(receiver.socketId).emit("notification", notification);
}
})
.catch(err => console.log('cant find receiver', err));
})
.catch(err => console.log('cant find sender', err));
}
function handleJoinGroupApproved(data) {
const { id } = jwt_decode(data.jwt);
User.findOne({ _id: id }).then((sender) => {
User.findOne({ _id: data.approvedUserId }).then(async (receiver) => {
let notification = {
timeCreated: Date.now(),
senderId: sender._id,
senderName: sender.firstName + " " + sender.lastName,
type: notificationTypes.accepted,
group: {...data.group, isAdmin: false, didAnswerSurvey: false}
};
await User.updateOne(
{ _id: receiver._id },
{ unseenNotifications: receiver.unseenNotifications.concat(notification) }
);
if (receiver.socketId) {
io.to(receiver.socketId).emit("notification", notification);
}
StudyGroup.findOne({ _id: data.group._id }).then(async (studyGroup) => {
const participants = studyGroup.participants;
const newParticipantName =
receiver.firstName + " " + receiver.lastName;
participants.push({
name: newParticipantName,
id: receiver._id.toString(),
isCreator: false,
didAnswerSurvey: false,
});
const isFull = participants.length === studyGroup.maxParticipants;
const pendingUsers = isFull ? [] : studyGroup.pendingUsers.filter(userId => userId !== receiver._id.toString());
const seenNotifications = isFull
? removeFullGroupNotification(sender.seenNotifications, studyGroup._id.toString())
: sender.seenNotifications.filter(notification => notification.timeCreated !== data.notificationId);
const unseenNotifications = isFull
? removeFullGroupNotification(sender.unseenNotifications, studyGroup._id.toString())
: sender.unseenNotifications;
await User.updateOne( { _id: sender._id }, { seenNotifications, unseenNotifications });
await StudyGroup.updateOne({ _id: studyGroup._id }, { participants, isFull, pendingUsers });
});
});
});
}
function handleJoinGroupIgnored(data) {
const { id } = jwt_decode(data.jwt);
User.findOne({ _id: id }).then((sender) => {
User.findOne({ _id: data.ignoredUserId }).then(async (receiver) => {
StudyGroup.findOne({ _id: data.group._id }).then(async (studyGroup) => {
const pendingUsers = studyGroup.pendingUsers.filter(userId => userId !== receiver._id.toString());
const seenNotifications = sender.seenNotifications.filter(notification => {
return notification.group._id !== studyGroup._id.toString()
|| notification.type !== notificationTypes.join
|| notification.senderId.toString() !== data.ignoredUserId;
});
await User.updateOne( { _id: sender._id }, { seenNotifications });
await StudyGroup.updateOne({ _id: studyGroup._id }, { pendingUsers });
});
});
});
}
function handleCollaborationMsg(data) {
const { id } = jwt_decode(data.jwt);
User.findOne({ _id: id }).then((sender) => {
StudyGroup.findOne({ _id: data.group._id }).then(async (studyGroup) => {
const recieverUsers = studyGroup.participants.filter(participant => participant.id !== id);
let notification = {
timeCreated: Date.now(),
senderId: sender._id,
senderName: sender.firstName + " " + sender.lastName,
type: notificationTypes.collaborationMsg,
};
recieverUsers.forEach((recieverUser) => {
sendGroupNotificationAndUpdateDB(recieverUser, notification, data.group)
});
});
});
}
function handlePostComment(data) {
const { id } = jwt_decode(data.jwt);
User.findOne({ _id: id }).then((sender) => {
Forum.findOne({ _id: data.forumId }).then(async (forum) => {
let recieverUsers = new Set();
const post = forum.posts.find(post => post._id === data.post._id);
post.comments.forEach(comment => {
if(comment.creatorId !== id) {
recieverUsers.add(comment.creatorId);
}
});
if(data.post.creatorId !== id) {
recieverUsers.add(data.post.creatorId);
}
let notification = {
timeCreated: Date.now(),
senderId: sender._id,
senderName: sender.firstName + " " + sender.lastName,
type: notificationTypes.postComment,
post,
forumId: data.forumId,
};
recieverUsers.forEach((recieverUserId) => {
sendPostNotificationAndUpdateDB(recieverUserId, notification);
});
});
});
}
function sendGroupNotificationAndUpdateDB(user, notification, studyGroup) {
User.findOne({ _id: user.id }).then(async (receiver) => {
let newNotification = {...notification, group: {...studyGroup, isAdmin: user.isCreator, didAnswerSurvey: user.didAnswerSurvey}};
await User.updateOne(
{ _id: user.id },
{ unseenNotifications: receiver.unseenNotifications.concat(newNotification) }
);
if (receiver.socketId) {
io.to(receiver.socketId).emit("notification", notification);
}
});
}
function sendPostNotificationAndUpdateDB(userId, notification) {
User.findOne({ _id: userId }).then(async (receiver) => {
await User.updateOne(
{ _id: userId },
{ unseenNotifications: receiver.unseenNotifications.concat(notification) }
);
if (receiver.socketId) {
io.to(receiver.socketId).emit("notification", notification);
}
});
}
function removeFullGroupNotification(notifications, groupId) {
return notifications.filter( notification => {
return notification.group._id !== groupId || notification.type !== notificationTypes.join;
});
}
module.exports = { handlePostComment, handleCollaborationMsg, handleJoinGroupIgnored, handleJoinGroupApproved, handleRequestJoinGroup };