@@ -3,19 +3,53 @@ import {
3
3
OnInit ,
4
4
Inject
5
5
} from '@angular/core' ;
6
+ import * as _ from 'lodash' ;
6
7
import { Observable } from 'rxjs' ;
7
8
import { Thread } from '../thread/thread.model' ;
9
+ import { Message } from '../message/message.model' ;
8
10
import { ThreadsService } from './../thread/threads.service' ;
11
+ import { MessagesService } from './../message/messages.service' ;
9
12
10
13
@Component ( {
11
14
selector : 'chat-threads' ,
12
15
templateUrl : './chat-threads.component.html' ,
13
16
styleUrls : [ './chat-threads.component.css' ]
14
17
} )
15
- export class ChatThreadsComponent {
18
+ export class ChatThreadsComponent implements OnInit {
16
19
threads : Observable < any > ;
20
+ unreadThread : any ;
17
21
18
- constructor ( public threadsService : ThreadsService ) {
22
+ constructor ( public threadsService : ThreadsService , public messagesService : MessagesService ) {
19
23
this . threads = threadsService . orderedThreads ;
20
24
}
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
+ }
21
55
}
0 commit comments