This repository was archived by the owner on Mar 14, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
205 lines (160 loc) · 6.4 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
const express = require('express');
const axios = require('axios').default;
const { createServer } = require('http');
const makeCappedMap = require('capped_map');
const { simpleflake } = require('simpleflakes');
const { Server: IOServer } = require('socket.io');
const assert = require('assert')
const messageCache = new Map();
const app = express();
const server = createServer(app);
const snowflake = () => simpleflake(Date.now(), null, 1581983347347).toString();
const io = new IOServer(server, {
cors: {
origin: true,
methods: ['GET', 'POST', 'PATCH', 'DELETE'],
credentials: true
}
});
interface UserInterface {
id: string;
username?: string;
avatar?: string | null;
system: boolean;
token?: string | null;
};
class User {
id: string;
username: string;
avatar: string;
system: boolean;
constructor(user: UserInterface) {
this.id = user.id,
this.username = user.username || 'Desconocido',
this.avatar = user.avatar || 'https://cdn.chatglobal.ml/assets/error.png',
this.system = user.system || false
}
}
interface MessageInterface {
content: string | null;
};
class Message {
id: string;
content: string;
author: {
id: string;
username: string;
avatar: string;
system: boolean;
};
timestamp: number;
constructor(author: UserInterface, message: MessageInterface) {
this.id = snowflake();
this.content = (message.content && message.content.trim()) ? message.content.trim() : 'Sin contenido.';
this.author = new User(author);
this.timestamp = new Date().getTime();
}
}
class SystemMessage extends Message {
constructor(content: string) {
super(
{
id: '000000',
username: 'Chat Global',
avatar: 'https://cdn.chatglobal.ml/assets/logo.png',
system: true
},
{
content: content
}
);
}
}
app.get('/', (req: any, res: any): void => {
res.json({ uri: 'wss://gateway.chatglobal.ml' });
});
io.use(async (socket: any, next: any): Promise<void> => {
if (socket.handshake.auth && socket.handshake.auth.token && socket.handshake.auth.interchat) {
const token = socket.handshake.auth.token;
if (!token.includes('.')) {
return next(new Error('Malformed Token'));
}
if (token.split('.').length !== 3) {
return next(new Error('Malformed Token'));
}
const user = token.split('.')[0];
if (!user) {
return next(new Error('Malformed Token'));
}
const userData = await axios(
{
method: 'post',
url: `https://accounts.chatglobal.ml/authorize/user/${user}`,
headers: {
authorization: token
}
}
).catch((): boolean => false);
if (!userData) {
return next(new Error('Authentication error'));
}
const interchats = [
{
id: 'es',
avatar: 'https://cdn.chatglobal.ml/assets/logo.png',
name: 'Interchat español',
description: 'Interchat español de Chat Global Oficial.',
banner: 'https://cdn.chatglobal.ml/assets/thumbnail.png'
},
{
id: 'en',
avatar: 'https://cdn.discordapp.com/attachments/923615661031317554/924342056405585970/1200px-SAS_Group_logo.png',
name: 'English Interchat',
description: 'amongus',
banner: 'https://cdn.discordapp.com/attachments/923615661031317554/924342056405585970/1200px-SAS_Group_logo.png'
},
{
id: 'amogus',
avatar: 'https://logos-marcas.com/wp-content/uploads/2021/08/Among-Us-Logo.png',
name: 'amogus',
description: 'amongus',
banner: 'https://logos-marcas.com/wp-content/uploads/2021/08/Among-Us-Logo.png'
}
];
const interchat = interchats.find(interchat => interchat.id === socket.handshake.auth.interchat)
if (!interchat) return next(new Error('Invalid interchat provided'));
socket.user = userData.data;
socket.interchat = interchat;
return next();
} else {
return next(new Error('Authentication error'));
}
}).on('connection', (socket: any): void => {
socket.join(socket.interchat.id);
const interchatRoom = io.sockets.adapter.rooms.get(socket.interchat.id)
const usersInstances: User[] = interchatRoom ? Array.from(interchatRoom.keys()).map(socketID => new User(io.sockets.sockets.get(socketID).user)) : [];
const ids = usersInstances.map(userInstance => userInstance.id);
const users = usersInstances.filter(({ id }, index) => !ids.includes(id, index + 1));
const isUniqueConnection = usersInstances.filter(({ id }) => id === socket.user.id).length === 1;
if (!messageCache.has(socket.interchat.id)) messageCache.set(socket.interchat.id, makeCappedMap(new Map(), 50));
socket.emit('MESSAGES_LIST', Array.from(messageCache.get(socket.interchat.id).values()));
socket.emit('MEMBERS_LIST', users);
if (isUniqueConnection) socket.broadcast.to(socket.interchat.id).emit('MEMBER_UPDATE', 'connect', new User(socket.user));
socket.emit('MESSAGE_CREATE', new SystemMessage('Te has conectado al interchat.'));
if (isUniqueConnection) socket.broadcast.to(socket.interchat.id).emit('MESSAGE_CREATE', new SystemMessage(`${socket.user.username} se conecto al interchat.`));
socket.on('disconnect', (): void => {
const room = io.sockets.adapter.rooms.get(socket.interchat.id);
if (room && !(Array.from(room.keys()).find(socketID => new User(io.sockets.sockets.get(socketID).user).id === socket.user.id))) {
socket.broadcast.to(socket.interchat.id).emit('MEMBER_UPDATE', 'disconnect', new User(socket.user));
socket.broadcast.to(socket.interchat.id).emit('MESSAGE_CREATE', new SystemMessage(`${socket.user.username} se desconecto del interchat.`));
}
});
socket.on('MESSAGE_CREATE', (msg: MessageInterface): void => {
const message = new Message(socket.user, msg);
io.to(socket.interchat.id).emit('MESSAGE_CREATE', message);
messageCache.get(socket.interchat.id).set(message.id, message);
});
});
server.listen(3000, (): void => {
console.log('Listening on *:3000');
});