import type { APIClient } from '@wireapp/api-client' ; import { ClientMismatch , UserClients } from '@wireapp/api-client/src/conversation' ; import type { UserPreKeyBundleMap } from '@wireapp/api-client/src/user/' ; import { GenericMessage } from '@wireapp/protocol-messaging' ; import { MessageService } from '../conversation/message/MessageService' ; import type { CryptographyService } from '../cryptography/' ; export class BroadcastService { private readonly messageService : MessageService ; constructor ( private readonly apiClient , private readonly cryptographyService ) { this . messageService = new MessageService ( this . apiClient , this . cryptographyService ) ; } public async getPreKeyBundlesFromTeam ( teamId , skipOwnClients = false ) { const { members : teamMembers } = await this . apiClient . teams . member . api . getAllMembers ( teamId ) ; let members = teamMembers . map ( member => ( { id : member . user } ) ) ; if ( skipOwnClients ) { const selfUser = await this . apiClient . self . api . getSelf ( ) ; members = members . filter ( member => member . id !== selfUser . id ) ; } const preKeys = await Promise . all ( members . map ( member => this . apiClient . user . api . getUserPreKeys ( member . id ) ) ) ; return preKeys . reduce < UserPreKeyBundleMap > ( ( bundleMap , bundle ) => { bundleMap [ bundle . user ] = { } ; for ( const client of bundle . clients ) { bundleMap [ bundle . user ] [ client . client ] = client . prekey ; } return bundleMap ; } , { } ) ; } public async broadcastGenericMessage ( genericMessage , recipients , sendAsProtobuf ? ) { const plainTextArray = GenericMessage . encode ( genericMessage ) . finish ( ) ; return this . messageService . sendMessage ( this . apiClient . validatedClientId , recipients , plainTextArray , { sendAsProtobuf , reportMissing : Object . keys ( recipients ) , } ) ; } }