-
Notifications
You must be signed in to change notification settings - Fork 185
/
chats.ts
43 lines (38 loc) · 1.01 KB
/
chats.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import { Component, OnInit } from '@angular/core';
import { Chats, Messages } from 'api/collections';
import { Chat } from 'api/models';
import { NavController } from 'ionic-angular';
import { Observable } from 'rxjs';
import { MessagesPage } from '../messages/messages';
@Component({
templateUrl: 'chats.html'
})
export class ChatsPage implements OnInit {
chats;
constructor(private navCtrl: NavController) {
}
ngOnInit() {
this.chats = Chats
.find({})
.mergeMap((chats: Chat[]) =>
Observable.combineLatest(
...chats.map((chat: Chat) =>
Messages
.find({chatId: chat._id})
.startWith(null)
.map(messages => {
if (messages) chat.lastMessage = messages[0];
return chat;
})
)
)
).zone();
}
showMessages(chat): void {
this.navCtrl.push(MessagesPage, {chat});
}
removeChat(chat: Chat): void {
Chats.remove({_id: chat._id}).subscribe(() => {
});
}
}