@@ -3,19 +3,53 @@ import {
33 OnInit ,
44 Inject
55} from '@angular/core' ;
6+ import * as _ from 'lodash' ;
67import { Observable } from 'rxjs' ;
78import { Thread } from '../thread/thread.model' ;
9+ import { Message } from '../message/message.model' ;
810import { ThreadsService } from './../thread/threads.service' ;
11+ import { MessagesService } from './../message/messages.service' ;
912
1013@Component ( {
1114 selector : 'chat-threads' ,
1215 templateUrl : './chat-threads.component.html' ,
1316 styleUrls : [ './chat-threads.component.css' ]
1417} )
15- export class ChatThreadsComponent {
18+ export class ChatThreadsComponent implements OnInit {
1619 threads : Observable < any > ;
20+ unreadThread : any ;
1721
18- constructor ( public threadsService : ThreadsService ) {
22+ constructor ( public threadsService : ThreadsService , public messagesService : MessagesService ) {
1923 this . threads = threadsService . orderedThreads ;
2024 }
25+
26+ ngOnInit ( ) {
27+ this . messagesService . messages
28+ . combineLatest (
29+ this . threadsService . currentThread ,
30+ ( messages : Message [ ] , currentThread : Thread ) =>
31+ [ currentThread , messages ] )
32+
33+ . subscribe ( ( [ currentThread , messages ] : [ Thread , Message [ ] ] ) => {
34+ this . unreadThread = { } ;
35+ _ . reduce (
36+ messages ,
37+ ( sum : number , m : Message ) => {
38+ const messageIsInCurrentThread : boolean = m . thread &&
39+ currentThread &&
40+ ( currentThread . id === m . thread . id ) ;
41+ if ( m && ! m . isRead && ! messageIsInCurrentThread ) {
42+ sum = sum + 1 ;
43+ let cid = m . thread . id ;
44+ if ( this . unreadThread [ cid ] ) {
45+ this . unreadThread [ cid ] . sum = this . unreadThread [ cid ] . sum + 1 ;
46+ } else {
47+ this . unreadThread [ cid ] = { sum : 1 } ;
48+ }
49+ }
50+ return sum ;
51+ } ,
52+ 0 ) ;
53+ } ) ;
54+ }
2155}
0 commit comments