Browse Source

Use the new setIsFavorite api

pull/2397/head
ganfra 8 months ago
parent
commit
f3c1eb6738
  1. 16
      features/roomactions/impl/src/main/kotlin/io/element/android/features/roomactions/impl/DefaultSetRoomIsFavoriteAction.kt
  2. 2
      features/roomactions/impl/src/test/kotlin/io/element/android/features/roomactions/impl/DefaultSetRoomIsFavoriteActionTests.kt
  3. 2
      libraries/matrix/api/src/main/kotlin/io/element/android/libraries/matrix/api/room/MatrixRoom.kt
  4. 2
      libraries/matrix/api/src/main/kotlin/io/element/android/libraries/matrix/api/room/tags/RoomNotableTags.kt
  5. 9
      libraries/matrix/impl/src/main/kotlin/io/element/android/libraries/matrix/impl/room/RustMatrixRoom.kt
  6. 6
      libraries/matrix/impl/src/main/kotlin/io/element/android/libraries/matrix/impl/room/tags/RoomNotableTagsMapper.kt
  7. 18
      libraries/matrix/test/src/main/kotlin/io/element/android/libraries/matrix/test/room/FakeMatrixRoom.kt

16
features/roomactions/impl/src/main/kotlin/io/element/android/features/roomactions/impl/DefaultSetRoomIsFavoriteAction.kt

@ -22,25 +22,20 @@ import io.element.android.libraries.di.SessionScope
import io.element.android.libraries.matrix.api.MatrixClient import io.element.android.libraries.matrix.api.MatrixClient
import io.element.android.libraries.matrix.api.core.RoomId import io.element.android.libraries.matrix.api.core.RoomId
import io.element.android.libraries.matrix.api.room.MatrixRoom import io.element.android.libraries.matrix.api.room.MatrixRoom
import kotlinx.coroutines.flow.first
import javax.inject.Inject import javax.inject.Inject
import kotlin.coroutines.cancellation.CancellationException import kotlin.coroutines.cancellation.CancellationException
@ContributesBinding(SessionScope::class) @ContributesBinding(SessionScope::class)
class DefaultSetRoomIsFavoriteAction @Inject constructor(private val client: MatrixClient) : SetRoomIsFavoriteAction { class DefaultSetRoomIsFavoriteAction @Inject constructor(private val client: MatrixClient) : SetRoomIsFavoriteAction {
override suspend operator fun invoke(roomId: RoomId, isFavorite: Boolean): SetRoomIsFavoriteAction.Result { override suspend operator fun invoke(roomId: RoomId, isFavorite: Boolean): SetRoomIsFavoriteAction.Result {
return client.getRoom(roomId).use { room -> return client.getRoom(roomId)?.use { room ->
room?.setIsFavorite(isFavorite) ?: SetRoomIsFavoriteAction.Result.RoomNotFound invoke(room, isFavorite)
} } ?: SetRoomIsFavoriteAction.Result.RoomNotFound
} }
override suspend fun invoke(room: MatrixRoom, isFavorite: Boolean): SetRoomIsFavoriteAction.Result { override suspend fun invoke(room: MatrixRoom, isFavorite: Boolean): SetRoomIsFavoriteAction.Result {
return room.setIsFavorite(isFavorite) return room.setIsFavorite(isFavorite).fold(
}
private suspend fun MatrixRoom.setIsFavorite(isFavorite: Boolean): SetRoomIsFavoriteAction.Result {
val notableTags = notableTagsFlow.first().copy(isFavorite = isFavorite)
return updateNotableTags(notableTags).fold(
onSuccess = { onSuccess = {
SetRoomIsFavoriteAction.Result.Success SetRoomIsFavoriteAction.Result.Success
}, },
@ -53,4 +48,5 @@ class DefaultSetRoomIsFavoriteAction @Inject constructor(private val client: Mat
} }
) )
} }
} }

2
features/roomactions/impl/src/test/kotlin/io/element/android/features/roomactions/impl/DefaultSetRoomIsFavoriteActionTests.kt

@ -52,7 +52,7 @@ class DefaultSetRoomIsFavoriteActionTests {
@Test @Test
fun `given a room, when action is invoked and fail, then it returns Result_Exception`() = runTest { fun `given a room, when action is invoked and fail, then it returns Result_Exception`() = runTest {
val action = DefaultSetRoomIsFavoriteAction(FakeMatrixClient()) val action = DefaultSetRoomIsFavoriteAction(FakeMatrixClient())
room.givenUpdateNotableTagsResult(Result.failure(Exception())) room.givenSetIsFavoriteResult(Result.failure(Exception()))
val result = action(room, true) val result = action(room, true)
assert(result is SetRoomIsFavoriteAction.Result.Exception) assert(result is SetRoomIsFavoriteAction.Result.Exception)
} }

2
libraries/matrix/api/src/main/kotlin/io/element/android/libraries/matrix/api/room/MatrixRoom.kt

@ -156,7 +156,7 @@ interface MatrixRoom : Closeable {
suspend fun reportContent(eventId: EventId, reason: String, blockUserId: UserId?): Result<Unit> suspend fun reportContent(eventId: EventId, reason: String, blockUserId: UserId?): Result<Unit>
suspend fun updateNotableTags(notableTags: RoomNotableTags): Result<Unit> suspend fun setIsFavorite(isFavorite: Boolean): Result<Unit>
/** /**
* Share a location message in the room. * Share a location message in the room.

2
libraries/matrix/api/src/main/kotlin/io/element/android/libraries/matrix/api/room/tags/RoomNotableTags.kt

@ -19,7 +19,9 @@ package io.element.android.libraries.matrix.api.room.tags
/** /**
* Represents the notable tags of a room. * Represents the notable tags of a room.
* @param isFavorite true if the room is marked as favorite. * @param isFavorite true if the room is marked as favorite.
* @param isLowPriority true if the room is marked as low priority.
*/ */
data class RoomNotableTags( data class RoomNotableTags(
val isFavorite: Boolean = false, val isFavorite: Boolean = false,
val isLowPriority: Boolean = false,
) )

9
libraries/matrix/impl/src/main/kotlin/io/element/android/libraries/matrix/impl/room/RustMatrixRoom.kt

@ -435,14 +435,9 @@ class RustMatrixRoom(
} }
} }
override suspend fun updateNotableTags(notableTags: RoomNotableTags): Result<Unit> = withContext(roomDispatcher) { override suspend fun setIsFavorite(isFavorite: Boolean): Result<Unit> = withContext(roomDispatcher) {
runCatching { runCatching {
Timber.i("Update notable tags with : $notableTags") innerRoom.setIsFavorite(isFavorite, null)
innerRoom.updateNotableTags(notableTags.map())
}.onFailure {
Timber.w("Failed to update notable tags: $it")
}.onSuccess {
Timber.i("Successfully updated notable tags")
} }
} }

6
libraries/matrix/impl/src/main/kotlin/io/element/android/libraries/matrix/impl/room/tags/RoomNotableTagsMapper.kt

@ -20,9 +20,11 @@ import io.element.android.libraries.matrix.api.room.tags.RoomNotableTags
import uniffi.matrix_sdk_base.RoomNotableTags as RustRoomNotableTags import uniffi.matrix_sdk_base.RoomNotableTags as RustRoomNotableTags
fun RustRoomNotableTags.map() = RoomNotableTags( fun RustRoomNotableTags.map() = RoomNotableTags(
isFavorite = isFavorite isFavorite = isFavorite,
isLowPriority = isLowPriority
) )
fun RoomNotableTags.map() = RustRoomNotableTags( fun RoomNotableTags.map() = RustRoomNotableTags(
isFavorite = isFavorite isFavorite = isFavorite,
isLowPriority = isLowPriority
) )

18
libraries/matrix/test/src/main/kotlin/io/element/android/libraries/matrix/test/room/FakeMatrixRoom.kt

@ -114,7 +114,7 @@ class FakeMatrixRoom(
private var getWidgetDriverResult: Result<MatrixWidgetDriver> = Result.success(FakeWidgetDriver()) private var getWidgetDriverResult: Result<MatrixWidgetDriver> = Result.success(FakeWidgetDriver())
private var canUserTriggerRoomNotificationResult: Result<Boolean> = Result.success(true) private var canUserTriggerRoomNotificationResult: Result<Boolean> = Result.success(true)
private var canUserJoinCallResult: Result<Boolean> = Result.success(true) private var canUserJoinCallResult: Result<Boolean> = Result.success(true)
private var updateNotableTagsResult = Result.success(Unit) private var setIsFavoriteResult = Result.success(Unit)
var sendMessageMentions = emptyList<Mention>() var sendMessageMentions = emptyList<Mention>()
val editMessageCalls = mutableListOf<Pair<String, String?>>() val editMessageCalls = mutableListOf<Pair<String, String?>>()
private val _typingRecord = mutableListOf<Boolean>() private val _typingRecord = mutableListOf<Boolean>()
@ -171,7 +171,7 @@ class FakeMatrixRoom(
private val _roomInfoFlow: MutableSharedFlow<MatrixRoomInfo> = MutableSharedFlow(replay = 1) private val _roomInfoFlow: MutableSharedFlow<MatrixRoomInfo> = MutableSharedFlow(replay = 1)
override val roomInfoFlow: Flow<MatrixRoomInfo> = _roomInfoFlow override val roomInfoFlow: Flow<MatrixRoomInfo> = _roomInfoFlow
private val _notableTagsFlow: MutableSharedFlow<RoomNotableTags> = MutableStateFlow(aRoomNotableTags()) private val _notableTagsFlow: MutableStateFlow<RoomNotableTags> = MutableStateFlow(aRoomNotableTags())
override val notableTagsFlow: Flow<RoomNotableTags> = _notableTagsFlow override val notableTagsFlow: Flow<RoomNotableTags> = _notableTagsFlow
override val membersStateFlow: MutableStateFlow<MatrixRoomMembersState> = MutableStateFlow(MatrixRoomMembersState.Unknown) override val membersStateFlow: MutableStateFlow<MatrixRoomMembersState> = MutableStateFlow(MatrixRoomMembersState.Unknown)
@ -379,9 +379,15 @@ class FakeMatrixRoom(
return reportContentResult return reportContentResult
} }
override suspend fun updateNotableTags(notableTags: RoomNotableTags): Result<Unit> { override suspend fun setIsFavorite(isFavorite: Boolean): Result<Unit> {
return updateNotableTagsResult.also { result -> return setIsFavoriteResult.also { result ->
if (result.isSuccess) { if (result.isSuccess) {
val lowPriority = if (isFavorite) {
false
} else {
_notableTagsFlow.value.isLowPriority
}
val notableTags = RoomNotableTags(isFavorite, lowPriority)
_notableTagsFlow.emit(notableTags) _notableTagsFlow.emit(notableTags)
} }
} }
@ -584,8 +590,8 @@ class FakeMatrixRoom(
getWidgetDriverResult = result getWidgetDriverResult = result
} }
fun givenUpdateNotableTagsResult(result: Result<Unit>) { fun givenSetIsFavoriteResult(result: Result<Unit>) {
updateNotableTagsResult = result setIsFavoriteResult = result
} }
fun givenRoomInfo(roomInfo: MatrixRoomInfo) { fun givenRoomInfo(roomInfo: MatrixRoomInfo) {

Loading…
Cancel
Save