Files
NodeBB/src/socket.io/posts.js

192 lines
6.0 KiB
JavaScript
Raw Normal View History

2017-02-18 01:56:23 -07:00
'use strict';
2014-03-11 14:48:35 -04:00
2019-09-09 19:19:56 -04:00
const db = require('../database');
const posts = require('../posts');
const privileges = require('../privileges');
const plugins = require('../plugins');
const meta = require('../meta');
const topics = require('../topics');
const user = require('../user');
const notifications = require('../notifications');
2019-09-09 19:19:56 -04:00
const socketHelpers = require('./helpers');
const utils = require('../utils');
const api = require('../api');
const apiHelpers = require('../api/helpers');
2016-03-08 11:24:32 +02:00
const sockets = require('.');
2019-09-09 19:19:56 -04:00
const SocketPosts = module.exports;
require('./posts/move')(SocketPosts);
2016-10-08 19:09:48 +03:00
require('./posts/votes')(SocketPosts);
require('./posts/tools')(SocketPosts);
2019-09-09 19:19:56 -04:00
SocketPosts.reply = async function (socket, data) {
sockets.warnDeprecated(socket, 'POST /api/v3/topics/:tid');
if (!data || !data.tid || (meta.config.minimumPostLength !== 0 && !data.content)) {
2019-09-09 19:19:56 -04:00
throw new Error('[[error:invalid-data]]');
2014-01-16 22:06:23 -05:00
}
apiHelpers.setDefaultPostData(socket, data);
2019-09-09 19:19:56 -04:00
await meta.blacklist.test(data.req.ip);
const shouldQueue = await posts.shouldQueue(socket.uid, data);
if (shouldQueue) {
return await posts.addToQueue(data);
}
return await postReply(socket, data);
};
2019-09-09 19:19:56 -04:00
async function postReply(socket, data) {
const postData = await topics.reply(data);
const result = {
posts: [postData],
'reputation:disabled': meta.config['reputation:disabled'] === 1,
'downvote:disabled': meta.config['downvote:disabled'] === 1,
};
if (socket.emit) {
socket.emit('event:new_post', result);
}
2019-09-09 19:19:56 -04:00
user.updateOnlineUsers(socket.uid);
2019-09-09 19:19:56 -04:00
socketHelpers.notifyNew(socket.uid, 'newPost', result);
2019-09-09 19:19:56 -04:00
return postData;
}
2014-09-18 17:09:40 -04:00
SocketPosts.getRawPost = async function (socket, pid) {
const canRead = await privileges.posts.can('topics:read', pid, socket.uid);
if (!canRead) {
throw new Error('[[error:no-privileges]]');
}
const postData = await posts.getPostFields(pid, ['content', 'deleted']);
if (postData.deleted) {
throw new Error('[[error:no-post]]');
}
postData.pid = pid;
const result = await plugins.hooks.fire('filter:post.getRawPost', { uid: socket.uid, postData: postData });
return result.postData.content;
2014-01-10 16:00:03 -05:00
};
SocketPosts.getPostSummaryByIndex = async function (socket, data) {
2019-09-09 19:19:56 -04:00
if (data.index < 0) {
data.index = 0;
}
let pid;
if (data.index === 0) {
2019-11-29 11:35:00 -05:00
pid = await topics.getTopicField(data.tid, 'mainPid');
2019-09-09 19:19:56 -04:00
} else {
2021-02-03 23:59:08 -07:00
pid = await db.getSortedSetRange(`tid:${data.tid}:posts`, data.index - 1, data.index - 1);
2019-09-09 19:19:56 -04:00
}
pid = Array.isArray(pid) ? pid[0] : pid;
if (!pid) {
return 0;
}
const topicPrivileges = await privileges.topics.get(data.tid, socket.uid);
if (!topicPrivileges['topics:read']) {
2019-09-09 19:19:56 -04:00
throw new Error('[[error:no-privileges]]');
}
2020-12-06 12:22:39 -05:00
const postsData = await posts.getPostSummaryByPids([pid], socket.uid, { stripTags: false });
posts.modifyPostByPrivilege(postsData[0], topicPrivileges);
return postsData[0];
2018-11-14 13:53:35 -05:00
};
SocketPosts.getPostSummaryByPid = async function (socket, data) {
if (!data || !data.pid) {
throw new Error('[[error:invalid-data]]');
}
const { pid } = data;
const tid = await posts.getPostField(pid, 'tid');
const topicPrivileges = await privileges.topics.get(tid, socket.uid);
if (!topicPrivileges['topics:read']) {
throw new Error('[[error:no-privileges]]');
}
const postsData = await posts.getPostSummaryByPids([pid], socket.uid, { stripTags: false });
posts.modifyPostByPrivilege(postsData[0], topicPrivileges);
return postsData[0];
};
2019-09-09 19:19:56 -04:00
SocketPosts.getPost = async function (socket, pid) {
sockets.warnDeprecated(socket, 'GET /api/v3/posts/:pid');
return await api.posts.get(socket, { pid });
2016-03-08 11:24:32 +02:00
};
2019-09-09 19:19:56 -04:00
SocketPosts.getCategory = async function (socket, pid) {
return await posts.getCidByPid(pid);
2014-03-24 14:30:11 -04:00
};
2014-03-11 18:46:16 -04:00
2019-09-09 19:19:56 -04:00
SocketPosts.getPidIndex = async function (socket, data) {
2015-09-14 21:04:56 -04:00
if (!data) {
2019-09-09 19:19:56 -04:00
throw new Error('[[error:invalid-data]]');
2015-09-14 21:04:56 -04:00
}
2019-09-09 19:19:56 -04:00
return await posts.getPidIndex(data.pid, data.tid, data.topicPostSort);
2014-08-27 15:03:36 -04:00
};
2019-09-09 19:19:56 -04:00
SocketPosts.getReplies = async function (socket, pid) {
if (!utils.isNumber(pid)) {
2019-09-09 19:19:56 -04:00
throw new Error('[[error:invalid-data]]');
}
const { topicPostSort } = await user.getSettings(socket.uid);
const pids = await posts.getPidsFromSet(`pid:${pid}:replies`, 0, -1, topicPostSort === 'newest_to_oldest');
2019-09-09 19:19:56 -04:00
2021-02-04 00:06:15 -07:00
let [postData, postPrivileges] = await Promise.all([
2019-09-09 19:19:56 -04:00
posts.getPostsByPids(pids, socket.uid),
privileges.posts.get(pids, socket.uid),
]);
postData = await topics.addPostData(postData, socket.uid);
postData.forEach((postData, index) => posts.modifyPostByPrivilege(postData, postPrivileges[index]));
postData = postData.filter((postData, index) => postData && postPrivileges[index].read);
return postData;
};
2019-09-09 19:19:56 -04:00
SocketPosts.accept = async function (socket, data) {
const result = await acceptOrReject(posts.submitFromQueue, socket, data);
await sendQueueNotification('post-queue-accepted', result.uid, `/post/${result.pid}`);
};
2019-09-09 19:19:56 -04:00
SocketPosts.reject = async function (socket, data) {
const result = await acceptOrReject(posts.removeFromQueue, socket, data);
await sendQueueNotification('post-queue-rejected', result.uid, '/');
};
2017-10-31 16:04:25 -04:00
2019-09-09 19:19:56 -04:00
async function acceptOrReject(method, socket, data) {
const canEditQueue = await posts.canEditQueue(socket.uid, data);
2019-09-09 19:19:56 -04:00
if (!canEditQueue) {
throw new Error('[[error:no-privileges]]');
}
return await method(data.id);
}
async function sendQueueNotification(type, targetUid, path) {
const notifData = {
type: type,
nid: `${type}-${targetUid}-${path}`,
bodyShort: type === 'post-queue-accepted' ?
'[[notifications:post-queue-accepted]]' : '[[notifications:post-queue-rejected]]',
path: path,
};
if (parseInt(meta.config.postQueueNotificationUid, 10) > 0) {
notifData.from = meta.config.postQueueNotificationUid;
}
const notifObj = await notifications.create(notifData);
await notifications.push(notifObj, [targetUid]);
2019-09-09 19:19:56 -04:00
}
SocketPosts.editQueuedContent = async function (socket, data) {
if (!data || !data.id || (!data.content && !data.title && !data.cid)) {
2019-09-09 19:19:56 -04:00
throw new Error('[[error:invalid-data]]');
2017-10-31 10:53:28 -04:00
}
await posts.editQueuedContent(socket.uid, data);
if (data.content) {
return await plugins.hooks.fire('filter:parse.post', { postData: data });
}
return { postData: data };
2017-10-31 10:53:28 -04:00
};
require('../promisify')(SocketPosts);