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
This commit is contained in:
Julian Lam
2015-12-11 12:07:02 -05:00
parent d12e8e1116
commit 2d203d7dca
8 changed files with 322 additions and 31 deletions

View File

@@ -106,6 +106,81 @@ var db = require('./database'),
});
};
Messaging.editMessage = function(mid, content, callback) {
async.series([
function(next) {
// Verify that the message actually changed
Messaging.getMessageField(mid, 'content', function(err, raw) {
if (raw === content) {
// No dice.
return callback();
}
next();
});
},
async.apply(Messaging.setMessageFields, mid, {
content: content,
edited: Date.now()
}),
function(next) {
Messaging.getMessageFields(mid, ['fromuid', 'touid'], function(err, data) {
getMessages([mid], data.fromuid, data.touid, true, function(err, messages) {
sockets.in('uid_' + data.fromuid).emit('event:chats.edit', {
messages: messages
});
sockets.in('uid_' + data.touid).emit('event:chats.edit', {
messages: messages
});
next();
});
});
}
], callback);
};
Messaging.deleteMessage = function(mid, callback) {
var uids = [];
async.series([
function(next) {
db.getObject('message:' + mid, function(err, messageObj) {
messageObj.fromuid = parseInt(messageObj.fromuid, 10);
messageObj.touid = parseInt(messageObj.touid, 10);
uids.push(messageObj.fromuid, messageObj.touid);
uids.sort(function(a, b) {
return a > b ? 1 : -1;
});
next();
});
},
function(next) {
next();
},
function(next) {
db.sortedSetRemove('messages:uid:' + uids[0] + ':to:' + uids[1], mid, next);
},
async.apply(db.delete, 'message:' + mid)
], callback);
};
Messaging.getMessageField = function(mid, field, callback) {
Messaging.getMessageFields(mid, [field], function(err, fields) {
callback(err, fields[field]);
});
};
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) {
var fromuid = params.fromuid,
touid = params.touid,
@@ -160,7 +235,12 @@ var db = require('./database'),
async.waterfall([
async.apply(db.getObjects, keys),
function(messages, next) {
messages = messages.filter(Boolean);
messages = messages.map(function(msg, idx) {
if (msg) {
msg.messageId = parseInt(mids[idx], 10);
}
return msg;
}).filter(Boolean);
async.map(messages, function(message, next) {
var self = parseInt(message.fromuid, 10) === parseInt(fromuid, 10);
message.fromUser = self ? userData[0] : userData[1];
@@ -169,6 +249,10 @@ var db = require('./database'),
message.self = self ? 1 : 0;
message.newSet = false;
if (message.hasOwnProperty('edited')) {
message.editedISO = new Date(parseInt(message.edited, 10)).toISOString();
}
Messaging.parse(message.content, message.fromuid, fromuid, userData[1], userData[0], isNew, function(result) {
message.content = result;
message.cleanedContent = S(result).stripTags().decodeHTMLEntities().s;
@@ -177,21 +261,50 @@ var db = require('./database'),
}, next);
},
function(messages, next) {
// 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;
}
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;
});
return message;
});
next(undefined, messages);
next(undefined, messages);
} else {
// For single messages, we don't know the context, so look up the previous message and compare
var uids = [fromuid, touid].sort(function(a, b) { return a > b ? 1 : -1 });
var key = 'messages:uid:' + uids[0] + ':to:' + uids[1];
async.waterfall([
async.apply(db.sortedSetRank, key, messages[0].messageId),
function(index, next) {
db.getSortedSetRange(key, index-1, index-1, next);
},
function(mid, next) {
Messaging.getMessageFields(mid, ['fromuid', 'timestamp'], next);
}
], function(err, fields) {
if (err) {
return next(err);
}
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);
});
}
}
], callback);
});
@@ -277,7 +390,7 @@ var db = require('./database'),
count: 1,
markRead: false
}, function(err, teaser) {
var teaser = teaser[0];
teaser = teaser[0];
teaser.content = S(teaser.content).stripTags().decodeHTMLEntities().s;
next(err, teaser);
});
@@ -415,6 +528,39 @@ var db = require('./database'),
], callback);
};
Messaging.canEdit = function(messageId, uid, callback) {
if (parseInt(meta.config.disableChat) === 1) {
return callback(null, false);
}
async.waterfall([
function (next) {
user.getUserFields(uid, ['banned', 'email:confirmed'], next);
},
function (userData, next) {
if (parseInt(userData.banned, 10) === 1) {
return callback(null, false);
}
if (parseInt(meta.config.requireEmailConfirmation, 10) === 1 && parseInt(userData['email:confirmed'], 10) !== 1) {
return callback(null, false);
}
Messaging.getMessageField(messageId, 'fromuid', next);
},
function(fromUid, next) {
if (parseInt(fromUid, 10) === parseInt(uid, 10)) {
return callback(null, true);
}
user.isAdministrator(uid, next);
},
function(isAdmin, next) {
next(null, isAdmin);
}
], callback);
};
function sendNotifications(fromuid, touid, messageObj, callback) {
user.isOnline(touid, function(err, isOnline) {
if (err || isOnline) {