mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-10-26 08:36:12 +01:00
breaking: remove socket.emit('posts.edit')
This commit is contained in:
@@ -46,6 +46,9 @@ postsAPI.edit = async function (caller, data) {
|
|||||||
if (!data || !data.pid || (meta.config.minimumPostLength !== 0 && !data.content)) {
|
if (!data || !data.pid || (meta.config.minimumPostLength !== 0 && !data.content)) {
|
||||||
throw new Error('[[error:invalid-data]]');
|
throw new Error('[[error:invalid-data]]');
|
||||||
}
|
}
|
||||||
|
if (!caller.uid) {
|
||||||
|
throw new Error('[[error:not-logged-in]]');
|
||||||
|
}
|
||||||
// Trim and remove HTML (latter for composers that send in HTML, like redactor)
|
// Trim and remove HTML (latter for composers that send in HTML, like redactor)
|
||||||
const contentLen = utils.stripHTMLTags(data.content).trim().length;
|
const contentLen = utils.stripHTMLTags(data.content).trim().length;
|
||||||
|
|
||||||
|
|||||||
@@ -17,7 +17,6 @@ const sockets = require('.');
|
|||||||
|
|
||||||
const SocketPosts = module.exports;
|
const SocketPosts = module.exports;
|
||||||
|
|
||||||
require('./posts/edit')(SocketPosts);
|
|
||||||
require('./posts/move')(SocketPosts);
|
require('./posts/move')(SocketPosts);
|
||||||
require('./posts/votes')(SocketPosts);
|
require('./posts/votes')(SocketPosts);
|
||||||
require('./posts/tools')(SocketPosts);
|
require('./posts/tools')(SocketPosts);
|
||||||
|
|||||||
@@ -1,16 +0,0 @@
|
|||||||
'use strict';
|
|
||||||
|
|
||||||
const api = require('../../api');
|
|
||||||
const websockets = require('../index');
|
|
||||||
|
|
||||||
module.exports = function (SocketPosts) {
|
|
||||||
SocketPosts.edit = async function (socket, data) {
|
|
||||||
websockets.warnDeprecated(socket, 'PUT /api/v3/posts/:pid');
|
|
||||||
|
|
||||||
if (!socket.uid) {
|
|
||||||
throw new Error('[[error:not-logged-in]]');
|
|
||||||
}
|
|
||||||
|
|
||||||
return await api.posts.edit(socket, data);
|
|
||||||
};
|
|
||||||
};
|
|
||||||
156
test/posts.js
156
test/posts.js
@@ -8,6 +8,9 @@ const nconf = require('nconf');
|
|||||||
const crypto = require('crypto');
|
const crypto = require('crypto');
|
||||||
const fs = require('fs');
|
const fs = require('fs');
|
||||||
const path = require('path');
|
const path = require('path');
|
||||||
|
const util = require('util');
|
||||||
|
|
||||||
|
const sleep = util.promisify(setTimeout);
|
||||||
|
|
||||||
const db = require('./mocks/databasemock');
|
const db = require('./mocks/databasemock');
|
||||||
const topics = require('../src/topics');
|
const topics = require('../src/topics');
|
||||||
@@ -481,73 +484,90 @@ describe('Post\'s', () => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should error if user is not logged in', (done) => {
|
it('should error if user is not logged in', async () => {
|
||||||
socketPosts.edit({ uid: 0 }, {}, (err) => {
|
try {
|
||||||
assert.equal(err.message, '[[error:not-logged-in]]');
|
await apiPosts.edit({ uid: 0 }, { pid: pid, content: 'gg' });
|
||||||
done();
|
} catch (err) {
|
||||||
});
|
return assert.equal(err.message, '[[error:not-logged-in]]');
|
||||||
|
}
|
||||||
|
assert(false);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should error if data is invalid or missing', (done) => {
|
it('should error if data is invalid or missing', async () => {
|
||||||
socketPosts.edit({ uid: voterUid }, {}, (err) => {
|
try {
|
||||||
assert.equal(err.message, '[[error:invalid-data]]');
|
await apiPosts.edit({ uid: voterUid }, {});
|
||||||
done();
|
} catch (err) {
|
||||||
});
|
return assert.equal(err.message, '[[error:invalid-data]]');
|
||||||
|
}
|
||||||
|
assert(false);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should error if title is too short', (done) => {
|
it('should error if title is too short', async () => {
|
||||||
socketPosts.edit({ uid: voterUid }, { pid: pid, content: 'edited post content', title: 'a' }, (err) => {
|
try {
|
||||||
assert.equal(err.message, `[[error:title-too-short, ${meta.config.minimumTitleLength}]]`);
|
await apiPosts.edit({ uid: voterUid }, { pid: pid, content: 'edited post content', title: 'a' });
|
||||||
done();
|
} catch (err) {
|
||||||
});
|
return assert.equal(err.message, `[[error:title-too-short, ${meta.config.minimumTitleLength}]]`);
|
||||||
|
}
|
||||||
|
assert(false);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should error if title is too long', (done) => {
|
it('should error if title is too long', async () => {
|
||||||
const longTitle = new Array(meta.config.maximumTitleLength + 2).join('a');
|
const longTitle = new Array(meta.config.maximumTitleLength + 2).join('a');
|
||||||
socketPosts.edit({ uid: voterUid }, { pid: pid, content: 'edited post content', title: longTitle }, (err) => {
|
try {
|
||||||
assert.equal(err.message, `[[error:title-too-long, ${meta.config.maximumTitleLength}]]`);
|
await apiPosts.edit({ uid: voterUid }, { pid: pid, content: 'edited post content', title: longTitle });
|
||||||
done();
|
} catch (err) {
|
||||||
});
|
return assert.equal(err.message, `[[error:title-too-long, ${meta.config.maximumTitleLength}]]`);
|
||||||
|
}
|
||||||
|
assert(false);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should error with too few tags', (done) => {
|
it('should error with too few tags', async () => {
|
||||||
const oldValue = meta.config.minimumTagsPerTopic;
|
const oldValue = meta.config.minimumTagsPerTopic;
|
||||||
meta.config.minimumTagsPerTopic = 1;
|
meta.config.minimumTagsPerTopic = 1;
|
||||||
socketPosts.edit({ uid: voterUid }, { pid: pid, content: 'edited post content', tags: [] }, (err) => {
|
try {
|
||||||
|
await apiPosts.edit({ uid: voterUid }, { pid: pid, content: 'edited post content', tags: [] });
|
||||||
|
} catch (err) {
|
||||||
assert.equal(err.message, `[[error:not-enough-tags, ${meta.config.minimumTagsPerTopic}]]`);
|
assert.equal(err.message, `[[error:not-enough-tags, ${meta.config.minimumTagsPerTopic}]]`);
|
||||||
meta.config.minimumTagsPerTopic = oldValue;
|
meta.config.minimumTagsPerTopic = oldValue;
|
||||||
done();
|
return;
|
||||||
});
|
}
|
||||||
|
assert(false);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should error with too many tags', (done) => {
|
it('should error with too many tags', async () => {
|
||||||
const tags = [];
|
const tags = [];
|
||||||
for (let i = 0; i < meta.config.maximumTagsPerTopic + 1; i += 1) {
|
for (let i = 0; i < meta.config.maximumTagsPerTopic + 1; i += 1) {
|
||||||
tags.push(`tag${i}`);
|
tags.push(`tag${i}`);
|
||||||
}
|
}
|
||||||
socketPosts.edit({ uid: voterUid }, { pid: pid, content: 'edited post content', tags: tags }, (err) => {
|
try {
|
||||||
assert.equal(err.message, `[[error:too-many-tags, ${meta.config.maximumTagsPerTopic}]]`);
|
await apiPosts.edit({ uid: voterUid }, { pid: pid, content: 'edited post content', tags: tags });
|
||||||
done();
|
} catch (err) {
|
||||||
});
|
return assert.equal(err.message, `[[error:too-many-tags, ${meta.config.maximumTagsPerTopic}]]`);
|
||||||
|
}
|
||||||
|
assert(false);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should error if content is too short', (done) => {
|
it('should error if content is too short', async () => {
|
||||||
socketPosts.edit({ uid: voterUid }, { pid: pid, content: 'e' }, (err) => {
|
try {
|
||||||
assert.equal(err.message, `[[error:content-too-short, ${meta.config.minimumPostLength}]]`);
|
await apiPosts.edit({ uid: voterUid }, { pid: pid, content: 'e' });
|
||||||
done();
|
} catch (err) {
|
||||||
});
|
return assert.equal(err.message, `[[error:content-too-short, ${meta.config.minimumPostLength}]]`);
|
||||||
|
}
|
||||||
|
assert(false);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should error if content is too long', (done) => {
|
it('should error if content is too long', async () => {
|
||||||
const longContent = new Array(meta.config.maximumPostLength + 2).join('a');
|
const longContent = new Array(meta.config.maximumPostLength + 2).join('a');
|
||||||
socketPosts.edit({ uid: voterUid }, { pid: pid, content: longContent }, (err) => {
|
try {
|
||||||
assert.equal(err.message, `[[error:content-too-long, ${meta.config.maximumPostLength}]]`);
|
await apiPosts.edit({ uid: voterUid }, { pid: pid, content: longContent });
|
||||||
done();
|
} catch (err) {
|
||||||
});
|
return assert.equal(err.message, `[[error:content-too-long, ${meta.config.maximumPostLength}]]`);
|
||||||
|
}
|
||||||
|
assert(false);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should edit post', async () => {
|
it('should edit post', async () => {
|
||||||
const data = await socketPosts.edit({ uid: voterUid }, {
|
const data = await apiPosts.edit({ uid: voterUid }, {
|
||||||
pid: pid,
|
pid: pid,
|
||||||
content: 'edited post content',
|
content: 'edited post content',
|
||||||
title: 'edited title',
|
title: 'edited title',
|
||||||
@@ -562,40 +582,34 @@ describe('Post\'s', () => {
|
|||||||
assert(!res.hasOwnProperty('bookmarks'));
|
assert(!res.hasOwnProperty('bookmarks'));
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should disallow post editing for new users if post was made past the threshold for editing', (done) => {
|
it('should disallow post editing for new users if post was made past the threshold for editing', async () => {
|
||||||
meta.config.newbiePostEditDuration = 1;
|
meta.config.newbiePostEditDuration = 1;
|
||||||
setTimeout(() => {
|
await sleep(1000);
|
||||||
socketPosts.edit({ uid: voterUid }, { pid: pid, content: 'edited post content again', title: 'edited title again', tags: ['edited-twice'] }, (err, data) => {
|
try {
|
||||||
assert.equal(err.message, '[[error:post-edit-duration-expired, 1]]');
|
await apiPosts.edit({ uid: voterUid }, { pid: pid, content: 'edited post content again', title: 'edited title again', tags: ['edited-twice'] });
|
||||||
meta.config.newbiePostEditDuration = 3600;
|
} catch (err) {
|
||||||
done();
|
assert.equal(err.message, '[[error:post-edit-duration-expired, 1]]');
|
||||||
});
|
meta.config.newbiePostEditDuration = 3600;
|
||||||
}, 1000);
|
return;
|
||||||
|
}
|
||||||
|
assert(false);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should edit a deleted post', (done) => {
|
it('should edit a deleted post', async () => {
|
||||||
socketPosts.delete({ uid: voterUid }, { pid: pid, tid: tid }, (err) => {
|
await apiPosts.delete({ uid: voterUid }, { pid: pid, tid: tid });
|
||||||
assert.ifError(err);
|
const data = await apiPosts.edit({ uid: voterUid }, { pid: pid, content: 'edited deleted content', title: 'edited deleted title', tags: ['deleted'] });
|
||||||
socketPosts.edit({ uid: voterUid }, { pid: pid, content: 'edited deleted content', title: 'edited deleted title', tags: ['deleted'] }, (err, data) => {
|
assert.equal(data.content, 'edited deleted content');
|
||||||
assert.ifError(err);
|
assert.equal(data.editor, voterUid);
|
||||||
assert.equal(data.content, 'edited deleted content');
|
assert.equal(data.topic.title, 'edited deleted title');
|
||||||
assert.equal(data.editor, voterUid);
|
assert.equal(data.topic.tags[0].value, 'deleted');
|
||||||
assert.equal(data.topic.title, 'edited deleted title');
|
|
||||||
assert.equal(data.topic.tags[0].value, 'deleted');
|
|
||||||
done();
|
|
||||||
});
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should edit a reply post', (done) => {
|
it('should edit a reply post', async () => {
|
||||||
socketPosts.edit({ uid: voterUid }, { pid: replyPid, content: 'edited reply' }, (err, data) => {
|
const data = await apiPosts.edit({ uid: voterUid }, { pid: replyPid, content: 'edited reply' });
|
||||||
assert.ifError(err);
|
assert.equal(data.content, 'edited reply');
|
||||||
assert.equal(data.content, 'edited reply');
|
assert.equal(data.editor, voterUid);
|
||||||
assert.equal(data.editor, voterUid);
|
assert.equal(data.topic.isMainPost, false);
|
||||||
assert.equal(data.topic.isMainPost, false);
|
assert.equal(data.topic.renamed, false);
|
||||||
assert.equal(data.topic.renamed, false);
|
|
||||||
done();
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should return diffs', (done) => {
|
it('should return diffs', (done) => {
|
||||||
@@ -650,8 +664,8 @@ describe('Post\'s', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it('should delete a post diff', async () => {
|
it('should delete a post diff', async () => {
|
||||||
await socketPosts.edit({ uid: voterUid }, { pid: replyPid, content: 'another edit has been made' });
|
await apiPosts.edit({ uid: voterUid }, { pid: replyPid, content: 'another edit has been made' });
|
||||||
await socketPosts.edit({ uid: voterUid }, { pid: replyPid, content: 'most recent edit' });
|
await apiPosts.edit({ uid: voterUid }, { pid: replyPid, content: 'most recent edit' });
|
||||||
const timestamp = (await posts.diffs.list(replyPid)).pop();
|
const timestamp = (await posts.diffs.list(replyPid)).pop();
|
||||||
await posts.diffs.delete(replyPid, timestamp, voterUid);
|
await posts.diffs.delete(replyPid, timestamp, voterUid);
|
||||||
const differentTimestamp = (await posts.diffs.list(replyPid)).pop();
|
const differentTimestamp = (await posts.diffs.list(replyPid)).pop();
|
||||||
|
|||||||
Reference in New Issue
Block a user