import type { APIClient } from '@wireapp/api-client' ; import type { QualifiedId , User } from '@wireapp/api-client/src/user/' ; import { Availability , GenericMessage } from '@wireapp/protocol-messaging' ; import UUID from 'uuidjs' ; import type { AvailabilityType , BroadcastService } from '../broadcast/' ; import { isQualifiedIdArray } from '../util/TypePredicateUtil' ; import { ConnectionService } from '../connection' ; import { ConnectionStatus } from '@wireapp/api-client/src/connection' ; import { ConversationService } from '../conversation' ; import { UserPreKeyBundleMap } from '@wireapp/api-client/src/user/' ; export class UserService { private readonly apiClient : APIClient ; private readonly broadcastService : BroadcastService ; private readonly connectionService : ConnectionService ; private readonly conversationService : ConversationService ; constructor ( apiClient , broadcastService , conversationService , connectionService ) { this . apiClient = apiClient ; this . broadcastService = broadcastService ; this . connectionService = connectionService ; this . conversationService = conversationService ; } public getUser ( userId ) { return this . apiClient . user . api . getUser ( userId as QualifiedId ) ; } public async getUsers ( userIds ) { if ( ! userIds . length ) { return [ ] ; } return isQualifiedIdArray ( userIds ) ? this . apiClient . user . api . postListUsers ( { qualified_ids : userIds } ) : this . apiClient . user . api . getUsers ( { ids : userIds } ) ; } public async setAvailability ( teamId , type , sendAsProtobuf ? ) { const preKeyBundlesFromTeam = await this . broadcastService . getPreKeyBundlesFromTeam ( teamId ) ; const connections = await this . connectionService . getConnections ( ) ; const acceptedConnections = connections . filter ( connection => connection . status === ConnectionStatus . ACCEPTED ) ; const preKeyBundlePromises = acceptedConnections . map ( connection => { const mappedConnection = { userId : connection . to , conversationId : connection . conversation , } ; return this . conversationService . getPreKeyBundleMap ( mappedConnection . conversationId , [ mappedConnection . userId ] ) ; } ) ; const preKeyBundlesFromConnections = await Promise . all ( preKeyBundlePromises ) ; const allPreKeyBundles = preKeyBundlesFromConnections . reduce ( ( accumulator , preKeyBundleMap ) => { return { ... accumulator , ... preKeyBundleMap , } ; } , preKeyBundlesFromTeam ) ; const genericMessage = GenericMessage . create ( { availability : new Availability ( { type } ) , messageId : UUID . genV4 ( ) . toString ( ) , } ) ; await this . broadcastService . broadcastGenericMessage ( genericMessage , allPreKeyBundles , sendAsProtobuf ) ; } }