import { EventBus } from '../core/modules/EventBus' ; import Storage from '../modules/storage/Storage' ; import DateModule from '../modules/DateModule' ; import { Data } from '../modules/Data/Data' ; import { Api } from '../modules/Api/Api' ; import InboxMessagesPublic from '../modules/InboxMessagesPublic' ; import { EVENT_ON_UPDATE_INBOX_MESSAGES } from '../constants' ; export default class InboxMessages { eventBus : EventBus ; data : Data ; api : Api ; storage : Storage ; storeName : TInboxMessagesStoreName ; dateModule : DateModule ; constructor ( eventBus , data , api , storage = new Storage ( ) , dateModule = new DateModule ( ) ) { this . eventBus = eventBus ; this . data = data ; this . api = api ; this . storage = storage ; this . storeName = 'inboxMessages' ; this . dateModule = dateModule ; } /**
     * Get inbox messages by api
     */ private async getInboxMessages ( ) { const response = await this . api . getInboxMessages ( ) ; await this . storeGetInboxMessagesRequestParams ( response . next , response . new_inbox ) ; return response ; } param next param newMessagesCount private async storeGetInboxMessagesRequestParams ( next , newMessagesCount ) { this . dateModule . date = new Date ( ) ; await this . data . setInboxLastRequestTime ( this . dateModule . getUtcTimestamp ( ) ) ; await this . data . setInboxLastRequestCode ( next ) ; await this . data . setInboxNewMessagesCount ( newMessagesCount ) ; } param messages private async putServerMessages ( messages ) { const putTransactions = messages . map ( async ( message ) => { const localMessage = await this . storage . get < IInboxMessage , { } > ( this . storeName , message . inbox_id , { } ) ; if ( 'status' in localMessage ) { message . status = localMessage . status ; } return this . putMessage ( message ) ; } ) ; return Promise . all ( putTransactions ) ; } /**
     * Create or update inbox message
     */ putMessage ( message ) { return < Promise < string > > this . storage . put ( this . storeName , message ) ; } param messages putBulkMessages ( messages ) { const putTransactions = messages . map ( message => this . putMessage ( message ) ) ; return Promise . all ( putTransactions ) ; } param codes deleteMessages ( codes ) { const deleteTransactions = codes . map ( code => this . storage . delete ( this . storeName , code ) ) ; return Promise . all ( deleteTransactions ) ; } /**
     * Delete expired by removal time messages
     */ async deleteExpiredMessages ( ) { this . dateModule . date = new Date ( ) ; const upperBound = this . dateModule . getTimestamp ( ) . toString ( ) ; const allMessages = await this . storage . getAll < IInboxMessage > ( this . storeName ) ; const codesToDelete = allMessages . filter ( ( msg ) => msg . rt > upperBound ) . map ( msg => msg . inbox_id ) ; return this . deleteMessages ( codesToDelete ) ; } param code getMessage ( code ) { return this . storage . get < IInboxMessage , IInboxMessage > ( this . storeName , code ) ; } /**
     * Get all read messages
     */ async getReadOpenMessages ( ) { const allMessages = await this . storage . getAll < IInboxMessage > ( this . storeName ) ; return allMessages . filter ( ( msg ) => < TInboxMessageStatusRead > msg . status === 2 || < TInboxMessageStatusOpen > msg . status === 3 ) ; } /**
     * Get all unread messages
     */ async getDeliveredMessages ( ) { const allMessages = await this . storage . getAll < IInboxMessage > ( this . storeName ) ; return allMessages . filter ( ( msg ) => < TInboxMessageStatusDelivered > msg . status === 1 ) ; } /**
     * All messages count
     */ async messagesCount ( ) { return this . storage . count ( this . storeName ) ; } /**
     * Get count of messages with status "Delivered"
     */ async getDeliveredMessagesCount ( ) { const deliveredStatus = 1 ; const statusIndexName = 'status' ; return this . storage . countByIndex ( this . storeName , statusIndexName , deliveredStatus ) ; } /**
     * Get count of messages with status "Read"
     */ async getReadMessagesCount ( ) { const readStatus = 2 ; const statusIndexName = 'status' ; return this . storage . countByIndex ( this . storeName , statusIndexName , readStatus ) ; } /**
     * Get count of messages with status "Delivered" and "Read"
     */ async getDeliveredReadMessagesCount ( ) { const [ readStatus , openStatus ] = [ 2 , 3 ] ; const keyRange = IDBKeyRange . bound ( readStatus , openStatus ) ; const statusIndexName = 'status' ; return this . storage . countByIndex ( this . storeName , statusIndexName , keyRange ) ; } /**
     * Load messages and sync with locally
     */ async updateMessages ( ) { const response = await this . getInboxMessages ( ) ; await this . deleteExpiredMessages ( ) ; await this . deleteMessages ( response . deleted ) ; await this . putServerMessages ( response . messages ) ; this . eventBus . dispatchEvent ( 'update-inbox-messages' , { messages : new InboxMessagesPublic ( this . data , this . api , this ) } ) ; } }