feat: message channel ban

This commit is contained in:
cha0s 2020-12-13 16:36:30 -06:00
parent de52716bbb
commit 8ad0b2ba1a
5 changed files with 62 additions and 0 deletions

View File

@ -0,0 +1,10 @@
import {useEffect} from 'react';
export default function useEvent(element, event, handler, passive = false) {
useEffect(() => {
element.addEventListener(event, handler, passive);
return () => {
element.removeEventListener(event, handler);
};
});
}

View File

@ -3,6 +3,7 @@ import Join from '../packets/join';
import Leave from '../packets/leave';
import Message from '../packets/message';
import MessageSiteBan from '../packets/message-site-ban';
import MessageChannelBan from '../packets/message-channel-ban';
import MessageDistinction from '../packets/message-distinction';
import chat from './state';
@ -17,6 +18,7 @@ export default {
Join: Join(latus),
Leave: Leave(latus),
Message: Message(latus),
MessageChannelBan: MessageChannelBan(latus),
MessageSiteBan: MessageSiteBan(latus),
MessageDistinction: MessageDistinction(latus),
}),

View File

@ -14,6 +14,7 @@ import Join from './packets/join.server';
import Leave from './packets/leave.server';
import Message from './packets/message.server';
import MessageDistinction from './packets/message-distinction.server';
import MessageChannelBan from './packets/message-channel-ban.server';
import MessageSiteBan from './packets/message-site-ban.server';
import ensureCanonical from './ensure-canonical';
@ -68,6 +69,7 @@ export default {
Leave: Leave(latus),
Message: Message(latus),
MessageDistinction: MessageDistinction(latus),
MessageChannelBan: MessageChannelBan(latus),
MessageSiteBan: MessageSiteBan(latus),
}),
'@latus/socket/connect': async (socket, latus) => {

View File

@ -0,0 +1,20 @@
import {Packet, ValidationError} from '@latus/socket/packets';
import {validate} from 'uuid';
export default () => class MessageChannelBan extends Packet {
static get data() {
return 'string';
}
static async validate({data: uuid}, {req}) {
const {user} = req;
if (!user || !user.isAdmin) {
throw new ValidationError({code: 400, reason: 'unauthorized'});
}
if (!validate(uuid)) {
throw new ValidationError({code: 400, reason: 'malformed'});
}
}
};

View File

@ -0,0 +1,28 @@
import {promisify} from 'util';
import {ModelMap} from '@latus/db';
import {createClient, keys} from '@latus/redis';
import {renderChannel} from '@reddichat/core';
import messageChannel from '../message-channel';
import MessageChannelBan from './message-channel-ban';
export default (latus) => class MessageChannelBanServer extends MessageChannelBan() {
static async respond({data: uuid}, socket) {
const {User} = ModelMap(latus);
const redisClient = createClient(latus);
const get = promisify(redisClient.get.bind(redisClient));
const key = (await keys(redisClient, `*:messages:${uuid}`)).pop();
const {owner} = JSON.parse((await get(key)));
if (0 === owner) {
return;
}
const channel = await messageChannel(latus, uuid);
const user = await User.findByPk(owner);
await user.addBannedFrom(renderChannel(channel));
await user.save();
socket.to(`/u/${owner}`).send(['Refresh']);
}
};