Files
NodeBB/src/socket.io/helpers.js

195 lines
5.5 KiB
JavaScript
Raw Normal View History

2015-10-02 18:55:23 -04:00
'use strict';
var async = require('async');
var winston = require('winston');
var S = require('string');
2015-10-02 18:55:23 -04:00
var db = require('../database');
2015-10-02 18:55:23 -04:00
var websockets = require('./index');
var user = require('../user');
var posts = require('../posts');
var topics = require('../topics');
var privileges = require('../privileges');
var notifications = require('../notifications');
var plugins = require('../plugins');
var SocketHelpers = {};
SocketHelpers.notifyOnlineUsers = function (uid, result) {
2016-03-24 20:55:10 +02:00
winston.warn('[deprecated] SocketHelpers.notifyOnlineUsers, consider using socketHelpers.notifyNew(uid, \'newPost\', result);');
SocketHelpers.notifyNew(uid, 'newPost', result);
};
SocketHelpers.notifyNew = function (uid, type, result) {
2015-10-02 18:55:23 -04:00
async.waterfall([
function (next) {
2015-10-02 18:55:23 -04:00
user.getUidsFromSet('users:online', 0, -1, next);
},
function (uids, next) {
2016-02-23 16:46:10 +02:00
privileges.topics.filterUids('read', result.posts[0].topic.tid, uids, next);
2015-10-02 18:55:23 -04:00
},
function (uids, next) {
filterTidCidIgnorers(uids, result.posts[0].topic.tid, result.posts[0].topic.cid, next);
2016-05-18 19:02:43 +03:00
},
function (uids, next) {
2017-02-18 12:30:49 -07:00
plugins.fireHook('filter:sockets.sendNewPostToUids', { uidsTo: uids, uidFrom: uid, type: type }, next);
2017-02-17 19:31:21 -07:00
},
], function (err, data) {
2015-10-02 18:55:23 -04:00
if (err) {
return winston.error(err.stack);
}
2016-03-24 20:55:10 +02:00
result.posts[0].ip = undefined;
2015-10-02 18:55:23 -04:00
data.uidsTo.forEach(function (toUid) {
2016-03-24 20:55:10 +02:00
if (parseInt(toUid, 10) !== uid) {
websockets.in('uid_' + toUid).emit('event:new_post', result);
if (result.topic && type === 'newTopic') {
websockets.in('uid_' + toUid).emit('event:new_topic', result.topic);
}
2015-10-02 18:55:23 -04:00
}
2016-03-24 20:55:10 +02:00
});
2015-10-02 18:55:23 -04:00
});
};
function filterTidCidIgnorers(uids, tid, cid, callback) {
async.waterfall([
function (next) {
async.parallel({
topicFollowed: function (next) {
db.isSetMembers('tid:' + tid + ':followers', uids, next);
},
topicIgnored: function (next) {
db.isSetMembers('tid:' + tid + ':ignorers', uids, next);
},
categoryIgnored: function (next) {
db.sortedSetScores('cid:' + cid + ':ignorers', uids, next);
2017-02-17 19:31:21 -07:00
},
}, next);
},
function (results, next) {
uids = uids.filter(function (uid, index) {
return results.topicFollowed[index] ||
(!results.topicFollowed[index] && !results.topicIgnored[index] && !results.categoryIgnored[index]);
});
next(null, uids);
2017-02-17 19:31:21 -07:00
},
], callback);
}
SocketHelpers.sendNotificationToPostOwner = function (pid, fromuid, command, notification) {
2015-10-02 18:55:23 -04:00
if (!pid || !fromuid || !notification) {
return;
}
2016-05-06 13:47:10 +03:00
fromuid = parseInt(fromuid, 10);
var postData;
async.waterfall([
function (next) {
posts.getPostFields(pid, ['tid', 'uid', 'content'], next);
},
function (_postData, next) {
postData = _postData;
2016-07-12 18:03:14 +03:00
privileges.posts.can('read', pid, postData.uid, next);
},
function (canRead, next) {
if (!canRead || !postData.uid || fromuid === parseInt(postData.uid, 10)) {
2015-10-02 18:55:23 -04:00
return;
}
2016-05-06 13:47:10 +03:00
async.parallel({
username: async.apply(user.getUserField, fromuid, 'username'),
topicTitle: async.apply(topics.getTopicField, postData.tid, 'title'),
2017-02-17 19:31:21 -07:00
postObj: async.apply(posts.parsePost, postData),
2016-05-06 13:47:10 +03:00
}, next);
},
function (results, next) {
var title = S(results.topicTitle).decodeHTMLEntities().s;
var titleEscaped = title.replace(/%/g, '%').replace(/,/g, ',');
2015-10-02 18:55:23 -04:00
notifications.create({
bodyShort: '[[' + notification + ', ' + results.username + ', ' + titleEscaped + ']]',
2015-10-02 18:55:23 -04:00
bodyLong: results.postObj.content,
pid: pid,
path: '/post/' + pid,
2016-05-27 11:35:58 -04:00
nid: command + ':post:' + pid + ':uid:' + fromuid,
2015-12-16 11:20:21 -05:00
from: fromuid,
mergeId: notification + '|' + pid,
2017-02-17 19:31:21 -07:00
topicTitle: results.topicTitle,
2016-05-06 13:47:10 +03:00
}, next);
2017-02-17 19:31:21 -07:00
},
], function (err, notification) {
2016-05-06 13:47:10 +03:00
if (err) {
return winston.error(err);
}
if (notification) {
notifications.push(notification, [postData.uid]);
}
2015-10-02 18:55:23 -04:00
});
};
SocketHelpers.sendNotificationToTopicOwner = function (tid, fromuid, command, notification) {
2015-10-02 18:55:23 -04:00
if (!tid || !fromuid || !notification) {
return;
}
2016-05-06 13:47:10 +03:00
fromuid = parseInt(fromuid, 10);
2015-10-02 18:55:23 -04:00
2016-05-06 13:47:10 +03:00
var ownerUid;
async.waterfall([
function (next) {
async.parallel({
username: async.apply(user.getUserField, fromuid, 'username'),
topicData: async.apply(topics.getTopicFields, tid, ['uid', 'slug', 'title']),
}, next);
},
function (results, next) {
if (fromuid === parseInt(results.topicData.uid, 10)) {
return;
2015-10-02 18:55:23 -04:00
}
2016-05-06 13:47:10 +03:00
ownerUid = results.topicData.uid;
var title = S(results.topicData.title).decodeHTMLEntities().s;
var titleEscaped = title.replace(/%/g, '%').replace(/,/g, ',');
notifications.create({
bodyShort: '[[' + notification + ', ' + results.username + ', ' + titleEscaped + ']]',
path: '/topic/' + results.topicData.slug,
2016-05-27 11:35:58 -04:00
nid: command + ':tid:' + tid + ':uid:' + fromuid,
2017-02-17 19:31:21 -07:00
from: fromuid,
2016-05-06 13:47:10 +03:00
}, next);
2017-02-17 19:31:21 -07:00
},
], function (err, notification) {
2016-05-06 13:47:10 +03:00
if (err) {
return winston.error(err);
}
if (notification && parseInt(ownerUid, 10)) {
notifications.push(notification, [ownerUid]);
}
2015-10-02 18:55:23 -04:00
});
};
SocketHelpers.rescindUpvoteNotification = function (pid, fromuid) {
2016-05-27 11:35:58 -04:00
var nid = 'upvote:post:' + pid + ':uid:' + fromuid;
notifications.rescind(nid);
2016-05-27 11:59:37 -04:00
posts.getPostField(pid, 'uid', function (err, uid) {
2016-08-16 19:46:59 +02:00
if (err) {
return winston.error(err);
}
user.notifications.getUnreadCount(uid, function (err, count) {
2016-08-16 19:46:59 +02:00
if (err) {
return winston.error(err);
}
2016-05-27 11:59:37 -04:00
websockets.in('uid_' + uid).emit('event:notifications.updateCount', count);
});
});
2016-05-27 11:35:58 -04:00
};
SocketHelpers.emitToTopicAndCategory = function (event, data) {
2015-10-02 18:55:23 -04:00
websockets.in('topic_' + data.tid).emit(event, data);
websockets.in('category_' + data.cid).emit(event, data);
};
module.exports = SocketHelpers;