breaking: remove socket.emit('posts.move')

isekai-main
Barış Soner Uşaklı 3 years ago
parent 4247f62441
commit 8427c5d9db

@ -208,6 +208,12 @@ async function isMainAndLastPost(pid) {
} }
postsAPI.move = async function (caller, data) { postsAPI.move = async function (caller, data) {
if (!caller.uid) {
throw new Error('[[error:not-logged-in]]');
}
if (!data || !data.pid || !data.tid) {
throw new Error('[[error:invalid-data]]');
}
const canMove = await Promise.all([ const canMove = await Promise.all([
privileges.topics.isAdminOrMod(data.tid, caller.uid), privileges.topics.isAdminOrMod(data.tid, caller.uid),
privileges.posts.canMove(data.pid, caller.uid), privileges.posts.canMove(data.pid, caller.uid),

@ -17,7 +17,6 @@ const sockets = require('.');
const SocketPosts = module.exports; const SocketPosts = module.exports;
require('./posts/move')(SocketPosts);
require('./posts/votes')(SocketPosts); require('./posts/votes')(SocketPosts);
require('./posts/tools')(SocketPosts); require('./posts/tools')(SocketPosts);

@ -1,33 +0,0 @@
'use strict';
const api = require('../../api');
const sockets = require('..');
module.exports = function (SocketPosts) {
function moveChecks(socket, typeCheck, data) {
if (!socket.uid) {
throw new Error('[[error:not-logged-in]]');
}
if (!data || !typeCheck || !data.tid) {
throw new Error('[[error:invalid-data]]');
}
}
SocketPosts.movePost = async function (socket, data) {
sockets.warnDeprecated(socket, 'PUT /api/v3/posts/:pid/move');
moveChecks(socket, isFinite(data.pid), data);
await api.posts.move(socket, data);
};
SocketPosts.movePosts = async function (socket, data) {
sockets.warnDeprecated(socket, 'PUT /api/v3/posts/:pid/move');
moveChecks(socket, !Array.isArray(data.pids), data);
await Promise.all(data.pids.map(async pid => api.posts.move(socket, {
tid: data.tid,
pid,
})));
};
};

@ -683,72 +683,62 @@ describe('Post\'s', () => {
let tid; let tid;
let moveTid; let moveTid;
before((done) => { before(async () => {
async.waterfall([ const topic1 = await topics.post({
function (next) { uid: voterUid,
topics.post({ cid: cid,
uid: voterUid, title: 'topic 1',
cid: cid, content: 'some content',
title: 'topic 1', });
content: 'some content', tid = topic1.topicData.tid;
}, next); const topic2 = await topics.post({
}, uid: voterUid,
function (data, next) { cid: cid,
tid = data.topicData.tid; title: 'topic 2',
topics.post({ content: 'some content',
uid: voterUid, });
cid: cid, moveTid = topic2.topicData.tid;
title: 'topic 2',
content: 'some content',
}, next);
},
function (data, next) {
moveTid = data.topicData.tid;
topics.reply({
uid: voterUid,
tid: tid,
timestamp: Date.now(),
content: 'A reply to move',
}, (err, data) => {
assert.ifError(err);
replyPid = data.pid;
next();
});
},
], done);
});
it('should error if uid is not logged in', (done) => { const reply = await topics.reply({
socketPosts.movePost({ uid: 0 }, {}, (err) => { uid: voterUid,
assert.equal(err.message, '[[error:not-logged-in]]'); tid: tid,
done(); timestamp: Date.now(),
content: 'A reply to move',
}); });
replyPid = reply.pid;
}); });
it('should error if data is invalid', (done) => { it('should error if uid is not logged in', async () => {
socketPosts.movePost({ uid: globalModUid }, {}, (err) => { try {
assert.equal(err.message, '[[error:invalid-data]]'); await apiPosts.move({ uid: 0 }, {});
done(); } catch (err) {
}); return assert.equal(err.message, '[[error:not-logged-in]]');
}
assert(false);
}); });
it('should error if user does not have move privilege', (done) => { it('should error if data is invalid', async () => {
socketPosts.movePost({ uid: voterUid }, { pid: replyPid, tid: moveTid }, (err) => { try {
assert.equal(err.message, '[[error:no-privileges]]'); await apiPosts.move({ uid: globalModUid }, {});
done(); } catch (err) {
}); return assert.equal(err.message, '[[error:invalid-data]]');
}
assert(false);
}); });
it('should error if user does not have move privilege', async () => {
try {
await apiPosts.move({ uid: voterUid }, { pid: replyPid, tid: moveTid });
} catch (err) {
return assert.equal(err.message, '[[error:no-privileges]]');
}
assert(false);
});
it('should move a post', (done) => { it('should move a post', async () => {
socketPosts.movePost({ uid: globalModUid }, { pid: replyPid, tid: moveTid }, (err) => { await apiPosts.move({ uid: globalModUid }, { pid: replyPid, tid: moveTid });
assert.ifError(err); const tid = await posts.getPostField(replyPid, 'tid');
posts.getPostField(replyPid, 'tid', (err, tid) => { assert(tid, moveTid);
assert.ifError(err);
assert(tid, moveTid);
done();
});
});
}); });
it('should fail to move post if not moderator of target category', async () => { it('should fail to move post if not moderator of target category', async () => {
@ -759,7 +749,7 @@ describe('Post\'s', () => {
await privileges.categories.give(privileges.categories.userPrivilegeList, cat1.cid, modUid); await privileges.categories.give(privileges.categories.userPrivilegeList, cat1.cid, modUid);
let err; let err;
try { try {
await socketPosts.movePost({ uid: modUid }, { pid: replyPid, tid: result.tid }); await apiPosts.move({ uid: modUid }, { pid: replyPid, tid: result.tid });
} catch (_err) { } catch (_err) {
err = _err; err = _err;
} }

Loading…
Cancel
Save