-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: typing indicator receiver events and observability (WPB-4591) (#…
…2069) * feat: add boilerplate for typing indicator handling * feat: add boilerplate for typing indicator handling * feat: add boilerplate for typing indicator handling * feat: add boilerplate for typing indicator handling + storage * feat: add boilerplate for worker * feat: remove out of scope in this pr * feat: cleanup * feat: cleanup * chore: pr comments * feat: add coverage to repo * feat: add coverage to event receiver * feat: add coverage to typing handler * fix: pr comments * fix: pr comments thread safe ops * fix: pr comments thread safe ops * fix: pr comments map per user * fix: pr comments map per user * feat: pr comments, threadsafe mutating collection value * feat: pr comments, threadsafe mutating collection value * feat: pr comments, threadsafe mutating collection value
- Loading branch information
1 parent
47c310d
commit 048b06f
Showing
16 changed files
with
537 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 74 additions & 0 deletions
74
...rc/commonMain/kotlin/com/wire/kalium/logic/data/conversation/TypingIndicatorRepository.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
/* | ||
* 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.data.conversation | ||
|
||
import co.touchlab.stately.collections.ConcurrentMutableMap | ||
import com.wire.kalium.logic.data.id.ConversationId | ||
import com.wire.kalium.logic.data.user.UserId | ||
import com.wire.kalium.logic.util.safeComputeAndMutateSetValue | ||
import kotlinx.coroutines.channels.BufferOverflow | ||
import kotlinx.coroutines.flow.Flow | ||
import kotlinx.coroutines.flow.MutableSharedFlow | ||
import kotlinx.coroutines.flow.distinctUntilChanged | ||
import kotlinx.coroutines.flow.map | ||
import kotlinx.coroutines.flow.onStart | ||
import kotlinx.datetime.Clock | ||
import kotlinx.datetime.Instant | ||
|
||
internal interface TypingIndicatorRepository { | ||
fun addTypingUserInConversation(conversationId: ConversationId, userId: UserId) | ||
fun removeTypingUserInConversation(conversationId: ConversationId, userId: UserId) | ||
suspend fun observeUsersTyping(conversationId: ConversationId): Flow<Set<UserId>> | ||
} | ||
|
||
internal class TypingIndicatorRepositoryImpl( | ||
private val userTypingCache: ConcurrentMutableMap<ConversationId, MutableSet<ExpiringUserTyping>> | ||
) : TypingIndicatorRepository { | ||
|
||
private val userTypingDataSourceFlow: MutableSharedFlow<Unit> = | ||
MutableSharedFlow(extraBufferCapacity = 1, onBufferOverflow = BufferOverflow.DROP_OLDEST) | ||
|
||
override fun addTypingUserInConversation(conversationId: ConversationId, userId: UserId) { | ||
userTypingCache.safeComputeAndMutateSetValue(conversationId) { ExpiringUserTyping(userId, Clock.System.now()) } | ||
userTypingDataSourceFlow.tryEmit(Unit) | ||
} | ||
|
||
override fun removeTypingUserInConversation(conversationId: ConversationId, userId: UserId) { | ||
userTypingCache.block { entry -> | ||
entry[conversationId]?.apply { this.removeAll { it.userId == userId } } | ||
} | ||
userTypingDataSourceFlow.tryEmit(Unit) | ||
} | ||
|
||
override suspend fun observeUsersTyping(conversationId: ConversationId): Flow<Set<UserId>> { | ||
return userTypingDataSourceFlow | ||
.map { userTypingCache.filterUsersIdTypingInConversation(conversationId) } | ||
.onStart { emit(userTypingCache.filterUsersIdTypingInConversation(conversationId)) } | ||
.distinctUntilChanged() | ||
} | ||
} | ||
|
||
private fun ConcurrentMutableMap<ConversationId, MutableSet<ExpiringUserTyping>>.filterUsersIdTypingInConversation( | ||
conversationId: ConversationId | ||
) = this[conversationId]?.map { it.userId }?.toSet().orEmpty() | ||
|
||
// todo expire by worker | ||
class ExpiringUserTyping( | ||
val userId: UserId, | ||
val date: Instant | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
...commonMain/kotlin/com/wire/kalium/logic/feature/conversation/ObserveUsersTypingUseCase.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/* | ||
* 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.feature.conversation | ||
|
||
import com.wire.kalium.logic.data.conversation.TypingIndicatorRepository | ||
import com.wire.kalium.logic.data.id.ConversationId | ||
import com.wire.kalium.logic.data.user.UserId | ||
import kotlinx.coroutines.flow.Flow | ||
|
||
/** | ||
* Use case for observing current users typing in a given conversation. | ||
*/ | ||
interface ObserveUsersTypingUseCase { | ||
suspend fun invoke(conversationId: ConversationId): Flow<Set<UserId>> | ||
} | ||
|
||
internal class ObserveUsersTypingUseCaseImpl( | ||
private val typingIndicatorRepository: TypingIndicatorRepository | ||
) : ObserveUsersTypingUseCase { | ||
override suspend fun invoke(conversationId: ConversationId): Flow<Set<UserId>> = | ||
typingIndicatorRepository.observeUsersTyping(conversationId) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.