From a4d84a66ddc97bd85f76f617b05628a2e66ff88c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bar=C4=B1=C5=9F=20Soner=20U=C5=9Fakl=C4=B1?= Date: Mon, 9 Sep 2019 19:34:20 -0400 Subject: [PATCH] refactor: remove dupe code --- src/socket.io/posts/tools.js | 55 ++++++++++++++++-------------------- 1 file changed, 24 insertions(+), 31 deletions(-) diff --git a/src/socket.io/posts/tools.js b/src/socket.io/posts/tools.js index 4fd2a1ad22..3aa10116d9 100644 --- a/src/socket.io/posts/tools.js +++ b/src/socket.io/posts/tools.js @@ -55,72 +55,65 @@ module.exports = function (SocketPosts) { }; SocketPosts.delete = async function (socket, data) { - if (!data || !data.pid) { - throw new Error('[[error:invalid-data]]'); - } - const postData = await posts.tools.delete(socket.uid, data.pid); - const results = await isMainAndLastPost(data.pid); - if (results.isMain && results.isLast) { - await deleteOrRestoreTopicOf('delete', data.pid, socket); - } - - websockets.in('topic_' + data.tid).emit('event:post_deleted', postData); - - await events.log({ + await deleteOrRestore(socket, data, { + command: 'delete', + event: 'event:post_deleted', type: 'post-delete', - uid: socket.uid, - pid: data.pid, - tid: postData.tid, - ip: socket.ip, }); }; SocketPosts.restore = async function (socket, data) { + await deleteOrRestore(socket, data, { + command: 'restore', + event: 'event:post_restored', + type: 'post-restore', + }); + }; + + async function deleteOrRestore(socket, data, params) { if (!data || !data.pid) { throw new Error('[[error:invalid-data]]'); } - const postData = await posts.tools.restore(socket.uid, data.pid); + const postData = await posts.tools[params.command](socket.uid, data.pid); const results = await isMainAndLastPost(data.pid); if (results.isMain && results.isLast) { - await deleteOrRestoreTopicOf('restore', data.pid, socket); + await deleteOrRestoreTopicOf(params.command, data.pid, socket); } - websockets.in('topic_' + data.tid).emit('event:post_restored', postData); + + websockets.in('topic_' + data.tid).emit(params.event, postData); await events.log({ - type: 'post-restore', + type: params.type, uid: socket.uid, pid: data.pid, tid: postData.tid, ip: socket.ip, }); - }; + } SocketPosts.deletePosts = async function (socket, data) { - if (!data || !Array.isArray(data.pids)) { - throw new Error('[[error:invalid-data]]'); - } - for (const pid of data.pids) { - /* eslint-disable no-await-in-loop */ - await SocketPosts.delete(socket, { pid: pid, tid: data.tid }); - } + await deletePurgePosts(socket, data, 'delete'); }; SocketPosts.purgePosts = async function (socket, data) { + await deletePurgePosts(socket, data, 'purge'); + }; + + async function deletePurgePosts(socket, data, command) { if (!data || !Array.isArray(data.pids)) { throw new Error('[[error:invalid-data]]'); } for (const pid of data.pids) { /* eslint-disable no-await-in-loop */ - await SocketPosts.purge(socket, { pid: pid, tid: data.tid }); + await SocketPosts[command](socket, { pid: pid, tid: data.tid }); } - }; + } SocketPosts.purge = async function (socket, data) { if (!data || !parseInt(data.pid, 10)) { throw new Error('[[error:invalid-data]]'); } - const results = await isMainAndLastPost(data.pid); if (results.isMain && !results.isLast) { throw new Error('[[error:cant-purge-main-post]]');