Skip to content

Commit

Permalink
fix: ignore unknown legal hold event
Browse files Browse the repository at this point in the history
  • Loading branch information
ohassine committed Dec 14, 2023
1 parent 1e78df6 commit 4dd16a8
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 84 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import com.wire.kalium.cryptography.exceptions.ProteusException
import com.wire.kalium.logger.KaliumLogger
import com.wire.kalium.logic.ProteusFailure
import com.wire.kalium.logic.data.conversation.ClientId
import com.wire.kalium.logic.data.conversation.Conversation
import com.wire.kalium.logic.data.conversation.ConversationRepository
import com.wire.kalium.logic.data.event.Event
import com.wire.kalium.logic.data.event.EventLoggingStatus
Expand Down Expand Up @@ -86,10 +87,12 @@ internal class NewMessageEventHandlerImpl(
}.onSuccess {
if (it is MessageUnpackResult.ApplicationMessage) {
handleSuccessfulResult(it)
conversationRepository.updateLegalHoldStatus(
conversationId = it.conversationId,
legalHoldStatus = it.content.legalHoldStatus
)
if (it.content.legalHoldStatus != Conversation.LegalHoldStatus.UNKNOWN) {
conversationRepository.updateLegalHoldStatus(
conversationId = it.conversationId,
legalHoldStatus = it.content.legalHoldStatus
)
}
onMessageInserted(it)
}
kaliumLogger
Expand All @@ -113,6 +116,7 @@ internal class NewMessageEventHandlerImpl(
is MLSMessageFailureResolution.Ignore -> {
logger.i("Ignoring event: ${logMap.toJsonElement()}")
}

is MLSMessageFailureResolution.InformUser -> {
logger.i("Informing users about decryption error: ${logMap.toJsonElement()}")
applicationMessageHandler.handleDecryptionError(
Expand All @@ -127,19 +131,25 @@ internal class NewMessageEventHandlerImpl(
)
)
}

is MLSMessageFailureResolution.OutOfSync -> {
logger.i("Epoch out of sync error: ${logMap.toJsonElement()}")
staleEpochVerifier.verifyEpoch(event.conversationId, event.timestampIso.toInstant())
staleEpochVerifier.verifyEpoch(
event.conversationId,
event.timestampIso.toInstant()
)
}
}
}.onSuccess {
it.forEach {
if (it is MessageUnpackResult.ApplicationMessage) {
handleSuccessfulResult(it)
conversationRepository.updateLegalHoldStatus(
conversationId = it.conversationId,
legalHoldStatus = it.content.legalHoldStatus
)
if (it.content.legalHoldStatus != Conversation.LegalHoldStatus.UNKNOWN) {
conversationRepository.updateLegalHoldStatus(
conversationId = it.conversationId,
legalHoldStatus = it.content.legalHoldStatus
)
}
onMessageInserted(it)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,25 +144,15 @@ class NewMessageEventHandlerTest {
}

@Test
fun givenUnpackingSuccess_whenHandling_thenHandleContent() = runTest {
fun givenAnMLSMessageWithLegalHoldUnknown_whenHandlingIt_thenDoNotUpdateLegalHoldStatus() = runTest {
val (arrangement, newMessageEventHandler) = Arrangement()
.withUpdateLegalHoldStatusSuccess()
.withMLSUnpackerReturning(
Either.Right(
listOf(
MessageUnpackResult.ApplicationMessage(
conversationId = ConversationId("conversationID", "domain"),
timestampIso = Instant.DISTANT_PAST.toIsoDateTimeString(),
senderUserId = UserId("otherUserId", "domain"),
senderClientId = ClientId("clientID"),
content = ProtoContent.Readable(
messageUid = "messageUID",
messageContent = MessageContent.Text(
value = "messageContent"
),
expectsReadConfirmation = false,
legalHoldStatus = Conversation.LegalHoldStatus.DISABLED,
expiresAfterMillis = 123L
applicationMessage.copy(
content = applicationMessage.content.copy(
legalHoldStatus = Conversation.LegalHoldStatus.UNKNOWN
)
)
)
Expand All @@ -174,6 +164,28 @@ class NewMessageEventHandlerTest {

newMessageEventHandler.handleNewMLSMessage(newMessageEvent)

verify(arrangement.conversationRepository)
.suspendFunction(arrangement.conversationRepository::updateLegalHoldStatus)
.with(any(), eq(Conversation.LegalHoldStatus.DISABLED))
.wasNotInvoked()

verify(arrangement.applicationMessageHandler)
.suspendFunction(arrangement.applicationMessageHandler::handleContent)
.with(any(), any(), any(), any(), any())
.wasInvoked(exactly = once)
}

@Test
fun givenUnpackingSuccess_whenHandling_thenHandleContent() = runTest {
val (arrangement, newMessageEventHandler) = Arrangement()
.withUpdateLegalHoldStatusSuccess()
.withMLSUnpackerReturning(Either.Right(listOf(applicationMessage)))
.arrange()

val newMessageEvent = TestEvent.newMLSMessageEvent(DateTimeUtil.currentInstant())

newMessageEventHandler.handleNewMLSMessage(newMessageEvent)

verify(arrangement.mlsMessageUnpacker)
.suspendFunction(arrangement.mlsMessageUnpacker::unpackMlsMessage)
.with(eq(newMessageEvent))
Expand All @@ -192,29 +204,9 @@ class NewMessageEventHandlerTest {

@Test
fun givenEphemeralMessageFromSelf_whenHandling_thenEnqueueForSelfDelete() = runTest {
val conversationID = ConversationId("conversationID", "domain")
val senderUserId = SELF_USER_ID
val (arrangement, newMessageEventHandler) = Arrangement()
.withUpdateLegalHoldStatusSuccess()
.withProteusUnpackerReturning(
Either.Right(
MessageUnpackResult.ApplicationMessage(
conversationID,
Instant.DISTANT_PAST.toIsoDateTimeString(),
senderUserId,
ClientId("clientID"),
ProtoContent.Readable(
messageUid = "messageUID",
messageContent = MessageContent.Text(
value = "messageContent"
),
expectsReadConfirmation = false,
legalHoldStatus = Conversation.LegalHoldStatus.DISABLED,
expiresAfterMillis = 123L
)
)
)
)
.withProteusUnpackerReturning(Either.Right(applicationMessage))
.arrange()

val newMessageEvent = TestEvent.newMessageEvent("encryptedContent")
Expand All @@ -239,29 +231,9 @@ class NewMessageEventHandlerTest {

@Test
fun givenEphemeralMessage_whenHandling_thenDoNotEnqueueForSelfDelete() = runTest {
val conversationID = ConversationId("conversationID", "domain")
val senderUserId = UserId("otherUserId", "domain")
val (arrangement, newMessageEventHandler) = Arrangement()
.withUpdateLegalHoldStatusSuccess()
.withProteusUnpackerReturning(
Either.Right(
MessageUnpackResult.ApplicationMessage(
conversationID,
Instant.DISTANT_PAST.toIsoDateTimeString(),
senderUserId,
ClientId("clientID"),
ProtoContent.Readable(
messageUid = "messageUID",
messageContent = MessageContent.Text(
value = "messageContent"
),
expectsReadConfirmation = false,
legalHoldStatus = Conversation.LegalHoldStatus.DISABLED,
expiresAfterMillis = 123L
)
)
)
)
.withProteusUnpackerReturning(Either.Right(applicationMessage))
.arrange()

val newMessageEvent = TestEvent.newMessageEvent("encryptedContent")
Expand All @@ -285,24 +257,14 @@ class NewMessageEventHandlerTest {
}

@Test
fun givenMessageFromSelf_whenHandling_thenDoNotEnqueueForSelfDelete() = runTest {
val conversationID = ConversationId("conversationID", "domain")
val senderUserId = SELF_USER_ID
fun givenAMessageWithUnknownLegalHoldStatus_whenHandlingIt_thenDoNotUpdateCurrentLegalHold() = runTest {
val (arrangement, newMessageEventHandler) = Arrangement()
.withUpdateLegalHoldStatusSuccess()
.withProteusUnpackerReturning(
Either.Right(
MessageUnpackResult.ApplicationMessage(
conversationID,
Instant.DISTANT_PAST.toIsoDateTimeString(),
senderUserId,
ClientId("clientID"),
ProtoContent.Readable(
messageUid = "messageUID",
messageContent = MessageContent.Text(value = "messageContent"),
expectsReadConfirmation = false,
legalHoldStatus = Conversation.LegalHoldStatus.DISABLED,
expiresAfterMillis = null
applicationMessage.copy(
content = applicationMessage.content.copy(
legalHoldStatus = Conversation.LegalHoldStatus.UNKNOWN
)
)
)
Expand All @@ -313,6 +275,28 @@ class NewMessageEventHandlerTest {

newMessageEventHandler.handleNewProteusMessage(newMessageEvent)

verify(arrangement.proteusMessageUnpacker)
.suspendFunction(arrangement.proteusMessageUnpacker::unpackProteusMessage)
.with(eq(newMessageEvent))
.wasInvoked(exactly = once)

verify(arrangement.conversationRepository)
.suspendFunction(arrangement.conversationRepository::updateLegalHoldStatus)
.with(eq(conversationID), eq(Conversation.LegalHoldStatus.UNKNOWN))
.wasNotInvoked()
}

@Test
fun givenMessageFromSelf_whenHandling_thenDoNotEnqueueForSelfDelete() = runTest {
val (arrangement, newMessageEventHandler) = Arrangement()
.withUpdateLegalHoldStatusSuccess()
.withProteusUnpackerReturning(Either.Right(applicationMessage))
.arrange()

val newMessageEvent = TestEvent.newMessageEvent("encryptedContent")

newMessageEventHandler.handleNewProteusMessage(newMessageEvent)

verify(arrangement.proteusMessageUnpacker)
.suspendFunction(arrangement.proteusMessageUnpacker::unpackProteusMessage)
.with(eq(newMessageEvent))
Expand All @@ -334,10 +318,6 @@ class NewMessageEventHandlerTest {
.wasNotInvoked()
}

private companion object {
val SELF_USER_ID = UserId("selfUserId", "selfDomain")
}

@Test
fun givenMLSEventFailsWithWrongEpoch_whenHandling_shouldCallWrongEpochHandler() = runTest {
val (arrangement, newMessageEventHandler) = Arrangement()
Expand Down Expand Up @@ -442,4 +422,22 @@ class NewMessageEventHandlerTest {
fun arrange() = this to newMessageEventHandler

}

private companion object {
val SELF_USER_ID = UserId("selfUserId", "selfDomain")
val conversationID = ConversationId("conversationID", "domain")
val applicationMessage = MessageUnpackResult.ApplicationMessage(
ConversationId("conversationID", "domain"),
Instant.DISTANT_PAST.toIsoDateTimeString(),
SELF_USER_ID,
ClientId("clientID"),
ProtoContent.Readable(
messageUid = "messageUID",
messageContent = MessageContent.Text(value = "messageContent"),
expectsReadConfirmation = false,
legalHoldStatus = Conversation.LegalHoldStatus.DISABLED,
expiresAfterMillis = null
)
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class MetadataDAOImpl internal constructor(
metadataQueries.insertValue(key, value)
}

override suspend fun deleteValue(key: String) {
override suspend fun deleteValue(key: String) = withContext(queriesContext) {
metadataQueries.deleteValue(key)
}

Expand Down

0 comments on commit 4dd16a8

Please sign in to comment.