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
This commit is contained in:
HF 2022-01-12 11:46:46 +01:00
parent 031de42878
commit 22d2ffabab
3 changed files with 79 additions and 14 deletions

20
API.md
View File

@ -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)

BIN
public/cf/yy.gif Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 205 B

View File

@ -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}`);
}
}