mirror of
https://github.com/sudoxnym/habitica.git
synced 2026-07-14 07:52:15 +00:00
Added mute end date (#10566)
* Added mute end date * Added indefinite mute for users using slurs * Fixed user reload. Added no longer muted message. Added format for date * Fixed lint
This commit is contained in:
parent
2c921609c1
commit
2a7dfff88a
6 changed files with 90 additions and 13 deletions
|
|
@ -1,3 +1,6 @@
|
|||
import { IncomingWebhook } from '@slack/client';
|
||||
import nconf from 'nconf';
|
||||
import moment from 'moment';
|
||||
import {
|
||||
createAndPopulateGroup,
|
||||
generateUser,
|
||||
|
|
@ -15,8 +18,6 @@ import { getMatchesByWordArray } from '../../../../../website/server/libs/string
|
|||
import bannedWords from '../../../../../website/server/libs/bannedWords';
|
||||
import guildsAllowingBannedWords from '../../../../../website/server/libs/guildsAllowingBannedWords';
|
||||
import * as email from '../../../../../website/server/libs/email';
|
||||
import { IncomingWebhook } from '@slack/client';
|
||||
import nconf from 'nconf';
|
||||
|
||||
const BASE_URL = nconf.get('BASE_URL');
|
||||
|
||||
|
|
@ -80,12 +81,40 @@ describe('POST /chat', () => {
|
|||
});
|
||||
});
|
||||
|
||||
it('returns an error when chat privileges are revoked when sending a message to a public guild', async () => {
|
||||
let userWithChatRevoked = await member.update({'flags.chatRevoked': true});
|
||||
await expect(userWithChatRevoked.post(`/groups/${groupWithChat._id}/chat`, { message: testMessage})).to.eventually.be.rejected.and.eql({
|
||||
code: 401,
|
||||
error: 'NotAuthorized',
|
||||
message: t('chatPrivilegesRevoked'),
|
||||
describe('mute user', () => {
|
||||
it('returns an error when chat privileges are revoked when sending a message to a public guild', async () => {
|
||||
const userWithChatRevoked = await member.update({'flags.chatRevoked': true});
|
||||
await expect(userWithChatRevoked.post(`/groups/${groupWithChat._id}/chat`, { message: testMessage})).to.eventually.be.rejected.and.eql({
|
||||
code: 401,
|
||||
error: 'NotAuthorized',
|
||||
message: t('chatPrivilegesRevoked'),
|
||||
});
|
||||
});
|
||||
|
||||
it('returns an error when user is muted with date', async () => {
|
||||
const userWithChatRevoked = await member.update({
|
||||
'flags.chatRevoked': true,
|
||||
'flags.chatRevokedEndDate': moment().add(1, 'days').toDate(),
|
||||
});
|
||||
|
||||
await expect(userWithChatRevoked.post(`/groups/${groupWithChat._id}/chat`, { message: testMessage})).to.eventually.be.rejected.and.eql({
|
||||
code: 401,
|
||||
error: 'NotAuthorized',
|
||||
message: t('chatPrivilegesRevoked'),
|
||||
});
|
||||
});
|
||||
|
||||
it('allows a user to chat after revoked time has passed', async () => {
|
||||
const userWithChatRevoked = await member.update({
|
||||
'flags.chatRevoked': true,
|
||||
'flags.chatRevokedEndDate': moment().subtract(1, 'days').toDate(),
|
||||
});
|
||||
|
||||
const newMessage = await userWithChatRevoked.post(`/groups/${groupWithChat._id}/chat`, { message: testMessage});
|
||||
const groupMessages = await userWithChatRevoked.get(`/groups/${groupWithChat._id}/chat`);
|
||||
|
||||
expect(newMessage.message.id).to.exist;
|
||||
expect(groupMessages[0].id).to.exist;
|
||||
});
|
||||
});
|
||||
|
||||
|
|
@ -273,6 +302,7 @@ describe('POST /chat', () => {
|
|||
message: t('chatPrivilegesRevoked'),
|
||||
});
|
||||
|
||||
// @TODO: The next test should not depend on this. We should reset the user test in a beforeEach
|
||||
// Restore chat privileges to continue testing
|
||||
user.flags.chatRevoked = false;
|
||||
await user.update({'flags.chatRevoked': false});
|
||||
|
|
|
|||
|
|
@ -55,10 +55,21 @@
|
|||
div(v-if="expandAuth")
|
||||
pre {{hero.auth}}
|
||||
.form-group
|
||||
h5 User Mute Settings
|
||||
.checkbox
|
||||
label
|
||||
input(type='checkbox', v-if='hero.flags', v-model='hero.flags.chatRevoked')
|
||||
| Chat Privileges Revoked
|
||||
strong Chat Privileges Revoked
|
||||
div(v-if='hero.flags.chatRevoked && hero.flags.chatRevokedEndDate')
|
||||
strong User is currently muted until
|
||||
br
|
||||
div {{ userRevokedEndDate(hero) }}
|
||||
div(v-else-if='hero.flags.chatRevoked')
|
||||
strong User is currently muted indefinitely
|
||||
div
|
||||
strong For how many days from today do you want to mute this user? Leave as 0 for indefinite.
|
||||
br
|
||||
input(type='number', v-model='numberOfDaysToMute')
|
||||
.form-group
|
||||
.checkbox
|
||||
label
|
||||
|
|
@ -103,7 +114,7 @@
|
|||
</style>
|
||||
|
||||
<script>
|
||||
// import keys from 'lodash/keys';
|
||||
import moment from 'moment';
|
||||
import each from 'lodash/each';
|
||||
|
||||
import markdownDirective from 'client/directives/markdown';
|
||||
|
|
@ -132,6 +143,7 @@ export default {
|
|||
gear,
|
||||
expandItems: false,
|
||||
expandAuth: false,
|
||||
numberOfDaysToMute: 0,
|
||||
};
|
||||
},
|
||||
directives: {
|
||||
|
|
@ -144,6 +156,10 @@ export default {
|
|||
...mapState({user: 'user.data'}),
|
||||
},
|
||||
methods: {
|
||||
userRevokedEndDate (hero) {
|
||||
if (moment().isAfter(moment(hero.flags.chatRevokedEndDate))) return 'User is no longer muted';
|
||||
return moment(hero.flags.chatRevokedEndDate).format(this.user.preferences.dateFormat.toUpperCase());
|
||||
},
|
||||
getAllItemPaths () {
|
||||
// let questsFormat = this.getFormattedItemReference('items.quests', keys(this.quests), 'Numeric Quantity');
|
||||
// let mountsFormat = this.getFormattedItemReference('items.mounts', keys(this.mountInfo), 'Boolean');
|
||||
|
|
@ -174,7 +190,7 @@ export default {
|
|||
async loadHero (uuid, heroIndex) {
|
||||
this.currentHeroIndex = heroIndex;
|
||||
let hero = await this.$store.dispatch('hall:getHero', { uuid });
|
||||
this.hero = Object.assign({}, this.hero, hero);
|
||||
this.hero = Object.assign({}, hero);
|
||||
if (!this.hero.flags) {
|
||||
this.hero.flags = {
|
||||
chatRevoked: false,
|
||||
|
|
@ -184,6 +200,11 @@ export default {
|
|||
this.expandAuth = false;
|
||||
},
|
||||
async saveHero () {
|
||||
if (this.numberOfDaysToMute) {
|
||||
const dayToEndMute = moment().add(this.numberOfDaysToMute, 'days').utc().toDate();
|
||||
this.hero.flags.chatRevokedEndDate = dayToEndMute;
|
||||
}
|
||||
|
||||
this.hero.contributor.admin = this.hero.contributor.level > 7 ? true : false;
|
||||
let heroUpdated = await this.$store.dispatch('hall:updateHero', { heroDetails: this.hero });
|
||||
this.text('User updated');
|
||||
|
|
@ -191,6 +212,7 @@ export default {
|
|||
this.heroID = -1;
|
||||
this.heroes[this.currentHeroIndex] = heroUpdated;
|
||||
this.currentHeroIndex = -1;
|
||||
this.numberOfDaysToMute = 0;
|
||||
},
|
||||
populateContributorInput (id, index) {
|
||||
this.heroID = id;
|
||||
|
|
|
|||
|
|
@ -12,6 +12,7 @@ import { getUserInfo, getGroupUrl, sendTxn } from '../../libs/email';
|
|||
import slack from '../../libs/slack';
|
||||
import pusher from '../../libs/pusher';
|
||||
import { getAuthorEmailFromMessage } from '../../libs/chat';
|
||||
import { userIsMuted, muteUserForLife } from '../../libs/chat/mute';
|
||||
import { chatReporterFactory } from '../../libs/chatReporting/chatReporterFactory';
|
||||
import nconf from 'nconf';
|
||||
import bannedWords from '../../libs/bannedWords';
|
||||
|
|
@ -124,6 +125,7 @@ api.postChat = {
|
|||
if (textContainsBannedSlur(req.body.message)) {
|
||||
let message = req.body.message;
|
||||
user.flags.chatRevoked = true;
|
||||
muteUserForLife(user);
|
||||
await user.save();
|
||||
|
||||
// Email the mods
|
||||
|
|
@ -159,7 +161,8 @@ api.postChat = {
|
|||
}
|
||||
|
||||
if (!group) throw new NotFound(res.t('groupNotFound'));
|
||||
if (group.privacy !== 'private' && user.flags.chatRevoked) {
|
||||
|
||||
if (group.privacy !== 'private' && userIsMuted(user)) {
|
||||
throw new NotAuthorized(res.t('chatPrivilegesRevoked'));
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -143,7 +143,7 @@ api.getHeroes = {
|
|||
// Note, while the following routes are called getHero / updateHero
|
||||
// they can be used by admins to get/update any user
|
||||
|
||||
const heroAdminFields = 'contributor balance profile.name purchased items auth flags.chatRevoked';
|
||||
const heroAdminFields = 'contributor balance profile.name purchased items auth flags.chatRevoked flags.chatRevokedEndDate';
|
||||
|
||||
/**
|
||||
* @api {get} /api/v3/hall/heroes/:heroId Get any user ("hero") given the UUID
|
||||
|
|
@ -273,7 +273,9 @@ api.updateHero = {
|
|||
if (updateData.auth && updateData.auth.blocked === false) {
|
||||
hero.auth.blocked = false;
|
||||
}
|
||||
|
||||
if (updateData.flags && _.isBoolean(updateData.flags.chatRevoked)) hero.flags.chatRevoked = updateData.flags.chatRevoked;
|
||||
if (updateData.flags && updateData.flags.chatRevokedEndDate) hero.flags.chatRevokedEndDate = updateData.flags.chatRevokedEndDate;
|
||||
|
||||
let savedHero = await hero.save();
|
||||
let heroJSON = savedHero.toJSON();
|
||||
|
|
|
|||
19
website/server/libs/chat/mute.js
Normal file
19
website/server/libs/chat/mute.js
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
import moment from 'moment';
|
||||
|
||||
function userIsMuted (user) {
|
||||
if (!user.flags.chatRevoked) return false;
|
||||
|
||||
// User is muted indefinitely
|
||||
if (!user.flags.chatRevokedEndDate) return true;
|
||||
|
||||
return moment(user.flags.chatRevokedEndDate).isAfter(moment());
|
||||
}
|
||||
|
||||
function muteUserForLife (user) {
|
||||
user.flags.chatRevokedEndDate = moment().add(1000, 'years').toDate();
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
userIsMuted,
|
||||
muteUserForLife,
|
||||
};
|
||||
|
|
@ -223,6 +223,7 @@ let schema = new Schema({
|
|||
return {};
|
||||
}},
|
||||
chatRevoked: Boolean,
|
||||
chatRevokedEndDate: Date,
|
||||
// Used to track the status of recapture emails sent to each user,
|
||||
// can be 0 - no email sent - 1, 2, 3 or 4 - 4 means no more email will be sent to the user
|
||||
recaptureEmailsPhase: {type: Number, default: 0},
|
||||
|
|
|
|||
Loading…
Reference in a new issue