@@ -4,7 +4,7 @@ import Account, { IProfile } from "#schemas/profile";
4
4
import { ObjectId } from "mongoose" ;
5
5
6
6
import ChatLog , { IChatLog , IMessage } from "#schemas/chat" ;
7
- import { randomInt } from "crypto " ;
7
+ import { getRandomId } from "#util/random_id " ;
8
8
9
9
export { IChatLog , IMessage } ;
10
10
@@ -14,35 +14,28 @@ export function chatFind(chat_id:string) {
14
14
}
15
15
16
16
export function chatCreate ( members :IProfile [ ] = [ ] ) {
17
+ let chat_id :string = getRandomId ( global . chats ) ;
18
+ if ( chat_id === null ) return null ;
19
+
17
20
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
- }
31
21
22
+ chatlog . _id = chat_id ;
32
23
let chat = new Chat ( chatlog ) ;
33
24
34
25
for ( let member of members ) {
35
- chat . addMember ( member , true ) ;
26
+ chat . addMember ( member , null , true ) ;
36
27
}
37
28
38
29
chat . save ( ) ;
39
30
global . chats [ chat_id ] = chat ;
31
+
32
+ return chat ;
40
33
}
41
34
42
35
export class Chat {
43
36
chatlog : IChatLog ;
44
37
45
- online_members : Client [ ] ;
38
+ online_members : Client [ ] = [ ] ;
46
39
get messages ( ) : IMessage [ ] {
47
40
return this . chatlog . messages ;
48
41
}
@@ -66,13 +59,16 @@ export class Chat {
66
59
}
67
60
68
61
69
- addMember ( profile : IProfile , initial = false ) {
62
+ addMember ( profile : IProfile , client = null , initial = false ) {
70
63
if ( this . members . includes ( profile . id ) )
71
64
return ;
72
65
73
66
profile . chats . push ( this . chatlog . id ) ;
74
67
this . members . push ( profile . id ) ;
75
68
69
+ if ( client !== null )
70
+ this . connectMember ( client ) ;
71
+
76
72
if ( ! initial )
77
73
this . save ( ) ;
78
74
}
@@ -83,14 +79,10 @@ export class Chat {
83
79
this . members . splice ( idx , 1 ) ;
84
80
85
81
// disconnect the client
86
- idx = this . online_members . indexOf ( profile . id ) ;
82
+ idx = this . online_members . findIndex ( c => c . profile === profile ) ;
87
83
if ( idx !== - 1 ) {
88
84
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 ) ;
94
86
}
95
87
96
88
@@ -103,11 +95,11 @@ export class Chat {
103
95
}
104
96
105
97
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 ) ;
108
100
109
- this . online_members . push ( client ) ;
110
- client . chats . push ( this ) ;
101
+ if ( ! client . chats . includes ( this ) )
102
+ client . chats . push ( this ) ;
111
103
}
112
104
113
105
disconnectMember ( client : Client ) {
0 commit comments