Skip to content

Commit

Permalink
fix: newly created 1:1 with bots are not displayed [WPB-4464] (#2912)
Browse files Browse the repository at this point in the history
* fix display newly create bots 1:1

* add tests

* NoConsecutiveBlankLines

---------

Co-authored-by: Vitor Hugo Schwaab <[email protected]>
  • Loading branch information
MohamadJaara and vitorhugods authored Jul 30, 2024
1 parent fb092a8 commit e2ac7c3
Show file tree
Hide file tree
Showing 7 changed files with 106 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,8 @@ interface UserRepository {

suspend fun updateActiveOneOnOneConversation(userId: UserId, conversationId: ConversationId): Either<CoreFailure, Unit>

suspend fun updateActiveOneOnOneConversationIfNotSet(userId: UserId, conversationId: ConversationId): Either<CoreFailure, Unit>

suspend fun isAtLeastOneUserATeamMember(userId: List<UserId>, teamId: TeamId): Either<StorageFailure, Boolean>

suspend fun insertOrIgnoreIncompleteUsers(userIds: List<QualifiedID>): Either<StorageFailure, Unit>
Expand Down Expand Up @@ -418,7 +420,9 @@ internal class UserDataSource internal constructor(
kaliumLogger.i("$logPrefix: Succeeded")
userDetailsRefreshInstantCache[selfUserId] = DateTimeUtil.currentInstant()
})
} else { refreshUserDetailsIfNeeded(selfUserId) }
} else {
refreshUserDetailsIfNeeded(selfUserId)
}
}.filterNotNull().flatMapMerge { encodedValue ->
val selfUserID: QualifiedIDEntity = Json.decodeFromString(encodedValue)
userDAO.observeUserDetailsByQualifiedID(selfUserID)
Expand Down Expand Up @@ -511,6 +515,13 @@ internal class UserDataSource internal constructor(
override suspend fun updateActiveOneOnOneConversation(userId: UserId, conversationId: ConversationId): Either<CoreFailure, Unit> =
wrapStorageRequest { userDAO.updateActiveOneOnOneConversation(userId.toDao(), conversationId.toDao()) }

override suspend fun updateActiveOneOnOneConversationIfNotSet(
userId: UserId,
conversationId: ConversationId
): Either<CoreFailure, Unit> = wrapStorageRequest {
userDAO.updateActiveOneOnOneConversationIfNotSet(userId.toDao(), conversationId.toDao())
}

override suspend fun isAtLeastOneUserATeamMember(userId: List<UserId>, teamId: TeamId) = wrapStorageRequest {
userDAO.isAtLeastOneUserATeamMember(userId.map { it.toDao() }, teamId.value)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,21 @@ internal class MemberJoinEventHandlerImpl(
conversationRepository.persistMembers(event.members, event.conversationId)
}.onSuccess {
conversationRepository.detailsById(event.conversationId).onSuccess { conversation ->
if (conversation.type == Conversation.Type.GROUP) addSystemMessage(event)
when (conversation.type) {
Conversation.Type.ONE_ON_ONE -> {
if (event.members.size == 1) {
userRepository.updateActiveOneOnOneConversationIfNotSet(event.members.first().id, event.conversationId)
}
}

Conversation.Type.GROUP -> addSystemMessage(event)
Conversation.Type.SELF,
Conversation.Type.CONNECTION_PENDING -> {
/* no-op */
}
}
}

legalHoldHandler.handleConversationMembersChanged(event.conversationId)
eventLogger.logSuccess()
}.onFailure {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ class MemberJoinEventHandlerTest {

val (arrangement, eventHandler) = arrange {
withFetchConversationSucceeding()
withConversationDetailsByIdReturning(TestConversation.CONVERSATION.right())
withConversationDetailsByIdReturning(TEST_GROUP_CONVERSATION.right())
}

eventHandler.handle(event)
Expand All @@ -78,7 +78,7 @@ class MemberJoinEventHandlerTest {

val (arrangement, eventHandler) = arrange {
withFetchConversationSucceeding()
withConversationDetailsByIdReturning(TestConversation.CONVERSATION.right())
withConversationDetailsByIdReturning(TEST_GROUP_CONVERSATION.right())
}

eventHandler.handle(event)
Expand All @@ -96,7 +96,7 @@ class MemberJoinEventHandlerTest {
val (arrangement, eventHandler) = arrange {
withFetchConversationIfUnknownFailingWith(NetworkFailure.NoNetworkConnection(null))
withFetchConversation(NetworkFailure.NoNetworkConnection(null).left())
withConversationDetailsByIdReturning(TestConversation.CONVERSATION.right())
withConversationDetailsByIdReturning(TEST_GROUP_CONVERSATION.right())
}

eventHandler.handle(event)
Expand All @@ -113,7 +113,7 @@ class MemberJoinEventHandlerTest {

val (arrangement, eventHandler) = arrange {
withFetchConversationSucceeding()
withConversationDetailsByIdReturning(TestConversation.GROUP().right())
withConversationDetailsByIdReturning(TEST_GROUP_CONVERSATION.right())
}

eventHandler.handle(event)
Expand All @@ -129,17 +129,49 @@ class MemberJoinEventHandlerTest {

@Test
fun givenMemberJoinEventIn1o1Conversation_whenHandlingIt_thenShouldNotPersistSystemMessage() = runTest {
val newMembers = listOf(Member(TestUser.USER_ID, Member.Role.Admin))
val userId = TestUser.USER_ID
val conversation = TEST_ONE_ON_ONE_CONVERSATION
val newMembers = listOf(Member(userId, Member.Role.Admin))
val event = TestEvent.memberJoin(members = newMembers)

val (arrangement, eventHandler) = arrange {
withFetchConversationSucceeding()
withConversationDetailsByIdReturning(TestConversation.CONVERSATION.right())
withUpdateActiveOneOnOneConversationIfNotSet(Unit.right())
withConversationDetailsByIdReturning(conversation.right())
}

eventHandler.handle(event)

coVerify { arrangement.persistMessageUseCase(any()) }.wasNotInvoked()
coVerify {
arrangement.userRepository.updateActiveOneOnOneConversationIfNotSet(
userId = any(),
conversationId = any()
)
}.wasInvoked(exactly = 1)
}

@Test
fun givenMemberJoinEventIn1o1Conversation_whenHandlingIt_1o1ConversationForTheUserShouldBeSetIffItWasNotBefore() = runTest {
val userId = TestUser.USER_ID
val conversation = TEST_ONE_ON_ONE_CONVERSATION
val newMembers = listOf(Member(userId, Member.Role.Admin))
val event = TestEvent.memberJoin(members = newMembers)

val (arrangement, eventHandler) = arrange {
withFetchConversationSucceeding()
withUpdateActiveOneOnOneConversationIfNotSet(Unit.right())
withConversationDetailsByIdReturning(conversation.right())
}

eventHandler.handle(event)

coVerify {
arrangement.userRepository.updateActiveOneOnOneConversationIfNotSet(
userId = any(),
conversationId = any()
)
}.wasInvoked(exactly = 1)
}

@Test
Expand Down Expand Up @@ -179,7 +211,11 @@ class MemberJoinEventHandlerTest {
eventHandler.handle(event)
// then
coVerify {
arrangement.legalHoldHandler.handleConversationMembersChanged(eq(event.conversationId))
arrangement.persistMessageUseCase.invoke(
matches {
it is Message.System && it.content is MessageContent.MemberChange && it.id.isNotEmpty()
}
)
}.wasInvoked(exactly = once)
}

Expand Down Expand Up @@ -209,5 +245,9 @@ class MemberJoinEventHandlerTest {

private companion object {
suspend fun arrange(block: suspend Arrangement.() -> Unit) = Arrangement(block).arrange()

val TEST_GROUP_CONVERSATION = TestConversation.GROUP()
val TEST_ONE_ON_ONE_CONVERSATION = TestConversation.ONE_ON_ONE()

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,11 @@ internal interface UserRepositoryArrangement {

suspend fun withMarkAsDeleted(result: Either<StorageFailure, Unit>, userId: Matcher<List<UserId>>)
suspend fun withOneOnOnConversationId(result: Either<StorageFailure, ConversationId>, userId: Matcher<UserId> = AnyMatcher(valueOf()))
suspend fun withUpdateActiveOneOnOneConversationIfNotSet(
result: Either<CoreFailure, Unit>,
userId: Matcher<UserId> = AnyMatcher(valueOf()),
conversationId: Matcher<ConversationId> = AnyMatcher(valueOf())
)
}

@Suppress("INAPPLICABLE_JVM_NAME")
Expand Down Expand Up @@ -209,4 +214,17 @@ internal open class UserRepositoryArrangementImpl : UserRepositoryArrangement {
coEvery { userRepository.getOneOnOnConversationId(matches { userId.matches(it) }) }
.returns(result)
}

override suspend fun withUpdateActiveOneOnOneConversationIfNotSet(
result: Either<CoreFailure, Unit>,
userId: Matcher<UserId>,
conversationId: Matcher<ConversationId>
) {
coEvery {
userRepository.updateActiveOneOnOneConversationIfNotSet(
matches { userId.matches(it) },
matches { conversationId.matches(it) })
}
.returns(result)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,9 @@ UPDATE User SET supported_protocols = ? WHERE qualified_id = ?;
updateOneOnOnConversationId:
UPDATE User SET active_one_on_one_conversation_id = ? WHERE qualified_id = ?;

setOneOnOneConversationIdIfNotSet:
UPDATE User SET active_one_on_one_conversation_id = ? WHERE qualified_id = ? AND active_one_on_one_conversation_id IS NULL;

isOneUserATeamMember:
SELECT EXISTS (
SELECT 1 FROM User
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,11 @@ interface UserDAO {
*/
suspend fun updateActiveOneOnOneConversation(userId: QualifiedIDEntity, conversationId: QualifiedIDEntity)

/**
* Update which 1-1 conversation iff it was not set before.
*/
suspend fun updateActiveOneOnOneConversationIfNotSet(userId: QualifiedIDEntity, conversationId: QualifiedIDEntity)

suspend fun upsertConnectionStatuses(userStatuses: Map<QualifiedIDEntity, ConnectionEntity.State>)
suspend fun isAtLeastOneUserATeamMember(userId: List<UserIDEntity>, teamId: String): Boolean
suspend fun getOneOnOnConversationId(userId: UserIDEntity): QualifiedIDEntity?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -440,6 +440,13 @@ class UserDAOImpl internal constructor(
userQueries.updateOneOnOnConversationId(conversationId, userId)
}

override suspend fun updateActiveOneOnOneConversationIfNotSet(
userId: QualifiedIDEntity,
conversationId: QualifiedIDEntity,
) = withContext(queriesContext) {
userQueries.setOneOnOneConversationIdIfNotSet(conversationId, userId)
}

override suspend fun upsertConnectionStatuses(userStatuses: Map<QualifiedIDEntity, ConnectionEntity.State>) {
withContext(queriesContext) {
userQueries.transaction {
Expand Down

0 comments on commit e2ac7c3

Please sign in to comment.