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

Commit 6dca373e authored by Guillaume Jacquart's avatar Guillaume Jacquart
Browse files

feat:3196: allow to update message with first tag as id.

parent 16f6f0be
Loading
Loading
Loading
Loading
Loading
+27 −19
Original line number Diff line number Diff line
@@ -8,6 +8,7 @@ import foundation.e.notificationsreceiver.domain.entities.Topic
import foundation.e.notificationsreceiver.domain.repositories.MessagesRepository
import foundation.e.notificationsreceiver.domain.utils.runSuspendCatching
import io.heckel.ntfy.db.Database
import io.heckel.ntfy.db.Notification
import io.heckel.ntfy.db.NotificationDao
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.Flow
@@ -18,10 +19,8 @@ import javax.inject.Inject
import javax.inject.Singleton

@Singleton
class MessagesRepositoryImpl @Inject constructor(
    @ApplicationContext private val appContext: Context,

) : MessagesRepository {
class MessagesRepositoryImpl @Inject constructor(@ApplicationContext private val appContext: Context) :
    MessagesRepository {
    private val notificationsDao: NotificationDao

    init {
@@ -31,22 +30,25 @@ class MessagesRepositoryImpl @Inject constructor(

    override fun messages(subscriptions: List<Topic>): Flow<List<Message>> {
        return notificationsDao.listFlow(subscriptions.map { it.localId }).map { notifications ->
            notifications.mapNotNull {
                with(it) {
                    runSuspendCatching {
            notifications.mapNotNull(::notificationToMessage)
        }
    }

    internal fun notificationToMessage(notification: Notification): Message? {
        return runSuspendCatching {
            with(notification) {
                Message(
                            id = id,
                    id = tags.split(",").firstOrNull().takeIf { !it.isNullOrBlank() } ?: id,
                    timestamp = Instant.ofEpochSecond(timestamp),
                    title = title,
                    content = message,
                    icon = IconType.EOS,
                    read = deleted,
                    ntfyId = id,
                    notificationId = notificationId,
                )
                    }.getOrNull()
                }
            }
            }
        }.getOrNull()
    }

    override suspend fun markAsRead(localId: String) {
@@ -54,4 +56,10 @@ class MessagesRepositoryImpl @Inject constructor(
            notificationsDao.markAsDeleted(notificationId = localId)
        }
    }

    override suspend fun removeAll(localIds: List<String>) {
        withContext(Dispatchers.IO) {
            notificationsDao.removeAll(notificationIds = localIds)
        }
    }
}
+3 −0
Original line number Diff line number Diff line
@@ -477,6 +477,9 @@ interface NotificationDao {
    @Query("DELETE FROM notification WHERE subscriptionId = :subscriptionId")
    fun removeAll(subscriptionId: Long)

    @Query("DELETE FROM notification WHERE id IN (:notificationIds)")
    fun removeAll(notificationIds: List<String>)

    @Query("SELECT * FROM notification WHERE subscriptionId IN (:subscriptionIds) ORDER BY timestamp DESC")
    fun listFlow(subscriptionIds: List<Long>): Flow<List<Notification>>
}
+1 −0
Original line number Diff line number Diff line
@@ -41,6 +41,7 @@ spotless {
            "ktlint_standard_property-naming": "disabled",
            "ktlint_standard_discouraged-comment-location": "disabled",
            "ktlint_standard_function-expression-body": "disabled",
            "max_line_length": 120
        ])
        target(
                 "app/src/main/java/foundation/e/notificationsreceiver/**/*.kt",
+1 −0
Original line number Diff line number Diff line
@@ -26,5 +26,6 @@ val messageFixture = Message(
    title = "Message Fixture",
    content = "Simple content of the base fixture of a Message\n",
    read = false,
    ntfyId = "ntfyid_1",
    notificationId = 36
)
+1 −0
Original line number Diff line number Diff line
@@ -26,6 +26,7 @@ data class Message(
    val content: String,
    val icon: IconType = IconType.EOS,
    val read: Boolean = false,
    val ntfyId: String,
    val notificationId: Int,
) {
    enum class IconType {
Loading