Skip to content

Commit

Permalink
feat(core/notifications): auto mark as read
Browse files Browse the repository at this point in the history
  • Loading branch information
rhunk committed Apr 10, 2024
1 parent 8da3bf8 commit 5c6c1c3
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>) {
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)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
})
}
Expand Down

0 comments on commit 5c6c1c3

Please sign in to comment.