All Downloads are FREE. Search and download functionalities are using the official Maven repository.

commonMain.net.folivo.trixnity.clientserverapi.client.KeyApiClient.kt Maven / Gradle / Ivy

There is a newer version: 4.11.2
Show newest version
package net.folivo.trixnity.clientserverapi.client

import kotlinx.serialization.json.Json
import kotlinx.serialization.json.encodeToJsonElement
import net.folivo.trixnity.clientserverapi.model.keys.*
import net.folivo.trixnity.core.model.RoomId
import net.folivo.trixnity.core.model.UserId
import net.folivo.trixnity.core.model.keys.*

interface KeyApiClient {
    /**
     * @see [SetKeys]
     */
    suspend fun setKeys(
        deviceKeys: SignedDeviceKeys? = null,
        oneTimeKeys: Keys? = null,
        fallbackKeys: Keys? = null,
        asUserId: UserId? = null
    ): Result>

    /**
     * @see [GetKeys]
     */
    suspend fun getKeys(
        deviceKeys: Map>,
        timeout: Long? = 10000,
        asUserId: UserId? = null
    ): Result

    /**
     * @see [ClaimKeys]
     */
    suspend fun claimKeys(
        oneTimeKeys: Map>,
        timeout: Long? = 10000,
        asUserId: UserId? = null
    ): Result

    /**
     * @see [GetKeyChanges]
     */
    suspend fun getKeyChanges(
        from: String,
        to: String,
        asUserId: UserId? = null
    ): Result

    /**
     * @see [SetCrossSigningKeys]
     */
    suspend fun setCrossSigningKeys(
        masterKey: SignedCrossSigningKeys?,
        selfSigningKey: SignedCrossSigningKeys?,
        userSigningKey: SignedCrossSigningKeys?,
        asUserId: UserId? = null
    ): Result>

    /**
     * @see [AddSignatures]
     */
    suspend fun addSignatures(
        signedDeviceKeys: Set,
        signedCrossSigningKeys: Set,
        asUserId: UserId? = null
    ): Result

    /**
     * @see [GetRoomsKeyBackup]
     */
    suspend fun getRoomKeys(
        version: String,
        asUserId: UserId? = null
    ): Result

    /**
     * @see [GetRoomKeyBackup]
     */
    suspend fun getRoomKeys(
        version: String,
        roomId: RoomId,
        asUserId: UserId? = null
    ): Result

    /**
     * @see [GetRoomKeyBackupData]
     */
    suspend fun getRoomKeys(
        version: String,
        roomId: RoomId,
        sessionId: String,
        asUserId: UserId? = null
    ): Result

    /**
     * @see [SetRoomsKeyBackup]
     */
    suspend fun setRoomKeys(
        version: String,
        backup: RoomsKeyBackup,
        asUserId: UserId? = null
    ): Result

    /**
     * @see [SetRoomKeyBackup]
     */
    suspend fun setRoomKeys(
        version: String,
        roomId: RoomId,
        backup: RoomKeyBackup,
        asUserId: UserId? = null
    ): Result

    /**
     * @see [SetRoomKeyBackupData]
     */
    suspend fun setRoomKeys(
        version: String,
        roomId: RoomId,
        sessionId: String,
        backup: RoomKeyBackupData,
        asUserId: UserId? = null
    ): Result

    /**
     * @see [DeleteRoomsKeyBackup]
     */
    suspend fun deleteRoomKeys(
        version: String,
        asUserId: UserId? = null
    ): Result

    /**
     * @see [DeleteRoomKeyBackup]
     */
    suspend fun deleteRoomKeys(
        version: String,
        roomId: RoomId,
        asUserId: UserId? = null
    ): Result

    /**
     * @see [DeleteRoomKeyBackupData]
     */
    suspend fun deleteRoomKeys(
        version: String,
        roomId: RoomId,
        sessionId: String,
        asUserId: UserId? = null
    ): Result

    /**
     * @see [GetRoomKeyBackupVersion]
     */
    suspend fun getRoomKeysVersion(
        asUserId: UserId? = null
    ): Result

    /**
     * @see [GetRoomKeyBackupVersionByVersion]
     */
    suspend fun getRoomKeysVersion(
        version: String,
        asUserId: UserId? = null
    ): Result

    /**
     * @see [SetRoomKeyBackupVersion]
     * @see [SetRoomKeyBackupVersionByVersion]
     */
    suspend fun setRoomKeysVersion(
        request: SetRoomKeyBackupVersionRequest,
        asUserId: UserId? = null
    ): Result

    /**
     * @see [DeleteRoomKeyBackupVersion]
     */
    suspend fun deleteRoomKeysVersion(
        version: String,
        asUserId: UserId? = null
    ): Result
}

class KeyApiClientImpl(
    private val httpClient: MatrixClientServerApiHttpClient,
    private val json: Json
) : KeyApiClient {
    override suspend fun setKeys(
        deviceKeys: SignedDeviceKeys?,
        oneTimeKeys: Keys?,
        fallbackKeys: Keys?,
        asUserId: UserId?
    ): Result> =
        httpClient.request(SetKeys(asUserId), SetKeys.Request(deviceKeys, oneTimeKeys, fallbackKeys))
            .mapCatching { it.oneTimeKeyCounts }

    override suspend fun getKeys(
        deviceKeys: Map>,
        timeout: Long?,
        asUserId: UserId?
    ): Result =
        httpClient.request(GetKeys(asUserId), GetKeys.Request(deviceKeys, timeout))

    override suspend fun claimKeys(
        oneTimeKeys: Map>,
        timeout: Long?,
        asUserId: UserId?
    ): Result =
        httpClient.request(ClaimKeys(asUserId), ClaimKeys.Request(oneTimeKeys, timeout))

    override suspend fun getKeyChanges(
        from: String,
        to: String,
        asUserId: UserId?
    ): Result =
        httpClient.request(GetKeyChanges(from, to, asUserId))

    override suspend fun setCrossSigningKeys(
        masterKey: SignedCrossSigningKeys?,
        selfSigningKey: SignedCrossSigningKeys?,
        userSigningKey: SignedCrossSigningKeys?,
        asUserId: UserId?
    ): Result> =
        httpClient.uiaRequest(
            SetCrossSigningKeys(asUserId),
            SetCrossSigningKeys.Request(
                masterKey = masterKey,
                selfSigningKey = selfSigningKey,
                userSigningKey = userSigningKey
            )
        )

    override suspend fun addSignatures(
        signedDeviceKeys: Set,
        signedCrossSigningKeys: Set,
        asUserId: UserId?
    ): Result =
        httpClient.request(
            AddSignatures(asUserId),
            (signedDeviceKeys.associate {
                Pair(it.signed.userId, it.signed.deviceId) to json.encodeToJsonElement(it)
            } + signedCrossSigningKeys.associate {
                Pair(
                    it.signed.userId, it.signed.keys.keys.filterIsInstance().first().value
                ) to json.encodeToJsonElement(it)
            }).entries.groupBy { it.key.first }
                .map { group -> group.key to group.value.associate { it.key.second to it.value } }.toMap()
        )

    override suspend fun getRoomKeys(
        version: String,
        asUserId: UserId?
    ): Result =
        httpClient.request(GetRoomsKeyBackup(version, asUserId))

    override suspend fun getRoomKeys(
        version: String,
        roomId: RoomId,
        asUserId: UserId?
    ): Result =
        httpClient.request(GetRoomKeyBackup(roomId, version, asUserId))

    override suspend fun getRoomKeys(
        version: String,
        roomId: RoomId,
        sessionId: String,
        asUserId: UserId?
    ): Result =
        httpClient.request(GetRoomKeyBackupData(roomId, sessionId, version, asUserId))

    override suspend fun setRoomKeys(
        version: String,
        backup: RoomsKeyBackup,
        asUserId: UserId?
    ): Result =
        httpClient.request(SetRoomsKeyBackup(version, asUserId), backup)

    override suspend fun setRoomKeys(
        version: String,
        roomId: RoomId,
        backup: RoomKeyBackup,
        asUserId: UserId?
    ): Result =
        httpClient.request(SetRoomKeyBackup(roomId, version, asUserId), backup)

    override suspend fun setRoomKeys(
        version: String,
        roomId: RoomId,
        sessionId: String,
        backup: RoomKeyBackupData,
        asUserId: UserId?
    ): Result =
        httpClient.request(SetRoomKeyBackupData(roomId, sessionId, version, asUserId), backup)

    override suspend fun deleteRoomKeys(
        version: String,
        asUserId: UserId?
    ): Result =
        httpClient.request(DeleteRoomsKeyBackup(version, asUserId))

    override suspend fun deleteRoomKeys(
        version: String,
        roomId: RoomId,
        asUserId: UserId?
    ): Result =
        httpClient.request(DeleteRoomKeyBackup(roomId, version, asUserId))

    override suspend fun deleteRoomKeys(
        version: String,
        roomId: RoomId,
        sessionId: String,
        asUserId: UserId?
    ): Result =
        httpClient.request(DeleteRoomKeyBackupData(roomId, sessionId, version, asUserId))

    override suspend fun getRoomKeysVersion(
        asUserId: UserId?
    ): Result =
        httpClient.request(GetRoomKeyBackupVersion(asUserId))

    override suspend fun getRoomKeysVersion(
        version: String,
        asUserId: UserId?
    ): Result =
        httpClient.request(GetRoomKeyBackupVersionByVersion(version, asUserId))

    override suspend fun setRoomKeysVersion(
        request: SetRoomKeyBackupVersionRequest,
        asUserId: UserId?
    ): Result {
        val version = request.version
        return if (version == null) {
            httpClient.request(SetRoomKeyBackupVersion(asUserId), request).map { it.version }
        } else {
            httpClient.request(SetRoomKeyBackupVersionByVersion(version, asUserId), request).map { version }
        }
    }

    override suspend fun deleteRoomKeysVersion(
        version: String,
        asUserId: UserId?
    ): Result =
        httpClient.request(DeleteRoomKeyBackupVersion(version, asUserId))
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy