From 5c6c1c374dee4b5a2079c59e73ebb6aceecc2148 Mon Sep 17 00:00:00 2001 From: rhunk <101876869+rhunk@users.noreply.github.com> Date: Wed, 10 Apr 2024 23:15:06 +0200 Subject: [PATCH] feat(core/notifications): auto mark as read --- .../features/impl/messaging/AutoMarkAsRead.kt | 26 ++++++++++++------- .../features/impl/messaging/Notifications.kt | 1 + 2 files changed, 17 insertions(+), 10 deletions(-) diff --git a/core/src/main/kotlin/me/rhunk/snapenhance/core/features/impl/messaging/AutoMarkAsRead.kt b/core/src/main/kotlin/me/rhunk/snapenhance/core/features/impl/messaging/AutoMarkAsRead.kt index 80bd5dcc4..6bdec45a6 100644 --- a/core/src/main/kotlin/me/rhunk/snapenhance/core/features/impl/messaging/AutoMarkAsRead.kt +++ b/core/src/main/kotlin/me/rhunk/snapenhance/core/features/impl/messaging/AutoMarkAsRead.kt @@ -6,20 +6,26 @@ import me.rhunk.snapenhance.core.features.FeatureLoadParams import me.rhunk.snapenhance.core.features.impl.spying.StealthMode class AutoMarkAsRead : Feature("Auto Mark As Read", loadParams = FeatureLoadParams.INIT_SYNC) { + val isEnabled by lazy { context.config.messaging.autoMarkAsRead.get() } + + fun markConversationsAsRead(conversationIds: List) { + conversationIds.forEach { conversationId -> + val lastClientMessageId = context.database.getMessagesFromConversationId(conversationId, 1)?.firstOrNull()?.clientMessageId?.toLong() ?: Long.MAX_VALUE + context.feature(StealthMode::class).addDisplayedMessageException(lastClientMessageId) + context.feature(Messaging::class).conversationManager?.displayedMessages(conversationId, lastClientMessageId) { + if (it != null) { + context.log.warn("Failed to mark message $lastClientMessageId as read in conversation $conversationId") + } + } + } + } + override fun init() { - if (!context.config.messaging.autoMarkAsRead.get()) return + if (!isEnabled) return context.event.subscribe(SendMessageWithContentEvent::class) { event -> event.addCallbackResult("onSuccess") { - event.destinations.conversations!!.map { it.toString() }.forEach { conversationId -> - val lastClientMessageId = context.database.getMessagesFromConversationId(conversationId, 1)?.firstOrNull()?.clientMessageId?.toLong() ?: Long.MAX_VALUE - context.feature(StealthMode::class).addDisplayedMessageException(lastClientMessageId) - context.feature(Messaging::class).conversationManager?.displayedMessages(conversationId, lastClientMessageId) { - if (it != null) { - context.log.warn("Failed to mark message $lastClientMessageId as read in conversation $conversationId") - } - } - } + markConversationsAsRead(event.destinations.conversations?.map { it.toString() } ?: return@addCallbackResult) } } } diff --git a/core/src/main/kotlin/me/rhunk/snapenhance/core/features/impl/messaging/Notifications.kt b/core/src/main/kotlin/me/rhunk/snapenhance/core/features/impl/messaging/Notifications.kt index 7bb888360..f169b4f4b 100644 --- a/core/src/main/kotlin/me/rhunk/snapenhance/core/features/impl/messaging/Notifications.kt +++ b/core/src/main/kotlin/me/rhunk/snapenhance/core/features/impl/messaging/Notifications.kt @@ -201,6 +201,7 @@ class Notifications : Feature("Notifications", loadParams = FeatureLoadParams.IN }, onSuccess = { context.coroutineScope.launch(coroutineDispatcher) { appendNotificationText("${myUser.displayName ?: myUser.mutableUsername}: $input") + context.feature(AutoMarkAsRead::class).takeIf { it.isEnabled }?.markConversationsAsRead(listOf(conversationId)) } }) }