-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsocket.js
More file actions
113 lines (97 loc) · 3.5 KB
/
Copy pathsocket.js
File metadata and controls
113 lines (97 loc) · 3.5 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
const socketio = require("socket.io");
const Chatroom = require("./models/Chatroom");
const User = require("./models/User");
const sendMsgNotifEmail = require("./util/sendMsgNotifEmail");
module.exports.listen = (app) => {
const io = socketio.listen(app);
io.on("connection", (socket) => {
socket.on("msg", async (data) => {
try {
io.emit("msg", data);
// fetch chatroom
const chatroom = await Chatroom.findById(data.cid).populate(
"searcher listing"
);
// set other user to unread
const otherUserUid = chatroom.uids.filter((uid) => uid !== data.uid)[0];
const updatedNotifUids = [...chatroom.notifUids];
if (!chatroom.notifUids.includes(otherUserUid)) {
updatedNotifUids.push(otherUserUid);
}
// remove cid from new msg
const newMsg = {
...data,
cid: undefined,
};
// update DB
const updateData = {
notifUids: updatedNotifUids,
$push: { msgs: newMsg },
};
await Chatroom.findByIdAndUpdate(data.cid, updateData, { new: true });
// send email notif to other user
const isSearcher = otherUserUid === chatroom.searcher.uid;
const targetEmail = isSearcher
? chatroom.searcher.email
: chatroom.listing.user.email;
const firstName = isSearcher
? chatroom.listing.user.name.split(" ")[0]
: chatroom.searcher.name.split(" ")[0];
/* removed because it's handling an impossible case:
listing owner hasn't created an account yet
*/
// if (!isSearcher) {
// console.log("email recipient is listing owner");
// // if listing owner hasn't created an account yet, don't send email alert
// console.log("otherUserUid", otherUserUid);
// const user = await User.findOne({ uid: otherUserUid });
// if (!user) {
// console.log("prevent send email notif");
// return;
// }
// }
sendMsgNotifEmail(targetEmail, firstName, data.content);
} catch (e) {
console.log("ERROR: socket > msg", e);
}
});
socket.on("new chatroom", async (chatroom) => {
try {
io.emit("new chatroom", chatroom);
// chatroom can only be created by searcher
// send email notif to listing owner
const { name } = chatroom.searcher;
const firstName = name.split(" ")[0];
const { email } = chatroom.listing.user;
const user = await User.findOne({ email });
if (user && chatroom.listing.user.uid === user.uid) {
console.log("passed uid validation");
sendMsgNotifEmail(email, firstName, chatroom.msgs[0].content);
} else {
console.log("failed uid validation");
}
} catch (e) {
console.log("ERROR: socket > new chatroom", e);
}
});
socket.on("chatroom seen", async (data) => {
try {
io.emit("chatroom seen", data);
// save to DB
const chatroom = await Chatroom.findById(data.cid);
const newNotifUids = [...chatroom.notifUids];
if (newNotifUids.includes(data.uid)) {
newNotifUids.splice(newNotifUids.indexOf(data.uid), 1);
}
chatroom.notifUids = newNotifUids;
await chatroom.save();
} catch (e) {
console.log("ERROR: socket > chatroom seen", e);
}
});
socket.on("disconnect", () => {
// handle disconnect
});
});
return io;
};