Skip to content

Commit

Permalink
changed the file topics/tools
Browse files Browse the repository at this point in the history
  • Loading branch information
Ashwaq Taib committed Sep 2, 2024
1 parent c818343 commit b91fad0
Showing 1 changed file with 23 additions and 36 deletions.
59 changes: 23 additions & 36 deletions src/topics/tools.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,12 @@ 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 @@ -27,25 +25,40 @@ module.exports = function (Topics) {

async function toggleDelete(tid, uid, isDelete) {
const topicData = await Topics.getTopicData(tid);
validateTopicData(topicData);

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);
const hook = isDelete ? 'delete' : 'restore';
const data = await plugins.hooks.fire(`filter:topic.${hook}`, { topicData, uid, isDelete, canDelete, canRestore: canDelete });

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

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 });
data.topicData.deleted = data.isDelete ? 1 : 0;

await fireActionHook(data);
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 });
}
const userData = await user.getUserFields(data.uid, ['username', 'userslug']);
return {
tid: data.topicData.tid,
Expand All @@ -57,32 +70,6 @@ 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 b91fad0

Please sign in to comment.