Skip to content

Commit

Permalink
nested functions split into smaller helper functions
Browse files Browse the repository at this point in the history
  • Loading branch information
raissg committed Sep 5, 2024
1 parent 09ad0fe commit feffbd8
Showing 1 changed file with 79 additions and 32 deletions.
111 changes: 79 additions & 32 deletions src/upgrades/1.1.1/upload_privileges.js
Original file line number Diff line number Diff line change
@@ -1,38 +1,85 @@
'use strict';

const async = require('async');
const db = require('../../database');


module.exports = {
name: 'Giving upload privileges',
timestamp: Date.UTC(2016, 6, 12),
method: function (callback) {
const privilegesAPI = require('../../privileges');
const meta = require('../../meta');

db.getSortedSetRange('categories:cid', 0, -1, (err, cids) => {
if (err) {
return callback(err);
}

async.eachSeries(cids, (cid, next) => {
privilegesAPI.categories.list(cid, (err, data) => {
if (err) {
return next(err);
}
async.eachSeries(data.groups, (group, next) => {
if (group.name === 'guests' && parseInt(meta.config.allowGuestUploads, 10) !== 1) {
return next();
}
if (group.privileges['groups:read']) {
privilegesAPI.categories.give(['upload:post:image'], cid, group.name, next);
} else {
next();
}
}, next);
});
}, callback);
});
},
name: 'Giving upload privileges',
timestamp: Date.UTC(2016, 6, 12),
method: async function () {
console.log('graissov: Executing method function');
try {
const privilegesAPI = require('../../privileges');
const meta = require('../../meta');

const cids = await getCategoryIds();
await processCategories(cids, privilegesAPI, meta);
// Return success status (optional depending on what is expected)
return;
} catch (err) {
// Throw error to handle it further up the chain
throw err;
}
},
};

// Helper function to get category IDs
function getCategoryIds() {
console.log('graissov: Executing getCategoryIds function');
return new Promise((resolve, reject) => {
db.getSortedSetRange('categories:cid', 0, -1, (err, cids) => {
if (err) {
return reject(err);
}
resolve(cids);
});
});
}

// Helper function to process each category
async function processCategories(cids, privilegesAPI, meta) {
console.log('graissov: Executing processCategories function');
await Promise.all(cids.map(async (cid) => {
const data = await getCategoryData(cid, privilegesAPI);
await processGroups(data.groups, cid, privilegesAPI, meta);
}));
}

// Helper function to get category data
function getCategoryData(cid, privilegesAPI) {
console.log('graissov: Executing getCategoryData function');
return new Promise((resolve, reject) => {
privilegesAPI.categories.list(cid, (err, data) => {
if (err) {
return reject(err);
}
resolve(data);
});
});
}

// Helper function to process groups within a category
async function processGroups(groups, cid, privilegesAPI, meta) {
console.log('graissov: Executing processGroups function');
await Promise.all(groups.map(async (group) => {
if (group.name === 'guests' && parseInt(meta.config.allowGuestUploads, 10) !== 1) {
return; // Skip guests if uploads are not allowed
}
if (group.privileges['groups:read']) {
await giveUploadPrivilege(cid, group.name, privilegesAPI);
}
}));
}

// Helper function to give upload privileges
function giveUploadPrivilege(cid, groupName, privilegesAPI) {
console.log('graissov: Executing giveUploadPrivilege function');
return new Promise((resolve, reject) => {
privilegesAPI.categories.give(['upload:post:image'], cid, groupName, (err) => {
if (err) {
return reject(err);
}
resolve();
});
});
}

0 comments on commit feffbd8

Please sign in to comment.