2018-03-10 15:47:00 +08:00
|
|
|
/*
|
2018-03-14 13:26:53 +08:00
|
|
|
Description: Generates a semi-unique channel name then broadcasts it to each client
|
2018-03-10 15:47:00 +08:00
|
|
|
*/
|
|
|
|
|
2020-03-07 07:12:19 +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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-30 14:44:36 +08:00
|
|
|
// 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)) {
|
2018-09-30 14:44:36 +08:00
|
|
|
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 });
|
2018-09-30 14:44:36 +08:00
|
|
|
|
2020-09-08 12:51:47 +08:00
|
|
|
if (targetClient.length === 0) {
|
2018-09-30 14:44:36 +08:00
|
|
|
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',
|
2020-02-17 11:18:13 +08:00
|
|
|
description: 'Sends an invite to the target client with the provided channel, or a random channel.',
|
2018-09-30 14:44:36 +08:00
|
|
|
usage: `
|
2020-02-17 11:18:13 +08:00
|
|
|
API: { cmd: 'invite', nick: '<target nickname>', to: '<optional destination channel>' }`,
|
2018-06-04 15:07:24 +08:00
|
|
|
};
|