Skip to content

Refactoring: src/user/settings.js Function with many returns (count = 14), Function with high complexity (count = 23)#338

Open
RogerJinIS wants to merge 1 commit into
mainfrom
ruijianj/task-1-context
Open

Refactoring: src/user/settings.js Function with many returns (count = 14), Function with high complexity (count = 23)#338
RogerJinIS wants to merge 1 commit into
mainfrom
ruijianj/task-1-context

Conversation

@RogerJinIS

Copy link
Copy Markdown

Summary
This PR refactors [settings.js] to reduce [exports] function complexity and number of returns from cyclomatic analysis reports.

Original issue: Function with many returns (count = 14), Function with high complexity (count = 23) in [exports].
New structure splits logic to standalone internal helpers.
[module.exports] now only maps [User methods to those helpers (single linear assignment path).

What changed

  1. Extracted helper functions
    parseJSONSetting(value, defaultValue)
    getSetting(settings, key, defaultValue)
    normalizePageSetting(value, defaultValue, maxValue)
    validatePagination(value, maxValue)
    onSettingsLoaded(uid, settings)
    getSettings(uid)
    getMultipleUserSettings(uids)
    saveSettings(uid, data)
    updateDigestSetting(uid, dailyDigestFreq)
    setSetting(uid, key, value)
  2. Simplified module.exports
    Before:
    module.exports = function(User) { ... deep logic, conditionals, multiple returns ... }
    After:
    module.exports = function(User) {
    User.getSettings = getSettings;
    User.getMultipleUserSettings = getMultipleUserSettings;
    User.saveSettings = saveSettings;
    User.updateDigestSetting = updateDigestSetting;
    User.setSetting = setSetting;
    }

Behavior preserved
All existing public functions remain:
.getSettings
.getMultipleUserSettings
.saveSettings
.updateDigestSetting
.setSetting
Input validation and sanitization kept.
Notification type handling preserved.
activitypub and default / remote fallback logic intact.

Notes
This PR is a refactor-only change, no behavior contract changes.
Keep existing integration tests around to validate real-world user settings flow.
Next improvement: add a minor test for getSetting remote fallback plus normalizePageSetting clamps.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant