mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-10-27 17:16:14 +01:00
feat(writeapi): post voting
This commit is contained in:
@@ -13,6 +13,7 @@ const utils = require('../../utils');
|
||||
|
||||
const helpers = require('../helpers');
|
||||
const sockets = require('../../socket.io');
|
||||
const socketPostHelpers = require('../../socket.io/posts/helpers'); // eehhh...
|
||||
const socketTopics = require('../../socket.io/topics'); // eehhh...
|
||||
|
||||
const Posts = module.exports;
|
||||
@@ -145,6 +146,31 @@ Posts.delete = async (req, res) => {
|
||||
helpers.formatApiResponse(200, res);
|
||||
};
|
||||
|
||||
Posts.vote = async (req, res) => {
|
||||
const tid = await posts.getPostField(req.params.pid, 'tid');
|
||||
const data = { pid: req.params.pid, room_id: `topic_${tid}` };
|
||||
const socketMock = { uid: req.user.uid };
|
||||
|
||||
if (req.body.delta > 0) {
|
||||
await socketPostHelpers.postCommand(socketMock, 'upvote', 'voted', 'notifications:upvoted_your_post_in', data);
|
||||
} else if (req.body.delta < 0) {
|
||||
await socketPostHelpers.postCommand(socketMock, 'downvote', 'voted', '', data);
|
||||
} else {
|
||||
await socketPostHelpers.postCommand(socketMock, 'unvote', 'voted', '', data);
|
||||
}
|
||||
|
||||
helpers.formatApiResponse(200, res);
|
||||
};
|
||||
|
||||
Posts.unvote = async (req, res) => {
|
||||
const tid = await posts.getPostField(req.params.pid, 'tid');
|
||||
const data = { pid: req.params.pid, room_id: `topic_${tid}` };
|
||||
const socketMock = { uid: req.user.uid };
|
||||
|
||||
await socketPostHelpers.postCommand(socketMock, 'unvote', 'voted', '', data);
|
||||
helpers.formatApiResponse(200, res);
|
||||
};
|
||||
|
||||
async function isMainAndLastPost(pid) {
|
||||
const [isMain, topicData] = await Promise.all([
|
||||
posts.isMain(pid),
|
||||
|
||||
Reference in New Issue
Block a user