mirror of
				https://github.com/NodeBB/NodeBB.git
				synced 2025-10-31 02:55:58 +01:00 
			
		
		
		
	closes #109
This commit is contained in:
		| @@ -287,12 +287,14 @@ | |||||||
| 		var username = $(this).parents('li').attr('data-username'); | 		var username = $(this).parents('li').attr('data-username'); | ||||||
| 		var touid = $(this).parents('li').attr('data-uid'); | 		var touid = $(this).parents('li').attr('data-uid'); | ||||||
|  |  | ||||||
| 		require(['chat'], function(chat){ | 		if(username === app.username || !app.username) | ||||||
|  | 			return; | ||||||
|  |  | ||||||
|  | 		require(['chat'], function(chat) { | ||||||
| 			var chatModal = chat.createModalIfDoesntExist(username, touid); | 			var chatModal = chat.createModalIfDoesntExist(username, touid); | ||||||
| 			chatModal.show(); | 			chatModal.show(); | ||||||
| 			chat.bringModalToTop(chatModal); | 			chat.bringModalToTop(chatModal); | ||||||
| 		}); | 		}); | ||||||
|  |  | ||||||
| 	}); | 	}); | ||||||
|  |  | ||||||
| 	ajaxify.register_events([ | 	ajaxify.register_events([ | ||||||
|   | |||||||
| @@ -2,7 +2,6 @@ define(['taskbar'], function(taskbar) { | |||||||
|  |  | ||||||
| 	var module = {}; | 	var module = {}; | ||||||
|  |  | ||||||
| 	 |  | ||||||
| 	module.bringModalToTop = function(chatModal) { | 	module.bringModalToTop = function(chatModal) { | ||||||
| 		var topZ = 0; | 		var topZ = 0; | ||||||
| 		$('.modal').each(function() { | 		$('.modal').each(function() { | ||||||
| @@ -11,7 +10,7 @@ define(['taskbar'], function(taskbar) { | |||||||
| 			topZ = thisZ; | 			topZ = thisZ; | ||||||
| 		  } | 		  } | ||||||
| 		}); | 		}); | ||||||
| 		chatModal.css('zIndex', topZ+1); | 		chatModal.css('zIndex', topZ + 1); | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	module.createModalIfDoesntExist = function(username, touid) { | 	module.createModalIfDoesntExist = function(username, touid) { | ||||||
| @@ -40,6 +39,8 @@ define(['taskbar'], function(taskbar) { | |||||||
| 			}); | 			}); | ||||||
|  |  | ||||||
| 			addSendHandler(chatModal, touid); | 			addSendHandler(chatModal, touid); | ||||||
|  |  | ||||||
|  | 			getChatMessages(chatModal, touid); | ||||||
| 		} | 		} | ||||||
|  |  | ||||||
| 		taskbar.push('chat', chatModal.attr('UUID'), {title:'chat with '+username}); | 		taskbar.push('chat', chatModal.attr('UUID'), {title:'chat with '+username}); | ||||||
| @@ -58,6 +59,15 @@ define(['taskbar'], function(taskbar) { | |||||||
| 		taskbar.minimize('chat', uuid); | 		taskbar.minimize('chat', uuid); | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
|  | 	function getChatMessages(chatModal, touid) { | ||||||
|  | 		socket.emit('getChatMessages', {touid:touid}, function(messages) { | ||||||
|  | 			console.log(messages); | ||||||
|  | 			for(var i = 0; i<messages.length; ++i) { | ||||||
|  | 				module.appendChatMessage(chatModal, messages[i].content); | ||||||
|  | 			} | ||||||
|  | 		}); | ||||||
|  | 	} | ||||||
|  |  | ||||||
| 	function addSendHandler(chatModal, touid) { | 	function addSendHandler(chatModal, touid) { | ||||||
| 		chatModal.find('#chat-message-input').off('keypress'); | 		chatModal.find('#chat-message-input').off('keypress'); | ||||||
| 		chatModal.find('#chat-message-input').on('keypress', function(e) { | 		chatModal.find('#chat-message-input').on('keypress', function(e) { | ||||||
|   | |||||||
							
								
								
									
										77
									
								
								src/messaging.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										77
									
								
								src/messaging.js
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,77 @@ | |||||||
|  |  | ||||||
|  | var RDB = require('./redis'), | ||||||
|  | 	async = require('async'); | ||||||
|  |  | ||||||
|  |  | ||||||
|  | (function(Messaging) { | ||||||
|  |  | ||||||
|  | 	function sortUids(fromuid, touid) { | ||||||
|  | 		var uids = [fromuid, touid]; | ||||||
|  | 		uids.sort(); | ||||||
|  | 		return uids; | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	Messaging.addMessage = function(fromuid, touid, content, callback) { | ||||||
|  | 		var uids = sortUids(fromuid, touid); | ||||||
|  |  | ||||||
|  | 		RDB.incr('global:next_message_id', function(err, mid) { | ||||||
|  | 			if(err) | ||||||
|  | 				return callback(err, null); | ||||||
|  | 			console.log('creating message', mid); | ||||||
|  |  | ||||||
|  | 			var message = { | ||||||
|  | 				content: content, | ||||||
|  | 				timestamp: Date.now(), | ||||||
|  | 				fromuid: fromuid, | ||||||
|  | 				touid: touid | ||||||
|  | 			}; | ||||||
|  |  | ||||||
|  | 			RDB.hmset('message:' + mid, message); | ||||||
|  | 			RDB.rpush('messages:' + uids[0] + ':' + uids[1], mid); | ||||||
|  |  | ||||||
|  | 			callback(null, message); | ||||||
|  | 		}); | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | 	Messaging.getMessages = function(fromuid, touid, callback) { | ||||||
|  | 		var uids = sortUids(fromuid, touid); | ||||||
|  |  | ||||||
|  | 		RDB.lrange('messages:' + uids[0] + ':' + uids[1], 0, -1, function(err, mids) { | ||||||
|  | 			if(err) | ||||||
|  | 				return callback(err, null); | ||||||
|  |  | ||||||
|  | 			if(!mids || !mids.length) { | ||||||
|  | 				return callback(null, []); | ||||||
|  | 			} | ||||||
|  |  | ||||||
|  |  | ||||||
|  | 			user.getUserField(touid, 'username', function(err, tousername) { | ||||||
|  |  | ||||||
|  | 				var messages = []; | ||||||
|  |  | ||||||
|  | 				function getMessage(mid, next) { | ||||||
|  | 					RDB.hgetall('message:' + mid, function(err, message) { | ||||||
|  | 						if(err) | ||||||
|  | 							return next(err); | ||||||
|  |  | ||||||
|  | 						if(message.fromuid === fromuid) | ||||||
|  | 							message.content = 'You : ' + message.content; | ||||||
|  | 						else | ||||||
|  | 							message.content = tousername + ' : ' + message.content; | ||||||
|  |  | ||||||
|  | 						messages.push(message); | ||||||
|  | 						next(null); | ||||||
|  | 					}); | ||||||
|  | 				} | ||||||
|  |  | ||||||
|  | 				async.eachSeries(mids, getMessage, function(err) { | ||||||
|  | 					if(err) | ||||||
|  | 						return callback(err, null); | ||||||
|  |  | ||||||
|  | 					callback(null, messages); | ||||||
|  | 				}); | ||||||
|  | 			}); | ||||||
|  | 		}); | ||||||
|  | 	} | ||||||
|  |  | ||||||
|  | }(exports)); | ||||||
| @@ -506,9 +506,22 @@ var SocketIO = require('socket.io').listen(global.server, { log:false }), | |||||||
| 			}); | 			}); | ||||||
| 		}); | 		}); | ||||||
|  |  | ||||||
|  | 		socket.on('getChatMessages', function(data, callback) { | ||||||
|  | 			var touid = data.touid; | ||||||
|  | 			require('./messaging').getMessages(uid, touid, function(err, messages) { | ||||||
|  | 				if(err) | ||||||
|  | 					return callback(null); | ||||||
|  |  | ||||||
|  | 				callback(messages); | ||||||
|  | 			}); | ||||||
|  | 		}); | ||||||
|  |  | ||||||
| 		socket.on('sendChatMessage', function(data) { | 		socket.on('sendChatMessage', function(data) { | ||||||
|  |  | ||||||
| 			var touid = data.touid; | 			var touid = data.touid; | ||||||
|  | 			if(touid === uid || uid === 0) { | ||||||
|  | 				return; | ||||||
|  | 			} | ||||||
|  |  | ||||||
| 			if(userSockets[touid]) { | 			if(userSockets[touid]) { | ||||||
| 				var msg = utils.strip_tags(data.message), | 				var msg = utils.strip_tags(data.message), | ||||||
| @@ -521,11 +534,15 @@ var SocketIO = require('socket.io').listen(global.server, { log:false }), | |||||||
| 						userSockets[touid][x].emit('chatMessage', {fromuid:uid, username:username, message:finalMessage}); | 						userSockets[touid][x].emit('chatMessage', {fromuid:uid, username:username, message:finalMessage}); | ||||||
| 					} | 					} | ||||||
|  |  | ||||||
| 					notifications.create(finalMessage, 5, '#', 'notification_'+uid+'_'+touid, function(nid) { | 					notifications.create(finalMessage, 5, '#', 'notification_' + uid + '_' + touid, function(nid) { | ||||||
| 						notifications.push(nid, [touid], function(success) { | 						notifications.push(nid, [touid], function(success) { | ||||||
|  |  | ||||||
| 						}); | 						}); | ||||||
| 					}); | 					}); | ||||||
|  |  | ||||||
|  | 					require('./messaging').addMessage(uid, touid, msg, function(err, message) { | ||||||
|  |  | ||||||
|  | 					}); | ||||||
| 				}); | 				}); | ||||||
| 			} | 			} | ||||||
| 		}); | 		}); | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user