Skip to content

Commit 5c599b8

Browse files
committed
Particular thread's unread count: Issue#49
1 parent d2fa852 commit 5c599b8

File tree

4 files changed

+39
-3
lines changed

4 files changed

+39
-3
lines changed

src/app/chat-thread/chat-thread.component.html

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
<div class="media-body">
77
<h5 class="media-heading contact-name">{{thread.name}}
88
<span *ngIf="selected">&bull;</span>
9+
<span class="badge">{{ unread?.sum }}</span>
910
</h5>
1011
<small class="message-preview">{{thread.lastMessage.text}}</small>
1112
</div>

src/app/chat-thread/chat-thread.component.ts

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import { Thread } from '../thread/thread.model';
1616
})
1717
export class ChatThreadComponent implements OnInit {
1818
@Input() thread: Thread;
19+
@Input() unread: any;
1920
selected = false;
2021

2122
constructor(public threadsService: ThreadsService) {

src/app/chat-threads/chat-threads.component.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
<chat-thread
66
*ngFor="let thread of threads | async"
7-
[thread]="thread">
7+
[thread]="thread" [unread]="unreadThread[thread.id]">
88
</chat-thread>
99

1010
</div>

src/app/chat-threads/chat-threads.component.ts

+36-2
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,53 @@ import {
33
OnInit,
44
Inject
55
} from '@angular/core';
6+
import * as _ from 'lodash';
67
import { Observable } from 'rxjs';
78
import { Thread } from '../thread/thread.model';
9+
import { Message } from '../message/message.model';
810
import { 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

Comments
 (0)