mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-10-30 18:46:01 +01:00
closes #2515
This commit is contained in:
26
src/posts.js
26
src/posts.js
@@ -16,6 +16,7 @@ var async = require('async'),
|
|||||||
require('./posts/create')(Posts);
|
require('./posts/create')(Posts);
|
||||||
require('./posts/delete')(Posts);
|
require('./posts/delete')(Posts);
|
||||||
require('./posts/user')(Posts);
|
require('./posts/user')(Posts);
|
||||||
|
require('./posts/topics')(Posts);
|
||||||
require('./posts/category')(Posts);
|
require('./posts/category')(Posts);
|
||||||
require('./posts/summary')(Posts);
|
require('./posts/summary')(Posts);
|
||||||
require('./posts/recent')(Posts);
|
require('./posts/recent')(Posts);
|
||||||
@@ -25,20 +26,6 @@ var async = require('async'),
|
|||||||
db.isSortedSetMember('posts:pid', pid, callback);
|
db.isSortedSetMember('posts:pid', pid, callback);
|
||||||
};
|
};
|
||||||
|
|
||||||
Posts.getPostsByTid = function(tid, set, start, end, uid, reverse, callback) {
|
|
||||||
Posts.getPidsFromSet(set, start, end, reverse, function(err, pids) {
|
|
||||||
if(err) {
|
|
||||||
return callback(err);
|
|
||||||
}
|
|
||||||
|
|
||||||
if(!Array.isArray(pids) || !pids.length) {
|
|
||||||
return callback(null, []);
|
|
||||||
}
|
|
||||||
|
|
||||||
Posts.getPostsByPids(pids, uid, callback);
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
Posts.getPidsFromSet = function(set, start, end, reverse, callback) {
|
Posts.getPidsFromSet = function(set, start, end, reverse, callback) {
|
||||||
if (isNaN(start) || isNaN(end)) {
|
if (isNaN(start) || isNaN(end)) {
|
||||||
return callback(null, []);
|
return callback(null, []);
|
||||||
@@ -243,17 +230,6 @@ var async = require('async'),
|
|||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
Posts.isMain = function(pid, callback) {
|
|
||||||
Posts.getPostField(pid, 'tid', function(err, tid) {
|
|
||||||
if (err) {
|
|
||||||
return callback(err);
|
|
||||||
}
|
|
||||||
topics.getTopicField(tid, 'mainPid', function(err, mainPid) {
|
|
||||||
callback(err, parseInt(pid, 10) === parseInt(mainPid, 10));
|
|
||||||
});
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
Posts.updatePostVoteCount = function(pid, voteCount, callback) {
|
Posts.updatePostVoteCount = function(pid, voteCount, callback) {
|
||||||
async.parallel([
|
async.parallel([
|
||||||
function(next) {
|
function(next) {
|
||||||
|
|||||||
44
src/posts/topics.js
Normal file
44
src/posts/topics.js
Normal file
@@ -0,0 +1,44 @@
|
|||||||
|
|
||||||
|
'use strict';
|
||||||
|
|
||||||
|
var async = require('async'),
|
||||||
|
topics = require('../topics');
|
||||||
|
|
||||||
|
module.exports = function(Posts) {
|
||||||
|
|
||||||
|
Posts.getPostsByTid = function(tid, set, start, end, uid, reverse, callback) {
|
||||||
|
Posts.getPidsFromSet(set, start, end, reverse, function(err, pids) {
|
||||||
|
if (err) {
|
||||||
|
return callback(err);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!Array.isArray(pids) || !pids.length) {
|
||||||
|
return callback(null, []);
|
||||||
|
}
|
||||||
|
|
||||||
|
Posts.getPostsByPids(pids, uid, callback);
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
Posts.isMain = function(pid, callback) {
|
||||||
|
Posts.getPostField(pid, 'tid', function(err, tid) {
|
||||||
|
if (err) {
|
||||||
|
return callback(err);
|
||||||
|
}
|
||||||
|
topics.getTopicField(tid, 'mainPid', function(err, mainPid) {
|
||||||
|
callback(err, parseInt(pid, 10) === parseInt(mainPid, 10));
|
||||||
|
});
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
Posts.getTopicFields = function(pid, fields, callback) {
|
||||||
|
Posts.getPostField(pid, 'tid', function(err, tid) {
|
||||||
|
if (err) {
|
||||||
|
return callback(err);
|
||||||
|
}
|
||||||
|
|
||||||
|
topics.getTopicFields(tid, fields, callback);
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
};
|
||||||
@@ -16,6 +16,7 @@ var async = require('async'),
|
|||||||
groups = require('../groups'),
|
groups = require('../groups'),
|
||||||
user = require('../user'),
|
user = require('../user'),
|
||||||
websockets = require('./index'),
|
websockets = require('./index'),
|
||||||
|
socketTopics = require('./topics'),
|
||||||
events = require('../events'),
|
events = require('../events'),
|
||||||
utils = require('../../public/src/utils'),
|
utils = require('../../public/src/utils'),
|
||||||
|
|
||||||
@@ -345,27 +346,64 @@ function deleteOrRestore(command, socket, data, callback) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
SocketPosts.purge = function(socket, data, callback) {
|
SocketPosts.purge = function(socket, data, callback) {
|
||||||
if(!data || !parseInt(data.pid, 10)) {
|
function purgePost() {
|
||||||
|
postTools.purge(socket.uid, data.pid, function(err) {
|
||||||
|
if (err) {
|
||||||
|
return callback(err);
|
||||||
|
}
|
||||||
|
|
||||||
|
websockets.in('topic_' + data.tid).emit('event:post_purged', data.pid);
|
||||||
|
|
||||||
|
events.log({
|
||||||
|
type: 'post-purge',
|
||||||
|
uid: socket.uid,
|
||||||
|
pid: data.pid,
|
||||||
|
ip: socket.ip
|
||||||
|
});
|
||||||
|
|
||||||
|
callback();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!data || !parseInt(data.pid, 10)) {
|
||||||
return callback(new Error('[[error:invalid-data]]'));
|
return callback(new Error('[[error:invalid-data]]'));
|
||||||
}
|
}
|
||||||
postTools.purge(socket.uid, data.pid, function(err) {
|
|
||||||
if(err) {
|
isMainAndLastPost(data.pid, function(err, results) {
|
||||||
|
if (err) {
|
||||||
return callback(err);
|
return callback(err);
|
||||||
}
|
}
|
||||||
|
|
||||||
websockets.in('topic_' + data.tid).emit('event:post_purged', data.pid);
|
if (!results.isMain) {
|
||||||
|
return purgePost();
|
||||||
|
}
|
||||||
|
|
||||||
events.log({
|
if (!results.isLast) {
|
||||||
type: 'post-purge',
|
return callback(new Error('[[error:cant-purge-main-post]]'));
|
||||||
uid: socket.uid,
|
}
|
||||||
pid: data.pid,
|
|
||||||
ip: socket.ip
|
posts.getTopicFields(data.pid, ['tid', 'cid'], function(err, topic) {
|
||||||
|
if (err) {
|
||||||
|
return callback(err);
|
||||||
|
}
|
||||||
|
socketTopics.doTopicAction('delete', 'event:topic_deleted', socket, {tids: [topic.tid], cid: topic.cid}, callback);
|
||||||
});
|
});
|
||||||
|
|
||||||
callback();
|
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
function isMainAndLastPost(pid, callback) {
|
||||||
|
async.parallel({
|
||||||
|
isMain: function(next) {
|
||||||
|
posts.isMain(pid, next);
|
||||||
|
},
|
||||||
|
isLast: function(next) {
|
||||||
|
posts.getTopicFields(pid, ['postcount'], function(err, topic) {
|
||||||
|
next(err, topic ? parseInt(topic.postcount, 10) === 1 : false);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}, callback);
|
||||||
|
}
|
||||||
|
|
||||||
SocketPosts.getPrivileges = function(socket, pids, callback) {
|
SocketPosts.getPrivileges = function(socket, pids, callback) {
|
||||||
privileges.posts.get(pids, socket.uid, function(err, privileges) {
|
privileges.posts.get(pids, socket.uid, function(err, privileges) {
|
||||||
if (err) {
|
if (err) {
|
||||||
|
|||||||
@@ -205,34 +205,34 @@ SocketTopics.markAsUnreadForAll = function(socket, tids, callback) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
SocketTopics.delete = function(socket, data, callback) {
|
SocketTopics.delete = function(socket, data, callback) {
|
||||||
doTopicAction('delete', 'event:topic_deleted', socket, data, callback);
|
SocketTopics.doTopicAction('delete', 'event:topic_deleted', socket, data, callback);
|
||||||
};
|
};
|
||||||
|
|
||||||
SocketTopics.restore = function(socket, data, callback) {
|
SocketTopics.restore = function(socket, data, callback) {
|
||||||
doTopicAction('restore', 'event:topic_restored', socket, data, callback);
|
SocketTopics.doTopicAction('restore', 'event:topic_restored', socket, data, callback);
|
||||||
};
|
};
|
||||||
|
|
||||||
SocketTopics.purge = function(socket, data, callback) {
|
SocketTopics.purge = function(socket, data, callback) {
|
||||||
doTopicAction('purge', 'event:topic_purged', socket, data, callback);
|
SocketTopics.doTopicAction('purge', 'event:topic_purged', socket, data, callback);
|
||||||
};
|
};
|
||||||
|
|
||||||
SocketTopics.lock = function(socket, data, callback) {
|
SocketTopics.lock = function(socket, data, callback) {
|
||||||
doTopicAction('lock', 'event:topic_locked', socket, data, callback);
|
SocketTopics.doTopicAction('lock', 'event:topic_locked', socket, data, callback);
|
||||||
};
|
};
|
||||||
|
|
||||||
SocketTopics.unlock = function(socket, data, callback) {
|
SocketTopics.unlock = function(socket, data, callback) {
|
||||||
doTopicAction('unlock', 'event:topic_unlocked', socket, data, callback);
|
SocketTopics.doTopicAction('unlock', 'event:topic_unlocked', socket, data, callback);
|
||||||
};
|
};
|
||||||
|
|
||||||
SocketTopics.pin = function(socket, data, callback) {
|
SocketTopics.pin = function(socket, data, callback) {
|
||||||
doTopicAction('pin', 'event:topic_pinned', socket, data, callback);
|
SocketTopics.doTopicAction('pin', 'event:topic_pinned', socket, data, callback);
|
||||||
};
|
};
|
||||||
|
|
||||||
SocketTopics.unpin = function(socket, data, callback) {
|
SocketTopics.unpin = function(socket, data, callback) {
|
||||||
doTopicAction('unpin', 'event:topic_unpinned', socket, data, callback);
|
SocketTopics.doTopicAction('unpin', 'event:topic_unpinned', socket, data, callback);
|
||||||
};
|
};
|
||||||
|
|
||||||
function doTopicAction(action, event, socket, data, callback) {
|
SocketTopics.doTopicAction = function(action, event, socket, data, callback) {
|
||||||
if (!socket.uid) {
|
if (!socket.uid) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -274,7 +274,7 @@ function doTopicAction(action, event, socket, data, callback) {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
}, callback);
|
}, callback);
|
||||||
}
|
};
|
||||||
|
|
||||||
function emitToTopicAndCategory(event, data) {
|
function emitToTopicAndCategory(event, data) {
|
||||||
websockets.in('topic_' + data.tid).emit(event, data);
|
websockets.in('topic_' + data.tid).emit(event, data);
|
||||||
|
|||||||
Reference in New Issue
Block a user