import { Injectable } from '@angular/core' ; import { Http } from '@angular/http' ; import { map } from 'rxjs/operators/map' ; import { AngularFireAuth } from 'angularfire2/auth' ; import { AngularFireDatabase , AngularFireList , AngularFireObject , AngularFireAction , DatabaseSnapshot } from 'angularfire2/database' ; import { BaseService } from "./base.service" ; import { Chat } from './../models/chat.model' ; import * as firebase from 'firebase/app' ; import { Observable } from 'rxjs/Observable' ; @ Injectable ( ) export class ChatService extends BaseService { chats : AngularFireList < Chat > ; constructor ( public afAuth , public db , public http ) { super ( ) ; this . setChats ( ) ; } private setChats ( ) { this . afAuth . authState . subscribe ( ( authUser ) => { if ( authUser ) { this . chats = this . db . list < Chat > ( `/chats/${ authUser . uid }` , ( ref ) => ref . orderByChild ( 'timestamp' ) ) ; } } ) ; } create ( chat , userId1 , userId2 ) { return this . db . object < Chat > ( `/chats/${ userId1 }/${ userId2 }` ) . set ( chat ) . catch ( this . handlePromiseError ) ; } getDeepChat ( userId1 , userId2 ) { return this . db . object < Chat > ( `/chats/${ userId1 }/${ userId2 }` ) ; } updatePhoto ( chat , chatPhoto , recipientUserPhoto ) { if ( chatPhoto != recipientUserPhoto ) { return chat . update ( { photo : recipientUserPhoto } ) . then ( ( ) => { return true ; } ) . catch ( this . handlePromiseError ) ; } return Promise . resolve ( false ) ; } }