@@ -4,7 +4,7 @@ import Account, { IProfile } from "#schemas/profile";
44import { ObjectId } from "mongoose" ;
55
66import ChatLog , { IChatLog , IMessage } from "#schemas/chat" ;
7- import { randomInt } from "crypto " ;
7+ import { getRandomId } from "#util/random_id " ;
88
99export { IChatLog , IMessage } ;
1010
@@ -14,35 +14,28 @@ export function chatFind(chat_id:string) {
1414}
1515
1616export function chatCreate ( members :IProfile [ ] = [ ] ) {
17+ let chat_id :string = getRandomId ( global . chats ) ;
18+ if ( chat_id === null ) return null ;
19+
1720 let chatlog = new ChatLog ( ) ;
18- let chat_id :string ;
19-
20- while ( true ) {
21- // a random 6-digit number
22- chat_id = randomInt ( 100000 , 999999 ) . toString ( ) ;
23- if ( chat_id in global . chats ) { // just in case of a collision
24- continue ;
25- }
26- else {
27- chatlog . _id = chat_id ;
28- break ;
29- }
30- }
3121
22+ chatlog . _id = chat_id ;
3223 let chat = new Chat ( chatlog ) ;
3324
3425 for ( let member of members ) {
35- chat . addMember ( member , true ) ;
26+ chat . addMember ( member , null , true ) ;
3627 }
3728
3829 chat . save ( ) ;
3930 global . chats [ chat_id ] = chat ;
31+
32+ return chat ;
4033}
4134
4235export class Chat {
4336 chatlog : IChatLog ;
4437
45- online_members : Client [ ] ;
38+ online_members : Client [ ] = [ ] ;
4639 get messages ( ) : IMessage [ ] {
4740 return this . chatlog . messages ;
4841 }
@@ -66,13 +59,16 @@ export class Chat {
6659 }
6760
6861
69- addMember ( profile : IProfile , initial = false ) {
62+ addMember ( profile : IProfile , client = null , initial = false ) {
7063 if ( this . members . includes ( profile . id ) )
7164 return ;
7265
7366 profile . chats . push ( this . chatlog . id ) ;
7467 this . members . push ( profile . id ) ;
7568
69+ if ( client !== null )
70+ this . connectMember ( client ) ;
71+
7672 if ( ! initial )
7773 this . save ( ) ;
7874 }
@@ -83,14 +79,10 @@ export class Chat {
8379 this . members . splice ( idx , 1 ) ;
8480
8581 // disconnect the client
86- idx = this . online_members . indexOf ( profile . id ) ;
82+ idx = this . online_members . findIndex ( c => c . profile === profile ) ;
8783 if ( idx !== - 1 ) {
8884 let client = this . online_members [ idx ] ;
89- this . online_members . splice ( idx , 1 ) ;
90-
91- idx = client . chats . indexOf ( this ) ;
92- if ( idx !== - 1 )
93- client . chats . splice ( idx , 1 ) ;
85+ this . disconnectMember ( client ) ;
9486 }
9587
9688
@@ -103,11 +95,11 @@ export class Chat {
10395 }
10496
10597 connectMember ( client : Client ) {
106- if ( this . online_members . includes ( client ) )
107- return ;
98+ if ( ! this . online_members . some ( c => ( c === client || c . profile ?. id === client . profile ?. id ) ) )
99+ this . online_members . push ( client ) ;
108100
109- this . online_members . push ( client ) ;
110- client . chats . push ( this ) ;
101+ if ( ! client . chats . includes ( this ) )
102+ client . chats . push ( this ) ;
111103 }
112104
113105 disconnectMember ( client : Client ) {
0 commit comments