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

264 lines
7.2 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 _ = require('lodash');
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 categories = require('../categories');
2015-10-02 18:55:23 -04:00
var privileges = require('../privileges');
var notifications = require('../notifications');
var plugins = require('../plugins');
2017-10-13 21:02:41 -06:00
var utils = require('../utils');
2015-10-02 18:55:23 -04:00
var SocketHelpers = module.exports;
2015-10-02 18:55:23 -04:00
SocketHelpers.notifyNew = function (uid, type, result) {
let watchStateUids;
let categoryWatchStates;
let topicFollowState;
const post = result.posts[0];
const tid = post.topic.tid;
const cid = post.topic.cid;
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) {
2018-12-24 16:05:11 -05:00
uids = uids.filter(toUid => parseInt(toUid, 10) !== uid);
privileges.topics.filterUids('topics:read', tid, uids, next);
2015-10-02 18:55:23 -04:00
},
function (uids, next) {
watchStateUids = uids;
getWatchStates(watchStateUids, tid, cid, next);
2016-05-18 19:02:43 +03:00
},
function (watchStates, next) {
categoryWatchStates = _.zipObject(watchStateUids, watchStates.categoryWatchStates);
topicFollowState = _.zipObject(watchStateUids, watchStates.topicFollowed);
const uids = filterTidCidIgnorers(watchStateUids, watchStates);
user.blocks.filterUids(uid, uids, next);
},
function (uids, next) {
user.blocks.filterUids(post.topic.uid, uids, next);
},
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);
}
post.ip = undefined;
2015-10-02 18:55:23 -04:00
data.uidsTo.forEach(function (toUid) {
post.categoryWatchState = categoryWatchStates[toUid];
post.topic.isFollowing = topicFollowState[toUid];
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 getWatchStates(uids, tid, cid, callback) {
async.parallel({
topicFollowed: function (next) {
db.isSetMembers('tid:' + tid + ':followers', uids, next);
},
topicIgnored: function (next) {
db.isSetMembers('tid:' + tid + ':ignorers', uids, next);
2017-02-17 19:31:21 -07:00
},
categoryWatchStates: function (next) {
categories.getUidsWatchStates(cid, uids, next);
},
}, callback);
}
function filterTidCidIgnorers(uids, watchStates) {
return uids.filter(function (uid, index) {
return watchStates.topicFollowed[index] ||
(!watchStates.topicIgnored[index] && watchStates.categoryWatchStates[index] !== categories.watchStates.ignoring);
});
}
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;
2017-08-02 13:45:00 -04:00
async.parallel({
canRead: async.apply(privileges.posts.can, 'topics:read', pid, postData.uid),
2017-08-02 13:45:00 -04:00
isIgnoring: async.apply(topics.isIgnoring, [postData.tid], postData.uid),
}, next);
2016-07-12 18:03:14 +03:00
},
2017-08-02 13:45:00 -04:00
function (results, next) {
2018-10-23 13:59:20 -04:00
if (!results.canRead || results.isIgnoring[0] || !postData.uid || fromuid === postData.uid) {
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) {
2017-10-13 21:02:41 -06:00
var title = utils.decodeHTMLEntities(results.topicTitle);
var titleEscaped = title.replace(/%/g, '%').replace(/,/g, ',');
2015-10-02 18:55:23 -04:00
notifications.create({
2017-03-14 23:03:03 +03:00
type: command,
bodyShort: '[[' + notification + ', ' + results.username + ', ' + titleEscaped + ']]',
2015-10-02 18:55:23 -04:00
bodyLong: results.postObj.content,
pid: pid,
2018-02-14 13:40:12 -05:00
tid: postData.tid,
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) {
2018-10-23 13:59:20 -04:00
if (fromuid === results.topicData.uid) {
2016-05-06 13:47:10 +03:00
return;
2015-10-02 18:55:23 -04:00
}
2016-05-06 13:47:10 +03:00
ownerUid = results.topicData.uid;
2017-10-13 21:02:41 -06:00
var title = utils.decodeHTMLEntities(results.topicData.title);
2016-05-06 13:47:10 +03:00
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);
}
2018-10-23 13:59:20 -04:00
if (notification && ownerUid) {
2016-05-06 13:47:10 +03:00
notifications.push(notification, [ownerUid]);
}
2015-10-02 18:55:23 -04:00
});
};
SocketHelpers.upvote = function (data, notification) {
if (!data || !data.post || !data.post.uid || !data.post.votes || !data.post.pid || !data.fromuid) {
return;
}
var votes = data.post.votes;
var touid = data.post.uid;
var fromuid = data.fromuid;
var pid = data.post.pid;
var shouldNotify = {
all: function () {
return votes > 0;
},
first: function () {
return votes === 1;
},
everyTen: function () {
return votes > 0 && votes % 10 === 0;
},
threshold: function () {
return [1, 5, 10, 25].includes(votes) || (votes >= 50 && votes % 50 === 0);
},
logarithmic: function () {
return votes > 1 && Math.log10(votes) % 1 === 0;
},
disabled: function () {
return false;
},
};
async.waterfall([
function (next) {
user.getSettings(touid, next);
},
function (settings, next) {
var should = shouldNotify[settings.upvoteNotifFreq] || shouldNotify.all;
if (should()) {
SocketHelpers.sendNotificationToPostOwner(pid, fromuid, 'upvote', notification);
}
next();
},
], function (err) {
if (err) {
winston.error(err);
}
});
};
SocketHelpers.rescindUpvoteNotification = function (pid, fromuid) {
2017-05-20 21:01:55 -04:00
var uid;
async.waterfall([
function (next) {
notifications.rescind('upvote:post:' + pid + ':uid:' + fromuid, next);
},
function (next) {
posts.getPostField(pid, 'uid', next);
},
function (_uid, next) {
uid = _uid;
user.notifications.getUnreadCount(uid, next);
},
function (count, next) {
websockets.in('uid_' + uid).emit('event:notifications.updateCount', count);
next();
},
], function (err) {
2016-08-16 19:46:59 +02:00
if (err) {
2017-05-20 21:01:55 -04:00
winston.error(err);
2016-08-16 19:46:59 +02:00
}
2016-05-27 11:59:37 -04:00
});
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);
};