You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

187 lines
5.4 KiB
JavaScript

9 years ago
'use strict';
var async = require('async');
var winston = require('winston');
var S = require('string');
9 years ago
var db = require('../database');
9 years ago
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) {
winston.warn('[deprecated] SocketHelpers.notifyOnlineUsers, consider using socketHelpers.notifyNew(uid, \'newPost\', result);');
SocketHelpers.notifyNew(uid, 'newPost', result);
};
SocketHelpers.notifyNew = function(uid, type, result) {
9 years ago
async.waterfall([
function(next) {
user.getUidsFromSet('users:online', 0, -1, next);
},
function(uids, next) {
9 years ago
privileges.topics.filterUids('read', result.posts[0].topic.tid, uids, next);
9 years ago
},
9 years ago
function(uids, next) {
filterTidCidIgnorers(uids, result.posts[0].topic.tid, result.posts[0].topic.cid, next);
9 years ago
},
9 years ago
function(uids, next) {
plugins.fireHook('filter:sockets.sendNewPostToUids', {uidsTo: uids, uidFrom: uid, type: type}, next);
9 years ago
}
], function(err, data) {
if (err) {
return winston.error(err.stack);
}
result.posts[0].ip = undefined;
9 years ago
data.uidsTo.forEach(function(toUid) {
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);
}
9 years ago
}
});
9 years ago
});
};
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);
}
}, 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);
}
], callback);
}
SocketHelpers.sendNotificationToPostOwner = function(pid, fromuid, command, notification) {
9 years ago
if (!pid || !fromuid || !notification) {
return;
}
9 years ago
fromuid = parseInt(fromuid, 10);
var postData;
async.waterfall([
function (next) {
posts.getPostFields(pid, ['tid', 'uid', 'content'], next);
},
function (_postData, next) {
postData = _postData;
9 years ago
privileges.posts.can('read', pid, postData.uid, next);
},
function (canRead, next) {
if (!canRead || !postData.uid || fromuid === parseInt(postData.uid, 10)) {
9 years ago
return;
}
9 years ago
async.parallel({
username: async.apply(user.getUserField, fromuid, 'username'),
topicTitle: async.apply(topics.getTopicField, postData.tid, 'title'),
postObj: async.apply(posts.parsePost, postData)
}, next);
},
function (results, next) {
var title = S(results.topicTitle).decodeHTMLEntities().s;
var titleEscaped = title.replace(/%/g, '%').replace(/,/g, ',');
9 years ago
notifications.create({
bodyShort: '[[' + notification + ', ' + results.username + ', ' + titleEscaped + ']]',
9 years ago
bodyLong: results.postObj.content,
pid: pid,
path: '/post/' + pid,
nid: command + ':post:' + pid + ':uid:' + fromuid,
9 years ago
from: fromuid,
mergeId: notification + '|' + pid,
9 years ago
topicTitle: results.topicTitle
9 years ago
}, next);
}
], function(err, notification) {
if (err) {
return winston.error(err);
}
if (notification) {
notifications.push(notification, [postData.uid]);
}
9 years ago
});
};
SocketHelpers.sendNotificationToTopicOwner = function(tid, fromuid, command, notification) {
9 years ago
if (!tid || !fromuid || !notification) {
return;
}
9 years ago
fromuid = parseInt(fromuid, 10);
9 years ago
9 years ago
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;
9 years ago
}
9 years ago
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,
nid: command + ':tid:' + tid + ':uid:' + fromuid,
9 years ago
from: fromuid
}, next);
}
], function(err, notification) {
if (err) {
return winston.error(err);
}
if (notification && parseInt(ownerUid, 10)) {
notifications.push(notification, [ownerUid]);
}
9 years ago
});
};
SocketHelpers.rescindUpvoteNotification = function(pid, fromuid) {
var nid = 'upvote:post:' + pid + ':uid:' + fromuid;
notifications.rescind(nid);
9 years ago
posts.getPostField(pid, 'uid', function(err, uid) {
user.notifications.getUnreadCount(uid, function(err, count) {
websockets.in('uid_' + uid).emit('event:notifications.updateCount', count);
});
});
};
9 years ago
SocketHelpers.emitToTopicAndCategory = function(event, data) {
websockets.in('topic_' + data.tid).emit(event, data);
websockets.in('category_' + data.cid).emit(event, data);
};
module.exports = SocketHelpers;