refactor(api): post move to write API
parent
0fa4c11ea6
commit
966c4117ec
@ -0,0 +1,36 @@
|
||||
put:
|
||||
tags:
|
||||
- posts
|
||||
summary: move a post
|
||||
description: This operation moves a post to a different topic.
|
||||
parameters:
|
||||
- in: path
|
||||
name: pid
|
||||
schema:
|
||||
type: number
|
||||
required: true
|
||||
description: a valid post id
|
||||
example: 5
|
||||
requestBody:
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
type: object
|
||||
properties:
|
||||
tid:
|
||||
type: number
|
||||
description: a valid topic id
|
||||
example: 4
|
||||
responses:
|
||||
'200':
|
||||
description: Post successfully moved
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
type: object
|
||||
properties:
|
||||
status:
|
||||
$ref: ../../../components/schemas/Status.yaml#/Status
|
||||
response:
|
||||
type: object
|
||||
properties: {}
|
@ -1,45 +1,19 @@
|
||||
'use strict';
|
||||
|
||||
const privileges = require('../../privileges');
|
||||
const topics = require('../../topics');
|
||||
const posts = require('../../posts');
|
||||
const socketHelpers = require('../helpers');
|
||||
const api = require('../../api');
|
||||
const sockets = require('..');
|
||||
|
||||
module.exports = function (SocketPosts) {
|
||||
SocketPosts.movePost = async function (socket, data) {
|
||||
await SocketPosts.movePosts(socket, { pids: [data.pid], tid: data.tid });
|
||||
sockets.warnDeprecated(socket, 'PUT /api/v3/posts/:pid/move');
|
||||
await api.posts.move(socket, data);
|
||||
};
|
||||
|
||||
SocketPosts.movePosts = async function (socket, data) {
|
||||
if (!socket.uid) {
|
||||
throw new Error('[[error:not-logged-in]]');
|
||||
}
|
||||
|
||||
if (!data || !Array.isArray(data.pids) || !data.tid) {
|
||||
throw new Error('[[error:invalid-data]]');
|
||||
}
|
||||
|
||||
const canMove = await privileges.topics.isAdminOrMod(data.tid, socket.uid);
|
||||
if (!canMove) {
|
||||
throw new Error('[[error:no-privileges]]');
|
||||
}
|
||||
|
||||
for (const pid of data.pids) {
|
||||
/* eslint-disable no-await-in-loop */
|
||||
const canMove = await privileges.posts.canMove(pid, socket.uid);
|
||||
if (!canMove) {
|
||||
throw new Error('[[error:no-privileges]]');
|
||||
}
|
||||
await topics.movePostToTopic(socket.uid, pid, data.tid);
|
||||
|
||||
const [postDeleted, topicDeleted] = await Promise.all([
|
||||
posts.getPostField(pid, 'deleted'),
|
||||
topics.getTopicField(data.tid, 'deleted'),
|
||||
]);
|
||||
|
||||
if (!postDeleted && !topicDeleted) {
|
||||
socketHelpers.sendNotificationToPostOwner(pid, socket.uid, 'move', 'notifications:moved_your_post');
|
||||
}
|
||||
}
|
||||
sockets.warnDeprecated(socket, 'PUT /api/v3/posts/:pid/move');
|
||||
await Promise.all(data.pids.map(async pid => api.posts.move(socket, {
|
||||
tid: data.tid,
|
||||
pid,
|
||||
})));
|
||||
};
|
||||
};
|
||||
|
Loading…
Reference in New Issue