mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-11-17 19:21:04 +01:00
refactor: started work on porting socket methods to write API [breaking]
The following socket calls have been removed: * `posts.getRawPost` * `posts.getPostSummaryByPid` Two new Write API routes have been added: - `GET /api/v3/posts/:pid/raw` - `GET /api/v3/posts/:pid/summary`
This commit is contained in:
@@ -8,6 +8,7 @@ const user = require('../user');
|
||||
const posts = require('../posts');
|
||||
const topics = require('../topics');
|
||||
const groups = require('../groups');
|
||||
const plugins = require('../plugins');
|
||||
const meta = require('../meta');
|
||||
const events = require('../events');
|
||||
const privileges = require('../privileges');
|
||||
@@ -23,17 +24,15 @@ postsAPI.get = async function (caller, data) {
|
||||
posts.getPostData(data.pid),
|
||||
posts.hasVoted(data.pid, caller.uid),
|
||||
]);
|
||||
if (!post) {
|
||||
return null;
|
||||
}
|
||||
Object.assign(post, voted);
|
||||
|
||||
const userPrivilege = userPrivileges[0];
|
||||
if (!userPrivilege.read || !userPrivilege['topics:read']) {
|
||||
|
||||
if (!post || !userPrivilege.read || !userPrivilege['topics:read']) {
|
||||
return null;
|
||||
}
|
||||
|
||||
Object.assign(post, voted);
|
||||
post.ip = userPrivilege.isAdminOrMod ? post.ip : undefined;
|
||||
|
||||
const selfPost = caller.uid && caller.uid === parseInt(post.uid, 10);
|
||||
if (post.deleted && !(userPrivilege.isAdminOrMod || selfPost)) {
|
||||
post.content = '[[topic:post_is_deleted]]';
|
||||
@@ -42,6 +41,36 @@ postsAPI.get = async function (caller, data) {
|
||||
return post;
|
||||
};
|
||||
|
||||
postsAPI.getSummary = async (caller, { pid }) => {
|
||||
const tid = await posts.getPostField(pid, 'tid');
|
||||
const topicPrivileges = await privileges.topics.get(tid, caller.uid);
|
||||
if (!topicPrivileges.read || !topicPrivileges['topics:read']) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const postsData = await posts.getPostSummaryByPids([pid], caller.uid, { stripTags: false });
|
||||
posts.modifyPostByPrivilege(postsData[0], topicPrivileges);
|
||||
return postsData[0];
|
||||
};
|
||||
|
||||
postsAPI.getRaw = async (caller, { pid }) => {
|
||||
const userPrivileges = await privileges.posts.get([pid], caller.uid);
|
||||
const userPrivilege = userPrivileges[0];
|
||||
if (!userPrivilege['topics:read']) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const postData = await posts.getPostFields(pid, ['content', 'deleted']);
|
||||
const selfPost = caller.uid && caller.uid === parseInt(postData.uid, 10);
|
||||
|
||||
if (postData.deleted && !(userPrivilege.isAdminOrMod || selfPost)) {
|
||||
return null;
|
||||
}
|
||||
postData.pid = pid;
|
||||
const result = await plugins.hooks.fire('filter:post.getRawPost', { uid: caller.uid, postData: postData });
|
||||
return result.postData.content;
|
||||
};
|
||||
|
||||
postsAPI.edit = async function (caller, data) {
|
||||
if (!data || !data.pid || (meta.config.minimumPostLength !== 0 && !data.content)) {
|
||||
throw new Error('[[error:invalid-data]]');
|
||||
|
||||
Reference in New Issue
Block a user