Skip to content

Commit

Permalink
made new functions in settings to make code complexity go from 16 to 15
Browse files Browse the repository at this point in the history
  • Loading branch information
Sarim Faraz committed Sep 2, 2024
1 parent 09ad0fe commit 089bf2f
Showing 1 changed file with 21 additions and 23 deletions.
44 changes: 21 additions & 23 deletions src/user/settings.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@

'use strict';

const validator = require('validator');

const meta = require('../meta');
const db = require('../database');
const plugins = require('../plugins');
Expand All @@ -16,6 +14,7 @@ module.exports = function (User) {
postsPerPage: 20,
topicsPerPage: 20,
};

User.getSettings = async function (uid) {
if (parseInt(uid, 10) <= 0) {
const isSpider = parseInt(uid, 10) === -1;
Expand Down Expand Up @@ -96,32 +95,29 @@ module.exports = function (User) {
return defaultValue;
}

function validatePaginationValue(value, max) {
if (!value || parseInt(value, 10) <= 1 || parseInt(value, 10) > max) {
throw new Error(`[[error:invalid-pagination-value, 2, ${max}]]`);
}
}

function validateLanguage(value, languageCodes) {
if (value && !languageCodes.includes(value)) {
throw new Error('[[error:invalid-language]]');
}
}

User.saveSettings = async function (uid, data) {
const maxPostsPerPage = meta.config.maxPostsPerPage || 20;
if (
!data.postsPerPage ||
parseInt(data.postsPerPage, 10) <= 1 ||
parseInt(data.postsPerPage, 10) > maxPostsPerPage
) {
throw new Error(`[[error:invalid-pagination-value, 2, ${maxPostsPerPage}]]`);
}
validatePaginationValue(data.postsPerPage, maxPostsPerPage);

const maxTopicsPerPage = meta.config.maxTopicsPerPage || 20;
if (
!data.topicsPerPage ||
parseInt(data.topicsPerPage, 10) <= 1 ||
parseInt(data.topicsPerPage, 10) > maxTopicsPerPage
) {
throw new Error(`[[error:invalid-pagination-value, 2, ${maxTopicsPerPage}]]`);
}
validatePaginationValue(data.topicsPerPage, maxTopicsPerPage);

const languageCodes = await languages.listCodes();
if (data.userLang && !languageCodes.includes(data.userLang)) {
throw new Error('[[error:invalid-language]]');
}
if (data.acpLang && !languageCodes.includes(data.acpLang)) {
throw new Error('[[error:invalid-language]]');
}
validateLanguage(data.userLang, languageCodes);
validateLanguage(data.acpLang, languageCodes);

data.userLang = data.userLang || meta.config.defaultLang;

plugins.hooks.fire('action:user.saveSettings', { uid: uid, settings: data });
Expand Down Expand Up @@ -149,12 +145,14 @@ module.exports = function (User) {
categoryTopicSort: data.categoryTopicSort,
topicPostSort: data.topicPostSort,
};

const notificationTypes = await notifications.getAllNotificationTypes();
notificationTypes.forEach((notificationType) => {
if (data[notificationType]) {
settings[notificationType] = data[notificationType];
}
});

const result = await plugins.hooks.fire('filter:user.saveSettings', { uid: uid, settings: settings, data: data });
await db.setObject(`user:${uid}:settings`, result.settings);
await User.updateDigestSetting(uid, data.dailyDigestFreq);
Expand All @@ -175,4 +173,4 @@ module.exports = function (User) {

await db.setObjectField(`user:${uid}:settings`, key, value);
};
};
};

Check failure on line 176 in src/user/settings.js

View workflow job for this annotation

GitHub Actions / lint-and-test / test

Newline required at end of file but not found

0 comments on commit 089bf2f

Please sign in to comment.