Skip to content

Reappearing notifications on slow homeservers #4238

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

Merged
merged 6 commits into from
Oct 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.d/3437.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fixes reappearing notifications when dismissing notifications from slow homeservers or delayed /sync responses
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Copyright (c) 2021 New Vector Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package im.vector.app.features.notifications

/**
* A FIFO circular buffer of T
* This class is not thread safe
*/
class CircularCache<T : Any>(cacheSize: Int, factory: (Int) -> Array<T?>) {

companion object {
inline fun <reified T : Any> create(cacheSize: Int) = CircularCache(cacheSize) { Array<T?>(cacheSize) { null } }
}

private val cache = factory(cacheSize)
private var writeIndex = 0

fun contains(key: T): Boolean = cache.contains(key)

fun put(key: T) {
if (writeIndex == cache.size) {
writeIndex = 0
}
cache[writeIndex] = key
writeIndex++
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,13 @@ class NotificationDrawerManager @Inject constructor(private val context: Context

private var useCompleteNotificationFormat = vectorPreferences.useCompleteNotificationFormat()

/**
* An in memory FIFO cache of the seen events.
* Acts as a notification debouncer to stop already dismissed push notifications from
* displaying again when the /sync response is delayed.
*/
private val seenEventIds = CircularCache.create<String>(cacheSize = 25)

/**
Should be called as soon as a new event is ready to be displayed.
The notification corresponding to this event will not be displayed until
Expand Down Expand Up @@ -141,7 +148,13 @@ class NotificationDrawerManager @Inject constructor(private val context: Context
}
} else {
// Not an edit
eventList.add(notifiableEvent)
if (seenEventIds.contains(notifiableEvent.eventId)) {
// we've already seen the event, lets skip
Timber.d("onNotifiableEventReceived(): skipping event, already seen")
} else {
seenEventIds.put(notifiableEvent.eventId)
eventList.add(notifiableEvent)
}
}
}
}
Expand Down Expand Up @@ -266,7 +279,7 @@ class NotificationDrawerManager @Inject constructor(private val context: Context
is InviteNotifiableEvent -> {
if (autoAcceptInvites.hideInvites) {
// Forget this event
eventIterator.remove()
eventIterator.remove()
} else {
invitationEvents.add(event)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
* Copyright (c) 2021 New Vector Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package im.vector.app.features.notifications

import org.amshove.kluent.shouldBeEqualTo
import org.junit.Test

class CircularCacheTest {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💯 for adding tests!


@Test
fun `when putting more than cache size then cache is limited to cache size`() {
val (cache, internalData) = createIntCache(cacheSize = 3)

cache.putInOrder(1, 1, 1, 1, 1, 1)

internalData shouldBeEqualTo arrayOf(1, 1, 1)
}

@Test
fun `when putting more than cache then acts as FIFO`() {
val (cache, internalData) = createIntCache(cacheSize = 3)

cache.putInOrder(1, 2, 3, 4)

internalData shouldBeEqualTo arrayOf(4, 2, 3)
}

@Test
fun `given empty cache when checking if contains key then is false`() {
val (cache, _) = createIntCache(cacheSize = 3)

val result = cache.contains(1)

result shouldBeEqualTo false
}

@Test
fun `given cached key when checking if contains key then is true`() {
val (cache, _) = createIntCache(cacheSize = 3)

cache.put(1)
val result = cache.contains(1)

result shouldBeEqualTo true
}

private fun createIntCache(cacheSize: Int): Pair<CircularCache<Int>, Array<Int?>> {
var internalData: Array<Int?>? = null
val factory: (Int) -> Array<Int?> = {
Array<Int?>(it) { null }.also { array -> internalData = array }
}
return CircularCache(cacheSize, factory) to internalData!!
}

private fun CircularCache<Int>.putInOrder(vararg keys: Int) {
keys.forEach { put(it) }
}
}