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

commonMain.net.folivo.trixnity.client.store.StoreExtensions.kt Maven / Gradle / Ivy

There is a newer version: 4.7.1
Show newest version
package net.folivo.trixnity.client.store

import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.coroutineScope
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.launch
import net.folivo.trixnity.core.model.RoomId
import net.folivo.trixnity.core.model.UserId
import net.folivo.trixnity.core.model.events.ClientEvent.*
import net.folivo.trixnity.core.model.events.GlobalAccountDataEventContent
import net.folivo.trixnity.core.model.events.RoomAccountDataEventContent
import net.folivo.trixnity.core.model.events.StateEventContent
import net.folivo.trixnity.core.model.events.m.room.MemberEventContent
import net.folivo.trixnity.core.model.events.m.room.Membership
import net.folivo.trixnity.core.model.events.m.room.Membership.JOIN
import net.folivo.trixnity.crypto.olm.StoredInboundMegolmSession

inline fun  RoomStateStore.get(
    roomId: RoomId,
): Flow?>>> = get(roomId, C::class)

inline fun  RoomStateStore.getByStateKey(
    roomId: RoomId,
    stateKey: String = "",
): Flow?> = getByStateKey(roomId, C::class, stateKey)

suspend inline fun  RoomStateStore.getByRooms(
    roomIds: Set,
    stateKey: String = "",
): List> = getByRooms(roomIds, C::class, stateKey)

inline fun  RoomStateStore.getContentByStateKey(
    roomId: RoomId,
    stateKey: String = "",
): Flow = getByStateKey(roomId, C::class, stateKey).map { it?.content }

inline fun  RoomAccountDataStore.get(
    roomId: RoomId,
    key: String = "",
): Flow?> = get(roomId, C::class, key)

inline fun  GlobalAccountDataStore.get(
    key: String = "",
): Flow?> = get(C::class, key)

suspend fun RoomStateStore.members(
    roomId: RoomId,
    memberships: Set,
): Set =
    get(roomId).first()
        .filter { memberships.contains(it.value.first()?.content?.membership) }
        .map { UserId(it.key) }.toSet()

suspend fun RoomStateStore.membersCount(
    roomId: RoomId,
    membership: Membership,
    vararg moreMemberships: Membership
): Long {
    val allMemberships = moreMemberships.toList() + membership
    return get(roomId).first()
        .count { allMemberships.contains(it.value.first()?.content?.membership) }.toLong()
}

suspend fun RoomStore.encryptedJoinedRooms(): Set =
    getAll().first().values
        .map { it.first() }
        .filter { it?.encrypted == true && it.membership == JOIN }
        .mapNotNull { it?.roomId }
        .toSet()

fun RoomTimelineStore.getNext(
    event: TimelineEvent,
): Flow? =
    event.nextEventId?.let { get(it, event.roomId) }

suspend fun RoomTimelineStore.getPrevious(event: TimelineEvent): TimelineEvent? =
    event.previousEventId?.let { get(it, event.roomId) }?.first()

suspend fun OlmCryptoStore.waitForInboundMegolmSession(
    roomId: RoomId,
    sessionId: String,
    firstKnownIndexLessThen: Long? = null,
    onNotExisting: (suspend CoroutineScope.() -> Unit)? = null
): Unit = coroutineScope {
    fun StoredInboundMegolmSession?.matches() =
        this != null && (firstKnownIndexLessThen == null || this.firstKnownIndex < firstKnownIndexLessThen)

    val onNotExistingJob =
        if (getInboundMegolmSession(sessionId, roomId).first().matches().not() && onNotExisting != null)
            launch { onNotExisting() }
        else null
    getInboundMegolmSession(sessionId, roomId)
        .first { it.matches() }
    onNotExistingJob?.cancel()
}

val RoomUser.originalName
    get() = this.event.content.displayName

val RoomUser.avatarUrl
    get() = this.event.content.avatarUrl

val RoomUser.membership
    get() = this.event.content.membership




© 2015 - 2024 Weber Informatics LLC | Privacy Policy