mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-10-29 10:06:13 +01:00
@@ -7,6 +7,8 @@ define('forum/chats', ['string', 'sounds'], function(S, sounds) {
|
|||||||
initialised: false
|
initialised: false
|
||||||
};
|
};
|
||||||
|
|
||||||
|
var newMessage = false;
|
||||||
|
|
||||||
Chats.init = function() {
|
Chats.init = function() {
|
||||||
var containerEl = $('.expanded-chat ul');
|
var containerEl = $('.expanded-chat ul');
|
||||||
|
|
||||||
@@ -75,6 +77,15 @@ define('forum/chats', ['string', 'sounds'], function(S, sounds) {
|
|||||||
|
|
||||||
Chats.addGlobalEventListeners = function() {
|
Chats.addGlobalEventListeners = function() {
|
||||||
$(window).on('resize', Chats.resizeMainWindow);
|
$(window).on('resize', Chats.resizeMainWindow);
|
||||||
|
$(window).on('mousemove keypress click', function() {
|
||||||
|
if (newMessage) {
|
||||||
|
var recipientUid = Chats.getRecipientUid();
|
||||||
|
if (recipientUid) {
|
||||||
|
socket.emit('modules.chats.markRead', recipientUid);
|
||||||
|
newMessage = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
Chats.addSocketListeners = function() {
|
Chats.addSocketListeners = function() {
|
||||||
@@ -83,6 +94,8 @@ define('forum/chats', ['string', 'sounds'], function(S, sounds) {
|
|||||||
containerEl = $('.expanded-chat ul');
|
containerEl = $('.expanded-chat ul');
|
||||||
|
|
||||||
if (Chats.isCurrentChat(data.withUid)) {
|
if (Chats.isCurrentChat(data.withUid)) {
|
||||||
|
newMessage = data.message.self === 0;
|
||||||
|
|
||||||
Chats.parseMessage(data.message, function(html) {
|
Chats.parseMessage(data.message, function(html) {
|
||||||
var newMessage = $(html);
|
var newMessage = $(html);
|
||||||
newMessage.insertBefore(typingNotifEl);
|
newMessage.insertBefore(typingNotifEl);
|
||||||
@@ -163,8 +176,12 @@ define('forum/chats', ['string', 'sounds'], function(S, sounds) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
Chats.setActive = function() {
|
Chats.setActive = function() {
|
||||||
|
var recipientUid = Chats.getRecipientUid();
|
||||||
|
if (recipientUid) {
|
||||||
|
socket.emit('modules.chats.markRead', recipientUid);
|
||||||
|
}
|
||||||
$('.chats-list li').removeClass('bg-primary');
|
$('.chats-list li').removeClass('bg-primary');
|
||||||
$('.chats-list li[data-uid="' + Chats.getRecipientUid() + '"]').addClass('bg-primary');
|
$('.chats-list li[data-uid="' + recipientUid + '"]').addClass('bg-primary');
|
||||||
};
|
};
|
||||||
|
|
||||||
Chats.parseMessage = function(data, callback) {
|
Chats.parseMessage = function(data, callback) {
|
||||||
|
|||||||
@@ -4,19 +4,30 @@ define('forum/footer', ['notifications', 'chat'], function(Notifications, Chat)
|
|||||||
Chat.prepareDOM();
|
Chat.prepareDOM();
|
||||||
translator.prepareDOM();
|
translator.prepareDOM();
|
||||||
|
|
||||||
function updateUnreadCount(err, count) {
|
function updateUnreadTopicCount(err, count) {
|
||||||
var unreadEl = $('#unread-count');
|
|
||||||
|
|
||||||
if (err) {
|
if (err) {
|
||||||
console.warn('Error updating unread count', err);
|
return console.warn('Error updating unread count', err);
|
||||||
}
|
}
|
||||||
|
|
||||||
unreadEl
|
$('#unread-count')
|
||||||
|
.toggleClass('unread-count', count > 0)
|
||||||
|
.attr('data-content', count > 20 ? '20+' : count);
|
||||||
|
}
|
||||||
|
|
||||||
|
function updateUnreadChatCount(err, count) {
|
||||||
|
if (err) {
|
||||||
|
return console.warn('Error updating unread count', err);
|
||||||
|
}
|
||||||
|
|
||||||
|
$('#chat-count')
|
||||||
.toggleClass('unread-count', count > 0)
|
.toggleClass('unread-count', count > 0)
|
||||||
.attr('data-content', count > 20 ? '20+' : count);
|
.attr('data-content', count > 20 ? '20+' : count);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
socket.on('event:unread.updateCount', updateUnreadCount);
|
socket.on('event:unread.updateCount', updateUnreadTopicCount);
|
||||||
socket.emit('user.getUnreadCount', updateUnreadCount);
|
socket.emit('user.getUnreadCount', updateUnreadTopicCount);
|
||||||
|
|
||||||
|
socket.on('event:unread.updateChatCount', updateUnreadChatCount);
|
||||||
|
socket.emit('user.getUnreadChatCount', updateUnreadChatCount);
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -4,6 +4,7 @@
|
|||||||
define('chat', ['taskbar', 'string', 'sounds', 'forum/chats'], function(taskbar, S, sounds, Chats) {
|
define('chat', ['taskbar', 'string', 'sounds', 'forum/chats'], function(taskbar, S, sounds, Chats) {
|
||||||
|
|
||||||
var module = {};
|
var module = {};
|
||||||
|
var newMessage = false;
|
||||||
|
|
||||||
module.prepareDOM = function() {
|
module.prepareDOM = function() {
|
||||||
// Chats Dropdown
|
// Chats Dropdown
|
||||||
@@ -66,7 +67,7 @@ define('chat', ['taskbar', 'string', 'sounds', 'forum/chats'], function(taskbar,
|
|||||||
if (isSelf) {
|
if (isSelf) {
|
||||||
username = data.message.toUser.username;
|
username = data.message.toUser.username;
|
||||||
}
|
}
|
||||||
|
newMessage = data.message.self === 0;
|
||||||
if (module.modalExists(data.withUid)) {
|
if (module.modalExists(data.withUid)) {
|
||||||
var modal = module.getModal(data.withUid);
|
var modal = module.getModal(data.withUid);
|
||||||
module.appendChatMessage(modal, data.message);
|
module.appendChatMessage(modal, data.message);
|
||||||
@@ -135,7 +136,7 @@ define('chat', ['taskbar', 'string', 'sounds', 'forum/chats'], function(taskbar,
|
|||||||
};
|
};
|
||||||
|
|
||||||
function checkStatus(chatModal) {
|
function checkStatus(chatModal) {
|
||||||
socket.emit('user.isOnline', chatModal.touid, function(err, data) {
|
socket.emit('user.isOnline', chatModal.attr('touid'), function(err, data) {
|
||||||
translator.translate('[[global:' + data.status + ']]', function(translated) {
|
translator.translate('[[global:' + data.status + ']]', function(translated) {
|
||||||
$('#chat-user-status').attr('class', 'fa fa-circle status ' + data.status)
|
$('#chat-user-status').attr('class', 'fa fa-circle status ' + data.status)
|
||||||
.attr('title', translated)
|
.attr('title', translated)
|
||||||
@@ -145,10 +146,10 @@ define('chat', ['taskbar', 'string', 'sounds', 'forum/chats'], function(taskbar,
|
|||||||
}
|
}
|
||||||
|
|
||||||
function checkOnlineStatus(chatModal) {
|
function checkOnlineStatus(chatModal) {
|
||||||
if(chatModal.intervalId === 0) {
|
if(parseInt(chatModal.attr('intervalId'), 10) === 0) {
|
||||||
chatModal.intervalId = setInterval(function() {
|
chatModal.attr('intervalId', setInterval(function() {
|
||||||
checkStatus(chatModal);
|
checkStatus(chatModal);
|
||||||
}, 1000);
|
}, 1000));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -160,11 +161,9 @@ define('chat', ['taskbar', 'string', 'sounds', 'forum/chats'], function(taskbar,
|
|||||||
var chatModal = $(chatTpl),
|
var chatModal = $(chatTpl),
|
||||||
uuid = utils.generateUUID();
|
uuid = utils.generateUUID();
|
||||||
|
|
||||||
chatModal.intervalId = 0;
|
|
||||||
chatModal.touid = touid;
|
|
||||||
chatModal.username = username;
|
|
||||||
|
|
||||||
chatModal.attr('id', 'chat-modal-' + touid);
|
chatModal.attr('id', 'chat-modal-' + touid);
|
||||||
|
chatModal.attr('touid', touid);
|
||||||
|
chatModal.attr('intervalId', 0);
|
||||||
chatModal.attr('UUID', uuid);
|
chatModal.attr('UUID', uuid);
|
||||||
chatModal.css("position", "fixed");
|
chatModal.css("position", "fixed");
|
||||||
chatModal.appendTo($('body'));
|
chatModal.appendTo($('body'));
|
||||||
@@ -211,6 +210,13 @@ define('chat', ['taskbar', 'string', 'sounds', 'forum/chats'], function(taskbar,
|
|||||||
module.bringModalToTop(chatModal);
|
module.bringModalToTop(chatModal);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
chatModal.on('mousemove keypress click', function() {
|
||||||
|
if (newMessage) {
|
||||||
|
socket.emit('modules.chats.markRead', touid);
|
||||||
|
console.log('sent')
|
||||||
|
newMessage = false;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
addSendHandler(chatModal);
|
addSendHandler(chatModal);
|
||||||
|
|
||||||
@@ -234,12 +240,12 @@ define('chat', ['taskbar', 'string', 'sounds', 'forum/chats'], function(taskbar,
|
|||||||
};
|
};
|
||||||
|
|
||||||
module.close = function(chatModal) {
|
module.close = function(chatModal) {
|
||||||
clearInterval(chatModal.intervalId);
|
clearInterval(chatModal.attr('intervalId'));
|
||||||
chatModal.intervalId = 0;
|
chatModal.attr('intervalId', 0);
|
||||||
chatModal.remove();
|
chatModal.remove();
|
||||||
chatModal.data('modal', null);
|
chatModal.data('modal', null);
|
||||||
taskbar.discard('chat', chatModal.attr('UUID'));
|
taskbar.discard('chat', chatModal.attr('UUID'));
|
||||||
Chats.notifyTyping(chatModal.touid, false);
|
Chats.notifyTyping(chatModal.attr('touid'), false);
|
||||||
};
|
};
|
||||||
|
|
||||||
module.center = function(chatModal) {
|
module.center = function(chatModal) {
|
||||||
@@ -258,19 +264,20 @@ define('chat', ['taskbar', 'string', 'sounds', 'forum/chats'], function(taskbar,
|
|||||||
Chats.scrollToBottom(chatModal.find('#chat-content'));
|
Chats.scrollToBottom(chatModal.find('#chat-content'));
|
||||||
module.center(chatModal);
|
module.center(chatModal);
|
||||||
module.bringModalToTop(chatModal);
|
module.bringModalToTop(chatModal);
|
||||||
|
socket.emit('modules.chats.markRead', chatModal.attr('touid'));
|
||||||
};
|
};
|
||||||
|
|
||||||
module.minimize = function(uuid) {
|
module.minimize = function(uuid) {
|
||||||
var chatModal = $('div[UUID="' + uuid + '"]');
|
var chatModal = $('div[UUID="' + uuid + '"]');
|
||||||
chatModal.addClass('hide');
|
chatModal.addClass('hide');
|
||||||
taskbar.minimize('chat', uuid);
|
taskbar.minimize('chat', uuid);
|
||||||
clearInterval(chatModal.intervalId);
|
clearInterval(chatModal.attr('intervalId'));
|
||||||
chatModal.intervalId = 0;
|
chatModal.attr('intervalId', 0);
|
||||||
Chats.notifyTyping(chatModal.touid, false);
|
Chats.notifyTyping(chatModal.attr('touid'), false);
|
||||||
};
|
};
|
||||||
|
|
||||||
function getChatMessages(chatModal, callback) {
|
function getChatMessages(chatModal, callback) {
|
||||||
socket.emit('modules.chats.get', {touid:chatModal.touid}, function(err, messages) {
|
socket.emit('modules.chats.get', {touid: chatModal.attr('touid')}, function(err, messages) {
|
||||||
module.appendChatMessage(chatModal, messages, callback);
|
module.appendChatMessage(chatModal, messages, callback);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@@ -279,20 +286,20 @@ define('chat', ['taskbar', 'string', 'sounds', 'forum/chats'], function(taskbar,
|
|||||||
var input = chatModal.find('#chat-message-input');
|
var input = chatModal.find('#chat-message-input');
|
||||||
input.off('keypress').on('keypress', function(e) {
|
input.off('keypress').on('keypress', function(e) {
|
||||||
if(e.which === 13) {
|
if(e.which === 13) {
|
||||||
Chats.sendMessage(chatModal.touid, chatModal.find('#chat-message-input'));
|
Chats.sendMessage(chatModal.attr('touid'), chatModal.find('#chat-message-input'));
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
input.off('keyup').on('keyup', function() {
|
input.off('keyup').on('keyup', function() {
|
||||||
if ($(this).val()) {
|
if ($(this).val()) {
|
||||||
socket.emit('modules.chats.userStartTyping', {touid:chatModal.touid, fromUid: app.uid});
|
socket.emit('modules.chats.userStartTyping', {touid:chatModal.attr('touid'), fromUid: app.uid});
|
||||||
} else {
|
} else {
|
||||||
Chats.notifyTyping(chatModal.touid, false);
|
Chats.notifyTyping(chatModal.attr('touid'), false);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
chatModal.find('#chat-message-send-btn').off('click').on('click', function(e){
|
chatModal.find('#chat-message-send-btn').off('click').on('click', function(e){
|
||||||
Chats.sendMessage(chatModal.touid, chatModal.find('#chat-message-input'));
|
Chats.sendMessage(chatModal.attr('touid'), chatModal.find('#chat-message-input'));
|
||||||
return false;
|
return false;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@@ -308,7 +315,9 @@ define('chat', ['taskbar', 'string', 'sounds', 'forum/chats'], function(taskbar,
|
|||||||
message.insertBefore(typingNotif);
|
message.insertBefore(typingNotif);
|
||||||
Chats.scrollToBottom(chatContent);
|
Chats.scrollToBottom(chatContent);
|
||||||
|
|
||||||
if (typeof done === 'function') done();
|
if (typeof done === 'function') {
|
||||||
|
done();
|
||||||
|
}
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -43,8 +43,21 @@ var db = require('./database'),
|
|||||||
Messaging.updateChatTime(fromuid, touid);
|
Messaging.updateChatTime(fromuid, touid);
|
||||||
Messaging.updateChatTime(touid, fromuid);
|
Messaging.updateChatTime(touid, fromuid);
|
||||||
|
|
||||||
getMessages([mid], fromuid, touid, true, function(err, messages) {
|
async.parallel([
|
||||||
callback(err, messages ? messages[0] : null);
|
function(next) {
|
||||||
|
Messaging.markRead(fromuid, touid, next);
|
||||||
|
},
|
||||||
|
function(next) {
|
||||||
|
Messaging.markUnread(touid, fromuid, next);
|
||||||
|
}
|
||||||
|
], function(err, results) {
|
||||||
|
if (err) {
|
||||||
|
return callback(err);
|
||||||
|
}
|
||||||
|
|
||||||
|
getMessages([mid], fromuid, touid, true, function(err, messages) {
|
||||||
|
callback(err, messages ? messages[0] : null);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
@@ -122,11 +135,8 @@ var db = require('./database'),
|
|||||||
};
|
};
|
||||||
|
|
||||||
Messaging.updateChatTime = function(uid, toUid, callback) {
|
Messaging.updateChatTime = function(uid, toUid, callback) {
|
||||||
db.sortedSetAdd('uid:' + uid + ':chats', Date.now(), toUid, function(err) {
|
callback = callback || function() {};
|
||||||
if (callback) {
|
db.sortedSetAdd('uid:' + uid + ':chats', Date.now(), toUid, callback);
|
||||||
callback(err);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
};
|
};
|
||||||
|
|
||||||
Messaging.getRecentChats = function(uid, start, end, callback) {
|
Messaging.getRecentChats = function(uid, start, end, callback) {
|
||||||
@@ -157,6 +167,18 @@ var db = require('./database'),
|
|||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Messaging.getUnreadCount = function(uid, callback) {
|
||||||
|
db.sortedSetCard('uid:' + uid + ':chats:unread', callback);
|
||||||
|
};
|
||||||
|
|
||||||
|
Messaging.markRead = function(uid, toUid, callback) {
|
||||||
|
db.sortedSetRemove('uid:' + uid + ':chats:unread', toUid, callback);
|
||||||
|
};
|
||||||
|
|
||||||
|
Messaging.markUnread = function(uid, toUid, callback) {
|
||||||
|
db.sortedSetAdd('uid:' + uid + ':chats:unread', Date.now(), toUid, callback);
|
||||||
|
};
|
||||||
|
|
||||||
// todo #1798 -- this utility method creates a room name given an array of uids.
|
// todo #1798 -- this utility method creates a room name given an array of uids.
|
||||||
Messaging.uidsToRoom = function(uids, callback) {
|
Messaging.uidsToRoom = function(uids, callback) {
|
||||||
uid = parseInt(uid, 10);
|
uid = parseInt(uid, 10);
|
||||||
|
|||||||
@@ -189,6 +189,7 @@ SocketModules.chats.send = function(socket, data, callback) {
|
|||||||
recipMessage.self = 0;
|
recipMessage.self = 0;
|
||||||
|
|
||||||
// Recipient
|
// Recipient
|
||||||
|
SocketModules.chats.pushUnreadCount(touid);
|
||||||
server.getUserSockets(touid).forEach(function(s) {
|
server.getUserSockets(touid).forEach(function(s) {
|
||||||
s.emit('event:chats.receive', {
|
s.emit('event:chats.receive', {
|
||||||
withUid: socket.uid,
|
withUid: socket.uid,
|
||||||
@@ -197,6 +198,7 @@ SocketModules.chats.send = function(socket, data, callback) {
|
|||||||
});
|
});
|
||||||
|
|
||||||
// Sender
|
// Sender
|
||||||
|
SocketModules.chats.pushUnreadCount(socket.uid);
|
||||||
server.getUserSockets(socket.uid).forEach(function(s) {
|
server.getUserSockets(socket.uid).forEach(function(s) {
|
||||||
s.emit('event:chats.receive', {
|
s.emit('event:chats.receive', {
|
||||||
withUid: touid,
|
withUid: touid,
|
||||||
@@ -221,6 +223,25 @@ function sendChatNotification(fromuid, touid, messageObj) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
SocketModules.chats.pushUnreadCount = function(uid) {
|
||||||
|
Messaging.getUnreadCount(uid, function(err, unreadCount) {
|
||||||
|
if (err) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
server.getUserSockets(uid).forEach(function(s) {
|
||||||
|
s.emit('event:unread.updateChatCount', null, unreadCount);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
SocketModules.chats.markRead = function(socket, touid, callback) {
|
||||||
|
Messaging.markRead(socket.uid, touid, function(err) {
|
||||||
|
if (!err) {
|
||||||
|
SocketModules.chats.pushUnreadCount(socket.uid);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
SocketModules.chats.userStartTyping = function(socket, data, callback) {
|
SocketModules.chats.userStartTyping = function(socket, data, callback) {
|
||||||
sendTypingNotification('event:chats.userStartTyping', socket, data, callback);
|
sendTypingNotification('event:chats.userStartTyping', socket, data, callback);
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ var async = require('async'),
|
|||||||
user = require('../user'),
|
user = require('../user'),
|
||||||
groups = require('../groups'),
|
groups = require('../groups'),
|
||||||
topics = require('../topics'),
|
topics = require('../topics'),
|
||||||
|
messaging = require('../messaging'),
|
||||||
utils = require('./../../public/src/utils'),
|
utils = require('./../../public/src/utils'),
|
||||||
meta = require('../meta'),
|
meta = require('../meta'),
|
||||||
SocketUser = {};
|
SocketUser = {};
|
||||||
@@ -216,6 +217,10 @@ SocketUser.getUnreadCount = function(socket, data, callback) {
|
|||||||
topics.getTotalUnread(socket.uid, callback);
|
topics.getTotalUnread(socket.uid, callback);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
SocketUser.getUnreadChatCount = function(socket, data, callback) {
|
||||||
|
messaging.getUnreadCount(socket.uid, callback);
|
||||||
|
};
|
||||||
|
|
||||||
SocketUser.getActiveUsers = function(socket, data, callback) {
|
SocketUser.getActiveUsers = function(socket, data, callback) {
|
||||||
module.parent.exports.emitOnlineUserCount(callback);
|
module.parent.exports.emitOnlineUserCount(callback);
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user