feat: check mute when messaging, closes #11932

isekai-main
Barış Soner Uşaklı 1 year ago
parent 8b533d5467
commit 9534f9b9b9

@ -380,6 +380,7 @@ Messaging.canMessageRoom = async (uid, roomId) => {
Messaging.isUserInRoom(uid, roomId), Messaging.isUserInRoom(uid, roomId),
privileges.global.can('chat', uid), privileges.global.can('chat', uid),
checkReputation(uid), checkReputation(uid),
user.checkMuted(uid),
]); ]);
if (!roomData) { if (!roomData) {
throw new Error('[[error:no-room]]'); throw new Error('[[error:no-room]]');

@ -13,6 +13,20 @@ module.exports = function (User) {
await isReady(uid, cid, 'lastqueuetime'); await isReady(uid, cid, 'lastqueuetime');
}; };
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)}]]`);
}
}
};
async function isReady(uid, cid, field) { async function isReady(uid, cid, field) {
if (parseInt(uid, 10) === 0) { if (parseInt(uid, 10) === 0) {
return; return;
@ -30,17 +44,9 @@ module.exports = function (User) {
return; return;
} }
const now = Date.now(); await User.checkMuted(uid);
if (userData.mutedUntil > now) {
let muteLeft = ((userData.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 now = Date.now();
if (now - userData.joindate < meta.config.initialPostDelay * 1000) { if (now - userData.joindate < meta.config.initialPostDelay * 1000) {
throw new Error(`[[error:user-too-new, ${meta.config.initialPostDelay}]]`); throw new Error(`[[error:user-too-new, ${meta.config.initialPostDelay}]]`);
} }

@ -113,6 +113,28 @@ describe('Messaging Library', () => {
}); });
}); });
}); });
it('should not allow messaging room if user is muted', async () => {
const twoMinutesFromNow = Date.now() + (2 * 60 * 1000);
const twoHoursFromNow = Date.now() + (2 * 60 * 60 * 1000);
const roomId = 0;
await User.setUserField(mocks.users.herp.uid, 'mutedUntil', twoMinutesFromNow);
await assert.rejects(Messaging.canMessageRoom(mocks.users.herp.uid, roomId), (err) => {
assert(err.message.startsWith('[[error:user-muted-for-minutes,'));
return true;
});
await User.setUserField(mocks.users.herp.uid, 'mutedUntil', twoHoursFromNow);
await assert.rejects(Messaging.canMessageRoom(mocks.users.herp.uid, roomId), (err) => {
assert(err.message.startsWith('[[error:user-muted-for-hours,'));
return true;
});
await db.deleteObjectField(`user:${mocks.users.herp.uid}`, 'mutedUntil');
await assert.rejects(Messaging.canMessageRoom(mocks.users.herp.uid, roomId), {
message: '[[error:no-room]]',
});
});
}); });
describe('rooms', () => { describe('rooms', () => {

Loading…
Cancel
Save