From 22d2ffabab56267744ff7bae6bc745eae5354060 Mon Sep 17 00:00:00 2001 From: HF Date: Wed, 12 Jan 2022 11:46:46 +0100 Subject: [PATCH] only send messages from public channels to APIWebSocket send reply with public channels and their ids if APIWebSocket subs to chat add getflag apisocket request add yy flag for matrix --- API.md | 20 +++++++++- public/cf/yy.gif | Bin 0 -> 205 bytes src/socket/APISocketServer.js | 73 ++++++++++++++++++++++++++++------ 3 files changed, 79 insertions(+), 14 deletions(-) create mode 100644 public/cf/yy.gif diff --git a/API.md b/API.md index 74ad0bee..8a5bc332 100644 --- a/API.md +++ b/API.md @@ -19,9 +19,13 @@ send ```["sub", "chat"]``` +you get a reply with the list of public channels you are now receiving messages of + +```["chans", [id1, name1], [id2, name2], ...]``` + All chat messages, except the once you send with `chat` or `mcchat`, will be sent to you in the form: -```["msg", name, message, id, country, channelId]``` +```["msg", name, uid, message, country, channelId]``` channelId is an integer, channel 0 is `en` channel 1 is `int` and maybe more to come. id is the user id country is the [two-letter country code](https://www.nationsonline.org/oneworld/country_code_list.htm) in lowercase @@ -42,6 +46,18 @@ send All pixels (including your own) will be sent to you as typical binary packages +### Get flag of User + +send + +```['getflag', userId]``` + +receive + +```['flag', userId, flag]`` + +Flag is in lowercase two-letter country code and null if user doesn't exist or had no flag set yet + ### Set Pixel ```[ "setpxl", minecraftid, ip, x, y, clr ]``` @@ -64,7 +80,7 @@ coolDownSeconds is the added cooldown (negative if pixel couldn't be set because ### Send Chat Message -```["chat", name, message, country, channelId]``` +```["chat", name, userId, message, country, channelId]``` channelId is an integer, channel 0 is `en` channel 1 is `int` and maybe more to come. (messages with the name "info" will be displayed as red notifications in the chat window) diff --git a/public/cf/yy.gif b/public/cf/yy.gif new file mode 100644 index 0000000000000000000000000000000000000000..cf458bba328e9d43f89ba0cd2151be0f325764f0 GIT binary patch literal 205 zcmZ?wbhEHb6kyPUc>I M(cq!+PLaVH09zAg2mk;8 literal 0 HcmV?d00001 diff --git a/src/socket/APISocketServer.js b/src/socket/APISocketServer.js index 480a63ce..a0513218 100644 --- a/src/socket/APISocketServer.js +++ b/src/socket/APISocketServer.js @@ -11,6 +11,8 @@ import WebSocket from 'ws'; import socketEvents from './SocketEvents'; +import chatProvider from '../core/ChatProvider'; +import RegUser from '../data/models/RegUser'; import { getIPFromRequest } from '../utils/ip'; import { setPixelByCoords } from '../core/setPixel'; import logger from '../core/logger'; @@ -95,9 +97,16 @@ class APISocketServer { sendapi, ws = null, ) { - if (!sendapi) return; + if (!sendapi) { + return; + } + // send only messages from default and lang channels + if (!chatProvider.defaultChannels[channelId] + && !chatProvider.langChannels[channelId]) { + return; + } - const sendmsg = JSON.stringify(['msg', name, msg, country, channelId]); + const sendmsg = JSON.stringify(['msg', name, id, msg, country, channelId]); this.wss.clients.forEach((client) => { if (client !== ws && client.subChat @@ -153,6 +162,41 @@ class APISocketServer { this.broadcast(buffer, (client) => client.subPxl); } + static getPublicChannels() { + const chanReply = ['chans']; + const defaultChanKeys = Object.keys(chatProvider.defaultChannels); + const langChanKeys = Object.keys(chatProvider.langChannels); + for (let i = 0; i < defaultChanKeys.length; i += 1) { + const id = defaultChanKeys[i]; + const [name] = chatProvider.defaultChannels[id]; + chanReply.push([parseInt(id, 10), name]); + } + for (let i = 0; i < langChanKeys.length; i += 1) { + const name = langChanKeys[i]; + const { id } = chatProvider.langChannels[name]; + chanReply.push([id, name]); + } + return chanReply; + } + + static async getFlagOfUser(userId) { + try { + const reguser = await RegUser.findOne({ + attributes: ['flag'], + where: { + id: userId, + }, + }); + if (!reguser) { + throw new Error('User not found'); + } + return reguser.flag; + } catch { + logger.info(`Flag to user ${userId} could not be determined`); + } + return null; + } + async onTextMessage(message, ws) { try { const packet = JSON.parse(message); @@ -165,14 +209,15 @@ class APISocketServer { const even = packet[0]; if (even === 'chat') { ws.subChat = true; + ws.send(JSON.stringify(APISocketServer.getPublicChannels())); } else if (even === 'pxl') { ws.subPxl = true; } else if (even === 'online') { ws.subOnline = true; } else { - logger.info(`APISocket wanted to sub to unexisting ${command}`); + logger.info(`APISocket wanted to sub to unexisting ${even}`); } - logger.info(`APISocket client subscribed to ${command}`); + logger.info(`APISocket client subscribed to ${even}`); return; } if (command === 'setpxl') { @@ -186,20 +231,17 @@ class APISocketServer { // minecraftid support got removed return; } - logger.info(`APISocket message ${message}`); if (command === 'chat') { - const [name, msg, country, channelId] = packet; + const [name, id, msg, country, channelId] = packet; + const uid = id || 1; /* * do not send message back up ws that sent it - * TODO: user id should not be hardcoded, - * consider it whenever this actually gets used and - * becomes an issue. */ socketEvents.broadcastChatMessage( name, msg, channelId, - 1, + uid, country, false, ); @@ -207,15 +249,22 @@ class APISocketServer { name, msg, channelId, - 1, + uid, country, true, ws, ); return; } + if (command === 'getflag') { + const [uid] = packet; + const flag = await APISocketServer.getFlagOfUser(uid); + ws.send(JSON.stringify(['flag', uid, flag])); + return; + } + logger.info(`Received unknown api-ws message ${message}`); } catch (err) { - logger.error(`Got undecipherable api-ws message ${message}`); + logger.error(`Got undecipherable api-ws message ${message}, ${err}`); } }