import { default as InboxMessagesModel } from '../models/InboxMessages' ; import DateModule from './DateModule' ; import { Api } from './Api/Api' ; import { Data } from './Data/Data' ; export default class InboxMessages implements IInboxMessages { private readonly data : Data ; private readonly api : Api ; private readonly inboxModel : InboxMessagesModel ; private dateModule : DateModule ; constructor ( data , api , inboxModel , dateModule = new DateModule ( ) ) { this . data = data ; this . api = api ; this . inboxModel = inboxModel ; this . dateModule = dateModule ; this . publicMessageBuilder = this . publicMessageBuilder . bind ( this ) ; } param actionParams private messageTypeFactory ( actionParams ) { let messageType = 0 ; if ( 'h' in actionParams || 'rm' in actionParams || 'r' in actionParams ) { ( < TInboxMessageTypeRichmedia > messageType ) = 1 ; } else if ( 'l' in actionParams && actionParams . l != null ) { if ( actionParams . l . startsWith ( 'http' ) ) { ( < TInboxMessageTypeURL > messageType ) = 2 ; } else { ( < TInboxMessageTypeDeeplink > messageType ) = 3 ; } } return messageType ; } param codes param messages param status private async updateMessagesStatusWithCodes ( codes , messages , status ) { const updatedMessages = [ ] ; const inboxStatusQueries = [ ] ; messages . forEach ( async ( msg ) => { if ( codes . indexOf ( msg . inbox_id ) === - 1 ) { return ; } msg . status = status ; updatedMessages . push ( msg ) ; inboxStatusQueries . push ( this . api . inboxStatus ( msg . order , msg . status ) ) ; } ) ; await this . inboxModel . putBulkMessages ( updatedMessages ) ; await Promise . all ( inboxStatusQueries ) ; } param message async publicMessageBuilder ( message ) { const imageUrl = message . image || await this . data . getDefaultNotificationImage ( ) ; const title = message . title || await this . data . getDefaultNotificationTitle ( ) ; this . dateModule . date = new Date ( parseInt ( message . send_date ) * 1000 ) ; this . dateModule . setLocal ( ) ; return { title , imageUrl , code : message . inbox_id , message : message . text , sendDate : this . dateModule . date . toISOString ( ) , type : this . messageTypeFactory ( JSON . parse ( message . action_params ) ) , isRead : < TInboxMessageStatusRead > message . status === 2 || < TInboxMessageStatusOpen > message . status === 3 , isActionPerformed : < TInboxMessageStatusOpen > message . status === 3 } ; } /**
     * Count of messages with no action performed
     */ messagesWithNoActionPerformedCount ( ) { return this . inboxModel . getDeliveredReadMessagesCount ( ) ; } /**
     * All unread messages
     */ unreadMessagesCount ( ) { return this . inboxModel . getDeliveredMessagesCount ( ) ; } /**
     * All messages count
     */ messagesCount ( ) { return this . inboxModel . messagesCount ( ) ; } /**
     * Get all active messages
     */ async loadMessages ( ) { const readMessages = await this . inboxModel . getReadOpenMessages ( ) ; const unreadMessages = await this . inboxModel . getDeliveredMessages ( ) ; const buildMessagePromises = [ ... readMessages , ... unreadMessages ] . sort ( ( msgA , msgB ) => { return parseInt ( msgB . send_date , 10 ) - parseInt ( msgA . send_date , 10 ) ; } ) . sort ( ( msgA , msgB ) => { return parseInt ( msgB . order || '0' , 10 ) - parseInt ( msgA . order || '0' , 10 ) ; } ) . map ( this . publicMessageBuilder ) ; return Promise . all ( buildMessagePromises ) ; } param codes async readMessagesWithCodes ( codes ) { const unreadMessages = await this . inboxModel . getDeliveredMessages ( ) ; const statusRead = 2 ; await this . updateMessagesStatusWithCodes ( codes , unreadMessages , statusRead ) ; } param code async performActionForMessageWithCode ( code ) { const message = await this . inboxModel . getMessage ( code ) ; const actionParams = JSON . parse ( message . action_params ) ; const messageType = this . messageTypeFactory ( actionParams ) ; if ( < TInboxMessageTypeURL > messageType === 2 && actionParams . l != null ) { document . location . href = actionParams . l ; } else if ( < TInboxMessageTypeDeeplink > messageType === 3 && actionParams . l != null ) { window . history . go ( actionParams . l ) ; } ( < TInboxMessageStatusOpen > message . status ) = 3 ; await this . inboxModel . putMessage ( message ) ; await this . api . inboxStatus ( message . order , message . status ) ; } param codes async deleteMessagesWithCodes ( codes ) { const readMessages = await this . inboxModel . getReadOpenMessages ( ) ; const unreadMessages = await this . inboxModel . getDeliveredMessages ( ) ; const statusDeleted = 4 ; await this . updateMessagesStatusWithCodes ( codes , [ ... readMessages , ... unreadMessages ] , statusDeleted ) ; } /**
     * Sync inbox messages with server
     */ async syncMessages ( ) { await this . inboxModel . updateMessages ( ) ; } }