Node.js based forum software built for the modern web
refactor: add muted field to user
Barış Soner Uşaklı2f3b3ecfd912
diff --git a/public/language/en-GB/error.json b/public/language/en-GB/error.jsonindex 95c4931165..925d4c7489 100644--- a/public/language/en-GB/error.json+++ b/public/language/en-GB/error.json@@ -139,6 +139,7 @@"cant-mute-other-admins": "You can't mute other admins!","user-muted-for-hours": "You have been muted, you will be able to post in %1 hour(s)","user-muted-for-minutes": "You have been muted, you will be able to post in %1 minute(s)",+ "user-muted-indefinitely": "You have been muted indefinitely","cant-make-banned-users-admin": "You can't make banned users admin.","cant-remove-last-admin": "You are the only administrator. Add another user as an administrator before removing yourself as admin","account-deletion-disabled": "Account deletion is disabled",diff --git a/src/api/users.js b/src/api/users.jsindex c52c78dc51..e335c62e95 100644--- a/src/api/users.js+++ b/src/api/users.js@@ -263,6 +263,7 @@ usersAPI.mute = async function (caller, data) {}const reason = data.reason || '[[user:info.muted-no-reason]]';await db.setObject(`user:${data.uid}`, {+ muted: 1,mutedUntil: data.until,mutedReason: reason,});@@ -304,7 +305,7 @@ usersAPI.unmute = async function (caller, data) {throw new Error('[[error:no-privileges]]');}- await db.deleteObjectFields(`user:${data.uid}`, ['mutedUntil', 'mutedReason']);+ await db.deleteObjectFields(`user:${data.uid}`, ['muted', 'mutedUntil', 'mutedReason']);const now = Date.now();const unmuteKey = `uid:${data.uid}:unmute:${now}`;const unmuteData = {diff --git a/src/controllers/admin/users.js b/src/controllers/admin/users.jsindex 0c8bf960fd..316473459a 100644--- a/src/controllers/admin/users.js+++ b/src/controllers/admin/users.js@@ -15,7 +15,7 @@ const usersController = module.exports;const userFields = ['uid', 'username', 'userslug', 'email', 'postcount', 'joindate', 'banned',- 'mutedUntil', 'reputation', 'picture', 'flags', 'lastonline', 'email:confirmed',+ 'muted', 'mutedUntil', 'reputation', 'picture', 'flags', 'lastonline', 'email:confirmed',];usersController.index = async function (req, res) {diff --git a/src/posts/user.js b/src/posts/user.jsindex cdb9836bdb..05bc6d49d4 100644--- a/src/posts/user.js+++ b/src/posts/user.js@@ -105,7 +105,7 @@ module.exports = function (Posts) {'uid', 'username', 'fullname', 'userslug','reputation', 'postcount', 'topiccount', 'picture','signature', 'banned', 'banned:expire', 'status',- 'lastonline', 'groupTitle', 'mutedUntil',+ 'lastonline', 'groupTitle', 'muted', 'mutedUntil',];const result = await plugins.hooks.fire('filter:posts.addUserFields', {fields: fields,diff --git a/src/upgrades/4.12.0/muted_users_zset.js b/src/upgrades/4.12.0/muted_users_zset.jsindex bd94f56a36..edf90b17e7 100644--- a/src/upgrades/4.12.0/muted_users_zset.js+++ b/src/upgrades/4.12.0/muted_users_zset.js@@ -11,11 +11,13 @@ module.exports = {await batch.processSortedSet('users:joindate', async (uids) => {const userData = await db.getObjects(uids.map(uid => `user:${uid}`));const now = Date.now();- const bulkAdd = userData.filter(+ const mutedUserData = userData.filter(u => u && u.mutedReason && (u.mutedUntil > now || parseInt(u.mutedUntil, 10) === 0)- )- .map(u => (['users:muted', now, u.uid]));+ );+ const bulkAdd = mutedUserData.map(u => (['users:muted', now, u.uid]));+ const bulkSet = mutedUserData.map(u => ([`user:${u.uid}`, { muted: 1 }]));+ await db.setObjectBulk(bulkSet);await db.sortedSetAddBulk(bulkAdd);progress.incr(uids.length);}, {diff --git a/src/user/data.js b/src/user/data.jsindex 876c2dda13..26b3a3a144 100644--- a/src/user/data.js+++ b/src/user/data.js@@ -20,7 +20,7 @@ const intFields = ['uid', 'postcount', 'topiccount', 'reputation', 'profileviews','banned', 'banned:expire', 'email:confirmed', 'joindate', 'lastonline','lastqueuetime', 'lastposttime', 'followingCount', 'followerCount',- 'blocksCount', 'passwordExpiry', 'mutedUntil', 'flags',+ 'blocksCount', 'passwordExpiry', 'muted', 'mutedUntil', 'flags',];module.exports = function (User) {@@ -30,7 +30,7 @@ module.exports = function (User) {'aboutme', 'signature', 'uploadedpicture', 'profileviews', 'reputation','postcount', 'topiccount', 'lastposttime', 'banned', 'banned:expire','status', 'flags', 'followerCount', 'followingCount', 'cover:url',- 'cover:position', 'groupTitle', 'mutedUntil', 'mutedReason',+ 'cover:position', 'groupTitle', 'muted', 'mutedUntil', 'mutedReason',];let customFieldWhiteList = null;@@ -160,7 +160,8 @@ module.exports = function (User) {banned: 'banned:expire','banned:expire': 'banned',username: 'fullname',- mutedUntil: 'mutedReason',+ muted: 'mutedUntil',+ mutedUntil: 'muted',};for (const [key, field] of Object.entries(requiredFields)) {if (fields.includes(key) && !fields.includes(field)) {@@ -313,9 +314,10 @@ module.exports = function (User) {user.lastonlineISO = utils.toISOString(user.lastonline) || user.joindateISO;}- if (user.hasOwnProperty('mutedUntil')) {- user.muted = Boolean(user.mutedReason && (user.mutedUntil > Date.now() || user.mutedUntil === 0));- const unmute = !user.muted && user.mutedReason && user.mutedUntil && user.mutedUntil <= Date.now();+ if (user.hasOwnProperty('muted') && user.hasOwnProperty('mutedUntil')) {+ const isMuted = Boolean(user.muted);+ user.muted = Boolean(user.muted && (user.mutedUntil > Date.now() || user.mutedUntil === 0));+ const unmute = !user.muted && isMuted && user.mutedUntil && user.mutedUntil <= Date.now();if (unmute) {unmuteUids.push(user.uid);}diff --git a/src/user/posts.js b/src/user/posts.jsindex c8be6f81cd..c519e308c6 100644--- a/src/user/posts.js+++ b/src/user/posts.js@@ -18,14 +18,19 @@ module.exports = function (User) {User.checkMuted = async function (uid) {const now = Date.now();- const mutedUntil = await User.getUserField(uid, 'mutedUntil');- if (mutedUntil > now) {- let muteLeft = ((mutedUntil - now) / (1000 * 60));- if (muteLeft > 60) {- muteLeft = (muteLeft / 60).toFixed(0);- throw new Error(`[[error:user-muted-for-hours, ${muteLeft}]]`);- } else {- throw new Error(`[[error:user-muted-for-minutes, ${muteLeft.toFixed(0)}]]`);+ const { muted, mutedUntil } = await User.getUserFields(uid, ['muted', 'mutedUntil']);+ if (muted) {+ if (mutedUntil === 0) {+ throw new Error('[[error:user-muted-indefinitely]]');+ }+ if (mutedUntil > now) {+ let muteLeft = ((mutedUntil - now) / (1000 * 60));+ if (muteLeft > 60) {+ muteLeft = (muteLeft / 60).toFixed(0);+ throw new Error(`[[error:user-muted-for-hours, ${muteLeft}]]`);+ } else {+ throw new Error(`[[error:user-muted-for-minutes, ${muteLeft.toFixed(0)}]]`);+ }}}};diff --git a/test/messaging.js b/test/messaging.jsindex 99e382c27a..acfa66af14 100644--- a/test/messaging.js+++ b/test/messaging.js@@ -112,12 +112,13 @@ describe('Messaging Library', () => {);});- it('should not allow messaging room if user is muted', async () => {+ it('should not allow messaging room if user is muted temporarily or permanently', async () => {const twoMinutesFromNow = Date.now() + (2 * 60 * 1000);const twoHoursFromNow = Date.now() + (2 * 60 * 60 * 1000);const roomId = 0;await User.setUserFields(mocks.users.herp.uid, {+ muted: 1,mutedUntil: twoMinutesFromNow,mutedReason: 'no reason',});@@ -127,6 +128,7 @@ describe('Messaging Library', () => {});await User.setUserFields(mocks.users.herp.uid, {+ muted: 1,mutedUntil: twoHoursFromNow,mutedReason: 'no reason',});@@ -134,7 +136,19 @@ describe('Messaging Library', () => {assert(err.message.startsWith('[[error:user-muted-for-hours,'));return true;});- await db.deleteObjectFields(`user:${mocks.users.herp.uid}`, ['mutedUntil', 'mutedReason']);++ await User.setUserFields(mocks.users.herp.uid, {+ muted: 1,+ mutedUntil: 0,+ mutedReason: 'no reason',+ });+ await assert.rejects(Messaging.canMessageRoom(mocks.users.herp.uid, roomId), {+ message: '[[error:user-muted-indefinitely]]',+ });++ await db.deleteObjectFields(`user:${mocks.users.herp.uid}`, [+ 'muted', 'mutedUntil', 'mutedReason',+ ]);await assert.rejects(Messaging.canMessageRoom(mocks.users.herp.uid, roomId), {message: '[[error:no-room]]',});