Skip to content

Commit c5a1b31

Browse files
committed
Handle "Mark as read" action for thread notification, but disable it for now.
1 parent bd5110a commit c5a1b31

File tree

6 files changed

+39
-21
lines changed

6 files changed

+39
-21
lines changed

libraries/push/api/src/main/kotlin/io/element/android/libraries/push/api/notifications/NotificationCleaner.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,12 @@ package io.element.android.libraries.push.api.notifications
1010
import io.element.android.libraries.matrix.api.core.EventId
1111
import io.element.android.libraries.matrix.api.core.RoomId
1212
import io.element.android.libraries.matrix.api.core.SessionId
13+
import io.element.android.libraries.matrix.api.core.ThreadId
1314

1415
interface NotificationCleaner {
1516
fun clearAllMessagesEvents(sessionId: SessionId)
1617
fun clearMessagesForRoom(sessionId: SessionId, roomId: RoomId)
18+
fun clearMessagesForThread(sessionId: SessionId, roomId: RoomId, threadId: ThreadId)
1719
fun clearEvent(sessionId: SessionId, eventId: EventId)
1820

1921
fun clearMembershipNotificationForSession(sessionId: SessionId)

libraries/push/impl/src/main/kotlin/io/element/android/libraries/push/impl/notifications/DefaultNotificationDrawerManager.kt

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -94,10 +94,10 @@ class DefaultNotificationDrawerManager(
9494
)
9595
}
9696
is NavigationState.Thread -> {
97-
onEnteringThread(
98-
navigationState.parentRoom.parentSpace.parentSession.sessionId,
99-
navigationState.parentRoom.roomId,
100-
navigationState.threadId
97+
clearMessagesForThread(
98+
sessionId = navigationState.parentRoom.parentSpace.parentSession.sessionId,
99+
roomId = navigationState.parentRoom.roomId,
100+
threadId = navigationState.threadId,
101101
)
102102
}
103103
}
@@ -146,6 +146,16 @@ class DefaultNotificationDrawerManager(
146146
clearSummaryNotificationIfNeeded(sessionId)
147147
}
148148

149+
/**
150+
* Should be called when the application is currently opened and showing timeline for the given threadId.
151+
* Used to ignore events related to that thread (no need to display notification) and clean any existing notification on this room.
152+
*/
153+
override fun clearMessagesForThread(sessionId: SessionId, roomId: RoomId, threadId: ThreadId) {
154+
val tag = NotificationCreator.messageTag(roomId, threadId)
155+
notificationManager.cancel(tag, NotificationIdProvider.getRoomMessagesNotificationId(sessionId))
156+
clearSummaryNotificationIfNeeded(sessionId)
157+
}
158+
149159
override fun clearMembershipNotificationForSession(sessionId: SessionId) {
150160
activeNotificationsProvider.getMembershipNotificationForSession(sessionId)
151161
.forEach { notificationManager.cancel(it.tag, it.id) }
@@ -177,16 +187,6 @@ class DefaultNotificationDrawerManager(
177187
}
178188
}
179189

180-
/**
181-
* Should be called when the application is currently opened and showing timeline for the given threadId.
182-
* Used to ignore events related to that thread (no need to display notification) and clean any existing notification on this room.
183-
*/
184-
private fun onEnteringThread(sessionId: SessionId, roomId: RoomId, threadId: ThreadId) {
185-
val tag = NotificationCreator.messageTag(roomId, threadId)
186-
notificationManager.cancel(tag, NotificationIdProvider.getRoomMessagesNotificationId(sessionId))
187-
clearSummaryNotificationIfNeeded(sessionId)
188-
}
189-
190190
private suspend fun renderEvents(eventsToRender: List<NotifiableEvent>) {
191191
// Group by sessionId
192192
val eventsForSessions = eventsToRender.groupBy {

libraries/push/impl/src/main/kotlin/io/element/android/libraries/push/impl/notifications/NotificationBroadcastReceiverHandler.kt

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,12 @@ class NotificationBroadcastReceiverHandler(
7272
notificationCleaner.clearEvent(sessionId, eventId)
7373
}
7474
actionIds.markRoomRead -> if (roomId != null) {
75-
notificationCleaner.clearMessagesForRoom(sessionId, roomId)
76-
handleMarkAsRead(sessionId, roomId)
75+
if (threadId == null) {
76+
notificationCleaner.clearMessagesForRoom(sessionId, roomId)
77+
} else {
78+
notificationCleaner.clearMessagesForThread(sessionId, roomId, threadId)
79+
}
80+
handleMarkAsRead(sessionId, roomId, threadId)
7781
}
7882
actionIds.join -> if (roomId != null) {
7983
notificationCleaner.clearMembershipNotificationForRoom(sessionId, roomId)
@@ -96,7 +100,8 @@ class NotificationBroadcastReceiverHandler(
96100
client.getRoom(roomId)?.leave()
97101
}
98102

99-
private fun handleMarkAsRead(sessionId: SessionId, roomId: RoomId) = appCoroutineScope.launch {
103+
private fun handleMarkAsRead(sessionId: SessionId, roomId: RoomId, threadId: ThreadId?) = appCoroutineScope.launch {
104+
// TODO Use threadId at some point.
100105
val client = matrixClientProvider.getOrRestore(sessionId).getOrNull() ?: return@launch
101106
val isSendPublicReadReceiptsEnabled = sessionPreferencesStore.get(sessionId, this).isSendPublicReadReceiptsEnabled().first()
102107
val receiptType = if (isSendPublicReadReceiptsEnabled) {

libraries/push/impl/src/main/kotlin/io/element/android/libraries/push/impl/notifications/factories/NotificationCreator.kt

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -209,8 +209,11 @@ class DefaultNotificationCreator(
209209
// Clear existing actions since we might be updating an existing notification
210210
clearActions()
211211
// Add actions and notification intents
212-
// Mark room as read
213-
addAction(markAsReadActionFactory.create(roomInfo))
212+
// Mark room/thread as read
213+
// TODO Add this action for thread at some point.
214+
if (threadId == null) {
215+
addAction(markAsReadActionFactory.create(roomInfo, threadId))
216+
}
214217
// Quick reply
215218
if (!roomInfo.hasSmartReplyError) {
216219
val latestEventId = events.lastOrNull()?.eventId

libraries/push/impl/src/main/kotlin/io/element/android/libraries/push/impl/notifications/factories/action/MarkAsReadActionFactory.kt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import io.element.android.appconfig.NotificationConfig
1616
import io.element.android.libraries.androidutils.uri.createIgnoredUri
1717
import io.element.android.libraries.designsystem.icons.CompoundDrawables
1818
import io.element.android.libraries.di.annotations.ApplicationContext
19+
import io.element.android.libraries.matrix.api.core.ThreadId
1920
import io.element.android.libraries.push.impl.R
2021
import io.element.android.libraries.push.impl.notifications.NotificationActionIds
2122
import io.element.android.libraries.push.impl.notifications.NotificationBroadcastReceiver
@@ -30,15 +31,16 @@ class MarkAsReadActionFactory(
3031
private val stringProvider: StringProvider,
3132
private val clock: SystemClock,
3233
) {
33-
fun create(roomInfo: RoomEventGroupInfo): NotificationCompat.Action? {
34+
fun create(roomInfo: RoomEventGroupInfo, threadId: ThreadId?): NotificationCompat.Action? {
3435
if (!NotificationConfig.SHOW_MARK_AS_READ_ACTION) return null
3536
val sessionId = roomInfo.sessionId.value
3637
val roomId = roomInfo.roomId.value
3738
val intent = Intent(context, NotificationBroadcastReceiver::class.java)
3839
intent.action = actionIds.markRoomRead
39-
intent.data = createIgnoredUri("markRead/$sessionId/$roomId")
40+
intent.data = createIgnoredUri("markRead/$sessionId/$roomId" + threadId?.let { "/$it" }.orEmpty())
4041
intent.putExtra(NotificationBroadcastReceiver.KEY_SESSION_ID, sessionId)
4142
intent.putExtra(NotificationBroadcastReceiver.KEY_ROOM_ID, roomId)
43+
threadId?.let { intent.putExtra(NotificationBroadcastReceiver.KEY_THREAD_ID, threadId.value) }
4244
val pendingIntent = PendingIntent.getBroadcast(
4345
context,
4446
clock.epochMillis().toInt(),

libraries/push/test/src/main/kotlin/io/element/android/libraries/push/test/notifications/FakeNotificationCleaner.kt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,14 @@ package io.element.android.libraries.push.test.notifications
1010
import io.element.android.libraries.matrix.api.core.EventId
1111
import io.element.android.libraries.matrix.api.core.RoomId
1212
import io.element.android.libraries.matrix.api.core.SessionId
13+
import io.element.android.libraries.matrix.api.core.ThreadId
1314
import io.element.android.libraries.push.api.notifications.NotificationCleaner
1415
import io.element.android.tests.testutils.lambda.lambdaError
1516

1617
class FakeNotificationCleaner(
1718
private val clearAllMessagesEventsLambda: (SessionId) -> Unit = { lambdaError() },
1819
private val clearMessagesForRoomLambda: (SessionId, RoomId) -> Unit = { _, _ -> lambdaError() },
20+
private val clearMessagesForThreadLambda: (SessionId, RoomId, ThreadId) -> Unit = { _, _, _ -> lambdaError() },
1921
private val clearEventLambda: (SessionId, EventId) -> Unit = { _, _ -> lambdaError() },
2022
private val clearMembershipNotificationForSessionLambda: (SessionId) -> Unit = { lambdaError() },
2123
private val clearMembershipNotificationForRoomLambda: (SessionId, RoomId) -> Unit = { _, _ -> lambdaError() }
@@ -28,6 +30,10 @@ class FakeNotificationCleaner(
2830
clearMessagesForRoomLambda(sessionId, roomId)
2931
}
3032

33+
override fun clearMessagesForThread(sessionId: SessionId, roomId: RoomId, threadId: ThreadId) {
34+
clearMessagesForThreadLambda(sessionId, roomId, threadId)
35+
}
36+
3137
override fun clearEvent(sessionId: SessionId, eventId: EventId) {
3238
clearEventLambda(sessionId, eventId)
3339
}

0 commit comments

Comments
 (0)