Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit b1a32713 authored by Guillaume Jacquart's avatar Guillaume Jacquart
Browse files

fix:sentry-8669029d: fix crash on unexpected double subscription creation.

parent 2d2c78be
Loading
Loading
Loading
Loading
Loading
+7 −1
Original line number Diff line number Diff line
@@ -17,10 +17,12 @@
package foundation.e.notificationsreceiver.repositories

import android.content.Context
import android.database.sqlite.SQLiteConstraintException
import dagger.hilt.android.qualifiers.ApplicationContext
import foundation.e.notificationsreceiver.domain.entities.Topic
import foundation.e.notificationsreceiver.domain.repositories.SubscriptionsRepository
import foundation.e.notificationsreceiver.domain.utils.AppBackgroundScope
import foundation.e.notificationsreceiver.domain.utils.w
import io.heckel.ntfy.R
import io.heckel.ntfy.db.Database
import io.heckel.ntfy.db.Repository
@@ -95,7 +97,11 @@ class SubscriptionsRepositoryImpl @Inject constructor(
            lastActive = Date().time / 1000,
        )

        try {
            subscriptionDao.add(subscription)
        } catch (e: SQLiteConstraintException) {
            w("Skip insert subscription, looks already there: ${e.message}")
        }
        debouncedRefreshSubscriptions()
    }

+17 −11
Original line number Diff line number Diff line
@@ -24,6 +24,8 @@ import foundation.e.notificationsreceiver.domain.utils.AppBackgroundScope
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.sync.Mutex
import kotlinx.coroutines.sync.withLock
import javax.inject.Inject
import javax.inject.Singleton

@@ -41,6 +43,8 @@ class RegisterToEosNotifications @Inject constructor(
        private val SUPPORTED_LANGUAGES = setOf("de", "en", "es", "fr", "it")
    }

    private val mutex = Mutex()

    fun register() {
        backgroundScope.launch(Dispatchers.IO) {
            updateRegistration()
@@ -48,6 +52,7 @@ class RegisterToEosNotifications @Inject constructor(
    }

    internal suspend fun updateRegistration() {
        mutex.withLock {
            val toSubscribeTopics = buildTopicsList()

            val savedEOSTopics = subscriptionRepository.getSubscriptions()
@@ -63,6 +68,7 @@ class RegisterToEosNotifications @Inject constructor(
                subscriptionRepository.createSubscription(it)
            }
        }
    }

    internal suspend fun buildTopicsList(): List<Topic> {
        val topics = mutableListOf(ALL_DEVICES_TOPIC_BASE)