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
Open
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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
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)
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.