Skip to content

Commit

Permalink
merge project1 changes to team repo
Browse files Browse the repository at this point in the history
  • Loading branch information
ariel0206qyx committed Sep 16, 2024
1 parent 8dd2c19 commit 30833b5
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 15 deletions.
25 changes: 10 additions & 15 deletions src/user/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,22 +98,10 @@ module.exports = function (User) {

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}]]`);
}

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}]]`);
}

validatePagination(data.postsPerPage, maxPostsPerPage);
validatePagination(data.topicsPerPage, maxTopicsPerPage);

const languageCodes = await languages.listCodes();
if (data.userLang && !languageCodes.includes(data.userLang)) {
Expand Down Expand Up @@ -161,6 +149,13 @@ module.exports = function (User) {
return await User.getSettings(uid);
};

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

User.updateDigestSetting = async function (uid, dailyDigestFreq) {
await db.sortedSetsRemove(['digest:day:uids', 'digest:week:uids', 'digest:month:uids'], uid);
if (['day', 'week', 'biweek', 'month'].includes(dailyDigestFreq)) {
Expand Down
22 changes: 22 additions & 0 deletions test/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -2698,4 +2698,26 @@ describe('User', () => {
});
});
});
describe('User.saveSettings', () => {
it('should throw an error if postsPerPage exceeds the maximum limit', async () => {
const mockUid = 'testUser';
const invalidData = { postsPerPage: 1000, topicsPerPage: 10 };
try {
await User.saveSettings(mockUid, invalidData);
} catch (error) {
assert.strictEqual(error.message, '[[error:invalid-pagination-value, 2, 20]]');
}
});

it('should throw an error if topicsPerPage is less than the minimum limit', async () => {
const mockUid = 'testUser';
const invalidData = { postsPerPage: 10, topicsPerPage: 0 };

try {
await User.saveSettings(mockUid, invalidData);
} catch (error) {
assert.strictEqual(error.message, '[[error:invalid-pagination-value, 2, 20]]');
}
});
});
});

0 comments on commit 30833b5

Please sign in to comment.