From feffbd8b168a1ee5e4a925688743e11fd23c570f Mon Sep 17 00:00:00 2001 From: Ghani Date: Thu, 5 Sep 2024 20:56:27 +0300 Subject: [PATCH 1/2] nested functions split into smaller helper functions --- src/upgrades/1.1.1/upload_privileges.js | 111 +++++++++++++++++------- 1 file changed, 79 insertions(+), 32 deletions(-) diff --git a/src/upgrades/1.1.1/upload_privileges.js b/src/upgrades/1.1.1/upload_privileges.js index d343f4ebfa..57d2e73575 100644 --- a/src/upgrades/1.1.1/upload_privileges.js +++ b/src/upgrades/1.1.1/upload_privileges.js @@ -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(); + }); + }); +} + From b8750229ef401bbe5a14d65293a0e84c41570cee Mon Sep 17 00:00:00 2001 From: Ghani Date: Thu, 5 Sep 2024 21:07:35 +0300 Subject: [PATCH 2/2] fixed-lint-errors --- src/upgrades/1.1.1/upload_privileges.js | 108 +++++++++++------------- 1 file changed, 50 insertions(+), 58 deletions(-) diff --git a/src/upgrades/1.1.1/upload_privileges.js b/src/upgrades/1.1.1/upload_privileges.js index 57d2e73575..dc3529079a 100644 --- a/src/upgrades/1.1.1/upload_privileges.js +++ b/src/upgrades/1.1.1/upload_privileges.js @@ -3,83 +3,75 @@ const db = require('../../database'); module.exports = { - 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'); + name: 'Giving upload privileges', + timestamp: Date.UTC(2016, 6, 12), + method: async function () { + console.log('graissov: Executing method function'); + 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; - } - }, + const cids = await getCategoryIds(); + await processCategories(cids, privilegesAPI, meta); + }, }; // 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); - }); - }); + 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); - })); + 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); - }); - }); + 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); - } - })); + 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(); - }); - }); + 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(); + }); + }); } -