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