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),
privileges.global.can('chat', uid),
checkReputation(uid),
user.checkMuted(uid),
]);
if (!roomData) {
throw new Error('[[error:no-room]]');

@ -13,6 +13,20 @@ module.exports = function (User) {
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) {
if (parseInt(uid, 10) === 0) {
return;
@ -30,17 +44,9 @@ module.exports = function (User) {
return;
}
const now = Date.now();
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)}]]`);
}
}
await User.checkMuted(uid);
const now = Date.now();
if (now - userData.joindate < meta.config.initialPostDelay * 1000) {
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', () => {

Loading…
Cancel
Save