import type { AxiosRequestConfig } from 'axios' ; import type { HttpClient , ProgressCallback } from '../http' ; import type { Client , Self , UserPreKeyList , UserPreKeyDataList , UserSeenByService , ClientSeenByService , ServiceMessage , } from './Service' ; import type { PreKey } from '../auth' ; import { AssetOptions , AssetAPI } from '../asset' ; export class ServicesAPI { private readonly assetAPI : AssetAPI ; constructor ( private readonly client ) { this . assetAPI = new AssetAPI ( this . client ) ; } public static readonly URL = { ASSETS : 'assets' , BOT : '/bot' , CLIENT : 'client' , CLIENTS : 'clients' , CONVERSATION : 'conversation' , MESSAGES : 'messages' , PREKEYS : 'prekeys' , SELF : 'self' , USERS : 'users' , } ; /**
     * Delete the service, thereby removing it from the conversation it is in.
     *
     * The service will receive a final `conversation.member-leave` message.
     */ public async deleteSelf ( ) { const config = { method : 'delete' , url : `${ ServicesAPI . URL . BOT }/${ ServicesAPI . URL . SELF }` , } ; await this . client . sendJSON ( config ) ; } /**
     * Fetch the service's own client information
     */ public async getClient ( ) { const config = { method : 'get' , url : `${ ServicesAPI . URL . BOT }/${ ServicesAPI . URL . CLIENT }` , } ; const response = await this . client . sendJSON < Client > ( config ) ; return response . data ; } /**
     * List the service's remaining prekey IDs.
     */ public async getClientPreKeys ( ) { const config = { method : 'get' , url : `${ ServicesAPI . URL . BOT }/${ ServicesAPI . URL . CLIENT }/${ ServicesAPI . URL . PREKEYS }` , } ; const response = await this . client . sendJSON < number [ ] > ( config ) ; return response . data ; } /**
     * Fetch a user's list of clients.
     */ public async getClientsByUserId ( userId ) { const config = { method : 'get' , url : `${ ServicesAPI . URL . BOT }/${ ServicesAPI . URL . USERS }/${ userId }${ ServicesAPI . URL . CLIENTS }` , } ; const response = await this . client . sendJSON < ClientSeenByService [ ] > ( config ) ; return response . data ; } /**
     * Fetch a user's list of clients.
     */ public async getConversation ( userId ) { const config = { method : 'get' , url : `${ ServicesAPI . URL . BOT }/${ ServicesAPI . URL . USERS }/${ userId }${ ServicesAPI . URL . CLIENTS }` , } ; const response = await this . client . sendJSON < ClientSeenByService [ ] > ( config ) ; return response . data ; } /**
     * Fetch the service's own user profile information.
     */ public async getSelf ( ) { const config = { method : 'get' , url : `${ ServicesAPI . URL . BOT }/${ ServicesAPI . URL . SELF }` , } ; const response = await this . client . sendJSON < Self > ( config ) ; return response . data ; } /**
     * Claim prekeys from one or more clients of one or more users.
     *
     * The result is a nested JSON object structure where the first-level keys
     * are user IDs and the second-level keys client IDs. The values paired with
     * the client IDs are the base64-encoded prekeys.
     */ public async postUserPreKeysList ( userPreKeyList ) { const config = { data : userPreKeyList , method : 'post' , url : `${ ServicesAPI . URL . BOT }/${ ServicesAPI . URL . CLIENT }/${ ServicesAPI . URL . PREKEYS }` , } ; const response = await this . client . sendJSON < UserPreKeyDataList > ( config ) ; return response . data ; } /**
     * Fetch other user's profiles.
     */ public async getUsers ( ids ) { const config = { method : 'get' , params : { ids , } , url : `${ ServicesAPI . URL . BOT }/${ ServicesAPI . URL . USERS }` , } ; const response = await this . client . sendJSON < UserSeenByService [ ] > ( config ) ; return response . data ; } /**
     * List the service's remaining prekey IDs.
     */ public async postClientPreKeys ( preKeys ) { const config = { data : { prekeys : preKeys } , method : 'post' , url : `${ ServicesAPI . URL . BOT }/${ ServicesAPI . URL . CLIENT }/${ ServicesAPI . URL . PREKEYS }` , } ; await this . client . sendJSON ( config ) ; } /**
     * Upload an asset.
     * Note that resumable uploads are not currently supported for services.
     */ public postAsset ( asset , options ? , progressCallback ? ) { return this . assetAPI . postServiceAsset ( asset , options , progressCallback ) ; } /**
     * Download an asset.
     */ public getAsset ( assetId , token ? , forceCaching = false , progressCallback ? ) { return this . assetAPI . getServiceAsset ( assetId , token , forceCaching , progressCallback ) ; } /**
     * Delete an asset.
     */ public async deleteAsset ( assetId ) { const config = { method : 'delete' , url : `${ ServicesAPI . URL . BOT }/${ ServicesAPI . URL . ASSETS }/${ assetId }` , } ; await this . client . sendJSON ( config ) ; } /**
     * Post an end-to-end encrypted generic message to the conversation the service is in.
     */ public async postMessage ( messageData ) { const config = { data : messageData , method : 'post' , url : `${ ServicesAPI . URL . BOT }/${ ServicesAPI . URL . MESSAGES }` , } ; await this . client . sendJSON < ServiceMessage > ( config ) ; } }