11import { ModalWindowStateService } from '../../services/states/modalWindowState.service' ;
2- import { Component , OnDestroy , OnInit } from '@angular/core' ;
2+ import {
3+ Component ,
4+ OnChanges ,
5+ OnDestroy ,
6+ OnInit ,
7+ Sanitizer ,
8+ SecurityContext ,
9+ SimpleChanges
10+ } from '@angular/core' ;
311import { TokensService } from '../../services/messenger/tokens.service' ;
412import { Chat } from '../../types/models/Chat' ;
513import { CommunitiesService } from '../../services/api/communities.service' ;
@@ -15,11 +23,21 @@ import { ValidationService } from '../../services/messenger/validation.service';
1523import * as signalR from '@microsoft/signalr' ;
1624import { EditMessageNotification } from '../../types/models/EditMessageNotification' ;
1725import { DeleteMessageNotification } from '../../types/models/DeleteMessageNotification' ;
18- import { firstValueFrom , Subject , takeUntil } from 'rxjs' ;
26+ import {
27+ Subject ,
28+ takeUntil ,
29+ of ,
30+ BehaviorSubject ,
31+ firstValueFrom ,
32+ distinctUntilKeyChanged
33+ } from 'rxjs' ;
1934import { DisplayNameColours } from 'src/app/types/enums/DisplayNameColours' ;
2035import { DeleteMessageCommand } from 'src/app/types/requests/DeleteMessageCommand' ;
2136import ApiBaseService from 'src/app/services/api/apiBase.service' ;
2237import { SendMessageResponse } from '../../types/responses/SendMessageResponse' ;
38+ import { ReplyStateSerivce } from 'src/app/services/states/replyState.service' ;
39+ import { Reply } from 'src/app/types/models/Reply' ;
40+ import { DomSanitizer , SafeUrl } from '@angular/platform-browser' ;
2341
2442@Component ( {
2543 selector : 'app-chats' ,
@@ -36,7 +54,8 @@ export class ChatsComponent implements OnInit, OnDestroy {
3654 private _router : Router ,
3755 private _validationService : ValidationService ,
3856 private _apiBaseService : ApiBaseService ,
39- public _modalWindowStateService : ModalWindowStateService
57+ public _modalWindowStateService : ModalWindowStateService ,
58+ public _replyStateService : ReplyStateSerivce
4059 ) { }
4160
4261 private connectionBuilder : signalR . HubConnectionBuilder = new signalR . HubConnectionBuilder ( ) ;
@@ -68,16 +87,19 @@ export class ChatsComponent implements OnInit, OnDestroy {
6887 title : ''
6988 } ;
7089
71- public activeChatId = '' ;
90+ public activeChatId : string = '' ;
7291 public messages : Message [ ] = [ ] ;
7392
7493 public messageAttachment : File | null = null ;
94+ public messageAttachmentBlobUrl : string | ArrayBuffer | null = '' ;
7595 public messageText = '' ;
76- public messageAttachmentUrl = '' ;
96+ public messageAttachmentUrl : string = '' ;
7797 public searchChatQuery = '' ;
7898 public searchMessagesQuery = '' ;
7999 public chatFilter = 'All chats' ;
80100
101+ public activeChatBehaviorSubject = new BehaviorSubject < Chat > ( this . activeChat ) ;
102+
81103 componentDestroyed$ : Subject < boolean > = new Subject ( ) ;
82104
83105 public get routingConstants ( ) : typeof RoutingConstants {
@@ -89,6 +111,14 @@ export class ChatsComponent implements OnInit, OnDestroy {
89111 }
90112
91113 initializeView ( ) : void {
114+ this . activeChatBehaviorSubject
115+ . pipe ( distinctUntilKeyChanged ( 'chatId' ) )
116+ . subscribe ( ( ) => {
117+ this . _replyStateService . setReplyNull ( )
118+ this . messageAttachment = null
119+ this . messageAttachmentBlobUrl = null
120+ } ) ;
121+
92122 const tokens = this . _tokensService . getTokens ( ) ;
93123
94124 if ( ! tokens ) {
@@ -144,8 +174,32 @@ export class ChatsComponent implements OnInit, OnDestroy {
144174 . catch ( ( err ) => console . error ( err . toString ( ) ) ) ;
145175 }
146176
147- imageSelected ( event : any ) {
177+ onReplyClick (
178+ messageId : string ,
179+ displayName : string ,
180+ text : string ,
181+ displayNameColour : DisplayNameColours
182+ ) {
183+ const replyEntity = new Reply ( messageId , displayName , text , displayNameColour ) ;
184+
185+ this . _replyStateService . setReply ( replyEntity ) ;
186+ this . scrollToEnd ( ) ;
187+ }
188+
189+ async imageSelected ( event : any ) {
148190 this . messageAttachment = event . target . files [ 0 ] ;
191+
192+ var reader = new FileReader ( ) ;
193+ reader . onload = ( ) => {
194+ console . log ( reader . result ) ;
195+ this . messageAttachmentBlobUrl = reader . result
196+ } ;
197+ reader . readAsDataURL ( event . target . files [ 0 ] ) ;
198+ }
199+
200+ removeImageSelected ( ) {
201+ this . messageAttachment = null ;
202+ this . messageAttachmentBlobUrl = null ;
149203 }
150204
151205 onOpenImageClick ( imageLink : string ) : void {
@@ -275,8 +329,13 @@ export class ChatsComponent implements OnInit, OnDestroy {
275329 }
276330
277331 this . activeChatId = chatId ;
278- this . activeChat = this . chats . filter ( ( x ) => x . chatId === this . activeChatId ) [ 0 ] ;
332+ const newActiveChat = this . chats . filter ( ( x ) => x . chatId === this . activeChatId ) [ 0 ] ;
333+ this . activeChat = newActiveChat ;
334+
335+ this . activeChatBehaviorSubject . next ( newActiveChat ) ;
336+
279337 this . getChatMessages ( this . activeChatId ) ;
338+ this . scrollToEnd ( ) ;
280339 }
281340
282341 chatContainsMessages ( chat : Chat ) : boolean {
@@ -426,6 +485,11 @@ export class ChatsComponent implements OnInit, OnDestroy {
426485 sendMessageFormData . append ( 'messageText' , newMessageText ) ;
427486 sendMessageFormData . append ( 'chatId' , this . activeChatId ) ;
428487 sendMessageFormData . append ( 'messageId' , messageId ) ;
488+ sendMessageFormData . append (
489+ 'inReplayToAuthor' ,
490+ this . _replyStateService . reply ?. displayName ?? ''
491+ ) ;
492+ sendMessageFormData . append ( 'inReplayToText' , this . _replyStateService . reply ?. text ?? '' ) ;
429493
430494 if ( this . messageAttachment ) {
431495 sendMessageFormData . append ( 'Attachment' , this . messageAttachment ) ;
@@ -440,13 +504,17 @@ export class ChatsComponent implements OnInit, OnDestroy {
440504 newMessageText ,
441505 isoString ,
442506 true ,
443- tokens . userProfilePictureUrl
507+ tokens . userProfilePictureUrl ,
508+ this . _replyStateService . reply ?. displayName ?? null ,
509+ this . _replyStateService . reply ?. text ?? null
444510 ) ;
445511
446512 this . clearMessageInput ( ) ;
447513
448514 this . messages . push ( newMessage ) ;
449515
516+ this . _replyStateService . setReplyNull ( ) ;
517+
450518 const sendMessage$ = this . _messagesService . sendMessage ( sendMessageFormData ) ;
451519
452520 const response = await firstValueFrom < SendMessageResponse > ( sendMessage$ ) ;
0 commit comments