1
0
mirror of https://github.com/hack-chat/main.git synced 2024-03-22 13:20:33 +08:00
hack-chat-main/server/src/commands/core/invite.js

83 lines
2.1 KiB
JavaScript
Raw Normal View History

2018-03-10 15:47:00 +08:00
/*
Description: Generates a semi-unique channel name then broadcasts it to each client
2018-03-10 15:47:00 +08:00
*/
// module support functions
/**
* Returns the channel that should be invited to.
* @param {any} channel
* @return {string}
*/
export function getChannel (channel=undefined) {
if (typeof channel === 'string') {
return channel;
} else {
return Math.random().toString(36).substr(2, 8);
}
}
// module main
2019-11-07 15:35:23 +08:00
export async function run(core, server, socket, data) {
2018-06-04 15:07:24 +08:00
// check for spam
2019-11-07 15:35:23 +08:00
if (server.police.frisk(socket.address, 2)) {
return server.reply({
2020-09-08 12:51:47 +08:00
cmd: 'warn', // @todo Remove english and change to numeric id
2019-11-07 15:35:23 +08:00
text: 'You are sending invites too fast. Wait a moment before trying again.',
2018-04-29 13:29:38 +08:00
}, socket);
}
2018-06-04 15:07:24 +08:00
// verify user input
2020-09-08 12:51:47 +08:00
if (typeof data.userid !== 'number' || typeof data.channel !== 'string') {
2019-11-07 15:35:23 +08:00
return true;
2018-03-10 15:47:00 +08:00
}
2018-06-04 15:07:24 +08:00
2020-09-08 12:51:47 +08:00
// why would you invite yourself?
if (data.userid === socket.userid) {
return true;
}
2018-03-10 15:47:00 +08:00
2020-09-08 12:51:47 +08:00
// @todo Verify this socket is part of data.channel - multichannel patch
// find target user
let targetClient = server.findSockets({ channel: data.channel, userid: data.userid });
2020-09-08 12:51:47 +08:00
if (targetClient.length === 0) {
return server.reply({
2020-09-08 12:51:47 +08:00
cmd: 'warn', // @todo Remove english and change to numeric id
text: 'Could not find user in that channel',
2018-03-10 15:47:00 +08:00
}, socket);
}
2020-09-08 12:51:47 +08:00
[targetClient] = targetClient;
// generate common channel
const channel = getChannel(data.to);
// build invite
const payload = {
cmd: 'invite',
channel: socket.channel,
from: socket.userid,
to: data.userid,
inviteChannel: channel,
};
// send invite notice to target client
server.reply(payload, targetClient);
// send invite notice to this client
server.reply(payload, socket);
2018-03-10 15:47:00 +08:00
2018-06-04 15:07:24 +08:00
// stats are fun
2019-03-19 14:36:21 +08:00
core.stats.increment('invites-sent');
2018-03-10 15:47:00 +08:00
2019-11-07 15:35:23 +08:00
return true;
}
2020-09-08 12:51:47 +08:00
export const requiredData = [];//['nick'];
2019-11-07 15:35:23 +08:00
export const info = {
2018-05-13 18:33:22 +08:00
name: 'invite',
description: 'Sends an invite to the target client with the provided channel, or a random channel.',
usage: `
API: { cmd: 'invite', nick: '<target nickname>', to: '<optional destination channel>' }`,
2018-06-04 15:07:24 +08:00
};