mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-11-02 12:05:57 +01:00
closes #3701
This commit is contained in:
113
src/socket.io/helpers.js
Normal file
113
src/socket.io/helpers.js
Normal file
@@ -0,0 +1,113 @@
|
||||
'use strict';
|
||||
|
||||
var async = require('async');
|
||||
var winston = require('winston');
|
||||
var nconf = require('nconf');
|
||||
|
||||
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) {
|
||||
var cid = result.posts[0].topic.cid;
|
||||
async.waterfall([
|
||||
function(next) {
|
||||
user.getUidsFromSet('users:online', 0, -1, next);
|
||||
},
|
||||
function(uids, next) {
|
||||
privileges.categories.filterUids('read', cid, uids, next);
|
||||
},
|
||||
function(uids, next) {
|
||||
plugins.fireHook('filter:sockets.sendNewPostToUids', {uidsTo: uids, uidFrom: uid, type: 'newPost'}, next);
|
||||
}
|
||||
], function(err, data) {
|
||||
if (err) {
|
||||
return winston.error(err.stack);
|
||||
}
|
||||
|
||||
var uids = data.uidsTo;
|
||||
|
||||
for(var i=0; i<uids.length; ++i) {
|
||||
if (parseInt(uids[i], 10) !== uid) {
|
||||
websockets.in('uid_' + uids[i]).emit('event:new_post', result);
|
||||
}
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
SocketHelpers.sendNotificationToPostOwner = function(pid, fromuid, notification) {
|
||||
if (!pid || !fromuid || !notification) {
|
||||
return;
|
||||
}
|
||||
posts.getPostFields(pid, ['tid', 'uid', 'content'], function(err, postData) {
|
||||
if (err) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!postData.uid || fromuid === parseInt(postData.uid, 10)) {
|
||||
return;
|
||||
}
|
||||
|
||||
async.parallel({
|
||||
username: async.apply(user.getUserField, fromuid, 'username'),
|
||||
topicTitle: async.apply(topics.getTopicField, postData.tid, 'title'),
|
||||
postObj: async.apply(posts.parsePost, postData)
|
||||
}, function(err, results) {
|
||||
if (err) {
|
||||
return;
|
||||
}
|
||||
|
||||
notifications.create({
|
||||
bodyShort: '[[' + notification + ', ' + results.username + ', ' + results.topicTitle + ']]',
|
||||
bodyLong: results.postObj.content,
|
||||
pid: pid,
|
||||
nid: 'post:' + pid + ':uid:' + fromuid,
|
||||
from: fromuid
|
||||
}, function(err, notification) {
|
||||
if (!err && notification) {
|
||||
notifications.push(notification, [postData.uid]);
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
|
||||
SocketHelpers.sendNotificationToTopicOwner = function(tid, fromuid, notification) {
|
||||
if (!tid || !fromuid || !notification) {
|
||||
return;
|
||||
}
|
||||
|
||||
async.parallel({
|
||||
username: async.apply(user.getUserField, fromuid, 'username'),
|
||||
topicData: async.apply(topics.getTopicFields, tid, ['uid', 'slug']),
|
||||
}, function(err, results) {
|
||||
if (err || fromuid === parseInt(results.topicData.uid, 10)) {
|
||||
return;
|
||||
}
|
||||
|
||||
notifications.create({
|
||||
bodyShort: '[[' + notification + ', ' + results.username + ']]',
|
||||
path: nconf.get('relative_path') + '/topic/' + results.topicData.slug,
|
||||
nid: 'tid:' + tid + ':uid:' + fromuid,
|
||||
from: fromuid
|
||||
}, function(err, notification) {
|
||||
if (!err && notification) {
|
||||
notifications.push(notification, [results.topicData.uid]);
|
||||
}
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
SocketHelpers.emitToTopicAndCategory = function(event, data) {
|
||||
websockets.in('topic_' + data.tid).emit(event, data);
|
||||
websockets.in('category_' + data.cid).emit(event, data);
|
||||
};
|
||||
|
||||
module.exports = SocketHelpers;
|
@@ -2,18 +2,14 @@
|
||||
|
||||
var async = require('async'),
|
||||
|
||||
winston = require('winston'),
|
||||
|
||||
|
||||
posts = require('../posts'),
|
||||
plugins = require('../plugins'),
|
||||
privileges = require('../privileges'),
|
||||
meta = require('../meta'),
|
||||
topics = require('../topics'),
|
||||
notifications = require('../notifications'),
|
||||
user = require('../user'),
|
||||
websockets = require('./index'),
|
||||
socketTopics = require('./topics'),
|
||||
socketHelpers = require('./helpers'),
|
||||
utils = require('../../public/src/utils'),
|
||||
|
||||
SocketPosts = {};
|
||||
@@ -53,7 +49,7 @@ SocketPosts.reply = function(socket, data, callback) {
|
||||
|
||||
user.updateOnlineUsers(socket.uid);
|
||||
|
||||
SocketPosts.notifyOnlineUsers(socket.uid, result);
|
||||
socketHelpers.notifyOnlineUsers(socket.uid, result);
|
||||
|
||||
if (data.lock) {
|
||||
socketTopics.doTopicAction('lock', 'event:topic_locked', socket, {tids: [postData.topic.tid], cid: postData.topic.cid});
|
||||
@@ -61,70 +57,6 @@ SocketPosts.reply = function(socket, data, callback) {
|
||||
});
|
||||
};
|
||||
|
||||
SocketPosts.notifyOnlineUsers = function(uid, result) {
|
||||
var cid = result.posts[0].topic.cid;
|
||||
async.waterfall([
|
||||
function(next) {
|
||||
user.getUidsFromSet('users:online', 0, -1, next);
|
||||
},
|
||||
function(uids, next) {
|
||||
privileges.categories.filterUids('read', cid, uids, next);
|
||||
},
|
||||
function(uids, next) {
|
||||
plugins.fireHook('filter:sockets.sendNewPostToUids', {uidsTo: uids, uidFrom: uid, type: 'newPost'}, next);
|
||||
}
|
||||
], function(err, data) {
|
||||
if (err) {
|
||||
return winston.error(err.stack);
|
||||
}
|
||||
|
||||
var uids = data.uidsTo;
|
||||
|
||||
for(var i=0; i<uids.length; ++i) {
|
||||
if (parseInt(uids[i], 10) !== uid) {
|
||||
websockets.in('uid_' + uids[i]).emit('event:new_post', result);
|
||||
}
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
SocketPosts.sendNotificationToPostOwner = function(pid, fromuid, notification) {
|
||||
if(!pid || !fromuid || !notification) {
|
||||
return;
|
||||
}
|
||||
posts.getPostFields(pid, ['tid', 'uid', 'content'], function(err, postData) {
|
||||
if (err) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!postData.uid || fromuid === parseInt(postData.uid, 10)) {
|
||||
return;
|
||||
}
|
||||
|
||||
async.parallel({
|
||||
username: async.apply(user.getUserField, fromuid, 'username'),
|
||||
topicTitle: async.apply(topics.getTopicField, postData.tid, 'title'),
|
||||
postObj: async.apply(posts.parsePost, postData)
|
||||
}, function(err, results) {
|
||||
if (err) {
|
||||
return;
|
||||
}
|
||||
|
||||
notifications.create({
|
||||
bodyShort: '[[' + notification + ', ' + results.username + ', ' + results.topicTitle + ']]',
|
||||
bodyLong: results.postObj.content,
|
||||
pid: pid,
|
||||
nid: 'post:' + pid + ':uid:' + fromuid,
|
||||
from: fromuid
|
||||
}, function(err, notification) {
|
||||
if (!err && notification) {
|
||||
notifications.push(notification, [postData.uid]);
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
SocketPosts.getRawPost = function(socket, pid, callback) {
|
||||
async.waterfall([
|
||||
function(next) {
|
||||
|
@@ -9,6 +9,7 @@ var favourites = require('../../favourites');
|
||||
var plugins = require('../../plugins');
|
||||
var websockets = require('../index');
|
||||
var privileges = require('../../privileges');
|
||||
var socketHelpers = require('../helpers');
|
||||
|
||||
module.exports = function(SocketPosts) {
|
||||
SocketPosts.getVoters = function(socket, data, callback) {
|
||||
@@ -148,7 +149,7 @@ module.exports = function(SocketPosts) {
|
||||
}
|
||||
|
||||
if (result && notification) {
|
||||
SocketPosts.sendNotificationToPostOwner(data.pid, socket.uid, notification);
|
||||
socketHelpers.sendNotificationToPostOwner(data.pid, socket.uid, notification);
|
||||
}
|
||||
callback();
|
||||
});
|
||||
|
@@ -3,7 +3,7 @@
|
||||
var async = require('async');
|
||||
var privileges = require('../../privileges');
|
||||
var topics = require('../../topics');
|
||||
|
||||
var socketHelpers = require('../helpers');
|
||||
|
||||
module.exports = function(SocketPosts) {
|
||||
|
||||
@@ -28,7 +28,7 @@ module.exports = function(SocketPosts) {
|
||||
topics.movePostToTopic(data.pid, data.tid, next);
|
||||
},
|
||||
function (next) {
|
||||
SocketPosts.sendNotificationToPostOwner(data.pid, socket.uid, 'notifications:moved_your_post');
|
||||
socketHelpers.sendNotificationToPostOwner(data.pid, socket.uid, 'notifications:moved_your_post');
|
||||
next();
|
||||
}
|
||||
], callback);
|
||||
|
@@ -101,12 +101,6 @@ SocketTopics.bookmark = function(socket, data, callback) {
|
||||
topics.setUserBookmark(data.tid, socket.uid, data.index, callback);
|
||||
};
|
||||
|
||||
|
||||
SocketTopics.emitToTopicAndCategory = function(event, data) {
|
||||
websockets.in('topic_' + data.tid).emit(event, data);
|
||||
websockets.in('category_' + data.cid).emit(event, data);
|
||||
};
|
||||
|
||||
SocketTopics.createTopicFromPosts = function(socket, data, callback) {
|
||||
if (!socket.uid) {
|
||||
return callback(new Error('[[error:not-logged-in]]'));
|
||||
@@ -119,33 +113,6 @@ SocketTopics.createTopicFromPosts = function(socket, data, callback) {
|
||||
topics.createTopicFromPosts(socket.uid, data.title, data.pids, callback);
|
||||
};
|
||||
|
||||
SocketTopics.sendNotificationToTopicOwner = function(tid, fromuid, notification) {
|
||||
if (!tid || !fromuid) {
|
||||
return;
|
||||
}
|
||||
|
||||
async.parallel({
|
||||
username: async.apply(user.getUserField, fromuid, 'username'),
|
||||
topicData: async.apply(topics.getTopicFields, tid, ['uid', 'slug']),
|
||||
}, function(err, results) {
|
||||
if (err || fromuid === parseInt(results.topicData.uid, 10)) {
|
||||
return;
|
||||
}
|
||||
|
||||
notifications.create({
|
||||
bodyShort: '[[' + notification + ', ' + results.username + ']]',
|
||||
path: nconf.get('relative_path') + '/topic/' + results.topicData.slug,
|
||||
nid: 'tid:' + tid + ':uid:' + fromuid,
|
||||
from: fromuid
|
||||
}, function(err, notification) {
|
||||
if (!err && notification) {
|
||||
notifications.push(notification, [results.topicData.uid]);
|
||||
}
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
|
||||
SocketTopics.toggleFollow = function(socket, tid, callback) {
|
||||
followCommand(topics.toggleFollow, socket, tid, callback);
|
||||
};
|
||||
|
@@ -4,6 +4,7 @@ var async = require('async');
|
||||
var topics = require('../../topics');
|
||||
var categories = require('../../categories');
|
||||
var privileges = require('../../privileges');
|
||||
var socketHelpers = require('../helpers');
|
||||
|
||||
module.exports = function(SocketTopics) {
|
||||
|
||||
@@ -37,9 +38,9 @@ module.exports = function(SocketTopics) {
|
||||
return next(err);
|
||||
}
|
||||
|
||||
SocketTopics.emitToTopicAndCategory('event:topic_moved', topicData);
|
||||
socketHelpers.emitToTopicAndCategory('event:topic_moved', topicData);
|
||||
|
||||
SocketTopics.sendNotificationToTopicOwner(tid, socket.uid, 'notifications:moved_your_topic');
|
||||
socketHelpers.sendNotificationToTopicOwner(tid, socket.uid, 'notifications:moved_your_topic');
|
||||
|
||||
next();
|
||||
});
|
||||
|
@@ -3,6 +3,7 @@
|
||||
var async = require('async');
|
||||
var topics = require('../../topics');
|
||||
var events = require('../../events');
|
||||
var socketHelpers = require('../helpers');
|
||||
|
||||
module.exports = function(SocketTopics) {
|
||||
|
||||
@@ -55,7 +56,7 @@ module.exports = function(SocketTopics) {
|
||||
return next(err);
|
||||
}
|
||||
|
||||
SocketTopics.emitToTopicAndCategory(event, data);
|
||||
socketHelpers.emitToTopicAndCategory(event, data);
|
||||
|
||||
if (action === 'delete' || action === 'restore' || action === 'purge') {
|
||||
events.log({
|
||||
|
Reference in New Issue
Block a user