@@ -13,15 +13,18 @@ export function chatFind(chat_id:string) {
1313 return global . chats [ chat_id ] ;
1414}
1515
16- export function chatCreate ( members :IProfile [ ] = [ ] ) {
16+ export function chatCreate ( members :IProfile [ ] = [ ] , isPrivate = false ) {
1717 let chat_id :string = getRandomId ( global . chats ) ;
1818 if ( chat_id === null ) return null ;
1919
2020 let chatlog = new ChatLog ( ) ;
2121
2222 chatlog . _id = chat_id ;
23+ chatlog . private = isPrivate ;
24+
2325 let chat = new Chat ( chatlog ) ;
2426
27+
2528 for ( let member of members ) {
2629 chat . addMember ( member , null , true ) ;
2730 }
@@ -32,6 +35,12 @@ export function chatCreate(members:IProfile[] = []) {
3235 return chat ;
3336}
3437
38+ export interface SerializedChat {
39+ chat_id : string ,
40+ online_members : string [ ] ,
41+ members : string [ ]
42+ }
43+
3544export class Chat {
3645 chatlog : IChatLog ;
3746
@@ -100,6 +109,8 @@ export class Chat {
100109
101110 if ( ! client . chats . includes ( this ) )
102111 client . chats . push ( this ) ;
112+
113+ client . sendChatHistory ( this . chat_id , this . messages ) ;
103114 }
104115
105116 disconnectMember ( client : Client ) {
@@ -112,38 +123,41 @@ export class Chat {
112123 client . chats . splice ( idx , 1 ) ;
113124 }
114125
115- writeMessage ( client : Client , content : string ) {
116- const message :IMessage = {
117- profile_id : client . profile . id ,
118- name : client . name ,
119- content
120- } ;
126+ writeMessage ( content : string , author : Client | string = 'SYSTEM' ) {
127+ let name :string , profile_id :string ;
128+
129+ // author is not logged in/anonymous
130+ if ( typeof author === 'string' ) {
131+ name = author ;
132+ profile_id = null ;
133+ }
134+ else {
135+ name = author . name ;
136+ profile_id = author . profile ?. id ?? null ;
137+ }
138+
139+ const message = {
140+ name,
141+ content,
142+ profile_id
143+ }
144+
121145 this . messages . push ( message ) ;
122146 this . save ( ) ;
123147
124148 // broadcast to all online users
125149 this . online_members . forEach (
126- member => member . sendChatMessage ( this . chat_id , message )
150+ client => client . sendChatMessage ( this . chat_id , message )
127151 ) ;
128152 }
153+
154+ serialize ( ) :SerializedChat {
155+ return {
156+ chat_id : this . chat_id ,
157+ members : this . members . map ( profile_id => profile_id . toString ( ) ) ,
158+ online_members : this . online_members . map ( client => client . name )
159+ }
160+ }
129161}
130162
131- export default Chat ;
132-
133- // export class GlobalChat extends Chat {
134- // constructor() {
135- // // super(global.clients);
136- // }
137- // }
138-
139- // export class DirectChat extends Chat {
140- // constructor(client1:Client, client2:Client) {
141- // // super([client1, client2]);
142- // }
143- // }
144-
145- // export class GroupChat extends Chat {
146- // constructor(members: Client[]) {
147- // // super(members);
148- // }
149- // }
163+ export default Chat ;
0 commit comments