|
| 1 | +const express = require("express"); |
| 2 | +const router = express.Router(); |
| 3 | +const bcrypt = require("bcryptjs"); |
| 4 | +const jwt = require("jsonwebtoken"); |
| 5 | +var fs = require("fs"); |
| 6 | +const passport = require("passport"); |
| 7 | + |
| 8 | +var dateFormat = require("dateformat"); |
| 9 | + |
| 10 | +const nodemailer = require("nodemailer"); |
| 11 | + |
| 12 | +const multer = require("multer"); |
| 13 | +const path = require("path"); |
| 14 | + |
| 15 | +// Load User model |
| 16 | +const User = require("./models/User"); |
| 17 | +// Load Matcheduserlist model |
| 18 | +const Matcheduserlist = require("./models/Matcheduserlist"); |
| 19 | +// Load Communitychats model |
| 20 | +const Communitychats = require("./models/Communitychats"); |
| 21 | + |
| 22 | +const io = require("./mychatserver.js").io; |
| 23 | + |
| 24 | +const { |
| 25 | + VERIFY_USER, |
| 26 | + USER_CONNECTED, |
| 27 | + USER_DISCONNECTED, |
| 28 | + LOGOUT, |
| 29 | + COMMUNITY_CHAT, |
| 30 | + COMMUNITY_CHAT_HISTORY, |
| 31 | + MESSAGE_RECIEVED, |
| 32 | + MESSAGE_SENT, |
| 33 | + TYPING, |
| 34 | + PRIVATE_MESSAGE |
| 35 | +} = require("./client/src/Events"); |
| 36 | + |
| 37 | +const { |
| 38 | + createUser, |
| 39 | + createMessage, |
| 40 | + createChat |
| 41 | +} = require("./client/src/Factories"); |
| 42 | + |
| 43 | +let connectedUsers = {}; |
| 44 | + |
| 45 | +let communityChat = createChat({ isCommunity: true }); |
| 46 | + |
| 47 | +module.exports = function(socket) { |
| 48 | + // console.log('\x1bc'); //clears console |
| 49 | + |
| 50 | + console.log("New user connected where Socket Id:" + socket.id); |
| 51 | + |
| 52 | + let sendMessageToChatFromUser; |
| 53 | + |
| 54 | + let sendTypingFromUser; |
| 55 | + |
| 56 | + //listen on VERIFY_USER |
| 57 | + //Verify Username |
| 58 | + socket.on(VERIFY_USER, (nickname, callback) => { |
| 59 | + console.log("listen on VERIFY_USER"); |
| 60 | + if (isUser(connectedUsers, nickname)) { |
| 61 | + callback({ isUser: true, user: null }); |
| 62 | + } else { |
| 63 | + callback({ |
| 64 | + isUser: false, |
| 65 | + user: createUser({ name: nickname, socketId: socket.id }) |
| 66 | + }); |
| 67 | + } |
| 68 | + }); |
| 69 | + |
| 70 | + //In order to send an event to everyone, Socket.IO gives us the io.emit |
| 71 | + |
| 72 | + //listen on USER_CONNECTED |
| 73 | + //User Connects with username |
| 74 | + socket.on(USER_CONNECTED, user => { |
| 75 | + console.log("listen on USER_CONNECTED"); |
| 76 | + user.socketId = socket.id; |
| 77 | + connectedUsers = addUser(connectedUsers, user); |
| 78 | + socket.user = user; |
| 79 | + |
| 80 | + sendMessageToChatFromUser = sendMessageToChat(user.name); |
| 81 | + sendTypingFromUser = sendTypingToChat(user.name); |
| 82 | + |
| 83 | + //it will broadcast the new user connected list to all the connected users from our socket server in case always when new user is connected |
| 84 | + |
| 85 | + //thats why in the section of connected user left tab users list is always up to date |
| 86 | + io.emit(USER_CONNECTED, connectedUsers); |
| 87 | + console.log(connectedUsers); |
| 88 | + }); |
| 89 | + |
| 90 | + socket.on(COMMUNITY_CHAT_HISTORY, callback => { |
| 91 | + console.log("listen on COMMUNITY_CHAT_HISTORY"); |
| 92 | + |
| 93 | + Communitychats.find({}).then(communitychats => { |
| 94 | + //res.json(communitychats); |
| 95 | + console.log("communitychats history found!!"); |
| 96 | + callback(communitychats); |
| 97 | + }); |
| 98 | + }); |
| 99 | + |
| 100 | + socket.on(COMMUNITY_CHAT, callback => { |
| 101 | + console.log("listen on COMMUNITY_CHAT"); |
| 102 | + console.log(communityChat); |
| 103 | + callback(communityChat); |
| 104 | + }); |
| 105 | + |
| 106 | + //Each socket also fires a special disconnect event |
| 107 | + //User disconnects |
| 108 | + socket.on("disconnect", () => { |
| 109 | + console.log("listen on disconnect"); |
| 110 | + if ("user" in socket) { |
| 111 | + console.log( |
| 112 | + "disconnected user is remove from our socket connectedUsers object list" |
| 113 | + ); |
| 114 | + |
| 115 | + connectedUsers = removeUser(connectedUsers, socket.user.name); |
| 116 | + |
| 117 | + console.log("here we broadcast final connected users list"); |
| 118 | + |
| 119 | + io.emit(USER_DISCONNECTED, connectedUsers); |
| 120 | + console.log("Final Connected Users are : ", connectedUsers); |
| 121 | + } |
| 122 | + }); |
| 123 | + |
| 124 | + //listen on LOGOUT |
| 125 | + //User logout |
| 126 | + socket.on(LOGOUT, () => { |
| 127 | + console.log("listen on LOGOUT"); |
| 128 | + |
| 129 | + connectedUsers = removeUser(connectedUsers, socket.user.name); |
| 130 | + io.emit(USER_DISCONNECTED, connectedUsers); |
| 131 | + console.log("Disconnect", connectedUsers); |
| 132 | + }); |
| 133 | + |
| 134 | + //Get Community Chat |
| 135 | + socket.on(COMMUNITY_CHAT, callback => { |
| 136 | + console.log("listen on COMMUNITY_CHAT"); |
| 137 | + console.log(communityChat); |
| 138 | + callback(communityChat); |
| 139 | + }); |
| 140 | + |
| 141 | + //listen on MESSAGE_SENT |
| 142 | + socket.on(MESSAGE_SENT, ({ chatId, message }) => { |
| 143 | + console.log("listen on MESSAGE_SENT"); |
| 144 | + console.log("chatid is : " + chatId + " where message is : " + message); |
| 145 | + |
| 146 | + sendMessageToChatFromUser(chatId, message); |
| 147 | + }); |
| 148 | + |
| 149 | + socket.on(TYPING, ({ chatId, isTyping }) => { |
| 150 | + console.log("listen on TYPING"); |
| 151 | + |
| 152 | + sendTypingFromUser(chatId, isTyping); |
| 153 | + }); |
| 154 | + |
| 155 | + socket.on(PRIVATE_MESSAGE, ({ reciever, sender, activeChat }) => { |
| 156 | + console.log("listen on PRIVATE_MESSAGE"); |
| 157 | + if (reciever in connectedUsers) { |
| 158 | + console.log("reciever is : " + reciever + " and sender is : " + sender); |
| 159 | + |
| 160 | + const recieverSocket = connectedUsers[reciever].socketId; |
| 161 | + |
| 162 | + console.log("recieverSocket is : " + recieverSocket); |
| 163 | + |
| 164 | + if (activeChat === null || activeChat.id === communityChat.id) { |
| 165 | + console.log( |
| 166 | + "activeChat === null || activeChat.id === communityChat.id" |
| 167 | + ); |
| 168 | + |
| 169 | + const newChat = createChat({ |
| 170 | + name: `${reciever}&${sender}`, |
| 171 | + users: [reciever, sender] |
| 172 | + }); |
| 173 | + |
| 174 | + // sending to individual socketid (private message) |
| 175 | + //eg: socket.to(socket.id).emit('hey', 'I just met you'); |
| 176 | + |
| 177 | + //newChat means freash chats of two private users |
| 178 | + |
| 179 | + console.log("private fresh chat is : " + newChat.id); |
| 180 | + |
| 181 | + socket.to(recieverSocket).emit(PRIVATE_MESSAGE, newChat); |
| 182 | + socket.emit(PRIVATE_MESSAGE, newChat); |
| 183 | + } else { |
| 184 | + //activeChat means prev chats of two private users |
| 185 | + console.log("activeChat || activeChat.id != communityChat.id"); |
| 186 | + |
| 187 | + socket.to(recieverSocket).emit(PRIVATE_MESSAGE, activeChat); |
| 188 | + } |
| 189 | + } |
| 190 | + }); |
| 191 | +}; |
| 192 | +/* |
| 193 | + * Returns a function that will take a chat id and a boolean isTyping |
| 194 | + * and then emit a broadcast to the chat id that the sender is typing |
| 195 | + * @param sender {string} username of sender |
| 196 | + * @return function(chatId, message) |
| 197 | + */ |
| 198 | +function sendTypingToChat(user) { |
| 199 | + return (chatId, isTyping) => { |
| 200 | + io.emit(`${TYPING}-${chatId}`, { user, isTyping }); |
| 201 | + }; |
| 202 | +} |
| 203 | + |
| 204 | +/* |
| 205 | + * Returns a function that will take a chat id and message |
| 206 | + * and then emit a broadcast to the chat id. |
| 207 | + * @param sender {string} username of sender |
| 208 | + * @return function(chatId, message) |
| 209 | + */ |
| 210 | +function sendMessageToChat(sender) { |
| 211 | + return (chatId, message) => { |
| 212 | + io.emit( |
| 213 | + `${MESSAGE_RECIEVED}-${chatId}`, //MESSAGE_RECIEVED event |
| 214 | + createMessage({ message, sender }), |
| 215 | + sendMessageToChatSAVE(chatId, message, sender) |
| 216 | + ); |
| 217 | + }; |
| 218 | +} |
| 219 | + |
| 220 | +function sendMessageToChatSAVE(chatId, message, sender) { |
| 221 | + //console.log("communityChat.isCommunity is : " + communityChat.id); |
| 222 | + |
| 223 | + //console.log("chatId inside sendMessageToChatSAVE is : " + chatId); |
| 224 | + if (chatId === communityChat.id) { |
| 225 | + //if chatId equal to community chat id |
| 226 | + console.log("we are saving communityChat messages!!"); |
| 227 | + console.log("here we save chat in database!!"); |
| 228 | + const newCommunitychats = new Communitychats({ |
| 229 | + id: chatId, |
| 230 | + time: getTime(new Date(Date.now())), |
| 231 | + message: message, |
| 232 | + sender |
| 233 | + }); |
| 234 | + |
| 235 | + newCommunitychats |
| 236 | + .save() |
| 237 | + .then(newCommunitychats => { |
| 238 | + console.log("chat saved successfully!!"); |
| 239 | + }) //here we send back new user register information as a response from the success server side |
| 240 | + .catch(err => { |
| 241 | + console.log("chat saved error is :" + err); |
| 242 | + }); |
| 243 | + } else { |
| 244 | + console.log("we cannot save private message!!"); |
| 245 | + |
| 246 | + //console.log("chatId inside sendMessageToChatSAVE is : " + chatId); |
| 247 | + } |
| 248 | +} |
| 249 | + |
| 250 | +/* |
| 251 | + * Adds user to list passed in. |
| 252 | + * @param userList {Object} Object with key value pairs of users |
| 253 | + * @param user {User} the user to added to the list. |
| 254 | + * @return userList {Object} Object with key value pairs of Users |
| 255 | + */ |
| 256 | +function addUser(userList, user) { |
| 257 | + let newList = Object.assign({}, userList); |
| 258 | + newList[user.name] = user; |
| 259 | + return newList; |
| 260 | +} |
| 261 | + |
| 262 | +/* |
| 263 | + * Removes user from the list passed in. |
| 264 | + * @param userList {Object} Object with key value pairs of Users |
| 265 | + * @param username {string} name of user to be removed |
| 266 | + * @return userList {Object} Object with key value pairs of Users |
| 267 | + */ |
| 268 | +function removeUser(userList, username) { |
| 269 | + let newList = Object.assign({}, userList); |
| 270 | + delete newList[username]; |
| 271 | + return newList; |
| 272 | +} |
| 273 | + |
| 274 | +/* |
| 275 | + * Checks if the user is in list passed in. |
| 276 | + * @param userList {Object} Object with key value pairs of Users |
| 277 | + * @param username {String} |
| 278 | + * @return userList {Object} Object with key value pairs of Users |
| 279 | + */ |
| 280 | +function isUser(userList, username) { |
| 281 | + return username in userList; |
| 282 | +} |
| 283 | + |
| 284 | +/* |
| 285 | + * @param date {Date} |
| 286 | + * @return a string represented in 24hr time i.e. '11:30', '19:30' |
| 287 | + */ |
| 288 | +const getTime = date => { |
| 289 | + return `${date.getHours()}:${("0" + date.getMinutes()).slice(-2)}`; |
| 290 | +}; |
0 commit comments