Skip to content

Commit

Permalink
Merge branch 'f24' into add-report-bug-button
Browse files Browse the repository at this point in the history
  • Loading branch information
aattiyah authored Sep 25, 2024
2 parents 0e6847a + cd4bbcb commit 6ed0e5d
Show file tree
Hide file tree
Showing 7 changed files with 159 additions and 101 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ feeds/recent.rss
logs/

pidfile
dump.rdb

# templates
/public/templates
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions src/categories/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,11 +97,11 @@ module.exports = function (Categories) {

cache.del('categories:cid');
await clearParentCategoryCache(parentCid);

if (data.cloneFromCid && parseInt(data.cloneFromCid, 10)) {
category = await Categories.copySettingsFrom(data.cloneFromCid, category.cid, !data.parentCid);
console.log('Latifa : Refactored code executed');
const cloneFromCid = parseInt(data.cloneFromCid, 10);
if (cloneFromCid) {
category = await Categories.copySettingsFrom(cloneFromCid, category.cid, !data.parentCid);
}

if (data.cloneChildren) {
await duplicateCategoriesChildren(category.cid, data.cloneFromCid, data.uid);
}
Expand Down
49 changes: 27 additions & 22 deletions src/controllers/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,32 @@ const helpers = module.exports;
const relative_path = nconf.get('relative_path');
const url = nconf.get('url');

const handleSuccessfulResponse = (statusCode, res, payload) => {
console.log('Aisha');
if (res.req.loggedIn) {
res.set('cache-control', 'private');
}

let code = 'ok';
let message = 'OK';
switch (statusCode) {
case 202:
code = 'accepted';
message = 'Accepted';
break;

case 204:
code = 'no-content';
message = 'No Content';
break;
}

res.status(statusCode).json({
status: { code, message },
response: payload || {},
});
};

helpers.noScriptErrors = async function (req, res, error, httpStatus) {
if (req.body.noscript !== 'true') {
if (typeof error === 'string') {
Expand Down Expand Up @@ -454,28 +480,7 @@ helpers.formatApiResponse = async (statusCode, res, payload) => {
}

if (String(statusCode).startsWith('2')) {
if (res.req.loggedIn) {
res.set('cache-control', 'private');
}

let code = 'ok';
let message = 'OK';
switch (statusCode) {
case 202:
code = 'accepted';
message = 'Accepted';
break;

case 204:
code = 'no-content';
message = 'No Content';
break;
}

res.status(statusCode).json({
status: { code, message },
response: payload || {},
});
handleSuccessfulResponse(statusCode, res, payload);
} else if (payload instanceof Error || typeof payload === 'string') {
const message = payload instanceof Error ? payload.message : payload;
const response = {};
Expand Down
81 changes: 50 additions & 31 deletions src/groups/update.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,55 +14,82 @@ const cache = require('../cache');

module.exports = function (Groups) {
Groups.update = async function (groupName, values) {
await validateGroupExists(groupName);

values = await applyPluginFilters(groupName, values);

// Cast some values as bool (if not boolean already)
// 'true' and '1' = true, everything else false
values = castBooleanValues(values);

const payload = createPayload(values);

await handleSpecialProperties(groupName, values, payload);

await db.setObject(`group:${groupName}`, payload);
await Groups.renameGroup(groupName, values.name);

plugins.hooks.fire('action:group.update', {
name: groupName,
values: values,
});
};

async function validateGroupExists(groupName) {
const exists = await db.exists(`group:${groupName}`);
if (!exists) {
throw new Error('[[error:no-group]]');
}
}

({ values } = await plugins.hooks.fire('filter:group.update', {
async function applyPluginFilters(groupName, values) {
const result = await plugins.hooks.fire('filter:group.update', {
groupName: groupName,
values: values,
}));
});
return result.values;
}

// Cast some values as bool (if not boolean already)
// 'true' and '1' = true, everything else false
function castBooleanValues(values) {
['userTitleEnabled', 'private', 'hidden', 'disableJoinRequests', 'disableLeave'].forEach((prop) => {
if (values.hasOwnProperty(prop) && typeof values[prop] !== 'boolean') {
values[prop] = values[prop] === 'true' || parseInt(values[prop], 10) === 1;
}
});
return values;
}

function createPayload(values) {
const payload = {
description: values.description || '',
icon: values.icon || '',
labelColor: values.labelColor || '#000000',
textColor: values.textColor || '#ffffff',
};

if (values.hasOwnProperty('userTitle')) {
payload.userTitle = values.userTitle || '';
}

if (values.hasOwnProperty('userTitleEnabled')) {
payload.userTitleEnabled = values.userTitleEnabled ? '1' : '0';
}
const booleanFields = [
'userTitleEnabled',
'hidden',
'private',
'disableJoinRequests',
'disableLeave',
];

if (values.hasOwnProperty('hidden')) {
payload.hidden = values.hidden ? '1' : '0';
}
booleanFields.forEach((field) => {
if (values.hasOwnProperty(field)) {
payload[field] = values[field] ? '1' : '0';
}
});

if (values.hasOwnProperty('private')) {
payload.private = values.private ? '1' : '0';
if (values.hasOwnProperty('userTitle')) {
payload.userTitle = values.userTitle || '';
}

if (values.hasOwnProperty('disableJoinRequests')) {
payload.disableJoinRequests = values.disableJoinRequests ? '1' : '0';
}
return payload;
}

if (values.hasOwnProperty('disableLeave')) {
payload.disableLeave = values.disableLeave ? '1' : '0';
}

async function handleSpecialProperties(groupName, values, payload) {
if (values.hasOwnProperty('name')) {
await checkNameChange(groupName, values.name);
}
Expand All @@ -80,15 +107,7 @@ module.exports = function (Groups) {
const cidsArray = values.memberPostCids.split(',').map(cid => parseInt(cid.trim(), 10)).filter(Boolean);
payload.memberPostCids = cidsArray.filter(cid => validCids.includes(cid)).join(',') || '';
}

await db.setObject(`group:${groupName}`, payload);
await Groups.renameGroup(groupName, values.name);

plugins.hooks.fire('action:group.update', {
name: groupName,
values: values,
});
};
}

async function updateVisibility(groupName, hidden) {
if (hidden) {
Expand Down
Loading

0 comments on commit 6ed0e5d

Please sign in to comment.