Files
NodeBB/src/messaging/create.js

135 lines
3.5 KiB
JavaScript
Raw Normal View History

2015-12-15 14:10:32 +02:00
'use strict';
var async = require('async');
var meta = require('../meta');
var plugins = require('../plugins');
var db = require('../database');
2018-05-30 16:29:40 -04:00
var user = require('../user');
2015-12-15 14:10:32 +02:00
module.exports = function (Messaging) {
2018-05-31 15:05:12 -04:00
Messaging.sendMessage = function (data, callback) {
2015-12-15 14:10:32 +02:00
async.waterfall([
function (next) {
2018-05-31 15:05:12 -04:00
Messaging.checkContent(data.content, next);
2015-12-15 14:10:32 +02:00
},
function (next) {
2018-05-31 15:05:12 -04:00
Messaging.isUserInRoom(data.uid, data.roomId, next);
2015-12-15 14:10:32 +02:00
},
2015-12-15 20:00:51 +02:00
function (inRoom, next) {
if (!inRoom) {
return next(new Error('[[error:not-allowed]]'));
2015-12-15 14:10:32 +02:00
}
2015-12-16 15:09:14 +02:00
2018-05-31 15:05:12 -04:00
Messaging.addMessage(data, next);
2017-02-17 19:31:21 -07:00
},
2015-12-15 14:10:32 +02:00
], callback);
};
Messaging.checkContent = function (content, callback) {
2015-12-15 14:10:32 +02:00
if (!content) {
return callback(new Error('[[error:invalid-chat-message]]'));
}
plugins.fireHook('filter:messaging.checkContent', { content: content }, function (err, data) {
if (err) {
return callback(err);
}
content = String(data.content);
var maximumChatMessageLength = (meta.config.maximumChatMessageLength || 1000);
if (content.length > maximumChatMessageLength) {
return callback(new Error('[[error:chat-message-too-long, ' + maximumChatMessageLength + ']]'));
}
callback();
});
2015-12-15 14:10:32 +02:00
};
2018-05-31 15:05:12 -04:00
Messaging.addMessage = function (data, callback) {
2015-12-15 14:10:32 +02:00
var mid;
var message;
2015-12-16 21:52:42 +02:00
var isNewSet;
2015-12-16 15:09:14 +02:00
2015-12-15 14:10:32 +02:00
async.waterfall([
function (next) {
2018-05-31 15:05:12 -04:00
Messaging.checkContent(data.content, next);
2015-12-15 14:10:32 +02:00
},
function (next) {
db.incrObjectField('global', 'nextMid', next);
},
function (_mid, next) {
mid = _mid;
message = {
2018-05-31 15:05:12 -04:00
content: String(data.content),
timestamp: data.timestamp,
fromuid: data.uid,
roomId: data.roomId,
deleted: 0,
2015-12-15 14:10:32 +02:00
};
2018-05-31 15:05:12 -04:00
if (data.ip) {
message.ip = data.ip;
}
2015-12-15 14:10:32 +02:00
plugins.fireHook('filter:messaging.save', message, next);
},
function (message, next) {
db.setObject('message:' + mid, message, next);
},
function (next) {
2018-05-31 15:05:12 -04:00
Messaging.isNewSet(data.uid, data.roomId, data.timestamp, next);
2015-12-16 21:52:42 +02:00
},
function (_isNewSet, next) {
isNewSet = _isNewSet;
2018-05-31 15:05:12 -04:00
db.getSortedSetRange('chat:room:' + data.roomId + ':uids', 0, -1, next);
2015-12-15 14:10:32 +02:00
},
2018-05-30 16:29:40 -04:00
function (uids, next) {
2018-05-31 15:05:12 -04:00
user.blocks.filterUids(data.uid, uids, next);
2018-05-30 16:29:40 -04:00
},
2015-12-15 14:10:32 +02:00
function (uids, next) {
async.parallel([
2018-05-31 15:05:12 -04:00
async.apply(Messaging.addRoomToUsers, data.roomId, uids, data.timestamp),
async.apply(Messaging.addMessageToUsers, data.roomId, uids, mid, data.timestamp),
async.apply(Messaging.markUnread, uids, data.roomId),
2015-12-15 14:10:32 +02:00
], next);
},
function (results, next) {
2015-12-15 20:00:51 +02:00
async.parallel({
2018-05-31 15:05:12 -04:00
markRead: async.apply(Messaging.markRead, data.uid, data.roomId),
messages: async.apply(Messaging.getMessagesData, [mid], data.uid, data.roomId, true),
2015-12-15 20:00:51 +02:00
}, next);
2015-12-15 14:10:32 +02:00
},
2015-12-15 20:00:51 +02:00
function (results, next) {
if (!results.messages || !results.messages[0]) {
2015-12-15 14:10:32 +02:00
return next(null, null);
}
2015-12-16 21:52:42 +02:00
results.messages[0].newSet = isNewSet;
2015-12-15 20:00:51 +02:00
results.messages[0].mid = mid;
2018-05-31 15:05:12 -04:00
results.messages[0].roomId = data.roomId;
2015-12-15 20:00:51 +02:00
next(null, results.messages[0]);
2017-02-17 19:31:21 -07:00
},
2015-12-15 14:10:32 +02:00
], callback);
};
Messaging.addRoomToUsers = function (roomId, uids, timestamp, callback) {
2015-12-15 14:10:32 +02:00
if (!uids.length) {
return callback();
}
var keys = uids.map(function (uid) {
2015-12-15 14:10:32 +02:00
return 'uid:' + uid + ':chat:rooms';
});
2015-12-15 17:50:30 +02:00
db.sortedSetsAdd(keys, timestamp, roomId, callback);
2015-12-15 14:10:32 +02:00
};
Messaging.addMessageToUsers = function (roomId, uids, mid, timestamp, callback) {
2015-12-15 14:10:32 +02:00
if (!uids.length) {
return callback();
}
var keys = uids.map(function (uid) {
2015-12-15 14:10:32 +02:00
return 'uid:' + uid + ':chat:room:' + roomId + ':mids';
});
db.sortedSetsAdd(keys, timestamp, mid, callback);
};
2017-02-18 02:30:48 -07:00
};