allow private messages, mostly for bots

This commit is contained in:
hippoz 2020-12-05 21:06:28 +02:00
parent 9a6576fe98
commit 4147e4f6ca
3 changed files with 40 additions and 16 deletions

View file

@ -127,7 +127,7 @@ GatewayServer.prototype.eventSetup = function() {
socket.user = {
username: data.username,
_id: user._id,
_id: user._id.toString(),
token, // NOTE(hippoz): Maybe not secure
permissionLevel,
color: user.color
@ -152,7 +152,7 @@ GatewayServer.prototype.eventSetup = function() {
console.log(`[*] [gateway] [handshake] Got yoo from ${socket.user.username}, connection is finally completed!`);
socket.isConnected = true;
socket.on('message', ({ category, content, nickAuthor }) => {
socket.on('message', async ({ category, content, nickAuthor, destUser }) => {
if (!category || !content || !socket.joinedCategories || !socket.isConnected || !socket.user || !(typeof content === 'string') || !(typeof category._id === 'string')) return;
content = content.trim();
if (!content || content === '' || content === ' ' || content.length >= 2000) return;
@ -195,6 +195,14 @@ GatewayServer.prototype.eventSetup = function() {
return;
}
if (destUser && destUser._id && (typeof destUser._id) === 'string') {
const user = await this._findSocketInRoom(messageObject.category._id, destUser._id);
if (!user) return;
this._gateway.in(user.user.sid).emit('message', messageObject);
return;
}
this._gateway.in(category._id).emit('message', messageObject);
});
@ -249,13 +257,36 @@ GatewayServer.prototype._getSocketsInRoom = async function(room) {
user: {
username: client.user.username,
_id: client.user._id,
color: client.user.color
color: client.user.color,
sid: client.id
}
});
});
return updatedClientList;
};
GatewayServer.prototype._findSocketInRoom = async function(room, userid) {
// NOTE: I have no idea why i have to do this dumb thing, why can't socket io just let you simply get the sockets from a room? idk
// There kinda was a way in the previous version, but they want to change the api for the worse each version, i'm guessing
const clients = await this._gateway.in(room).allSockets();
const updatedClientList = [];
clients.forEach((sid) => {
const client = this._gateway.sockets.get(sid); // lol they also used dumb ass maps for the socket list, can you fucking not?
if (!client || !client.isConnected || !client.user) return;
if (userid !== client.user._id) return;
updatedClientList.push({
user: {
username: client.user.username,
_id: client.user._id,
color: client.user.color,
sid: client.id
}
});
});
return updatedClientList[0] || undefined;
};
GatewayServer.prototype._generateClientListUpdateObject = async function(room, categoryTitle='UNKNOWN') {
const clientList = await this._getSocketsInRoom(room);
return {

View file

@ -59,24 +59,16 @@ GatewayConnection.prototype.connect = function(token) {
this.socket.on('clientListUpdate', (e) => this.emit('clientListUpdate', e));
};
GatewayConnection.prototype.sendMessage = function(categoryId, content, nickAuthor=undefined) {
GatewayConnection.prototype.sendMessage = function(categoryId, content, { nickAuthor, destUser }) {
if (!this.isConnected) return 1;
if (content.length >= 2000) return 1;
if (nickAuthor) {
this.socket.emit('message', {
category: {
_id: categoryId
},
nickAuthor,
content
});
return;
}
this.socket.emit('message', {
category: {
_id: categoryId
},
nickAuthor,
destUser,
content
});
};

View file

@ -5,7 +5,7 @@ const main = async () => {
throwErrors: true
});
await client.setToken('eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VybmFtZSI6InVzZXIiLCJpYXQiOjE2MDcxNzkyMzAsImV4cCI6MTYwNzE5MDAzMH0.X7KJA-8KmcdwTORXXVZcQTId3iz4_c7H7FYgF2X6nq8');
await client.setToken('eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VybmFtZSI6InVzZXIiLCJpYXQiOjE2MDcxOTE5NjAsImV4cCI6MTYwNzIwMjc2MH0.dfI6CMeDNck-ubqwEmiMFdHbRR4iiuooeTuzgVc_8rY');
await client.gatewayConnect();
@ -14,7 +14,8 @@ const main = async () => {
client.gateway.on('message', (e) => {
if (e.author._id === client.user._id) return;
client.gateway.sendMessage('5fc829314e96e00725c17fd8', e.content, {
username: e.author.username
nickAuthor: {username: e.author.username},
//destUser: {_id: e.author._id}
});
});
});