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');
|
2021-04-26 14:50:44 -04:00
|
|
|
const notifications = require('../notifications');
|
2019-09-09 19:19:56 -04:00
|
|
|
const socketHelpers = require('./helpers');
|
|
|
|
|
const utils = require('../utils');
|
2020-12-28 19:38:00 -05:00
|
|
|
const api = require('../api');
|
2021-03-15 15:34:05 -04:00
|
|
|
const apiHelpers = require('../api/helpers');
|
2016-03-08 11:24:32 +02:00
|
|
|
|
2020-10-01 14:26:34 -04:00
|
|
|
const sockets = require('.');
|
2021-02-03 23:53:16 -07:00
|
|
|
|
2019-09-09 19:19:56 -04:00
|
|
|
const SocketPosts = module.exports;
|
2014-04-26 03:00:56 -04:00
|
|
|
|
2015-09-25 15:09:25 -04:00
|
|
|
require('./posts/move')(SocketPosts);
|
2016-10-08 19:09:48 +03:00
|
|
|
require('./posts/votes')(SocketPosts);
|
2015-09-25 15:09:25 -04:00
|
|
|
require('./posts/tools')(SocketPosts);
|
|
|
|
|
|
2019-09-09 19:19:56 -04:00
|
|
|
SocketPosts.reply = async function (socket, data) {
|
2020-10-07 11:14:37 -04:00
|
|
|
sockets.warnDeprecated(socket, 'POST /api/v3/topics/:tid');
|
2020-10-01 14:26:34 -04:00
|
|
|
|
2018-10-21 16:47:51 -04:00
|
|
|
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
|
|
|
}
|
|
|
|
|
|
2021-03-15 15:34:05 -04: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);
|
2017-08-15 12:59:40 -04:00
|
|
|
};
|
|
|
|
|
|
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,
|
|
|
|
|
};
|
2014-09-14 14:19:36 -04:00
|
|
|
|
2021-07-30 22:51:06 -04:00
|
|
|
if (socket.emit) {
|
|
|
|
|
socket.emit('event:new_post', result);
|
|
|
|
|
}
|
2019-09-09 19:19:56 -04:00
|
|
|
user.updateOnlineUsers(socket.uid);
|
2014-09-14 14:19:36 -04:00
|
|
|
|
2019-09-09 19:19:56 -04:00
|
|
|
socketHelpers.notifyNew(socket.uid, 'newPost', result);
|
2015-03-25 15:51:11 -04:00
|
|
|
|
2019-09-09 19:19:56 -04:00
|
|
|
return postData;
|
2017-08-15 12:59:40 -04:00
|
|
|
}
|
2014-09-18 17:09:40 -04:00
|
|
|
|
2019-08-07 17:38:23 -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]]');
|
|
|
|
|
}
|
2019-08-07 14:10:19 -04:00
|
|
|
|
2019-08-07 17:38:23 -04:00
|
|
|
const postData = await posts.getPostFields(pid, ['content', 'deleted']);
|
|
|
|
|
if (postData.deleted) {
|
|
|
|
|
throw new Error('[[error:no-post]]');
|
|
|
|
|
}
|
|
|
|
|
postData.pid = pid;
|
2020-11-20 16:06:26 -05:00
|
|
|
const result = await plugins.hooks.fire('filter:post.getRawPost', { uid: socket.uid, postData: postData });
|
2019-08-07 17:38:23 -04:00
|
|
|
return result.postData.content;
|
2014-01-10 16:00:03 -05:00
|
|
|
};
|
|
|
|
|
|
2020-12-04 11:56:10 -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;
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-22 14:29:25 -05:00
|
|
|
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-04 11:56:10 -05:00
|
|
|
|
2020-12-06 12:22:39 -05:00
|
|
|
const postsData = await posts.getPostSummaryByPids([pid], socket.uid, { stripTags: false });
|
2020-12-22 14:29:25 -05:00
|
|
|
posts.modifyPostByPrivilege(postsData[0], topicPrivileges);
|
2020-12-04 11:56:10 -05:00
|
|
|
return postsData[0];
|
2018-11-14 13:53:35 -05:00
|
|
|
};
|
|
|
|
|
|
2021-11-01 15:09:05 -04: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) {
|
2020-12-28 19:38:00 -05:00
|
|
|
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) {
|
2016-10-27 18:45:21 -05:00
|
|
|
if (!utils.isNumber(pid)) {
|
2019-09-09 19:19:56 -04:00
|
|
|
throw new Error('[[error:invalid-data]]');
|
2016-10-27 18:45:21 -05:00
|
|
|
}
|
2021-04-11 19:53:40 -04:00
|
|
|
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;
|
2016-10-27 18:45:21 -05:00
|
|
|
};
|
2017-08-15 12:59:40 -04:00
|
|
|
|
2019-09-09 19:19:56 -04:00
|
|
|
SocketPosts.accept = async function (socket, data) {
|
2021-04-26 14:50:44 -04:00
|
|
|
const result = await acceptOrReject(posts.submitFromQueue, socket, data);
|
|
|
|
|
await sendQueueNotification('post-queue-accepted', result.uid, `/post/${result.pid}`);
|
2017-08-15 12:59:40 -04:00
|
|
|
};
|
|
|
|
|
|
2019-09-09 19:19:56 -04:00
|
|
|
SocketPosts.reject = async function (socket, data) {
|
2021-04-26 14:50:44 -04:00
|
|
|
const result = await acceptOrReject(posts.removeFromQueue, socket, data);
|
|
|
|
|
await sendQueueNotification('post-queue-rejected', result.uid, '/');
|
2017-08-15 12:59:40 -04:00
|
|
|
};
|
2017-10-31 16:04:25 -04:00
|
|
|
|
2019-09-09 19:19:56 -04:00
|
|
|
async function acceptOrReject(method, socket, data) {
|
2020-07-23 16:43:56 -04:00
|
|
|
const canEditQueue = await posts.canEditQueue(socket.uid, data);
|
2019-09-09 19:19:56 -04:00
|
|
|
if (!canEditQueue) {
|
|
|
|
|
throw new Error('[[error:no-privileges]]');
|
|
|
|
|
}
|
2021-04-26 14:50:44 -04:00
|
|
|
return await method(data.id);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function sendQueueNotification(type, targetUid, path) {
|
2021-10-03 15:05:30 -04:00
|
|
|
const notifData = {
|
2021-04-26 14:50:44 -04:00
|
|
|
type: type,
|
|
|
|
|
nid: `${type}-${targetUid}-${path}`,
|
|
|
|
|
bodyShort: type === 'post-queue-accepted' ?
|
|
|
|
|
'[[notifications:post-queue-accepted]]' : '[[notifications:post-queue-rejected]]',
|
|
|
|
|
path: path,
|
2021-10-03 15:05:30 -04:00
|
|
|
};
|
|
|
|
|
if (parseInt(meta.config.postQueueNotificationUid, 10) > 0) {
|
|
|
|
|
notifData.from = meta.config.postQueueNotificationUid;
|
|
|
|
|
}
|
|
|
|
|
const notifObj = await notifications.create(notifData);
|
2021-04-26 14:50:44 -04:00
|
|
|
await notifications.push(notifObj, [targetUid]);
|
2019-09-09 19:19:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
SocketPosts.editQueuedContent = async function (socket, data) {
|
2020-07-23 16:43:56 -04:00
|
|
|
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
|
|
|
}
|
2020-07-23 16:43:56 -04:00
|
|
|
await posts.editQueuedContent(socket.uid, data);
|
2020-07-22 17:03:29 -04:00
|
|
|
if (data.content) {
|
2020-11-20 16:06:26 -05:00
|
|
|
return await plugins.hooks.fire('filter:parse.post', { postData: data });
|
2020-07-22 17:03:29 -04:00
|
|
|
}
|
|
|
|
|
return { postData: data };
|
2017-10-31 10:53:28 -04:00
|
|
|
};
|
2017-08-15 12:59:40 -04:00
|
|
|
|
2019-08-07 17:38:23 -04:00
|
|
|
require('../promisify')(SocketPosts);
|