Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactoring the src/upgrades/1.1.1/upload_privileges.js file to resolve warnings #107

Open
wants to merge 2 commits into
base: f24
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 62 additions & 23 deletions src/upgrades/1.1.1/upload_privileges.js
Original file line number Diff line number Diff line change
@@ -1,38 +1,77 @@
'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) {
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);
},
};

// 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 callback(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);
}));
}

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);
// 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();
});
});
}