routes/Sockets.js
// Propagate message to end user & send receipt to sender
Propagate message to end user & send receipt to sender
Propagate message to end user & send receipt to sender
function handleMessage(data, isTyping) {
		if(!user) {
			let message = new WSError('Unauthorized user');
			ws.send(WSMessage.toString(message));
			return;
		}
		Thread.findOne({_id: data.thread}, (err, thread) => {
			if(err) {
				let message = new WSError(err);
				ws.send(WSMessage.toString(message));
				return;
			}
			if(thread) {
				if(isTyping) {
					let wsMessage = new WSMessage('typing', data);
					User.findOne({_id: data.sender}, (err, user) => {
						wsMessage.payload.sender = user; 
						let text = WSMessage.toString(wsMessage); 
						let users = _.filter(thread.members, (member) => !user._id.equals(member));
						Notify.notifyUsers(users, text, ['websocket']);
					});
				} else {
					let message = new Message(data);
					Thread.update({_id: thread._id}, {
						$push: {
							messages: message
						}
					})
						.then(() => {
							Message.populate(message, 'sender', (err, savedMessage) => {
								let wsMessage = new WSMessage('message', savedMessage);
								let text = WSMessage.toString(wsMessage);
								let webpushUsers = _.filter(thread.members, (member) => !message.sender._id.equals(member));
								Notify.notifyUsers(thread.members, text, ['websocket']);
								Notify.notifyUsers(webpushUsers, text, ['webpush']);
							});
						});
				}
			} else {
				let message = new WSError('Thread not found');
				ws.send(WSMessage.toString());
			}
		});
	}
function handleMessage ( data , isTyping ) { if ( ! user ) { let message = new WSError ( 'Unauthorized user' ) ; ws . send ( WSMessage . toString ( message ) ) ; return ; } Thread . findOne ( { _id : data . thread } , ( err , thread ) => { if ( err ) { let message = new WSError ( err ) ; ws . send ( WSMessage . toString ( message ) ) ; return ; } if ( thread ) { if ( isTyping ) { let wsMessage = new WSMessage ( 'typing' , data ) ; User . findOne ( { _id : data . sender } , ( err , user ) => { wsMessage . payload . sender = user ; let text = WSMessage . toString ( wsMessage ) ; let users = _ . filter ( thread . members , ( member ) => ! user . _id . equals ( member ) ) ; Notify . notifyUsers ( users , text , [ 'websocket' ] ) ; } ) ; } else { let message = new Message ( data ) ; Thread . update ( { _id : thread . _id } , { $push : { messages : message } } ) . then ( ( ) => { Message . populate ( message , 'sender' , ( err , savedMessage ) => { let wsMessage = new WSMessage ( 'message' , savedMessage ) ; let text = WSMessage . toString ( wsMessage ) ; let webpushUsers = _ . filter ( thread . members , ( member ) => ! message . sender . _id . equals ( member ) ) ; Notify . notifyUsers ( thread . members , text , [ 'websocket' ] ) ; Notify . notifyUsers ( webpushUsers , text , [ 'webpush' ] ) ; } ) ; } ) ; } } else { let message = new WSError ( 'Thread not found' ) ; ws . send ( WSMessage . toString ( ) ) ; } } ) ; }
Propagate message to end user & send receipt to sender
Propagate message to end user & send receipt to sender
// If socket not auth'd // Find thread associated with message // If thread exists // If typing notification //throw user data into response object //convert response object to JSON string