Skip to content

Commit

Permalink
fixed topics/tools.js
Browse files Browse the repository at this point in the history
  • Loading branch information
Ashwaq Taib committed Sep 2, 2024
1 parent b91fad0 commit 12db095
Showing 1 changed file with 36 additions and 23 deletions.
59 changes: 36 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

0 comments on commit 12db095

Please sign in to comment.