feat: #7743, finish post module
parent
c4bb467ea5
commit
c0c6c652be
@ -1,53 +1,33 @@
|
||||
'use strict';
|
||||
|
||||
var async = require('async');
|
||||
var _ = require('lodash');
|
||||
const _ = require('lodash');
|
||||
|
||||
var db = require('../database');
|
||||
var privileges = require('../privileges');
|
||||
const db = require('../database');
|
||||
const privileges = require('../privileges');
|
||||
|
||||
|
||||
module.exports = function (Posts) {
|
||||
var terms = {
|
||||
const terms = {
|
||||
day: 86400000,
|
||||
week: 604800000,
|
||||
month: 2592000000,
|
||||
};
|
||||
|
||||
Posts.getRecentPosts = function (uid, start, stop, term, callback) {
|
||||
var min = 0;
|
||||
Posts.getRecentPosts = async function (uid, start, stop, term) {
|
||||
let min = 0;
|
||||
if (terms[term]) {
|
||||
min = Date.now() - terms[term];
|
||||
}
|
||||
|
||||
var count = parseInt(stop, 10) === -1 ? stop : stop - start + 1;
|
||||
|
||||
async.waterfall([
|
||||
function (next) {
|
||||
db.getSortedSetRevRangeByScore('posts:pid', start, count, '+inf', min, next);
|
||||
},
|
||||
function (pids, next) {
|
||||
privileges.posts.filter('topics:read', pids, uid, next);
|
||||
},
|
||||
function (pids, next) {
|
||||
Posts.getPostSummaryByPids(pids, uid, { stripTags: true }, next);
|
||||
},
|
||||
], callback);
|
||||
const count = parseInt(stop, 10) === -1 ? stop : stop - start + 1;
|
||||
let pids = await db.getSortedSetRevRangeByScore('posts:pid', start, count, '+inf', min);
|
||||
pids = await privileges.posts.filter('topics:read', pids, uid);
|
||||
return await Posts.getPostSummaryByPids(pids, uid, { stripTags: true });
|
||||
};
|
||||
|
||||
Posts.getRecentPosterUids = function (start, stop, callback) {
|
||||
async.waterfall([
|
||||
function (next) {
|
||||
db.getSortedSetRevRange('posts:pid', start, stop, next);
|
||||
},
|
||||
function (pids, next) {
|
||||
Posts.getPostsFields(pids, ['uid'], next);
|
||||
},
|
||||
function (postData, next) {
|
||||
var uids = _.uniq(postData.map(post => post && post.uid).filter(uid => parseInt(uid, 10)));
|
||||
|
||||
next(null, uids);
|
||||
},
|
||||
], callback);
|
||||
Posts.getRecentPosterUids = async function (start, stop) {
|
||||
const pids = await db.getSortedSetRevRange('posts:pid', start, stop);
|
||||
const postData = await Posts.getPostsFields(pids, ['uid']);
|
||||
return _.uniq(postData.map(p => p && p.uid).filter(uid => parseInt(uid, 10)));
|
||||
};
|
||||
};
|
||||
|
@ -1,72 +1,53 @@
|
||||
'use strict';
|
||||
|
||||
var async = require('async');
|
||||
|
||||
var privileges = require('../privileges');
|
||||
const privileges = require('../privileges');
|
||||
|
||||
module.exports = function (Posts) {
|
||||
Posts.tools = {};
|
||||
|
||||
Posts.tools.delete = function (uid, pid, callback) {
|
||||
togglePostDelete(uid, pid, true, callback);
|
||||
Posts.tools.delete = async function (uid, pid) {
|
||||
return await togglePostDelete(uid, pid, true);
|
||||
};
|
||||
|
||||
Posts.tools.restore = function (uid, pid, callback) {
|
||||
togglePostDelete(uid, pid, false, callback);
|
||||
Posts.tools.restore = async function (uid, pid) {
|
||||
return await togglePostDelete(uid, pid, false);
|
||||
};
|
||||
|
||||
function togglePostDelete(uid, pid, isDelete, callback) {
|
||||
async.waterfall([
|
||||
function (next) {
|
||||
Posts.exists(pid, next);
|
||||
},
|
||||
function (exists, next) {
|
||||
if (!exists) {
|
||||
return next(new Error('[[error:no-post]]'));
|
||||
}
|
||||
Posts.getPostField(pid, 'deleted', next);
|
||||
},
|
||||
function (deleted, next) {
|
||||
if (deleted && isDelete) {
|
||||
return next(new Error('[[error:post-already-deleted]]'));
|
||||
} else if (!deleted && !isDelete) {
|
||||
return next(new Error('[[error:post-already-restored]]'));
|
||||
}
|
||||
|
||||
privileges.posts.canDelete(pid, uid, next);
|
||||
},
|
||||
function (canDelete, next) {
|
||||
if (!canDelete.flag) {
|
||||
return next(new Error(canDelete.message));
|
||||
}
|
||||
|
||||
if (isDelete) {
|
||||
require('./cache').del(pid);
|
||||
Posts.delete(pid, uid, next);
|
||||
} else {
|
||||
Posts.restore(pid, uid, function (err, postData) {
|
||||
if (err) {
|
||||
return next(err);
|
||||
}
|
||||
Posts.parsePost(postData, next);
|
||||
});
|
||||
}
|
||||
},
|
||||
], callback);
|
||||
async function togglePostDelete(uid, pid, isDelete) {
|
||||
const [postData, canDelete] = await Promise.all([
|
||||
Posts.getPostData(pid),
|
||||
privileges.posts.canDelete(pid, uid),
|
||||
]);
|
||||
if (!postData) {
|
||||
throw new Error('[[error:no-post]]');
|
||||
}
|
||||
|
||||
if (postData.deleted && isDelete) {
|
||||
throw new Error('[[error:post-already-deleted]]');
|
||||
} else if (!postData.deleted && !isDelete) {
|
||||
throw new Error('[[error:post-already-restored]]');
|
||||
}
|
||||
|
||||
if (!canDelete.flag) {
|
||||
throw new Error(canDelete.message);
|
||||
}
|
||||
let post;
|
||||
if (isDelete) {
|
||||
require('./cache').del(pid);
|
||||
post = await Posts.delete(pid, uid);
|
||||
} else {
|
||||
post = await Posts.restore(pid, uid);
|
||||
post = await Posts.parsePost(post);
|
||||
}
|
||||
return post;
|
||||
}
|
||||
|
||||
Posts.tools.purge = function (uid, pid, callback) {
|
||||
async.waterfall([
|
||||
function (next) {
|
||||
privileges.posts.canPurge(pid, uid, next);
|
||||
},
|
||||
function (canPurge, next) {
|
||||
if (!canPurge) {
|
||||
return next(new Error('[[error:no-privileges]]'));
|
||||
}
|
||||
require('./cache').del(pid);
|
||||
Posts.purge(pid, uid, next);
|
||||
},
|
||||
], callback);
|
||||
Posts.tools.purge = async function (uid, pid) {
|
||||
const canPurge = await privileges.posts.canPurge(pid, uid);
|
||||
if (!canPurge) {
|
||||
throw new Error('[[error:no-privileges]]');
|
||||
}
|
||||
require('./cache').del(pid);
|
||||
await Posts.purge(pid, uid);
|
||||
};
|
||||
};
|
||||
|
Loading…
Reference in New Issue