-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
nested functions split into smaller helper functions
- Loading branch information
Showing
1 changed file
with
79 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); | ||
}); | ||
} | ||
|