Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: handle code update and delete events #1960

Merged
Show file tree
Hide file tree
Changes from 17 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,13 @@ internal class ConversationGroupRepositoryImpl(
)
}.onSuccess { response ->
if (response is ServiceAddedResponse.Changed) {
memberJoinEventHandler.handle(eventMapper.conversationMemberJoin(LocalId.generate(), response.event, true))
memberJoinEventHandler.handle(
eventMapper.conversationMemberJoin(
LocalId.generate(),
response.event,
true
)
)
}
}.map { Unit }
}
Expand Down Expand Up @@ -244,7 +250,10 @@ internal class ConversationGroupRepositoryImpl(
}
} else {
// when removing a member from an MLS group, don't need to call the api
mlsConversationRepository.removeMembersFromMLSGroup(GroupID(protocol.groupId), listOf(userId))
mlsConversationRepository.removeMembersFromMLSGroup(
GroupID(protocol.groupId),
listOf(userId)
)
}
}
}
Expand Down Expand Up @@ -280,37 +289,52 @@ internal class ConversationGroupRepositoryImpl(
}
}

override suspend fun fetchLimitedInfoViaInviteCode(code: String, key: String): Either<NetworkFailure, ConversationCodeInfo> =
override suspend fun fetchLimitedInfoViaInviteCode(
code: String,
key: String
): Either<NetworkFailure, ConversationCodeInfo> =
wrapApiRequest { conversationApi.fetchLimitedInformationViaCode(code, key) }

private suspend fun deleteMemberFromCloudAndStorage(userId: UserId, conversationId: ConversationId) =
wrapApiRequest {
conversationApi.removeMember(userId.toApi(), conversationId.toApi())
}.onSuccess { response ->
if (response is ConversationMemberRemovedResponse.Changed) {
memberLeaveEventHandler.handle(eventMapper.conversationMemberLeave(LocalId.generate(), response.event, false))
memberLeaveEventHandler.handle(
eventMapper.conversationMemberLeave(
LocalId.generate(),
response.event,
false
)
)
}
}.map { }

override suspend fun generateGuestRoomLink(conversationId: ConversationId): Either<NetworkFailure, Unit> =
wrapApiRequest {
conversationApi.generateGuestRoomLink(conversationId.toApi())
}.onSuccess {
it.data?.let { data -> conversationDAO.updateGuestRoomLink(conversationId.toDao(), data.uri) }
it.uri?.let { link -> conversationDAO.updateGuestRoomLink(conversationId.toDao(), link) }
it.data
// TODO: set the correct value for is passwordProtected
it.data?.let { data -> conversationDAO.updateGuestRoomLink(conversationId.toDao(), data.uri, false) }
it.uri?.let { link -> conversationDAO.updateGuestRoomLink(conversationId.toDao(), link, false) }
Comment on lines +318 to +320
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The model returned from the remote request needs to be changed and will be part of the next PR

}.map { Either.Right(Unit) }

override suspend fun revokeGuestRoomLink(conversationId: ConversationId): Either<NetworkFailure, Unit> =
wrapApiRequest {
conversationApi.revokeGuestRoomLink(conversationId.toApi())
}.onSuccess {
conversationDAO.updateGuestRoomLink(conversationId.toDao(), null)
// TODO: set the correct value for is passwordProtected
conversationDAO.updateGuestRoomLink(conversationId.toDao(), null, false)
}.map { }

override suspend fun observeGuestRoomLink(conversationId: ConversationId): Flow<String?> =
conversationDAO.observeGuestRoomLinkByConversationId(conversationId.toDao())

override suspend fun updateMessageTimer(conversationId: ConversationId, messageTimer: Long?): Either<CoreFailure, Unit> =
override suspend fun updateMessageTimer(
conversationId: ConversationId,
messageTimer: Long?
): Either<CoreFailure, Unit> =
wrapApiRequest { conversationApi.updateMessageTimer(conversationId.toApi(), messageTimer) }
.onSuccess {
conversationMessageTimerEventHandler.handle(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ sealed class Event(open val id: String, open val transient: Boolean) {
return "${toLogMap().toJsonElement()}"
}

open fun toLogMap(): Map<String, Any?> = mapOf(typeKey to "Event.Unknown")
abstract fun toLogMap(): Map<String, Any?>

sealed class Conversation(
id: String,
Expand Down Expand Up @@ -322,6 +322,26 @@ sealed class Event(open val id: String, open val transient: Boolean) {
timestampIsoKey to timestampIso
)
}

data class CodeUpdated(
override val id: String,
override val conversationId: ConversationId,
override val transient: Boolean,
val key: String,
val code: String,
val uri: String,
val isPasswordProtected: Boolean,
) : Conversation(id, transient, conversationId) {
override fun toLogMap(): Map<String, Any?> = mapOf(typeKey to "Conversation.CodeUpdated")
}

data class CodeDeleted(
override val id: String,
override val conversationId: ConversationId,
override val transient: Boolean,
) : Conversation(id, transient, conversationId) {
override fun toLogMap(): Map<String, Any?> = mapOf(typeKey to "Conversation.CodeDeleted")
}
}

sealed class Team(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,34 @@ class EventMapper(
is EventContentDTO.UserProperty.PropertiesDeleteDTO -> deleteUserProperties(id, eventContentDTO, transient)
is EventContentDTO.Conversation.ReceiptModeUpdate -> conversationReceiptModeUpdate(id, eventContentDTO, transient)
is EventContentDTO.Conversation.MessageTimerUpdate -> conversationMessageTimerUpdate(id, eventContentDTO, transient)
is EventContentDTO.Conversation.CodeDeleted -> conversationCodeDeleted(id, eventContentDTO, transient)
is EventContentDTO.Conversation.CodeUpdated -> conversationCodeUpdated(id, eventContentDTO, transient)
}

private fun conversationCodeDeleted(
id: String,
event: EventContentDTO.Conversation.CodeDeleted,
transient: Boolean
): Event.Conversation.CodeDeleted = Event.Conversation.CodeDeleted(
id = id,
transient = transient,
conversationId = event.qualifiedConversation.toModel()
)

private fun conversationCodeUpdated(
id: String,
event: EventContentDTO.Conversation.CodeUpdated,
transient: Boolean
): Event.Conversation.CodeUpdated = Event.Conversation.CodeUpdated(
id = id,
key = event.data.key,
code = event.data.code,
uri = event.data.uri,
isPasswordProtected = event.data.hasPassword,
conversationId = event.qualifiedConversation.toModel(),
transient = transient
)

fun conversationMessageTimerUpdate(
id: String,
eventContentDTO: EventContentDTO.Conversation.MessageTimerUpdate,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -314,13 +314,17 @@ import com.wire.kalium.logic.sync.receiver.conversation.message.NewMessageEventH
import com.wire.kalium.logic.sync.receiver.conversation.message.NewMessageEventHandlerImpl
import com.wire.kalium.logic.sync.receiver.conversation.message.ProteusMessageUnpacker
import com.wire.kalium.logic.sync.receiver.conversation.message.ProteusMessageUnpackerImpl
import com.wire.kalium.logic.sync.receiver.handler.CodeDeletedHandler
import com.wire.kalium.logic.sync.receiver.handler.ButtonActionConfirmationHandler
import com.wire.kalium.logic.sync.receiver.handler.ButtonActionConfirmationHandlerImpl
import com.wire.kalium.logic.sync.receiver.handler.ClearConversationContentHandlerImpl
import com.wire.kalium.logic.sync.receiver.handler.CodeUpdateHandlerImpl
import com.wire.kalium.logic.sync.receiver.handler.CodeUpdatedHandler
import com.wire.kalium.logic.sync.receiver.handler.CodeDeletedHandlerImpl
import com.wire.kalium.logic.sync.receiver.handler.MessageTextEditHandlerImpl
import com.wire.kalium.logic.sync.receiver.handler.DeleteForMeHandlerImpl
import com.wire.kalium.logic.sync.receiver.handler.DeleteMessageHandlerImpl
import com.wire.kalium.logic.sync.receiver.handler.LastReadContentHandlerImpl
import com.wire.kalium.logic.sync.receiver.handler.MessageTextEditHandlerImpl
import com.wire.kalium.logic.sync.receiver.handler.ReceiptMessageHandlerImpl
import com.wire.kalium.logic.sync.slow.SlowSlowSyncCriteriaProviderImpl
import com.wire.kalium.logic.sync.slow.SlowSyncCriteriaProvider
Expand Down Expand Up @@ -1108,6 +1112,16 @@ class UserSessionScope internal constructor(
persistMessage = persistMessage
)

private val conversationCodeUpdateHandler: CodeUpdatedHandler
get() = CodeUpdateHandlerImpl(
conversationDAO = userStorage.database.conversationDAO
)

private val conversationCodeDeletedHandler: CodeDeletedHandler
get() = CodeDeletedHandlerImpl(
conversationDAO = userStorage.database.conversationDAO
)

private val conversationEventReceiver: ConversationEventReceiver by lazy {
ConversationEventReceiverImpl(
newMessageHandler,
Expand All @@ -1119,7 +1133,9 @@ class UserSessionScope internal constructor(
mlsWelcomeHandler,
renamedConversationHandler,
receiptModeUpdateEventHandler,
conversationMessageTimerEventHandler
conversationMessageTimerEventHandler,
conversationCodeUpdateHandler,
conversationCodeDeletedHandler
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ import com.wire.kalium.logic.sync.receiver.conversation.NewConversationEventHand
import com.wire.kalium.logic.sync.receiver.conversation.ReceiptModeUpdateEventHandler
import com.wire.kalium.logic.sync.receiver.conversation.RenamedConversationEventHandler
import com.wire.kalium.logic.sync.receiver.conversation.message.NewMessageEventHandler
import com.wire.kalium.logic.sync.receiver.handler.CodeDeletedHandler
import com.wire.kalium.logic.sync.receiver.handler.CodeUpdatedHandler

internal interface ConversationEventReceiver : EventReceiver<Event.Conversation>

Expand All @@ -47,7 +49,9 @@ internal class ConversationEventReceiverImpl(
private val mlsWelcomeHandler: MLSWelcomeEventHandler,
private val renamedConversationHandler: RenamedConversationEventHandler,
private val receiptModeUpdateEventHandler: ReceiptModeUpdateEventHandler,
private val conversationMessageTimerEventHandler: ConversationMessageTimerEventHandler
private val conversationMessageTimerEventHandler: ConversationMessageTimerEventHandler,
private val codeUpdatedHandler: CodeUpdatedHandler,
private val codeDeletedHandler: CodeDeletedHandler
) : ConversationEventReceiver {
override suspend fun onEvent(event: Event.Conversation): Either<CoreFailure, Unit> {
// TODO: Make sure errors are accounted for by each handler.
Expand Down Expand Up @@ -108,6 +112,9 @@ internal class ConversationEventReceiverImpl(
conversationMessageTimerEventHandler.handle(event)
Either.Right(Unit)
}

is Event.Conversation.CodeDeleted -> codeDeletedHandler.handle(event)
is Event.Conversation.CodeUpdated -> codeUpdatedHandler.handle(event)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Wire
* Copyright (C) 2023 Wire Swiss GmbH
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see http://www.gnu.org/licenses/.
*/
package com.wire.kalium.logic.sync.receiver.handler

import com.wire.kalium.logic.StorageFailure
import com.wire.kalium.logic.data.event.Event
import com.wire.kalium.logic.data.id.toDao
import com.wire.kalium.logic.functional.Either
import com.wire.kalium.logic.wrapStorageRequest
import com.wire.kalium.persistence.dao.conversation.ConversationDAO

internal interface CodeDeletedHandler {
suspend fun handle(event: Event.Conversation.CodeDeleted): Either<StorageFailure, Unit>
}

internal class CodeDeletedHandlerImpl internal constructor(
private val conversationDAO: ConversationDAO
) : CodeDeletedHandler {
override suspend fun handle(event: Event.Conversation.CodeDeleted) = wrapStorageRequest {
conversationDAO.updateGuestRoomLink(
event.conversationId.toDao(), null, false
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Wire
* Copyright (C) 2023 Wire Swiss GmbH
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see http://www.gnu.org/licenses/.
*/
package com.wire.kalium.logic.sync.receiver.handler

import com.wire.kalium.logic.StorageFailure
import com.wire.kalium.logic.data.event.Event
import com.wire.kalium.logic.data.id.toDao
import com.wire.kalium.logic.functional.Either
import com.wire.kalium.logic.wrapStorageRequest
import com.wire.kalium.persistence.dao.conversation.ConversationDAO

internal interface CodeUpdatedHandler {
suspend fun handle(event: Event.Conversation.CodeUpdated): Either<StorageFailure, Unit>
}

internal class CodeUpdateHandlerImpl internal constructor(
private val conversationDAO: ConversationDAO
) : CodeUpdatedHandler {
override suspend fun handle(event: Event.Conversation.CodeUpdated) = wrapStorageRequest {
conversationDAO.updateGuestRoomLink(
event.conversationId.toDao(), event.uri, event.isPasswordProtected
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -573,7 +573,7 @@ class ConversationGroupRepositoryTest {

verify(arrangement.conversationDAO)
.suspendFunction(arrangement.conversationDAO::updateGuestRoomLink)
.with(any(), any())
.with(any(), any(), any())
.wasInvoked(exactly = once)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
* Wire
* Copyright (C) 2023 Wire Swiss GmbH
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see http://www.gnu.org/licenses/.
*/
package com.wire.kalium.logic.sync.receiver.conversation

import com.wire.kalium.logic.data.event.Event
import com.wire.kalium.logic.data.id.ConversationId
import com.wire.kalium.logic.sync.receiver.handler.CodeDeletedHandler
import com.wire.kalium.logic.sync.receiver.handler.CodeDeletedHandlerImpl
import com.wire.kalium.logic.util.arrangement.dao.ConversionDAOArrangement
import com.wire.kalium.logic.util.arrangement.dao.ConversionDAOArrangementImpl
import com.wire.kalium.persistence.dao.ConversationIDEntity
import io.mockative.eq
import io.mockative.once
import io.mockative.verify
import kotlinx.coroutines.test.runTest
import kotlin.test.Test

class CodeDeletedHandlerTest {
MohamadJaara marked this conversation as resolved.
Show resolved Hide resolved

@Test
fun givenCodeUpdateEvent_whenhandlerIsInvoked_thenCodeIsUpdated() = runTest {
MohamadJaara marked this conversation as resolved.
Show resolved Hide resolved
val (arrangement, handler) = Arrangement().arrange {
withUpdatedGuestRoomLink()
}

val event = Event.Conversation.CodeDeleted(
conversationId = ConversationId("conversationId", "domain"),
id = "event-id",
transient = false
)

handler.handle(event)

verify(arrangement.conversionDAO)
.suspendFunction(arrangement.conversionDAO::updateGuestRoomLink)
.with(
eq(ConversationIDEntity(
event.conversationId.value,
event.conversationId.domain
)),
eq(null as String?),
eq(false)
).wasInvoked(exactly = once)
}

private class Arrangement : ConversionDAOArrangement by ConversionDAOArrangementImpl() {

private val handler: CodeDeletedHandler = CodeDeletedHandlerImpl(conversionDAO)

fun arrange(block: Arrangement.() -> Unit) = apply(block).run {
this to handler
}
}
}
Loading
Loading