Files
NodeBB/src/messaging.js

441 lines
12 KiB
JavaScript
Raw Normal View History

2014-03-08 15:45:42 -05:00
'use strict';
2015-12-15 17:50:30 +02:00
var async = require('async'),
2014-07-25 16:46:04 -04:00
winston = require('winston'),
2015-09-16 15:44:10 -04:00
S = require('string'),
2015-12-16 11:15:43 +02:00
2015-09-16 15:44:10 -04:00
2015-12-15 17:50:30 +02:00
db = require('./database'),
2013-12-27 14:09:22 -05:00
user = require('./user'),
plugins = require('./plugins'),
2014-07-25 16:46:04 -04:00
meta = require('./meta'),
2014-08-08 15:28:39 -04:00
utils = require('../public/src/utils'),
2014-07-25 16:46:04 -04:00
notifications = require('./notifications'),
2015-12-16 11:15:43 +02:00
userNotifications = require('./user/notifications');
2013-08-26 13:18:20 -04:00
(function(Messaging) {
2015-12-15 14:10:32 +02:00
2015-12-15 17:50:30 +02:00
require('./messaging/create')(Messaging);
require('./messaging/delete')(Messaging);
require('./messaging/edit')(Messaging);
require('./messaging/rooms')(Messaging);
require('./messaging/unread')(Messaging);
2015-12-16 11:15:43 +02:00
require('./messaging/notifications')(Messaging);
2015-12-15 14:10:32 +02:00
var terms = {
day: 86400000,
week: 604800000,
month: 2592000000,
threemonths: 7776000000
};
Squashed commit of the following: Closes #2668 commit 3d4f494ed3257bceda8f6f82057cab83f0f252b3 Author: Julian Lam <julian@designcreateplay.com> Date: Fri Dec 11 12:06:42 2015 -0500 theme minvers for #2668 commit b608ce61854f8195143685bb9753b80d32b26e95 Author: Julian Lam <julian@designcreateplay.com> Date: Fri Dec 11 12:01:03 2015 -0500 Allowing chat modal to edit and delete messages re: #2668 commit 0104db90a4070582f3938b6929dae35f985bac35 Author: Julian Lam <julian@designcreateplay.com> Date: Fri Dec 11 11:51:23 2015 -0500 Fixed issue where newSet calculations were off ... sometimes. Also, rendering of edited messages now parses a template partial, instead of just replacing the content. commit 5cb6ca600425ca9320c599b32306e93dcc5aa4ce Author: Julian Lam <julian@designcreateplay.com> Date: Fri Dec 11 11:07:12 2015 -0500 If edited content matches existing content... ... then edit is aborted. commit 6e7495247b1895589c716db29f919a934087b924 Author: Julian Lam <julian@designcreateplay.com> Date: Fri Dec 11 11:05:08 2015 -0500 some linting and fixed issue where new msgs when deleted would crash server commit db4a9e40d6dff44569c2437378121db8fdf75cf8 Author: Julian Lam <julian@designcreateplay.com> Date: Tue Dec 8 17:25:56 2015 -0500 Message deletion for #2668, and fixed bug Fixed bug where chat modal would spawn even though user was sitting on the /chats page. commit a5aa2498ab4a8bba02a6daa43a9dbed7b3e37976 Author: Julian Lam <julian@designcreateplay.com> Date: Tue Dec 8 14:55:23 2015 -0500 wiring up the edit button, #2668 commit 5f2afdcf6f2b9eae6b5873ca100149e65e3d385d Author: Julian Lam <julian@designcreateplay.com> Date: Tue Dec 8 14:20:39 2015 -0500 added indicator to show if and when a message had been edited commit e8301132d525c1b9fd46c98cdb282ac7ea7a0d7f Author: Julian Lam <julian@designcreateplay.com> Date: Tue Dec 8 14:06:39 2015 -0500 Allowing editing of chat messages commit bfd991be1cb1769599f7d5d2b1638e313c3c2dcb Author: Julian Lam <julian@designcreateplay.com> Date: Tue Dec 8 10:33:49 2015 -0500 Added messageId to messages object return commit 0306ee6657b3288dd4547c66869d7d4ece0b31ad Author: Julian Lam <julian@designcreateplay.com> Date: Tue Dec 8 08:20:17 2015 -0500 WIP #2668
2015-12-11 12:07:02 -05:00
Messaging.getMessageField = function(mid, field, callback) {
Messaging.getMessageFields(mid, [field], function(err, fields) {
2015-12-15 17:50:30 +02:00
callback(err, fields ? fields[field] : null);
Squashed commit of the following: Closes #2668 commit 3d4f494ed3257bceda8f6f82057cab83f0f252b3 Author: Julian Lam <julian@designcreateplay.com> Date: Fri Dec 11 12:06:42 2015 -0500 theme minvers for #2668 commit b608ce61854f8195143685bb9753b80d32b26e95 Author: Julian Lam <julian@designcreateplay.com> Date: Fri Dec 11 12:01:03 2015 -0500 Allowing chat modal to edit and delete messages re: #2668 commit 0104db90a4070582f3938b6929dae35f985bac35 Author: Julian Lam <julian@designcreateplay.com> Date: Fri Dec 11 11:51:23 2015 -0500 Fixed issue where newSet calculations were off ... sometimes. Also, rendering of edited messages now parses a template partial, instead of just replacing the content. commit 5cb6ca600425ca9320c599b32306e93dcc5aa4ce Author: Julian Lam <julian@designcreateplay.com> Date: Fri Dec 11 11:07:12 2015 -0500 If edited content matches existing content... ... then edit is aborted. commit 6e7495247b1895589c716db29f919a934087b924 Author: Julian Lam <julian@designcreateplay.com> Date: Fri Dec 11 11:05:08 2015 -0500 some linting and fixed issue where new msgs when deleted would crash server commit db4a9e40d6dff44569c2437378121db8fdf75cf8 Author: Julian Lam <julian@designcreateplay.com> Date: Tue Dec 8 17:25:56 2015 -0500 Message deletion for #2668, and fixed bug Fixed bug where chat modal would spawn even though user was sitting on the /chats page. commit a5aa2498ab4a8bba02a6daa43a9dbed7b3e37976 Author: Julian Lam <julian@designcreateplay.com> Date: Tue Dec 8 14:55:23 2015 -0500 wiring up the edit button, #2668 commit 5f2afdcf6f2b9eae6b5873ca100149e65e3d385d Author: Julian Lam <julian@designcreateplay.com> Date: Tue Dec 8 14:20:39 2015 -0500 added indicator to show if and when a message had been edited commit e8301132d525c1b9fd46c98cdb282ac7ea7a0d7f Author: Julian Lam <julian@designcreateplay.com> Date: Tue Dec 8 14:06:39 2015 -0500 Allowing editing of chat messages commit bfd991be1cb1769599f7d5d2b1638e313c3c2dcb Author: Julian Lam <julian@designcreateplay.com> Date: Tue Dec 8 10:33:49 2015 -0500 Added messageId to messages object return commit 0306ee6657b3288dd4547c66869d7d4ece0b31ad Author: Julian Lam <julian@designcreateplay.com> Date: Tue Dec 8 08:20:17 2015 -0500 WIP #2668
2015-12-11 12:07:02 -05:00
});
};
Messaging.getMessageFields = function(mid, fields, callback) {
db.getObjectFields('message:' + mid, fields, callback);
};
Messaging.setMessageField = function(mid, field, content, callback) {
db.setObjectField('message:' + mid, field, content, callback);
};
Messaging.setMessageFields = function(mid, data, callback) {
db.setObject('message:' + mid, data, callback);
};
Messaging.getMessages = function(params, callback) {
2015-12-15 17:50:30 +02:00
var uid = params.uid,
roomId = params.roomId,
since = params.since,
isNew = params.isNew,
2016-07-13 17:04:45 +03:00
count = params.count || 250,
markRead = params.markRead || true;
2015-12-15 17:50:30 +02:00
var min = params.count ? 0 : Date.now() - (terms[since] || terms.day);
2013-08-26 13:18:20 -04:00
if (since === 'recent') {
count = 49;
min = 0;
}
2015-12-15 17:50:30 +02:00
db.getSortedSetRevRangeByScore('uid:' + uid + ':chat:room:' + roomId + ':mids', 0, count, '+inf', min, function(err, mids) {
2013-12-02 21:20:55 -05:00
if (err) {
2014-03-28 12:13:01 -04:00
return callback(err);
2013-12-02 21:20:55 -05:00
}
2013-08-26 13:18:20 -04:00
if (!Array.isArray(mids) || !mids.length) {
2013-08-26 13:18:20 -04:00
return callback(null, []);
}
2014-07-29 17:12:02 -04:00
mids.reverse();
2015-12-15 17:50:30 +02:00
Messaging.getMessagesData(mids, uid, roomId, isNew, callback);
2014-03-28 12:13:01 -04:00
});
2014-07-25 16:46:04 -04:00
if (markRead) {
2015-12-15 17:50:30 +02:00
notifications.markRead('chat_' + roomId + '_' + uid, uid, function(err) {
if (err) {
winston.error('[messaging] Could not mark notifications related to this chat as read: ' + err.message);
}
2014-07-25 16:46:04 -04:00
2015-12-15 17:50:30 +02:00
userNotifications.pushCount(uid);
});
}
2014-03-28 12:13:01 -04:00
};
2013-08-26 13:18:20 -04:00
2015-12-15 17:50:30 +02:00
Messaging.getMessagesData = function(mids, uid, roomId, isNew, callback) {
2015-12-15 17:50:30 +02:00
var keys = mids.map(function(mid) {
return 'message:' + mid;
});
2013-08-26 13:18:20 -04:00
2015-12-15 17:50:30 +02:00
var messages;
Squashed commit of the following: Closes #2668 commit 3d4f494ed3257bceda8f6f82057cab83f0f252b3 Author: Julian Lam <julian@designcreateplay.com> Date: Fri Dec 11 12:06:42 2015 -0500 theme minvers for #2668 commit b608ce61854f8195143685bb9753b80d32b26e95 Author: Julian Lam <julian@designcreateplay.com> Date: Fri Dec 11 12:01:03 2015 -0500 Allowing chat modal to edit and delete messages re: #2668 commit 0104db90a4070582f3938b6929dae35f985bac35 Author: Julian Lam <julian@designcreateplay.com> Date: Fri Dec 11 11:51:23 2015 -0500 Fixed issue where newSet calculations were off ... sometimes. Also, rendering of edited messages now parses a template partial, instead of just replacing the content. commit 5cb6ca600425ca9320c599b32306e93dcc5aa4ce Author: Julian Lam <julian@designcreateplay.com> Date: Fri Dec 11 11:07:12 2015 -0500 If edited content matches existing content... ... then edit is aborted. commit 6e7495247b1895589c716db29f919a934087b924 Author: Julian Lam <julian@designcreateplay.com> Date: Fri Dec 11 11:05:08 2015 -0500 some linting and fixed issue where new msgs when deleted would crash server commit db4a9e40d6dff44569c2437378121db8fdf75cf8 Author: Julian Lam <julian@designcreateplay.com> Date: Tue Dec 8 17:25:56 2015 -0500 Message deletion for #2668, and fixed bug Fixed bug where chat modal would spawn even though user was sitting on the /chats page. commit a5aa2498ab4a8bba02a6daa43a9dbed7b3e37976 Author: Julian Lam <julian@designcreateplay.com> Date: Tue Dec 8 14:55:23 2015 -0500 wiring up the edit button, #2668 commit 5f2afdcf6f2b9eae6b5873ca100149e65e3d385d Author: Julian Lam <julian@designcreateplay.com> Date: Tue Dec 8 14:20:39 2015 -0500 added indicator to show if and when a message had been edited commit e8301132d525c1b9fd46c98cdb282ac7ea7a0d7f Author: Julian Lam <julian@designcreateplay.com> Date: Tue Dec 8 14:06:39 2015 -0500 Allowing editing of chat messages commit bfd991be1cb1769599f7d5d2b1638e313c3c2dcb Author: Julian Lam <julian@designcreateplay.com> Date: Tue Dec 8 10:33:49 2015 -0500 Added messageId to messages object return commit 0306ee6657b3288dd4547c66869d7d4ece0b31ad Author: Julian Lam <julian@designcreateplay.com> Date: Tue Dec 8 08:20:17 2015 -0500 WIP #2668
2015-12-11 12:07:02 -05:00
2015-12-15 17:50:30 +02:00
async.waterfall([
function (next) {
db.getObjects(keys, next);
},
function (_messages, next) {
messages = _messages.map(function(msg, idx) {
if (msg) {
msg.messageId = parseInt(mids[idx], 10);
}
return msg;
}).filter(Boolean);
Squashed commit of the following: Closes #2668 commit 3d4f494ed3257bceda8f6f82057cab83f0f252b3 Author: Julian Lam <julian@designcreateplay.com> Date: Fri Dec 11 12:06:42 2015 -0500 theme minvers for #2668 commit b608ce61854f8195143685bb9753b80d32b26e95 Author: Julian Lam <julian@designcreateplay.com> Date: Fri Dec 11 12:01:03 2015 -0500 Allowing chat modal to edit and delete messages re: #2668 commit 0104db90a4070582f3938b6929dae35f985bac35 Author: Julian Lam <julian@designcreateplay.com> Date: Fri Dec 11 11:51:23 2015 -0500 Fixed issue where newSet calculations were off ... sometimes. Also, rendering of edited messages now parses a template partial, instead of just replacing the content. commit 5cb6ca600425ca9320c599b32306e93dcc5aa4ce Author: Julian Lam <julian@designcreateplay.com> Date: Fri Dec 11 11:07:12 2015 -0500 If edited content matches existing content... ... then edit is aborted. commit 6e7495247b1895589c716db29f919a934087b924 Author: Julian Lam <julian@designcreateplay.com> Date: Fri Dec 11 11:05:08 2015 -0500 some linting and fixed issue where new msgs when deleted would crash server commit db4a9e40d6dff44569c2437378121db8fdf75cf8 Author: Julian Lam <julian@designcreateplay.com> Date: Tue Dec 8 17:25:56 2015 -0500 Message deletion for #2668, and fixed bug Fixed bug where chat modal would spawn even though user was sitting on the /chats page. commit a5aa2498ab4a8bba02a6daa43a9dbed7b3e37976 Author: Julian Lam <julian@designcreateplay.com> Date: Tue Dec 8 14:55:23 2015 -0500 wiring up the edit button, #2668 commit 5f2afdcf6f2b9eae6b5873ca100149e65e3d385d Author: Julian Lam <julian@designcreateplay.com> Date: Tue Dec 8 14:20:39 2015 -0500 added indicator to show if and when a message had been edited commit e8301132d525c1b9fd46c98cdb282ac7ea7a0d7f Author: Julian Lam <julian@designcreateplay.com> Date: Tue Dec 8 14:06:39 2015 -0500 Allowing editing of chat messages commit bfd991be1cb1769599f7d5d2b1638e313c3c2dcb Author: Julian Lam <julian@designcreateplay.com> Date: Tue Dec 8 10:33:49 2015 -0500 Added messageId to messages object return commit 0306ee6657b3288dd4547c66869d7d4ece0b31ad Author: Julian Lam <julian@designcreateplay.com> Date: Tue Dec 8 08:20:17 2015 -0500 WIP #2668
2015-12-11 12:07:02 -05:00
2015-12-15 17:50:30 +02:00
var uids = messages.map(function(msg) {
return msg && msg.fromuid;
});
2014-07-24 22:04:33 -04:00
2015-12-15 17:50:30 +02:00
user.getUsersFields(uids, ['uid', 'username', 'userslug', 'picture', 'status'], next);
},
function (users, next) {
messages.forEach(function(message, index) {
message.fromUser = users[index];
var self = parseInt(message.fromuid, 10) === parseInt(uid, 10);
message.self = self ? 1 : 0;
message.timestampISO = utils.toISOString(message.timestamp);
message.newSet = false;
2016-07-13 17:32:00 +03:00
message.roomId = String(message.roomId || roomId);
2015-12-15 17:50:30 +02:00
if (message.hasOwnProperty('edited')) {
message.editedISO = new Date(parseInt(message.edited, 10)).toISOString();
}
});
Squashed commit of the following: Closes #2668 commit 3d4f494ed3257bceda8f6f82057cab83f0f252b3 Author: Julian Lam <julian@designcreateplay.com> Date: Fri Dec 11 12:06:42 2015 -0500 theme minvers for #2668 commit b608ce61854f8195143685bb9753b80d32b26e95 Author: Julian Lam <julian@designcreateplay.com> Date: Fri Dec 11 12:01:03 2015 -0500 Allowing chat modal to edit and delete messages re: #2668 commit 0104db90a4070582f3938b6929dae35f985bac35 Author: Julian Lam <julian@designcreateplay.com> Date: Fri Dec 11 11:51:23 2015 -0500 Fixed issue where newSet calculations were off ... sometimes. Also, rendering of edited messages now parses a template partial, instead of just replacing the content. commit 5cb6ca600425ca9320c599b32306e93dcc5aa4ce Author: Julian Lam <julian@designcreateplay.com> Date: Fri Dec 11 11:07:12 2015 -0500 If edited content matches existing content... ... then edit is aborted. commit 6e7495247b1895589c716db29f919a934087b924 Author: Julian Lam <julian@designcreateplay.com> Date: Fri Dec 11 11:05:08 2015 -0500 some linting and fixed issue where new msgs when deleted would crash server commit db4a9e40d6dff44569c2437378121db8fdf75cf8 Author: Julian Lam <julian@designcreateplay.com> Date: Tue Dec 8 17:25:56 2015 -0500 Message deletion for #2668, and fixed bug Fixed bug where chat modal would spawn even though user was sitting on the /chats page. commit a5aa2498ab4a8bba02a6daa43a9dbed7b3e37976 Author: Julian Lam <julian@designcreateplay.com> Date: Tue Dec 8 14:55:23 2015 -0500 wiring up the edit button, #2668 commit 5f2afdcf6f2b9eae6b5873ca100149e65e3d385d Author: Julian Lam <julian@designcreateplay.com> Date: Tue Dec 8 14:20:39 2015 -0500 added indicator to show if and when a message had been edited commit e8301132d525c1b9fd46c98cdb282ac7ea7a0d7f Author: Julian Lam <julian@designcreateplay.com> Date: Tue Dec 8 14:06:39 2015 -0500 Allowing editing of chat messages commit bfd991be1cb1769599f7d5d2b1638e313c3c2dcb Author: Julian Lam <julian@designcreateplay.com> Date: Tue Dec 8 10:33:49 2015 -0500 Added messageId to messages object return commit 0306ee6657b3288dd4547c66869d7d4ece0b31ad Author: Julian Lam <julian@designcreateplay.com> Date: Tue Dec 8 08:20:17 2015 -0500 WIP #2668
2015-12-11 12:07:02 -05:00
2015-12-15 17:50:30 +02:00
async.map(messages, function(message, next) {
2016-08-17 14:12:35 +03:00
Messaging.parse(message.content, message.fromuid, uid, roomId, isNew, function(err, result) {
if (err) {
return next(err);
}
2015-12-15 17:50:30 +02:00
message.content = result;
message.cleanedContent = S(result).stripTags().decodeHTMLEntities().s;
next(null, message);
});
}, next);
},
function(messages, next) {
if (messages.length > 1) {
// Add a spacer in between messages with time gaps between them
messages = messages.map(function(message, index) {
// Compare timestamps with the previous message, and check if a spacer needs to be added
if (index > 0 && parseInt(message.timestamp, 10) > parseInt(messages[index-1].timestamp, 10) + (1000*60*5)) {
// If it's been 5 minutes, this is a new set of messages
message.newSet = true;
} else if (index > 0 && message.fromuid !== messages[index-1].fromuid) {
// If the previous message was from the other person, this is also a new set
message.newSet = true;
}
return message;
});
next(undefined, messages);
} else if (messages.length === 1) {
2015-12-15 17:50:30 +02:00
// For single messages, we don't know the context, so look up the previous message and compare
var key = 'uid:' + uid + ':chat:room:' + roomId + ':mids';
async.waterfall([
async.apply(db.sortedSetRank, key, messages[0].messageId),
function(index, next) {
// Continue only if this isn't the first message in sorted set
if (index > 0) {
db.getSortedSetRange(key, index-1, index-1, next);
} else {
Squashed commit of the following: Closes #2668 commit 3d4f494ed3257bceda8f6f82057cab83f0f252b3 Author: Julian Lam <julian@designcreateplay.com> Date: Fri Dec 11 12:06:42 2015 -0500 theme minvers for #2668 commit b608ce61854f8195143685bb9753b80d32b26e95 Author: Julian Lam <julian@designcreateplay.com> Date: Fri Dec 11 12:01:03 2015 -0500 Allowing chat modal to edit and delete messages re: #2668 commit 0104db90a4070582f3938b6929dae35f985bac35 Author: Julian Lam <julian@designcreateplay.com> Date: Fri Dec 11 11:51:23 2015 -0500 Fixed issue where newSet calculations were off ... sometimes. Also, rendering of edited messages now parses a template partial, instead of just replacing the content. commit 5cb6ca600425ca9320c599b32306e93dcc5aa4ce Author: Julian Lam <julian@designcreateplay.com> Date: Fri Dec 11 11:07:12 2015 -0500 If edited content matches existing content... ... then edit is aborted. commit 6e7495247b1895589c716db29f919a934087b924 Author: Julian Lam <julian@designcreateplay.com> Date: Fri Dec 11 11:05:08 2015 -0500 some linting and fixed issue where new msgs when deleted would crash server commit db4a9e40d6dff44569c2437378121db8fdf75cf8 Author: Julian Lam <julian@designcreateplay.com> Date: Tue Dec 8 17:25:56 2015 -0500 Message deletion for #2668, and fixed bug Fixed bug where chat modal would spawn even though user was sitting on the /chats page. commit a5aa2498ab4a8bba02a6daa43a9dbed7b3e37976 Author: Julian Lam <julian@designcreateplay.com> Date: Tue Dec 8 14:55:23 2015 -0500 wiring up the edit button, #2668 commit 5f2afdcf6f2b9eae6b5873ca100149e65e3d385d Author: Julian Lam <julian@designcreateplay.com> Date: Tue Dec 8 14:20:39 2015 -0500 added indicator to show if and when a message had been edited commit e8301132d525c1b9fd46c98cdb282ac7ea7a0d7f Author: Julian Lam <julian@designcreateplay.com> Date: Tue Dec 8 14:06:39 2015 -0500 Allowing editing of chat messages commit bfd991be1cb1769599f7d5d2b1638e313c3c2dcb Author: Julian Lam <julian@designcreateplay.com> Date: Tue Dec 8 10:33:49 2015 -0500 Added messageId to messages object return commit 0306ee6657b3288dd4547c66869d7d4ece0b31ad Author: Julian Lam <julian@designcreateplay.com> Date: Tue Dec 8 08:20:17 2015 -0500 WIP #2668
2015-12-11 12:07:02 -05:00
messages[0].newSet = true;
2015-12-15 17:50:30 +02:00
return next(undefined, messages);
Squashed commit of the following: Closes #2668 commit 3d4f494ed3257bceda8f6f82057cab83f0f252b3 Author: Julian Lam <julian@designcreateplay.com> Date: Fri Dec 11 12:06:42 2015 -0500 theme minvers for #2668 commit b608ce61854f8195143685bb9753b80d32b26e95 Author: Julian Lam <julian@designcreateplay.com> Date: Fri Dec 11 12:01:03 2015 -0500 Allowing chat modal to edit and delete messages re: #2668 commit 0104db90a4070582f3938b6929dae35f985bac35 Author: Julian Lam <julian@designcreateplay.com> Date: Fri Dec 11 11:51:23 2015 -0500 Fixed issue where newSet calculations were off ... sometimes. Also, rendering of edited messages now parses a template partial, instead of just replacing the content. commit 5cb6ca600425ca9320c599b32306e93dcc5aa4ce Author: Julian Lam <julian@designcreateplay.com> Date: Fri Dec 11 11:07:12 2015 -0500 If edited content matches existing content... ... then edit is aborted. commit 6e7495247b1895589c716db29f919a934087b924 Author: Julian Lam <julian@designcreateplay.com> Date: Fri Dec 11 11:05:08 2015 -0500 some linting and fixed issue where new msgs when deleted would crash server commit db4a9e40d6dff44569c2437378121db8fdf75cf8 Author: Julian Lam <julian@designcreateplay.com> Date: Tue Dec 8 17:25:56 2015 -0500 Message deletion for #2668, and fixed bug Fixed bug where chat modal would spawn even though user was sitting on the /chats page. commit a5aa2498ab4a8bba02a6daa43a9dbed7b3e37976 Author: Julian Lam <julian@designcreateplay.com> Date: Tue Dec 8 14:55:23 2015 -0500 wiring up the edit button, #2668 commit 5f2afdcf6f2b9eae6b5873ca100149e65e3d385d Author: Julian Lam <julian@designcreateplay.com> Date: Tue Dec 8 14:20:39 2015 -0500 added indicator to show if and when a message had been edited commit e8301132d525c1b9fd46c98cdb282ac7ea7a0d7f Author: Julian Lam <julian@designcreateplay.com> Date: Tue Dec 8 14:06:39 2015 -0500 Allowing editing of chat messages commit bfd991be1cb1769599f7d5d2b1638e313c3c2dcb Author: Julian Lam <julian@designcreateplay.com> Date: Tue Dec 8 10:33:49 2015 -0500 Added messageId to messages object return commit 0306ee6657b3288dd4547c66869d7d4ece0b31ad Author: Julian Lam <julian@designcreateplay.com> Date: Tue Dec 8 08:20:17 2015 -0500 WIP #2668
2015-12-11 12:07:02 -05:00
}
2015-12-15 17:50:30 +02:00
},
function(mid, next) {
Messaging.getMessageFields(mid, ['fromuid', 'timestamp'], next);
}
], function(err, fields) {
if (err) {
return next(err);
}
Squashed commit of the following: Closes #2668 commit 3d4f494ed3257bceda8f6f82057cab83f0f252b3 Author: Julian Lam <julian@designcreateplay.com> Date: Fri Dec 11 12:06:42 2015 -0500 theme minvers for #2668 commit b608ce61854f8195143685bb9753b80d32b26e95 Author: Julian Lam <julian@designcreateplay.com> Date: Fri Dec 11 12:01:03 2015 -0500 Allowing chat modal to edit and delete messages re: #2668 commit 0104db90a4070582f3938b6929dae35f985bac35 Author: Julian Lam <julian@designcreateplay.com> Date: Fri Dec 11 11:51:23 2015 -0500 Fixed issue where newSet calculations were off ... sometimes. Also, rendering of edited messages now parses a template partial, instead of just replacing the content. commit 5cb6ca600425ca9320c599b32306e93dcc5aa4ce Author: Julian Lam <julian@designcreateplay.com> Date: Fri Dec 11 11:07:12 2015 -0500 If edited content matches existing content... ... then edit is aborted. commit 6e7495247b1895589c716db29f919a934087b924 Author: Julian Lam <julian@designcreateplay.com> Date: Fri Dec 11 11:05:08 2015 -0500 some linting and fixed issue where new msgs when deleted would crash server commit db4a9e40d6dff44569c2437378121db8fdf75cf8 Author: Julian Lam <julian@designcreateplay.com> Date: Tue Dec 8 17:25:56 2015 -0500 Message deletion for #2668, and fixed bug Fixed bug where chat modal would spawn even though user was sitting on the /chats page. commit a5aa2498ab4a8bba02a6daa43a9dbed7b3e37976 Author: Julian Lam <julian@designcreateplay.com> Date: Tue Dec 8 14:55:23 2015 -0500 wiring up the edit button, #2668 commit 5f2afdcf6f2b9eae6b5873ca100149e65e3d385d Author: Julian Lam <julian@designcreateplay.com> Date: Tue Dec 8 14:20:39 2015 -0500 added indicator to show if and when a message had been edited commit e8301132d525c1b9fd46c98cdb282ac7ea7a0d7f Author: Julian Lam <julian@designcreateplay.com> Date: Tue Dec 8 14:06:39 2015 -0500 Allowing editing of chat messages commit bfd991be1cb1769599f7d5d2b1638e313c3c2dcb Author: Julian Lam <julian@designcreateplay.com> Date: Tue Dec 8 10:33:49 2015 -0500 Added messageId to messages object return commit 0306ee6657b3288dd4547c66869d7d4ece0b31ad Author: Julian Lam <julian@designcreateplay.com> Date: Tue Dec 8 08:20:17 2015 -0500 WIP #2668
2015-12-11 12:07:02 -05:00
2015-12-15 17:50:30 +02:00
if (
(parseInt(messages[0].timestamp, 10) > parseInt(fields.timestamp, 10) + (1000*60*5)) ||
(parseInt(messages[0].fromuid, 10) !== parseInt(fields.fromuid, 10))
) {
// If it's been 5 minutes, this is a new set of messages
messages[0].newSet = true;
}
next(undefined, messages);
});
} else {
next(null, []);
2014-07-24 22:04:33 -04:00
}
2015-12-15 17:50:30 +02:00
}
], callback);
2015-12-15 14:10:32 +02:00
};
2013-08-26 13:18:20 -04:00
2015-12-15 17:50:30 +02:00
Messaging.parse = function (message, fromuid, uid, roomId, isNew, callback) {
plugins.fireHook('filter:parse.raw', message, function(err, parsed) {
2014-01-18 15:35:51 -05:00
if (err) {
2016-08-17 14:12:35 +03:00
return callback(err);
2014-01-18 15:35:51 -05:00
}
2014-01-26 16:50:59 -05:00
var messageData = {
message: message,
parsed: parsed,
fromuid: fromuid,
2015-12-15 17:50:30 +02:00
uid: uid,
roomId: roomId,
isNew: isNew,
2014-03-28 12:13:01 -04:00
parsedMessage: parsed
2014-01-26 16:50:59 -05:00
};
plugins.fireHook('filter:messaging.parse', messageData, function(err, messageData) {
2016-08-17 14:12:35 +03:00
callback(err, messageData ? messageData.parsedMessage : '');
2014-01-26 16:50:59 -05:00
});
2014-01-18 15:35:51 -05:00
});
};
2014-01-15 18:20:05 +01:00
2015-12-16 21:52:42 +02:00
Messaging.isNewSet = function(uid, roomId, timestamp, callback) {
2015-12-15 17:50:30 +02:00
var setKey = 'uid:' + uid + ':chat:room:' + roomId + ':mids';
2014-07-25 15:37:46 -04:00
async.waterfall([
2015-12-16 21:52:42 +02:00
function(next) {
db.getSortedSetRevRangeWithScores(setKey, 0, 0, next);
2014-07-25 15:37:46 -04:00
},
function(messages, next) {
2015-12-16 21:52:42 +02:00
if (messages && messages.length) {
next(null, parseInt(timestamp, 10) > parseInt(messages[0].score, 10) + (1000 * 60 * 5));
2014-07-25 15:37:46 -04:00
} else {
2015-12-16 21:52:42 +02:00
next(null, true);
2014-07-25 15:37:46 -04:00
}
2015-12-17 11:47:32 +02:00
}
2014-07-25 15:37:46 -04:00
], callback);
};
Messaging.getRecentChats = function(uid, start, stop, callback) {
2015-12-15 17:50:30 +02:00
db.getSortedSetRevRange('uid:' + uid + ':chat:rooms', start, stop, function(err, roomIds) {
if (err) {
2013-12-27 14:09:22 -05:00
return callback(err);
}
2013-12-27 14:09:22 -05:00
2014-09-15 16:26:25 -04:00
async.parallel({
roomData: function(next) {
Messaging.getRoomsData(roomIds, next);
},
2014-09-15 16:26:25 -04:00
unread: function(next) {
2015-12-15 17:50:30 +02:00
db.isSortedSetMembers('uid:' + uid + ':chat:rooms:unread', roomIds, next);
2014-09-15 16:26:25 -04:00
},
users: function(next) {
2015-12-15 17:50:30 +02:00
async.map(roomIds, function(roomId, next) {
db.getSortedSetRevRange('chat:room:' + roomId + ':uids', 0, 3, function(err, uids) {
if (err) {
return next(err);
}
uids = uids.filter(function(value) {
2015-12-15 17:50:30 +02:00
return value && parseInt(value, 10) !== parseInt(uid, 10);
});
user.getUsersFields(uids, ['uid', 'username', 'userslug', 'picture', 'status', 'lastonline'] , next);
2015-12-15 17:50:30 +02:00
});
}, next);
2015-08-27 17:58:29 -04:00
},
2015-08-28 17:36:44 -04:00
teasers: function(next) {
2015-12-15 17:50:30 +02:00
async.map(roomIds, function(roomId, next) {
2015-12-17 12:35:49 +02:00
Messaging.getTeaser(uid, roomId, next);
2015-09-14 15:01:35 -04:00
}, next);
2014-09-15 16:26:25 -04:00
}
}, function(err, results) {
2014-03-14 19:34:05 -04:00
if (err) {
2014-03-14 20:06:46 -04:00
return callback(err);
2014-03-14 19:34:05 -04:00
}
results.roomData.forEach(function(room, index) {
room.users = results.users[index];
room.unread = results.unread[index];
room.teaser = results.teasers[index];
room.users.forEach(function(userData) {
2015-12-15 17:50:30 +02:00
if (userData && parseInt(userData.uid, 10)) {
userData.status = user.getStatus(userData);
}
});
room.users = room.users.filter(function(user) {
2015-12-15 17:50:30 +02:00
return user && parseInt(user.uid, 10);
});
room.lastUser = room.users[0];
room.usernames = room.users.map(function(user) {
2015-12-17 11:47:32 +02:00
return user.username;
}).join(', ');
2014-09-15 16:26:25 -04:00
});
callback(null, {rooms: results.roomData, nextStart: stop + 1});
2014-03-14 19:34:05 -04:00
});
});
};
2015-12-17 12:35:49 +02:00
Messaging.getTeaser = function (uid, roomId, callback) {
async.waterfall([
function (next) {
db.getSortedSetRevRange('uid:' + uid + ':chat:room:' + roomId + ':mids', 0, 0, next);
},
function (mids, next) {
if (!mids || !mids.length) {
return next(null, null);
}
Messaging.getMessageFields(mids[0], ['content', 'timestamp'], next);
},
function (teaser, next) {
if (teaser && teaser.content) {
teaser.content = S(teaser.content).stripTags().decodeHTMLEntities().s;
teaser.timestampISO = utils.toISOString(teaser.timestamp);
}
next(null, teaser);
}
], callback);
};
2015-12-16 11:15:43 +02:00
Messaging.canMessageUser = function(uid, toUid, callback) {
if (parseInt(meta.config.disableChat) === 1 || !uid || uid === toUid) {
2016-02-29 10:36:20 +02:00
return callback(new Error('[[error:chat-disabled]]'));
2015-12-16 11:15:43 +02:00
}
2015-12-16 11:15:43 +02:00
async.waterfall([
function (next) {
user.exists(toUid, next);
},
function (exists, next) {
if (!exists) {
2016-02-29 10:36:20 +02:00
return callback(new Error('[[error:no-user]]'));
2015-12-16 11:15:43 +02:00
}
user.getUserFields(uid, ['banned', 'email:confirmed'], next);
},
function (userData, next) {
if (parseInt(userData.banned, 10) === 1) {
2016-02-29 10:36:20 +02:00
return callback(new Error('[[error:user-banned]]'));
2015-12-16 11:15:43 +02:00
}
2014-07-19 10:33:27 -04:00
2015-12-16 11:15:43 +02:00
if (parseInt(meta.config.requireEmailConfirmation, 10) === 1 && parseInt(userData['email:confirmed'], 10) !== 1) {
2016-02-29 10:36:20 +02:00
return callback(new Error('[[error:email-not-confirmed-chat]]'));
2015-12-16 11:15:43 +02:00
}
2016-02-29 10:36:20 +02:00
async.parallel({
settings: async.apply(user.getSettings, toUid),
isAdmin: async.apply(user.isAdministrator, uid),
isFollowing: async.apply(user.isFollowing, toUid, uid)
}, next);
2015-12-16 11:15:43 +02:00
},
2016-02-29 10:36:20 +02:00
function(results, next) {
if (!results.settings.restrictChat || results.isAdmin || results.isFollowing) {
return next();
2015-12-16 11:15:43 +02:00
}
2016-02-29 10:36:20 +02:00
next(new Error('[[error:chat-restricted]]'));
2015-12-16 11:15:43 +02:00
}
], callback);
};
2015-12-16 11:15:43 +02:00
Messaging.canMessageRoom = function(uid, roomId, callback) {
if (parseInt(meta.config.disableChat) === 1 || !uid) {
2016-02-01 21:09:35 +02:00
return callback(new Error('[[error:chat-disabled]]'));
}
2016-02-29 10:36:20 +02:00
2014-10-30 17:50:07 -04:00
async.waterfall([
2015-10-02 17:26:12 -04:00
function (next) {
2015-12-15 20:00:51 +02:00
Messaging.isUserInRoom(uid, roomId, next);
2015-10-02 17:26:12 -04:00
},
2015-12-15 20:00:51 +02:00
function (inRoom, next) {
if (!inRoom) {
2016-02-01 21:09:35 +02:00
return next(new Error('[[error:not-in-room]]'));
2015-10-02 17:26:12 -04:00
}
2016-02-29 10:36:20 +02:00
Messaging.getUserCountInRoom(roomId, next);
},
function(count, next) {
if (count < 2) {
return next(new Error('[[error:no-users-in-room]]'));
}
2016-02-29 10:36:20 +02:00
user.getUserFields(uid, ['banned', 'email:confirmed'], next);
2015-10-02 17:26:12 -04:00
},
function (userData, next) {
if (parseInt(userData.banned, 10) === 1) {
2016-02-01 21:09:35 +02:00
return next(new Error('[[error:user-banned]]'));
2015-10-02 17:26:12 -04:00
}
2015-10-02 17:26:12 -04:00
if (parseInt(meta.config.requireEmailConfirmation, 10) === 1 && parseInt(userData['email:confirmed'], 10) !== 1) {
2016-02-01 21:09:35 +02:00
return next(new Error('[[error:email-not-confirmed-chat]]'));
2015-10-02 17:26:12 -04:00
}
2016-02-01 21:09:35 +02:00
next();
2014-10-30 17:50:07 -04:00
}
2014-11-06 12:57:55 -05:00
], callback);
};
2014-10-30 17:50:07 -04:00
2016-01-18 15:35:24 +02:00
Messaging.hasPrivateChat = function(uid, withUid, callback) {
async.waterfall([
function (next) {
async.parallel({
myRooms: async.apply(db.getSortedSetRevRange, 'uid:' + uid + ':chat:rooms', 0, -1),
theirRooms: async.apply(db.getSortedSetRevRange, 'uid:' + withUid + ':chat:rooms', 0, -1)
}, next);
},
function (results, next) {
var roomIds = results.myRooms.filter(function(roomId) {
return roomId && results.theirRooms.indexOf(roomId) !== -1;
});
if (!roomIds.length) {
return callback();
}
var index = 0;
var roomId = 0;
async.whilst(function() {
return index < roomIds.length && !roomId;
}, function(next) {
Messaging.getUserCountInRoom(roomIds[index], function(err, count) {
if (err) {
return next(err);
}
if (count === 2) {
roomId = roomIds[index];
next(null, roomId);
} else {
++ index;
next();
}
});
2016-02-16 20:25:23 +02:00
}, function(err) {
next(err, roomId);
});
2016-01-18 15:35:24 +02:00
}
], callback);
};
Squashed commit of the following: Closes #2668 commit 3d4f494ed3257bceda8f6f82057cab83f0f252b3 Author: Julian Lam <julian@designcreateplay.com> Date: Fri Dec 11 12:06:42 2015 -0500 theme minvers for #2668 commit b608ce61854f8195143685bb9753b80d32b26e95 Author: Julian Lam <julian@designcreateplay.com> Date: Fri Dec 11 12:01:03 2015 -0500 Allowing chat modal to edit and delete messages re: #2668 commit 0104db90a4070582f3938b6929dae35f985bac35 Author: Julian Lam <julian@designcreateplay.com> Date: Fri Dec 11 11:51:23 2015 -0500 Fixed issue where newSet calculations were off ... sometimes. Also, rendering of edited messages now parses a template partial, instead of just replacing the content. commit 5cb6ca600425ca9320c599b32306e93dcc5aa4ce Author: Julian Lam <julian@designcreateplay.com> Date: Fri Dec 11 11:07:12 2015 -0500 If edited content matches existing content... ... then edit is aborted. commit 6e7495247b1895589c716db29f919a934087b924 Author: Julian Lam <julian@designcreateplay.com> Date: Fri Dec 11 11:05:08 2015 -0500 some linting and fixed issue where new msgs when deleted would crash server commit db4a9e40d6dff44569c2437378121db8fdf75cf8 Author: Julian Lam <julian@designcreateplay.com> Date: Tue Dec 8 17:25:56 2015 -0500 Message deletion for #2668, and fixed bug Fixed bug where chat modal would spawn even though user was sitting on the /chats page. commit a5aa2498ab4a8bba02a6daa43a9dbed7b3e37976 Author: Julian Lam <julian@designcreateplay.com> Date: Tue Dec 8 14:55:23 2015 -0500 wiring up the edit button, #2668 commit 5f2afdcf6f2b9eae6b5873ca100149e65e3d385d Author: Julian Lam <julian@designcreateplay.com> Date: Tue Dec 8 14:20:39 2015 -0500 added indicator to show if and when a message had been edited commit e8301132d525c1b9fd46c98cdb282ac7ea7a0d7f Author: Julian Lam <julian@designcreateplay.com> Date: Tue Dec 8 14:06:39 2015 -0500 Allowing editing of chat messages commit bfd991be1cb1769599f7d5d2b1638e313c3c2dcb Author: Julian Lam <julian@designcreateplay.com> Date: Tue Dec 8 10:33:49 2015 -0500 Added messageId to messages object return commit 0306ee6657b3288dd4547c66869d7d4ece0b31ad Author: Julian Lam <julian@designcreateplay.com> Date: Tue Dec 8 08:20:17 2015 -0500 WIP #2668
2015-12-11 12:07:02 -05:00
2014-04-10 20:31:57 +01:00
}(exports));