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 code to reduce cognitive complexity from 16 to 15 in src/user/settings.js #46

Open
wants to merge 4 commits into
base: f24
Choose a base branch
from
Open
Show file tree
Hide file tree
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
Binary file added dump.rdb
Binary file not shown.
47 changes: 25 additions & 22 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,34 @@ module.exports = function (User) {
return defaultValue;
}

// Co-pilot assisted code
function validatePaginationValue(value, max) {
console.log('pagination is saved');
if (!value || parseInt(value, 10) <= 1 || parseInt(value, 10) > max) {
throw new Error(`[[error:invalid-pagination-value, 2, ${max}]]`);
}
}

// Co-pilot assisted code
function validateLanguage(value, languageCodes) {
console.log('language is saved');
if (value && !languageCodes.includes(value)) {
throw new Error('[[error:invalid-language]]');
}
}

// Co-pilot assisted code
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 +150,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 Down