-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhandlers.ts
More file actions
294 lines (284 loc) · 8.58 KB
/
handlers.ts
File metadata and controls
294 lines (284 loc) · 8.58 KB
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
import Game from "./models/game.js";
import User from "./models/user.js";
import Message from "./models/message.js";
import BlockedUser from "./models/blocked_user.js";
import { toData } from "./auth/jwt.js";
import {
addPlayerClient,
getClientsByPlayerId,
removePlayerClient,
} from "./socketClients.js";
import {
countAllMessagesInLobby,
getAllMessagesInGame,
} from "./services/chat.js";
import registerVisit from "./services/visit.js";
import { fetchGames } from "./services/lobby.js";
import {
ALL_GAMES,
ALL_MESSAGES,
GAME_UPDATED,
LOGIN_OR_SIGNUP_ERROR,
MESSAGES_COUNT,
NEW_MESSAGE,
TOKEN_EXPIRED,
} from "./constants/outgoingMessageTypes.js";
import fetchGame from "./services/fetchGame.js";
import { notify } from "./services/notifications.js";
import { storePushToken } from "./services/expoPush.js";
import { MyServer, MySocket } from "./index";
const BOT_TRIGGER_TEXT = "бот ходи";
const BOT_TRIGGER_USER_ID = 1;
const receiveSaveAndSendNewMessage = async (
webSocketsServer: MyServer,
socket: MySocket,
payload: string,
ack?: (response: { success: boolean; error?: string }) => void
) => {
if (!socket.data.gameId) {
console.log("chat: no gameId on socket, player:", socket.data.playerId);
if (ack) ack({ success: false, error: "Not in a game" });
return;
}
if (
socket.data.user.id === BOT_TRIGGER_USER_ID &&
payload.trim().toLowerCase() === BOT_TRIGGER_TEXT
) {
webSocketsServer.to(socket.data.gameId.toString()).emit("message", {
type: "BOT_TRIGGER",
payload: { gameId: socket.data.gameId },
});
if (ack) ack({ success: true });
return;
}
// store the message in DB
try {
await Message.create({
text: payload,
name: socket.data.user.name,
GameId: socket.data.gameId,
UserId: socket.data.user.id,
});
const messagePayload = {
type: NEW_MESSAGE,
payload: {
userId: socket.data.playerId,
text: payload,
name: socket.data.user.name,
},
};
const room = webSocketsServer.sockets.adapter.rooms.get(
socket.data.gameId.toString()
);
if (room) {
for (const socketId of room) {
const targetSocket = webSocketsServer.sockets.sockets.get(socketId);
if (
targetSocket &&
!targetSocket.data.blockedUserIds?.has(socket.data.user.id)
) {
targetSocket.emit("message", messagePayload);
}
}
}
console.log("chat ack:", ack ? "calling with success" : "no ack callback");
if (ack) ack({ success: true });
} catch (error) {
console.log("problem storing and sending chat message:", error);
console.log("chat ack:", ack ? "calling with failure" : "no ack callback");
if (ack) ack({ success: false, error: "Failed to send message" });
return; // Don't continue with notifications if message failed
}
try {
const usersOfThisGame = await User.findAll({
include: {
model: Game,
as: "games",
where: { id: socket.data.gameId },
attributes: [],
},
attributes: ["id", "name", "email"],
});
await Promise.all(
usersOfThisGame
.filter((user) => user.id !== socket.data.user.id)
.map(async (user) => {
await notify(user.id, {
title: `New chat message`,
message: `${socket.data.user.name} in game ${socket.data.gameId}: ${payload}`,
gameId: socket.data.gameId,
type: "chat_message",
});
const clients = await Promise.all(getClientsByPlayerId(user.id));
clients
.filter((client) => !client.gameId)
.map(async (client) => {
client.emit("message", {
type: MESSAGES_COUNT,
payload: await countAllMessagesInLobby(user.id),
});
});
})
);
} catch (error) {
console.log("problem getting users of this game:", error);
}
};
const addUserToSocket = async (
_: MyServer,
socket: MySocket,
payload: string | { jwt: string; pushToken?: string }
) => {
let user = undefined;
// Handle both old (string) and new (object) payload formats
const jwt = typeof payload === "string" ? payload : payload.jwt;
const pushToken = typeof payload === "object" ? payload.pushToken : undefined;
console.log("addUserToSocket - JWT:", jwt, "PushToken:", pushToken);
try {
const data = toData(jwt);
user = await User.findByPk(data.userId, {
attributes: ["id", "name"],
});
} catch (error) {
console.log("problem retrieving user:", error);
if (error instanceof Error && error.name === "TokenExpiredError") {
socket.emit("message", { type: TOKEN_EXPIRED });
} else {
const errorMessage =
error instanceof Error
? `${error.name}: ${error.message}`
: String(error);
socket.emit("message", {
type: LOGIN_OR_SIGNUP_ERROR,
payload: errorMessage,
});
}
}
if (user && /^\[deleted_\d+\]$/.test(user.name)) {
socket.emit("message", {
type: LOGIN_OR_SIGNUP_ERROR,
payload: "session_expired",
});
return;
}
if (user) {
socket.data.playerId = user.id;
socket.data.user = user;
addPlayerClient(socket);
const blockedRows = await BlockedUser.findAll({
where: { UserId: user.id },
attributes: ["BlockedUserId"],
});
socket.data.blockedUserIds = new Set(
blockedRows.map((r) => r.BlockedUserId)
);
// Store Expo push token if provided
if (pushToken) {
storePushToken(user.id, pushToken);
}
try {
const count = await countAllMessagesInLobby(user.id);
socket.emit("message", { type: MESSAGES_COUNT, payload: count });
// if user was on a game page but not logged in, and then he logs in we need to send him more full game object
if (socket.data.gameId) {
const game = await fetchGame(socket.data.gameId);
socket.emit("message", {
type: GAME_UPDATED,
payload: { gameId: socket.data.gameId, game },
});
const allMessages = await getAllMessagesInGame(
socket.data.gameId,
user.id
);
if (allMessages.length > 0) {
socket.emit("message", { type: ALL_MESSAGES, payload: allMessages });
}
await registerVisit(socket.data.gameId, user.id);
}
} catch (error) {
console.log(error);
}
}
};
const removeUserFromSocket = (_: MyServer, socket: MySocket) => {
removePlayerClient(socket);
};
const addGameToSocket = async (
_: MyServer,
socket: MySocket,
gameId: number
) => {
if (!gameId) return;
socket.leave("lobby");
if (socket.data.gameId) {
socket.leave(socket.data.gameId.toString());
}
socket.join(gameId.toString());
socket.data.gameId = gameId;
try {
const allMessages = await getAllMessagesInGame(
gameId,
socket.data.playerId
);
if (allMessages.length > 0) {
socket.emit("message", { type: ALL_MESSAGES, payload: allMessages });
}
if (socket.data.user) {
await registerVisit(gameId, socket.data.user.id);
}
} catch (error) {
console.log(error);
}
};
const removeGameFromSocket = async (
_: MyServer,
socket: MySocket,
gameId: number
) => {
if (!gameId) {
console.log(
`gameId is ${gameId} for socket.data.playerId ${socket.data.playerId}`
);
} else {
socket.leave(gameId.toString());
if (socket.data.gameId === gameId) {
delete socket.data.gameId;
}
try {
await registerVisit(gameId, socket.data.playerId);
} catch (error) {
console.log(error);
}
}
};
const enterLobby = async (_: MyServer, socket: MySocket) => {
socket.join("lobby");
delete socket.data.gameId;
try {
const count = await countAllMessagesInLobby(socket.data.playerId);
socket.emit("message", { type: MESSAGES_COUNT, payload: count });
const games = await fetchGames();
socket.emit("message", { type: ALL_GAMES, payload: games });
} catch (error) {
console.log(error);
}
};
export const updateBlockedUserIds = async (userId: number) => {
const blockedRows = await BlockedUser.findAll({
where: { UserId: userId },
attributes: ["BlockedUserId"],
});
const blockedIds = new Set(blockedRows.map((r) => r.BlockedUserId));
const sockets = getClientsByPlayerId(userId);
for (const s of sockets) {
s.data.blockedUserIds = blockedIds;
}
};
export default {
SEND_CHAT_MESSAGE: receiveSaveAndSendNewMessage,
ADD_USER_TO_SOCKET: addUserToSocket,
REMOVE_USER_FROM_SOCKET: removeUserFromSocket,
ADD_GAME_TO_SOCKET: addGameToSocket,
REMOVE_GAME_FROM_SOCKET: removeGameFromSocket,
ENTER_LOBBY: enterLobby,
};