11import { Component , OnInit } from '@angular/core' ;
2- import { Chats , Messages } from 'api/collections' ;
3- import { Chat } from 'api/models' ;
2+ import { Chats , Messages , Users } from 'api/collections' ;
3+ import { Chat , Message } from 'api/models' ;
44import { NavController , PopoverController , ModalController } from 'ionic-angular' ;
5- import { Observable } from 'rxjs' ;
5+ import { MeteorObservable } from 'meteor-rxjs' ;
6+ import { Observable , Subscriber } from 'rxjs' ;
67import { MessagesPage } from '../messages/messages' ;
78import { ChatsOptionsComponent } from './chats-options' ;
89import { NewChatComponent } from './new-chat' ;
@@ -12,11 +13,13 @@ import { NewChatComponent } from './new-chat';
1213} )
1314export class ChatsPage implements OnInit {
1415 chats ;
16+ senderId : string ;
1517
1618 constructor (
1719 private navCtrl : NavController ,
1820 private popoverCtrl : PopoverController ,
1921 private modalCtrl : ModalController ) {
22+ this . senderId = Meteor . userId ( ) ;
2023 }
2124
2225 addChat ( ) : void {
@@ -25,23 +28,62 @@ export class ChatsPage implements OnInit {
2528 }
2629
2730 ngOnInit ( ) {
28- this . chats = Chats
29- . find ( { } )
30- . mergeMap ( ( chats : Chat [ ] ) =>
31- Observable . combineLatest (
32- ...chats . map ( ( chat : Chat ) =>
33- Messages
34- . find ( { chatId : chat . _id } )
35- . startWith ( null )
36- . map ( messages => {
37- if ( messages ) chat . lastMessage = messages [ 0 ] ;
38- return chat ;
39- } )
40- )
41- )
42- ) . zone ( ) ;
31+ this . chats = this . findChats ( ) ;
4332 }
4433
34+ findChats ( ) : Observable < Chat [ ] > {
35+ // Find chats and transform them
36+ return Chats . find ( ) . map ( chats => {
37+ chats . forEach ( chat => {
38+ chat . title = '' ;
39+ chat . picture = '' ;
40+
41+ const receiverId = chat . memberIds . find ( memberId => memberId !== this . senderId ) ;
42+ const receiver = Users . findOne ( receiverId ) ;
43+
44+ if ( receiver ) {
45+ chat . title = receiver . profile . name ;
46+ chat . picture = receiver . profile . picture ;
47+ }
48+
49+ // This will make the last message reactive
50+ this . findLastChatMessage ( chat . _id ) . subscribe ( ( message ) => {
51+ chat . lastMessage = message ;
52+ } ) ;
53+ } ) ;
54+
55+ return chats ;
56+ } ) ;
57+ }
58+
59+ findLastChatMessage ( chatId : string ) : Observable < Message > {
60+ return Observable . create ( ( observer : Subscriber < Message > ) => {
61+ const chatExists = ( ) => ! ! Chats . findOne ( chatId ) ;
62+
63+ // Re-compute until chat is removed
64+ MeteorObservable . autorun ( ) . takeWhile ( chatExists ) . subscribe ( ( ) => {
65+ Messages . find ( { chatId } , {
66+ sort : { createdAt : - 1 }
67+ } ) . subscribe ( {
68+ next : ( messages ) => {
69+ // Invoke subscription with the last message found
70+ if ( ! messages . length ) {
71+ return ;
72+ }
73+
74+ const lastMessage = messages [ 0 ] ;
75+ observer . next ( lastMessage ) ;
76+ } ,
77+ error : ( e ) => {
78+ observer . error ( e ) ;
79+ } ,
80+ complete : ( ) => {
81+ observer . complete ( ) ;
82+ }
83+ } ) ;
84+ } ) ;
85+ } ) ;
86+ }
4587
4688 showMessages ( chat ) : void {
4789 this . navCtrl . push ( MessagesPage , { chat} ) ;
0 commit comments