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

184 lines
5.8 KiB
JavaScript
Raw Normal View History

2017-02-18 01:56:23 -07:00
'use strict';
2014-03-11 14:48:35 -04:00
const validator = require('validator');
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 utils = require('../utils');
2022-03-18 15:54:40 -04:00
const events = require('../events');
2019-09-09 19:19:56 -04:00
const SocketPosts = module.exports;
2016-10-08 19:09:48 +03:00
require('./posts/votes')(SocketPosts);
require('./posts/tools')(SocketPosts);
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.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);
postData = await user.blocks.filter(socket.uid, postData);
2019-09-09 19:19:56 -04:00
return postData;
};
2019-09-09 19:19:56 -04:00
SocketPosts.accept = async function (socket, data) {
await canEditQueue(socket, data, 'accept');
const result = await posts.submitFromQueue(data.id);
if (result && socket.uid !== parseInt(result.uid, 10)) {
await sendQueueNotification('post-queue-accepted', result.uid, `/post/${result.pid}`);
}
2022-03-18 15:54:40 -04:00
await logQueueEvent(socket, result, 'accept');
};
2019-09-09 19:19:56 -04:00
SocketPosts.reject = async function (socket, data) {
await canEditQueue(socket, data, 'reject');
const result = await posts.removeFromQueue(data.id);
if (result && socket.uid !== parseInt(result.uid, 10)) {
await sendQueueNotification('post-queue-rejected', result.uid, '/');
}
2022-03-18 15:54:40 -04:00
await logQueueEvent(socket, result, 'reject');
};
2022-03-18 15:54:40 -04:00
async function logQueueEvent(socket, result, type) {
await events.log({
type: `post-queue-${result.type}-${type}`,
uid: socket.uid,
ip: socket.ip,
content: result.data.content,
targetUid: result.uid,
...(result.type === 'topic' ?
{
cid: result.data.cid,
title: result.data.title,
} :
{
tid: result.data.tid,
}),
...(result.pid ? { pid: result.pid } : {}),
});
}
SocketPosts.notify = async function (socket, data) {
await canEditQueue(socket, data, 'notify');
const result = await posts.getFromQueue(data.id);
if (result) {
await sendQueueNotification('post-queue-notify', result.uid, `/post-queue/${data.id}`, validator.escape(String(data.message)));
}
};
2017-10-31 16:04:25 -04:00
async function canEditQueue(socket, data, action) {
const canEditQueue = await posts.canEditQueue(socket.uid, data, action);
2019-09-09 19:19:56 -04:00
if (!canEditQueue) {
throw new Error('[[error:no-privileges]]');
}
}
async function sendQueueNotification(type, targetUid, path, notificationText) {
const notifData = {
type: type,
nid: `${type}-${targetUid}-${path}`,
bodyShort: notificationText ? `[[notifications:${type}, ${notificationText}]]` : `[[notifications:${type}]]`,
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);