Skip to content

Commit 40d8722

Browse files
committed
Rely on the SessionObserver to detect a sign out.
1 parent d304977 commit 40d8722

File tree

3 files changed

+77
-48
lines changed

3 files changed

+77
-48
lines changed

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

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,10 @@ import io.element.android.libraries.push.api.notifications.NotificationIdProvide
2424
import io.element.android.libraries.push.impl.notifications.factories.NotificationCreator
2525
import io.element.android.libraries.push.impl.notifications.model.NotifiableEvent
2626
import io.element.android.libraries.push.impl.notifications.model.shouldIgnoreEventInRoom
27+
import io.element.android.libraries.sessionstorage.api.observer.SessionListener
28+
import io.element.android.libraries.sessionstorage.api.observer.SessionObserver
2729
import io.element.android.services.appnavstate.api.AppNavigationStateService
2830
import io.element.android.services.appnavstate.api.NavigationState
29-
import io.element.android.services.appnavstate.api.currentSessionId
3031
import kotlinx.coroutines.CoroutineScope
3132
import kotlinx.coroutines.launch
3233

@@ -46,28 +47,30 @@ class DefaultNotificationDrawerManager(
4647
private val matrixClientProvider: MatrixClientProvider,
4748
private val imageLoaderHolder: ImageLoaderHolder,
4849
private val activeNotificationsProvider: ActiveNotificationsProvider,
50+
sessionObserver: SessionObserver,
4951
) : NotificationCleaner {
5052
// TODO EAx add a setting per user for this
5153
private var useCompleteNotificationFormat = true
5254

55+
private val sessionListener = object : SessionListener {
56+
override suspend fun onSessionDeleted(userId: String, wasLastSession: Boolean) {
57+
// User signed out, clear all notifications related to the session.
58+
clearAllEvents(SessionId(userId))
59+
}
60+
}
61+
5362
init {
5463
// Observe application state
5564
coroutineScope.launch {
5665
appNavigationStateService.appNavigationState
5766
.collect { onAppNavigationStateChange(it.navigationState) }
5867
}
68+
sessionObserver.addListener(sessionListener)
5969
}
6070

61-
private var currentAppNavigationState: NavigationState? = null
62-
6371
private fun onAppNavigationStateChange(navigationState: NavigationState) {
6472
when (navigationState) {
65-
NavigationState.Root -> {
66-
currentAppNavigationState?.currentSessionId()?.let { sessionId ->
67-
// User signed out, clear all notifications related to the session.
68-
clearAllEvents(sessionId)
69-
}
70-
}
73+
NavigationState.Root -> {}
7174
is NavigationState.Session -> {}
7275
is NavigationState.Space -> {}
7376
is NavigationState.Room -> {
@@ -85,7 +88,6 @@ class DefaultNotificationDrawerManager(
8588
)
8689
}
8790
}
88-
currentAppNavigationState = navigationState
8991
}
9092

9193
/**

libraries/push/impl/src/test/kotlin/io/element/android/libraries/push/impl/notifications/DefaultNotificationDrawerManagerTest.kt

Lines changed: 64 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,9 @@ import io.element.android.libraries.push.impl.notifications.fake.FakeRoomGroupMe
3131
import io.element.android.libraries.push.impl.notifications.fake.FakeSummaryGroupMessageCreator
3232
import io.element.android.libraries.push.impl.notifications.fixtures.aNotifiableMessageEvent
3333
import io.element.android.libraries.sessionstorage.api.SessionStore
34+
import io.element.android.libraries.sessionstorage.api.observer.SessionObserver
3435
import io.element.android.libraries.sessionstorage.test.InMemorySessionStore
36+
import io.element.android.libraries.sessionstorage.test.observer.FakeSessionObserver
3537
import io.element.android.services.appnavstate.api.AppNavigationState
3638
import io.element.android.services.appnavstate.api.AppNavigationStateService
3739
import io.element.android.services.appnavstate.api.NavigationState
@@ -205,35 +207,70 @@ class DefaultNotificationDrawerManagerTest {
205207
)
206208
}
207209

208-
private fun TestScope.createDefaultNotificationDrawerManager(
209-
notificationDisplayer: NotificationDisplayer = FakeNotificationDisplayer(),
210-
appNavigationStateService: AppNavigationStateService = FakeAppNavigationStateService(),
211-
roomGroupMessageCreator: RoomGroupMessageCreator = FakeRoomGroupMessageCreator(),
212-
summaryGroupMessageCreator: SummaryGroupMessageCreator = FakeSummaryGroupMessageCreator(),
213-
activeNotificationsProvider: FakeActiveNotificationsProvider = FakeActiveNotificationsProvider(),
214-
matrixClientProvider: FakeMatrixClientProvider = FakeMatrixClientProvider(),
215-
sessionStore: SessionStore = InMemorySessionStore(),
216-
enterpriseService: EnterpriseService = FakeEnterpriseService(),
217-
): DefaultNotificationDrawerManager {
218-
return DefaultNotificationDrawerManager(
210+
@Test
211+
fun `when a session is signed out, clearAllEvent is invoked`() = runTest {
212+
val cancelNotificationResult = lambdaRecorder<String?, Int, Unit> { _, _ -> }
213+
val notificationDisplayer = FakeNotificationDisplayer(
214+
cancelNotificationResult = cancelNotificationResult,
215+
)
216+
val summaryId = NotificationIdProvider.getSummaryNotificationId(A_SESSION_ID)
217+
val activeNotificationsProvider = FakeActiveNotificationsProvider(
218+
getNotificationsForSessionResult = {
219+
listOf(
220+
mockk {
221+
every { id } returns summaryId
222+
every { tag } returns null
223+
},
224+
)
225+
},
226+
countResult = { 1 },
227+
)
228+
val sessionObserver = FakeSessionObserver()
229+
createDefaultNotificationDrawerManager(
219230
notificationDisplayer = notificationDisplayer,
220-
notificationRenderer = NotificationRenderer(
221-
notificationDisplayer = FakeNotificationDisplayer(),
222-
notificationDataFactory = DefaultNotificationDataFactory(
223-
notificationCreator = FakeNotificationCreator(),
224-
roomGroupMessageCreator = roomGroupMessageCreator,
225-
summaryGroupMessageCreator = summaryGroupMessageCreator,
226-
activeNotificationsProvider = activeNotificationsProvider,
227-
stringProvider = FakeStringProvider(),
228-
),
229-
enterpriseService = enterpriseService,
230-
sessionStore = sessionStore,
231-
),
232-
appNavigationStateService = appNavigationStateService,
233-
coroutineScope = backgroundScope,
234-
matrixClientProvider = matrixClientProvider,
235-
imageLoaderHolder = FakeImageLoaderHolder(),
236231
activeNotificationsProvider = activeNotificationsProvider,
232+
sessionObserver = sessionObserver,
233+
)
234+
// Simulate a session sign out
235+
sessionObserver.onSessionDeleted(A_SESSION_ID.value)
236+
// Verify we asked to cancel the notification with summaryId
237+
cancelNotificationResult.assertions().isCalledExactly(1).withSequence(
238+
listOf(value(null), value(summaryId)),
237239
)
238240
}
239241
}
242+
243+
fun TestScope.createDefaultNotificationDrawerManager(
244+
notificationDisplayer: NotificationDisplayer = FakeNotificationDisplayer(),
245+
notificationRenderer: NotificationRenderer? = null,
246+
appNavigationStateService: AppNavigationStateService = FakeAppNavigationStateService(),
247+
roomGroupMessageCreator: RoomGroupMessageCreator = FakeRoomGroupMessageCreator(),
248+
summaryGroupMessageCreator: SummaryGroupMessageCreator = FakeSummaryGroupMessageCreator(),
249+
activeNotificationsProvider: FakeActiveNotificationsProvider = FakeActiveNotificationsProvider(),
250+
matrixClientProvider: FakeMatrixClientProvider = FakeMatrixClientProvider(),
251+
sessionStore: SessionStore = InMemorySessionStore(),
252+
enterpriseService: EnterpriseService = FakeEnterpriseService(),
253+
sessionObserver: SessionObserver = FakeSessionObserver(),
254+
): DefaultNotificationDrawerManager {
255+
return DefaultNotificationDrawerManager(
256+
notificationDisplayer = notificationDisplayer,
257+
notificationRenderer = notificationRenderer ?: NotificationRenderer(
258+
notificationDisplayer = FakeNotificationDisplayer(),
259+
notificationDataFactory = DefaultNotificationDataFactory(
260+
notificationCreator = FakeNotificationCreator(),
261+
roomGroupMessageCreator = roomGroupMessageCreator,
262+
summaryGroupMessageCreator = summaryGroupMessageCreator,
263+
activeNotificationsProvider = activeNotificationsProvider,
264+
stringProvider = FakeStringProvider(),
265+
),
266+
enterpriseService = enterpriseService,
267+
sessionStore = sessionStore,
268+
),
269+
appNavigationStateService = appNavigationStateService,
270+
coroutineScope = backgroundScope,
271+
matrixClientProvider = matrixClientProvider,
272+
imageLoaderHolder = FakeImageLoaderHolder(),
273+
activeNotificationsProvider = activeNotificationsProvider,
274+
sessionObserver = sessionObserver,
275+
)
276+
}

libraries/push/impl/src/test/kotlin/io/element/android/libraries/push/impl/notifications/DefaultOnMissedCallNotificationHandlerTest.kt

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,9 @@ import io.element.android.libraries.matrix.test.FakeMatrixClient
1616
import io.element.android.libraries.matrix.test.FakeMatrixClientProvider
1717
import io.element.android.libraries.matrix.test.notification.FakeNotificationService
1818
import io.element.android.libraries.matrix.test.notification.aNotificationData
19-
import io.element.android.libraries.matrix.ui.media.test.FakeImageLoaderHolder
20-
import io.element.android.libraries.push.impl.notifications.fake.FakeActiveNotificationsProvider
2119
import io.element.android.libraries.push.impl.notifications.fake.FakeNotificationDataFactory
22-
import io.element.android.libraries.push.impl.notifications.fake.FakeNotificationDisplayer
2320
import io.element.android.libraries.push.impl.notifications.fixtures.aNotifiableMessageEvent
2421
import io.element.android.libraries.push.test.notifications.FakeCallNotificationEventResolver
25-
import io.element.android.services.appnavstate.test.FakeAppNavigationStateService
2622
import io.element.android.tests.testutils.lambda.lambdaRecorder
2723
import kotlinx.coroutines.ExperimentalCoroutinesApi
2824
import kotlinx.coroutines.test.runCurrent
@@ -47,16 +43,10 @@ class DefaultOnMissedCallNotificationHandlerTest {
4743
})
4844
val defaultOnMissedCallNotificationHandler = DefaultOnMissedCallNotificationHandler(
4945
matrixClientProvider = matrixClientProvider,
50-
defaultNotificationDrawerManager = DefaultNotificationDrawerManager(
51-
notificationDisplayer = FakeNotificationDisplayer(),
46+
defaultNotificationDrawerManager = createDefaultNotificationDrawerManager(
5247
notificationRenderer = createNotificationRenderer(
5348
notificationDataFactory = dataFactory,
5449
),
55-
appNavigationStateService = FakeAppNavigationStateService(),
56-
coroutineScope = backgroundScope,
57-
matrixClientProvider = FakeMatrixClientProvider(),
58-
imageLoaderHolder = FakeImageLoaderHolder(),
59-
activeNotificationsProvider = FakeActiveNotificationsProvider(),
6050
),
6151
callNotificationEventResolver = FakeCallNotificationEventResolver(resolveEventLambda = { _, _, _ ->
6252
Result.success(aNotifiableMessageEvent())

0 commit comments

Comments
 (0)