Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix src/topics/tools.js #44

Open
wants to merge 5 commits into
base: f24
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 37 additions & 23 deletions src/topics/tools.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@ const plugins = require('../plugins');
const privileges = require('../privileges');
const utils = require('../utils');

console.log('Ashwaq : Refactored code executed');

module.exports = function (Topics) {
const topicTools = {};
Topics.tools = topicTools;

topicTools.delete = async function (tid, uid) {
console.log('Ashwaq: Refactored code executed');
return await toggleDelete(tid, uid, true);
};

Expand All @@ -25,40 +27,25 @@ module.exports = function (Topics) {

async function toggleDelete(tid, uid, isDelete) {
const topicData = await Topics.getTopicData(tid);
if (!topicData) {
throw new Error('[[error:no-topic]]');
}
// Scheduled topics can only be purged
if (topicData.scheduled) {
throw new Error('[[error:invalid-data]]');
}
const canDelete = await privileges.topics.canDelete(tid, uid);
validateTopicData(topicData);

const canDelete = await privileges.topics.canDelete(tid, uid);
const hook = isDelete ? 'delete' : 'restore';
const data = await plugins.hooks.fire(`filter:topic.${hook}`, { topicData: topicData, uid: uid, isDelete: isDelete, canDelete: canDelete, canRestore: canDelete });
const data = await plugins.hooks.fire(`filter:topic.${hook}`, { topicData, uid, isDelete, canDelete, canRestore: canDelete });

validatePermissions(data);

if ((!data.canDelete && data.isDelete) || (!data.canRestore && !data.isDelete)) {
throw new Error('[[error:no-privileges]]');
}
if (data.topicData.deleted && data.isDelete) {
throw new Error('[[error:topic-already-deleted]]');
} else if (!data.topicData.deleted && !data.isDelete) {
throw new Error('[[error:topic-already-restored]]');
}
if (data.isDelete) {
await Topics.delete(data.topicData.tid, data.uid);
} else {
await Topics.restore(data.topicData.tid);
}
const events = await Topics.events.log(tid, { type: isDelete ? 'delete' : 'restore', uid });

const events = await Topics.events.log(tid, { type: isDelete ? 'delete' : 'restore', uid });
data.topicData.deleted = data.isDelete ? 1 : 0;

if (data.isDelete) {
plugins.hooks.fire('action:topic.delete', { topic: data.topicData, uid: data.uid });
} else {
plugins.hooks.fire('action:topic.restore', { topic: data.topicData, uid: data.uid });
}
await fireActionHook(data);

const userData = await user.getUserFields(data.uid, ['username', 'userslug']);
return {
tid: data.topicData.tid,
Expand All @@ -70,6 +57,32 @@ module.exports = function (Topics) {
};
}

function validateTopicData(topicData) {
if (!topicData) {
throw new Error('[[error:no-topic]]');
}
if (topicData.scheduled) {
throw new Error('[[error:invalid-data]]');
}
}

function validatePermissions(data) {
if ((!data.canDelete && data.isDelete) || (!data.canRestore && !data.isDelete)) {
console.log('Ashwaq: Refactored code executed');
throw new Error('[[error:no-privileges]]');
}
if (data.topicData.deleted && data.isDelete) {
throw new Error('[[error:topic-already-deleted]]');
} else if (!data.topicData.deleted && !data.isDelete) {
throw new Error('[[error:topic-already-restored]]');
}
}

async function fireActionHook(data) {
const action = data.isDelete ? 'action:topic.delete' : 'action:topic.restore';
await plugins.hooks.fire(action, { topic: data.topicData, uid: data.uid });
}

topicTools.purge = async function (tid, uid) {
const topicData = await Topics.getTopicData(tid);
if (!topicData) {
Expand Down Expand Up @@ -293,3 +306,4 @@ module.exports = function (Topics) {
plugins.hooks.fire('action:topic.move', hookData);
};
};