refactor(api): post diffs to use write API
parent
c2e2370655
commit
e118e59ce0
@ -0,0 +1,41 @@
|
||||
get:
|
||||
tags:
|
||||
- posts
|
||||
summary: get post edit history
|
||||
description: This operation retrieves a post's edit history
|
||||
parameters:
|
||||
- in: path
|
||||
name: pid
|
||||
schema:
|
||||
type: string
|
||||
required: true
|
||||
description: a valid post id
|
||||
example: 2
|
||||
responses:
|
||||
'200':
|
||||
description: Post history successfully retrieved.
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
type: object
|
||||
properties:
|
||||
status:
|
||||
$ref: ../../../components/schemas/Status.yaml#/Status
|
||||
response:
|
||||
type: object
|
||||
properties:
|
||||
timestamps:
|
||||
type: array
|
||||
items:
|
||||
type: number
|
||||
revisions:
|
||||
type: array
|
||||
items:
|
||||
type: object
|
||||
properties:
|
||||
timestamp:
|
||||
type: number
|
||||
username:
|
||||
type: string
|
||||
editable:
|
||||
type: boolean
|
@ -0,0 +1,65 @@
|
||||
get:
|
||||
tags:
|
||||
- posts
|
||||
summary: get single post edit history
|
||||
description: This operation retrieves a post's edit history
|
||||
parameters:
|
||||
- in: path
|
||||
name: pid
|
||||
schema:
|
||||
type: string
|
||||
required: true
|
||||
description: a valid post id
|
||||
example: 2
|
||||
- in: path
|
||||
name: since
|
||||
schema:
|
||||
type: number
|
||||
required: true
|
||||
description: a valid UNIX timestamp
|
||||
example: 0
|
||||
responses:
|
||||
'200':
|
||||
description: Post history successfully retrieved.
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
type: object
|
||||
properties:
|
||||
status:
|
||||
$ref: ../../../../components/schemas/Status.yaml#/Status
|
||||
response:
|
||||
$ref: ../../../../components/schemas/PostObject.yaml#/PostObject
|
||||
put:
|
||||
tags:
|
||||
- posts
|
||||
summary: revert a post
|
||||
description: This operation reverts a post to an earlier version. The revert process will append a new history item to the post's edit history.
|
||||
parameters:
|
||||
- in: path
|
||||
name: pid
|
||||
schema:
|
||||
type: string
|
||||
required: true
|
||||
description: a valid post id
|
||||
example: 2
|
||||
- in: path
|
||||
name: since
|
||||
schema:
|
||||
type: number
|
||||
required: true
|
||||
description: a valid UNIX timestamp
|
||||
example: 0
|
||||
responses:
|
||||
'200':
|
||||
description: Post successfully reverted
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
type: object
|
||||
properties:
|
||||
status:
|
||||
$ref: ../../../../components/schemas/Status.yaml#/Status
|
||||
response:
|
||||
type: object
|
||||
properties: {}
|
@ -1,67 +1,21 @@
|
||||
'use strict';
|
||||
|
||||
const posts = require('../../posts');
|
||||
const user = require('../../user');
|
||||
const privileges = require('../../privileges');
|
||||
const apiHelpers = require('../../api/helpers');
|
||||
const api = require('../../api');
|
||||
const websockets = require('..');
|
||||
|
||||
module.exports = function (SocketPosts) {
|
||||
SocketPosts.getDiffs = async function (socket, data) {
|
||||
await privilegeCheck(data.pid, socket.uid);
|
||||
const timestamps = await posts.diffs.list(data.pid);
|
||||
const post = await posts.getPostFields(data.pid, ['timestamp', 'uid']);
|
||||
|
||||
const diffs = await posts.diffs.get(data.pid);
|
||||
const uids = diffs.map(diff => diff.uid || null);
|
||||
uids.push(post.uid);
|
||||
let usernames = await user.getUsersFields(uids, ['username']);
|
||||
usernames = usernames.map(userObj => (userObj.uid ? userObj.username : null));
|
||||
|
||||
let canEdit = true;
|
||||
try {
|
||||
await user.isPrivilegedOrSelf(socket.uid, post.uid);
|
||||
} catch (e) {
|
||||
canEdit = false;
|
||||
}
|
||||
|
||||
timestamps.push(post.timestamp);
|
||||
|
||||
return {
|
||||
timestamps: timestamps,
|
||||
revisions: timestamps.map((timestamp, idx) => ({
|
||||
timestamp: timestamp,
|
||||
username: usernames[idx],
|
||||
})),
|
||||
editable: canEdit,
|
||||
};
|
||||
websockets.warnDeprecated(socket, 'GET /api/v3/posts/:pid/diffs');
|
||||
return await api.posts.getDiffs(socket, data);
|
||||
};
|
||||
|
||||
SocketPosts.showPostAt = async function (socket, data) {
|
||||
await privilegeCheck(data.pid, socket.uid);
|
||||
return await posts.diffs.load(data.pid, data.since, socket.uid);
|
||||
websockets.warnDeprecated(socket, 'GET /api/v3/posts/:pid/diffs/:since');
|
||||
return await api.posts.loadDiff(socket, data);
|
||||
};
|
||||
|
||||
async function privilegeCheck(pid, uid) {
|
||||
const [deleted, privilegesData] = await Promise.all([
|
||||
posts.getPostField(pid, 'deleted'),
|
||||
privileges.posts.get([pid], uid),
|
||||
]);
|
||||
|
||||
const allowed = privilegesData[0]['posts:history'] && (deleted ? privilegesData[0]['posts:view_deleted'] : true);
|
||||
if (!allowed) {
|
||||
throw new Error('[[error:no-privileges]]');
|
||||
}
|
||||
}
|
||||
|
||||
SocketPosts.restoreDiff = async function (socket, data) {
|
||||
const cid = await posts.getCidByPid(data.pid);
|
||||
const canEdit = await privileges.categories.can('edit', cid, socket.uid);
|
||||
if (!canEdit) {
|
||||
throw new Error('[[error:no-privileges]]');
|
||||
}
|
||||
|
||||
const edit = await posts.diffs.restore(data.pid, data.since, socket.uid, apiHelpers.buildReqObject(socket));
|
||||
websockets.in('topic_' + edit.topic.tid).emit('event:post_edited', edit);
|
||||
websockets.warnDeprecated(socket, 'PUT /api/v3/posts/:pid/diffs/:since');
|
||||
return await api.posts.restoreDiff(socket, data);
|
||||
};
|
||||
};
|
||||
|
Loading…
Reference in New Issue