* Client requesting to join a room. If the client hasn't entered the room already, add a new client. * Otherwise all users just will be informed again that the user has joined.
(client: WebSocketClient, color?: JoinMessage['color'])
| 362 | * Otherwise all users just will be informed again that the user has joined. |
| 363 | */ |
| 364 | async join(client: WebSocketClient, color?: JoinMessage['color']) { |
| 365 | this.messenger.addClient(client); |
| 366 | |
| 367 | let added = false; |
| 368 | let clientColor: Color | undefined; |
| 369 | |
| 370 | if (!(await this.hasClient(client.uid))) { |
| 371 | await this.store(async (store) => { |
| 372 | const clients = await store.get('clients'); |
| 373 | added = true; |
| 374 | |
| 375 | const existingColors = clients.map((c) => c.color); |
| 376 | const colorsAvailable = COLORS.filter((color) => !existingColors.includes(color)); |
| 377 | |
| 378 | if (colorsAvailable.length === 0) { |
| 379 | colorsAvailable.push(...COLORS); |
| 380 | } |
| 381 | |
| 382 | if (color && colorsAvailable.includes(color)) { |
| 383 | clientColor = color; |
| 384 | } else { |
| 385 | clientColor = colorsAvailable[random(colorsAvailable.length - 1)]!; |
| 386 | } |
| 387 | |
| 388 | clients.push({ |
| 389 | uid: client.uid, |
| 390 | accountability: client.accountability!, |
| 391 | color: clientColor, |
| 392 | }); |
| 393 | |
| 394 | await store.set('clients', clients); |
| 395 | }); |
| 396 | } |
| 397 | |
| 398 | if (added && clientColor) { |
| 399 | await this.sendExcluding( |
| 400 | { |
| 401 | action: ACTION.SERVER.JOIN, |
| 402 | user: client.accountability!.user!, |
| 403 | connection: client.uid, |
| 404 | color: clientColor, |
| 405 | }, |
| 406 | client.uid, |
| 407 | ); |
| 408 | } |
| 409 | |
| 410 | const { changes, focuses, clients } = await this.store(async (store) => { |
| 411 | return { |
| 412 | changes: await store.get('changes'), |
| 413 | focuses: await store.get('focuses'), |
| 414 | clients: await store.get('clients'), |
| 415 | }; |
| 416 | }); |
| 417 | |
| 418 | const schema = await getSchema(); |
| 419 | const knex = getDatabase(); |
| 420 | |
| 421 | const allowedFields = await verifyPermissions(client.accountability, this.collection, this.item, 'read', { |