-
Notifications
You must be signed in to change notification settings - Fork 6
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(typing): send typing indicator events - epic (WP-4590) #2127
Merged
Merged
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
991c497
feat(typing): send typing indicator events networking layer pt1. (WPB…
yamilmedina 2a9fd0f
Merge branch 'develop' into epic/sending-typing-events
yamilmedina cbf5233
fix: not block delay, to not block send stop
yamilmedina a2149d9
feat: add missing usecase to clear reusing repository
yamilmedina 33597cc
feat: add missing usecase to clear reusing repository
yamilmedina File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
53 changes: 53 additions & 0 deletions
53
...nMain/kotlin/com/wire/kalium/logic/data/conversation/TypingIndicatorOutgoingRepository.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,53 @@ | ||
/* | ||
* 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 com.wire.kalium.logic.CoreFailure | ||
import com.wire.kalium.logic.data.id.ConversationId | ||
import com.wire.kalium.logic.data.properties.UserPropertyRepository | ||
import com.wire.kalium.logic.functional.Either | ||
|
||
internal interface TypingIndicatorOutgoingRepository { | ||
suspend fun sendTypingIndicatorStatus( | ||
conversationId: ConversationId, | ||
typingStatus: Conversation.TypingIndicatorMode | ||
): Either<CoreFailure, Unit> | ||
|
||
} | ||
|
||
internal class TypingIndicatorOutgoingRepositoryImpl( | ||
private val typingIndicatorSenderHandler: TypingIndicatorSenderHandler, | ||
private val userPropertyRepository: UserPropertyRepository | ||
) : TypingIndicatorOutgoingRepository { | ||
|
||
override suspend fun sendTypingIndicatorStatus( | ||
conversationId: ConversationId, | ||
typingStatus: Conversation.TypingIndicatorMode | ||
): Either<CoreFailure, Unit> { | ||
if (userPropertyRepository.getTypingIndicatorStatus()) { | ||
when (typingStatus) { | ||
Conversation.TypingIndicatorMode.STARTED -> | ||
typingIndicatorSenderHandler.sendStartedAndEnqueueStoppingEvent(conversationId) | ||
|
||
Conversation.TypingIndicatorMode.STOPPED -> typingIndicatorSenderHandler.sendStoppingEvent(conversationId) | ||
} | ||
} | ||
return Either.Right(Unit) | ||
} | ||
|
||
} |
106 changes: 106 additions & 0 deletions
106
...commonMain/kotlin/com/wire/kalium/logic/data/conversation/TypingIndicatorSenderHandler.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,106 @@ | ||
/* | ||
* 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 com.wire.kalium.logic.data.id.ConversationId | ||
import com.wire.kalium.logic.functional.fold | ||
import com.wire.kalium.logic.kaliumLogger | ||
import com.wire.kalium.util.KaliumDispatcher | ||
import com.wire.kalium.util.KaliumDispatcherImpl | ||
import kotlinx.coroutines.CoroutineScope | ||
import kotlinx.coroutines.delay | ||
import kotlinx.coroutines.launch | ||
import kotlinx.coroutines.sync.Mutex | ||
import kotlinx.coroutines.sync.withLock | ||
import kotlin.coroutines.CoroutineContext | ||
import kotlin.time.DurationUnit | ||
import kotlin.time.toDuration | ||
|
||
/** | ||
* Outgoing user typing sent events manager. | ||
* | ||
* - It will send started and stopped events. | ||
* - For each started sent event, will 'enqueue' a stopped event after a timeout. | ||
* | ||
*/ | ||
internal interface TypingIndicatorSenderHandler { | ||
fun sendStoppingEvent(conversationId: ConversationId) | ||
fun sendStartedAndEnqueueStoppingEvent(conversationId: ConversationId) | ||
} | ||
|
||
internal class TypingIndicatorSenderHandlerImpl( | ||
private val conversationRepository: ConversationRepository, | ||
private val kaliumDispatcher: KaliumDispatcher = KaliumDispatcherImpl, | ||
userSessionCoroutineScope: CoroutineScope | ||
) : TypingIndicatorSenderHandler, CoroutineScope by userSessionCoroutineScope { | ||
override val coroutineContext: CoroutineContext | ||
get() = kaliumDispatcher.default | ||
|
||
private val outgoingStoppedQueueTypingEventsMutex = Mutex() | ||
private val outgoingStoppedQueueTypingEvents = mutableMapOf<ConversationId, Unit>() | ||
private val typingIndicatorTimeoutInSeconds = 10.toDuration(DurationUnit.SECONDS) | ||
|
||
/** | ||
* Sends a stopping event and removes it from the 'queue'. | ||
*/ | ||
override fun sendStoppingEvent(conversationId: ConversationId) { | ||
launch { | ||
outgoingStoppedQueueTypingEventsMutex.withLock { | ||
if (!outgoingStoppedQueueTypingEvents.containsKey(conversationId)) { | ||
return@launch | ||
} | ||
outgoingStoppedQueueTypingEvents.remove(conversationId) | ||
sendTypingIndicatorStatus(conversationId, Conversation.TypingIndicatorMode.STOPPED) | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Sends a started event and enqueues a stopping event if sent successfully. | ||
*/ | ||
override fun sendStartedAndEnqueueStoppingEvent(conversationId: ConversationId) { | ||
launch { | ||
outgoingStoppedQueueTypingEventsMutex.withLock { | ||
if (outgoingStoppedQueueTypingEvents.containsKey(conversationId)) { | ||
return@launch | ||
} | ||
val isSent = sendTypingIndicatorStatus(conversationId, Conversation.TypingIndicatorMode.STARTED) | ||
when (isSent) { | ||
true -> { | ||
outgoingStoppedQueueTypingEvents[conversationId] = Unit | ||
delay(typingIndicatorTimeoutInSeconds) | ||
sendStoppingEvent(conversationId) | ||
} | ||
|
||
false -> Unit // do nothing | ||
} | ||
} | ||
} | ||
} | ||
|
||
private suspend fun sendTypingIndicatorStatus( | ||
conversationId: ConversationId, | ||
typingStatus: Conversation.TypingIndicatorMode | ||
): Boolean = conversationRepository.sendTypingIndicatorStatus(conversationId, typingStatus).fold({ | ||
kaliumLogger.w("Skipping failed to send typing indicator status: $typingStatus") | ||
false | ||
}) { | ||
kaliumLogger.i("Successfully sent typing started indicator status: $typingStatus") | ||
true | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
.../commonMain/kotlin/com/wire/kalium/logic/data/conversation/TypingIndicatorStatusMapper.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,33 @@ | ||
/* | ||
* 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 com.wire.kalium.network.api.base.authenticated.conversation.TypingIndicatorStatus | ||
import com.wire.kalium.network.api.base.authenticated.conversation.TypingIndicatorStatusDTO | ||
|
||
fun TypingIndicatorStatus.toModel(): Conversation.TypingIndicatorMode = when (this) { | ||
TypingIndicatorStatus.STARTED -> Conversation.TypingIndicatorMode.STARTED | ||
TypingIndicatorStatus.STOPPED -> Conversation.TypingIndicatorMode.STOPPED | ||
} | ||
|
||
fun Conversation.TypingIndicatorMode.toApi(): TypingIndicatorStatus = when (this) { | ||
Conversation.TypingIndicatorMode.STARTED -> TypingIndicatorStatus.STARTED | ||
Conversation.TypingIndicatorMode.STOPPED -> TypingIndicatorStatus.STOPPED | ||
} | ||
|
||
fun Conversation.TypingIndicatorMode.toStatusDto(): TypingIndicatorStatusDTO = TypingIndicatorStatusDTO(this.toApi()) |
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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure why we are using tryEmit if we are not checking the result value?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is so we can make the Observers of
userTypingDataSourceFlow
collect
the new state. Since we are relying on this flow to get new mapuserTypingCache
states to consumers.