mirror of
https://github.com/hack-chat/main.git
synced 2024-03-22 13:20:33 +08:00
Merge pull request #93 from MinusGix/inviteRework
Abstract invite functions, and make dumb.js use them
This commit is contained in:
commit
033a6fbc4d
|
@ -4,6 +4,79 @@
|
||||||
|
|
||||||
import * as UAC from '../utility/UAC/_info';
|
import * as UAC from '../utility/UAC/_info';
|
||||||
|
|
||||||
|
// module support functions
|
||||||
|
/**
|
||||||
|
* Returns a message for if it's a valid nickname to invite. Returns null if there was no error.
|
||||||
|
* @param {any} nick
|
||||||
|
* @return {(string|null)}
|
||||||
|
*/
|
||||||
|
export function checkNickname (nick, inviterNick) {
|
||||||
|
if (typeof nick !== 'string' || !UAC.verifyNickname(nick)) {
|
||||||
|
return "Nickname was invalid.";
|
||||||
|
} else if (nick === inviterNick) {
|
||||||
|
return "Why would you invite yourself?";
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates the payload that a user who is being invited would receive.
|
||||||
|
* @param {string} inviter The user who is inviting them.
|
||||||
|
* @param {string} channel The channel they are being invited to.
|
||||||
|
* @return {Object}
|
||||||
|
*/
|
||||||
|
export function createRecipientPayload (inviter, channel) {
|
||||||
|
return {
|
||||||
|
cmd: 'info',
|
||||||
|
type: 'invite',
|
||||||
|
from: inviter,
|
||||||
|
text: `${inviter} invited you to ?${channel}`,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates the payload that a user who invited users (and succeeded) would receive.
|
||||||
|
* @param {string} nick The user who was invited.
|
||||||
|
* @param {string} channel The channel they were invited to.
|
||||||
|
*/
|
||||||
|
export function createSuccessPayload (nick, channel) {
|
||||||
|
return {
|
||||||
|
cmd: 'info',
|
||||||
|
type: 'invite',
|
||||||
|
invite: channel,
|
||||||
|
text: `You invited ${nick} to ?${channel}`,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sends the invites to the recipients.
|
||||||
|
* @param {MainServer} server The server. Required to broadcast the messages.
|
||||||
|
* @param {string} recipientNick The user who is being invited.
|
||||||
|
* @param {string} inviterNick The user who is doing the inviting.
|
||||||
|
* @param {string} originalChannel The channel they have in common, and where the invite is sent in.
|
||||||
|
* @param {string} inviteChannel The channel they are being invited to.
|
||||||
|
*/
|
||||||
|
export function sendInvite (server, recipientNick, inviterNick, originalChannel, inviteChannel) {
|
||||||
|
return server.broadcast(createRecipientPayload(inviterNick, inviteChannel), {
|
||||||
|
channel: originalChannel,
|
||||||
|
nick: recipientNick,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
// module main
|
// module main
|
||||||
export async function run(core, server, socket, data) {
|
export async function run(core, server, socket, data) {
|
||||||
// check for spam
|
// check for spam
|
||||||
|
@ -15,30 +88,19 @@ export async function run(core, server, socket, data) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// verify user input
|
// verify user input
|
||||||
if (typeof data.nick !== 'string' || !UAC.verifyNickname(data.nick)) {
|
const nickValid = checkNickname(data.nick, socket.nick);
|
||||||
|
if (nickValid !== null) {
|
||||||
|
server.reply({
|
||||||
|
cmd: 'warn',
|
||||||
|
text: nickValid,
|
||||||
|
}, socket);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
// why would you invite yourself?
|
const channel = getChannel(data.to);
|
||||||
if (data.nick === socket.nick) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
let channel;
|
|
||||||
if (typeof data.to === 'string') {
|
|
||||||
channel = data.to;
|
|
||||||
} else {
|
|
||||||
channel = Math.random().toString(36).substr(2, 8);
|
|
||||||
}
|
|
||||||
|
|
||||||
// build and send invite
|
// build and send invite
|
||||||
const payload = {
|
const payload = createRecipientPayload(socket.nick, channel);
|
||||||
cmd: 'info',
|
|
||||||
type: 'invite',
|
|
||||||
from: socket.nick,
|
|
||||||
invite: channel,
|
|
||||||
text: `${socket.nick} invited you to ?${channel}`,
|
|
||||||
};
|
|
||||||
|
|
||||||
const inviteSent = server.broadcast(payload, {
|
const inviteSent = server.broadcast(payload, {
|
||||||
channel: socket.channel,
|
channel: socket.channel,
|
||||||
|
@ -54,12 +116,7 @@ export async function run(core, server, socket, data) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// reply with common channel
|
// reply with common channel
|
||||||
server.reply({
|
server.reply(createSuccessPayload(data.nick, channel), socket);
|
||||||
cmd: 'info',
|
|
||||||
type: 'invite',
|
|
||||||
invite: channel,
|
|
||||||
text: `You invited ${data.nick} to ?${channel}`,
|
|
||||||
}, socket);
|
|
||||||
|
|
||||||
// stats are fun
|
// stats are fun
|
||||||
core.stats.increment('invites-sent');
|
core.stats.increment('invites-sent');
|
||||||
|
|
|
@ -4,6 +4,7 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import * as UAC from '../utility/UAC/_info';
|
import * as UAC from '../utility/UAC/_info';
|
||||||
|
import * as Invite from '../core/invite';
|
||||||
|
|
||||||
// module constructor
|
// module constructor
|
||||||
export function init(core) {
|
export function init(core) {
|
||||||
|
@ -117,19 +118,21 @@ export function chatCheck(core, server, socket, payload) {
|
||||||
|
|
||||||
// shadow-prevent all invites from muzzled users
|
// shadow-prevent all invites from muzzled users
|
||||||
export function inviteCheck(core, server, socket, payload) {
|
export function inviteCheck(core, server, socket, payload) {
|
||||||
if (typeof payload.nick !== 'string') {
|
if (core.muzzledHashes[socket.hash]) {
|
||||||
|
const nickValid = Invite.checkNickname(payload.nick);
|
||||||
|
if (nickValid !== null) {
|
||||||
|
server.reply({
|
||||||
|
cmd: 'warn',
|
||||||
|
text: nickValid,
|
||||||
|
}, socket);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (core.muzzledHashes[socket.hash]) {
|
|
||||||
// generate common channel
|
// generate common channel
|
||||||
const channel = Math.random().toString(36).substr(2, 8);
|
const channel = Invite.getChannel();
|
||||||
|
|
||||||
// send fake reply
|
// send fake reply
|
||||||
server.reply({
|
server.reply(Invite.createSuccessPayload(payload.nick, channel), socket);
|
||||||
cmd: 'info',
|
|
||||||
text: `You invited ${payload.nick} to ?${channel}`,
|
|
||||||
}, socket);
|
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user