Files
P42_wssGateway/actions/chat.js
T
STEINNI de69b3d4de 2nd
2025-09-11 20:50:34 +00:00

167 lines
5.6 KiB
JavaScript

module.exports.methods = {
/* Creates (predictable) peer-to-peer chan if necessary, or check for lobby existence, then subscribe to it.
Request:
{
"action": "STARTCHAT",
"payload": "P" // or "C" => "P" = Peer-to-peer, "C" = (chat) Channel
}
reply:
{
"action": "STARTCHAT",
"success": true,
"payload" : "dynamically created chan"
}
*/
action_STARTCHAT(action, payload, reqid){
if(typeof(payload)!='string'){
this.sendErr(action, 'Invalid payload', reqid);
return;
};
let recipientId = payload.substring(2);
let chan;
if(payload[0]=='P') {
chan = (this.userId<recipientId) ? 'userchans:'+this.userId+'|'+recipientId : 'userchans:'+recipientId+'|'+this.userId;
} else if(payload[0]=='C') {
chan = 'lobbies:'+recipientId;
} else {
this.sendErr(action, 'Bad chat destination', reqid);
return;
}
// subscribe this connexion
if(!(chan in this.rediscnx.subscriptions)) this.rediscnx.subscriptions[chan] = [];
if(this.rediscnx.subscriptions[chan].indexOf(this.uuid)<0) {
this.rediscnx.subscriptions[chan].push(this.uuid);
if(this.debug) console.log('Subscribed to chat chan: ',chan);
}
// other connexion of this user might be on different chat, so don't subscribe them
let reply = {
'action': action,
'payload': chan,
'success': true,
};
if(reqid) reply.reqid = reqid;
this.send(JSON.stringify(reply));
},
/* Send human-message in chat chan.
Request:
{
"action": "SENDCHAT",
"payload": "P" // or "C" => "P" = Peer-to-peer, "C" = (chat) Channel
}
reply:
{
"action": "SENDCHAT",
"success": true,
"payload" : null
}
*/
action_SENDCHAT(action, payload, reqid){
//TODO: prevent unauthorized recipient !!
let recipientId = payload.recipient.substring(2);
let chan;
payload.event = 'CHATMSG'
if(payload.recipient[0]=='P') {
chan = (this.userId<recipientId) ? 'userchans:'+this.userId+'|'+recipientId : 'userchans:'+recipientId+'|'+this.userId;
} else if(payload.recipient[0]=='C') {
chan = 'lobbies:'+recipientId;
} else {
this.sendErr(action, 'Bad chat destination', reqid);
return;
}
if(this.debug) console.log('Publishing to chat chan: ',chan, payload.msg);
//NIKE TODO: be coherent, 'sender' is app-level, therefore should be inside payload, not outside !!!
// (see remark in Protocol description in FE MessageBus class)
this.rediscnx.redisPublish(chan, {
'sender': this.userId,
'msg' : payload.msg
});
let reply = {
'action': action,
'payload': null,
'success': true,
};
if(reqid) reply.reqid = reqid;
this.send(JSON.stringify(reply));
},
/* Send human-message in chat chan.
Request:
{
"action": "ISONLINE",
"payload": "P" // or "C" => "P" = Peer-to-peer, "C" = (chat) Channel
}
reply:
{
"action": "ISONLINE",
"success": true,
"payload" :
}
*/
// You can only ask the satus of a list of usernames you know (and have the right to)
action_ISONLINE(action, payload, reqid){
//TODO: can you really ask about those users ? (but that might cost too much time, because => ML?)
if(!Array.isArray(payload)){
this.sendErr(action, 'Invalid payload', reqid);
return;
};
let onlineUsers = Object.keys(this.wssSrv.getOnlineUsers());
let reply = {
'action': action,
'payload': payload.filter((x) => (onlineUsers.indexOf(x)>-1)),
'success': true,
};
if(reqid) reply.reqid = reqid;
this.send(JSON.stringify(reply));
},
// Same as ISONLINE, but subscribe to watch changes
action_WATCHUSERS(action, payload, reqid){
if(!Array.isArray(payload)){
this.sendErr(action, 'Invalid payload', reqid);
return;
}
//TODO: can you really ask about those users ? (but that might cost too much time, because => ML?)
this.usersWatched = payload;
let reply = {
'action': action,
'payload': null,
'success': true,
};
if(reqid) reply.reqid = reqid;
this.send(JSON.stringify(reply));
},
/*
Request:
{
"action": "CHANLST",
"payload": { "filter": "ChatRoom*" }
}
reply:
{
"action": "CHANLST",
"success": true,
"payload" : ["ChatRoom1","ChatRoom2","ChatRoom_Experts"]
}
*/
action_CHANLST(action, payload, reqid){
//TODO : Filter based on user rights!!
//
let reply = {
'action': action,
'payload': [
this.config.redis.basePrefix+"onlineUsers",
this.config.redis.basePrefix+"system:chan1",
this.config.redis.basePrefix+"proposals:updates"
],//this.config.redis.watchChannels,
'success': true,
};
if(reqid) reply.reqid = reqid;
this.send(JSON.stringify(reply));
},
}