@@ -3,67 +3,137 @@ import Account, { IProfile } from "#schemas/profile";
33
44import { ObjectId } from "mongoose" ;
55
6+ import ChatLog , { IChatLog , IMessage } from "#schemas/chat" ;
67
7- export interface IMessage {
8- profile_id : ObjectId | string ;
9- name : string ;
10- content : string ;
8+ export { IChatLog , IMessage } ;
9+
10+
11+ export function chatFind ( chat_id :string ) {
12+ return global . chats [ chat_id ] ;
1113}
1214
13- export class Message implements IMessage {
14- id : number ;
15- profile_id : string ;
16- name : string ;
17- content : string ;
15+ export function chatCreate ( members :IProfile [ ] = [ ] ) {
16+ let chat = new Chat ( new ChatLog ( ) ) ;
17+
18+ for ( let member of members ) {
19+ chat . addMember ( member , true ) ;
20+ }
21+
22+ chat . save ( ) ;
23+
24+ let chat_id = chat . chat_id ;
25+ global . chats [ chat_id ] = chat ;
1826}
1927
20- // export class Chat {
21- // chat_id: string; // id of the chat room
22- // online_members: Client[];
23- // members: string[]; // profile_id[]
24- // messages: Message[];
25-
26- // constructor(members?: Client[]) {
27- // if (members) {
28- // for(let member in members) {
29- // this.addMember(member);
30- // }
31- // }
32-
33- // }
28+ export class Chat {
29+ chatlog : IChatLog ;
3430
35- // async save() {
31+ online_members : Client [ ] ;
32+ get messages ( ) : IMessage [ ] {
33+ return this . chatlog . messages ;
34+ }
3635
37- // }
3836
39- // addMember(member: Client) {
40- // this.members.push(member);
41- // }
37+ // id of the chat room
38+ get chat_id ( ) : string {
39+ return this . chatlog . id . toString ( ) ;
40+ }
4241
43- // kickMember(member: Client) {
44- // let idx = this.members.indexOf(member);
45- // this.members.splice(idx, 1);
46- // }
42+ get members ( ) :ObjectId [ ] {
43+ return this . chatlog . members ;
44+ }
45+
46+ constructor ( chatlog : IChatLog ) {
47+ this . chatlog = chatlog ;
48+ }
49+
50+ save ( ) {
51+ return this . chatlog . save ( ) ;
52+ }
53+
54+
55+ addMember ( profile : IProfile , initial = false ) {
56+ if ( this . members . includes ( profile . id ) )
57+ return ;
58+
59+ profile . chats . push ( this . chatlog . id ) ;
60+ this . members . push ( profile . id ) ;
4761
48- // disconnectMember(member: Client) {
62+ if ( ! initial )
63+ this . save ( ) ;
64+ }
65+
66+ kickMember ( profile : IProfile ) {
67+ let idx = this . members . indexOf ( profile . id ) ;
68+ if ( idx !== - 1 )
69+ this . members . splice ( idx , 1 ) ;
70+
71+ // disconnect the client
72+ idx = this . online_members . indexOf ( profile . id ) ;
73+ if ( idx !== - 1 ) {
74+ let client = this . online_members [ idx ] ;
75+ this . online_members . splice ( idx , 1 ) ;
76+
77+ idx = client . chats . indexOf ( this ) ;
78+ if ( idx !== - 1 )
79+ client . chats . splice ( idx , 1 ) ;
80+ }
4981
50- // }
51- // }
82+
83+ // cut this chat from the profile's chats list
84+ idx = profile . chats . indexOf ( this . chatlog . id ) ;
85+ if ( idx !== - 1 )
86+ profile . chats . splice ( idx , 1 ) ;
87+
88+ this . save ( ) ;
89+ }
90+
91+ connectMember ( client : Client ) {
92+ if ( this . online_members . includes ( client ) )
93+ return ;
94+
95+ this . online_members . push ( client ) ;
96+ client . chats . push ( this ) ;
97+ }
98+
99+ disconnectMember ( client : Client ) {
100+ let idx = this . online_members . indexOf ( client ) ;
101+ if ( idx !== - 1 )
102+ this . online_members . splice ( idx , 1 ) ;
103+
104+ idx = client . chats . indexOf ( this ) ;
105+ if ( idx !== - 1 )
106+ client . chats . splice ( idx , 1 ) ;
107+ }
108+
109+ writeMessage ( client : Client , content : string ) {
110+ const message :IMessage = {
111+ profile_id : client . profile . id ,
112+ name : client . name ,
113+ content
114+ } ;
115+
116+ this . messages . push ( message ) ;
117+ this . save ( ) ;
118+ }
119+ }
120+
121+ export default Chat ;
52122
53123// export class GlobalChat extends Chat {
54124// constructor() {
55- // super(global.clients);
125+ // // super(global.clients);
56126// }
57127// }
58128
59129// export class DirectChat extends Chat {
60130// constructor(client1:Client, client2:Client) {
61- // super([client1, client2]);
131+ // // super([client1, client2]);
62132// }
63133// }
64134
65135// export class GroupChat extends Chat {
66136// constructor(members: Client[]) {
67- // super(members);
137+ // // super(members);
68138// }
69139// }
0 commit comments