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

Commit 08d362c3 authored by Philipp Heckel's avatar Philipp Heckel
Browse files

WIP: Update messages

parent fbf2a75e
Loading
Loading
Loading
Loading
+9 −3
Original line number Diff line number Diff line
@@ -2,7 +2,7 @@
  "formatVersion": 1,
  "database": {
    "version": 9,
    "identityHash": "5bab75c3b41c53c9855fe3a7ef8f0669",
    "identityHash": "9d62f6db149468db28b5e175be3a9669",
    "entities": [
      {
        "tableName": "Subscription",
@@ -82,7 +82,7 @@
      },
      {
        "tableName": "Notification",
        "createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`id` TEXT NOT NULL, `subscriptionId` INTEGER NOT NULL, `timestamp` INTEGER NOT NULL, `title` TEXT NOT NULL, `message` TEXT NOT NULL, `encoding` TEXT NOT NULL, `notificationId` INTEGER NOT NULL, `priority` INTEGER NOT NULL DEFAULT 3, `tags` TEXT NOT NULL, `click` TEXT NOT NULL, `deleted` INTEGER NOT NULL, `attachment_name` TEXT, `attachment_type` TEXT, `attachment_size` INTEGER, `attachment_expires` INTEGER, `attachment_url` TEXT, `attachment_contentUri` TEXT, `attachment_progress` INTEGER, PRIMARY KEY(`id`, `subscriptionId`))",
        "createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`id` TEXT NOT NULL, `subscriptionId` INTEGER NOT NULL, `timestamp` INTEGER NOT NULL, `updated` INTEGER NOT NULL, `title` TEXT NOT NULL, `message` TEXT NOT NULL, `encoding` TEXT NOT NULL, `notificationId` INTEGER NOT NULL, `priority` INTEGER NOT NULL DEFAULT 3, `tags` TEXT NOT NULL, `click` TEXT NOT NULL, `deleted` INTEGER NOT NULL, `attachment_name` TEXT, `attachment_type` TEXT, `attachment_size` INTEGER, `attachment_expires` INTEGER, `attachment_url` TEXT, `attachment_contentUri` TEXT, `attachment_progress` INTEGER, PRIMARY KEY(`id`, `subscriptionId`))",
        "fields": [
          {
            "fieldPath": "id",
@@ -102,6 +102,12 @@
            "affinity": "INTEGER",
            "notNull": true
          },
          {
            "fieldPath": "updated",
            "columnName": "updated",
            "affinity": "INTEGER",
            "notNull": true
          },
          {
            "fieldPath": "title",
            "columnName": "title",
@@ -290,7 +296,7 @@
    "views": [],
    "setupQueries": [
      "CREATE TABLE IF NOT EXISTS room_master_table (id INTEGER PRIMARY KEY,identity_hash TEXT)",
      "INSERT OR REPLACE INTO room_master_table (id,identity_hash) VALUES(42, '5bab75c3b41c53c9855fe3a7ef8f0669')"
      "INSERT OR REPLACE INTO room_master_table (id,identity_hash) VALUES(42, '9d62f6db149468db28b5e175be3a9669')"
    ]
  }
}
 No newline at end of file
+4 −1
Original line number Diff line number Diff line
@@ -122,10 +122,11 @@ class Backuper(val context: Context) {
                } else {
                    null
                }
                repository.addNotification(io.heckel.ntfy.db.Notification(
                repository.upsertNotification(io.heckel.ntfy.db.Notification(
                    id = n.id,
                    subscriptionId = n.subscriptionId,
                    timestamp = n.timestamp,
                    updated = n.updated ?: 0L,
                    title = n.title,
                    message = n.message,
                    encoding = n.encoding,
@@ -218,6 +219,7 @@ class Backuper(val context: Context) {
                id = n.id,
                subscriptionId = n.subscriptionId,
                timestamp = n.timestamp,
                updated = n.updated,
                title = n.title,
                message = n.message,
                encoding = n.encoding,
@@ -284,6 +286,7 @@ data class Notification(
    val id: String,
    val subscriptionId: Long,
    val timestamp: Long,
    val updated: Long?,
    val title: String,
    val message: String,
    val encoding: String, // "base64" or ""
+1 −0
Original line number Diff line number Diff line
@@ -48,6 +48,7 @@ data class Notification(
    @ColumnInfo(name = "id") val id: String,
    @ColumnInfo(name = "subscriptionId") val subscriptionId: Long,
    @ColumnInfo(name = "timestamp") val timestamp: Long, // Unix timestamp
    @ColumnInfo(name = "updated") val updated: Long, // Unix timestamp, may be zero
    @ColumnInfo(name = "title") val title: String,
    @ColumnInfo(name = "message") val message: String,
    @ColumnInfo(name = "encoding") val encoding: String, // "base64" or ""
+28 −6
Original line number Diff line number Diff line
@@ -109,15 +109,37 @@ class Repository(private val sharedPrefs: SharedPreferences, private val databas
        return notifications.filterNot { existingIds.contains(it.id) }
    }

    /**
     * Adds the notification to the database (if it does not exist), or updates it if it does.
     * Returns null if the notification was not added/updated, or the added/updated notification if it was.
     *
     * You should use the returned notification for further processing.
     */
    @Suppress("RedundantSuspendModifier")
    @WorkerThread
    suspend fun addNotification(notification: Notification): Boolean {
        val maybeExistingNotification = notificationDao.get(notification.id)
        if (maybeExistingNotification != null) {
            return false
    suspend fun upsertNotification(notification: Notification): Notification? {
        val existingNotification = notificationDao.get(notification.id)
        if (existingNotification != null) {
            return maybeUpdateExistingNotification(existingNotification, notification)
        }
        notificationDao.add(notification)
        return true
        return notification
    }

    private fun maybeUpdateExistingNotification(existingNotification: Notification, notification: Notification): Notification? {
        if (notification.updated == 0L) {
            return null
        } else if (notification.updated <= existingNotification.updated) {
            return null
        }
        val newNotification = existingNotification.copy(
            message = notification.message,
            title = notification.title,
            tags = notification.tags,
            priority = notification.priority,
            click = notification.click
        )
        notificationDao.update(newNotification)
        return newNotification
    }

    fun updateNotification(notification: Notification) {
+1 −0
Original line number Diff line number Diff line
@@ -8,6 +8,7 @@ import androidx.annotation.Keep
data class Message(
    val id: String,
    val time: Long,
    val updated: Long?,
    val event: String,
    val topic: String,
    val priority: Int?,
Loading