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

Commit f68bb5f3 authored by Hunter Kehoe's avatar Hunter Kehoe
Browse files

delete unreferenced icons periodically and download updates every 24 hours

parent c7edb50e
Loading
Loading
Loading
Loading
+8 −2
Original line number Diff line number Diff line
@@ -380,8 +380,14 @@ interface NotificationDao {
    @Query("SELECT * FROM notification WHERE deleted = 1 AND attachment_contentUri <> ''")
    fun listDeletedWithAttachments(): List<Notification>

    @Query("SELECT * FROM notification WHERE deleted = 1 AND icon_contentUri <> ''")
    fun listDeletedWithIcons(): List<Notification>
    @Query("SELECT DISTINCT icon_contentUri FROM notification WHERE deleted != 1 AND icon_contentUri <> ''")
    fun listActiveIconUris(): List<String>

    @Query("SELECT DISTINCT icon_contentUri FROM notification WHERE deleted = 1 AND icon_contentUri <> ''")
    fun listDeletedIconUris(): List<String>

    @Query("UPDATE notification SET icon_contentUri = null WHERE icon_contentUri = :uri")
    fun clearIconUri(uri: String)

    @Insert(onConflict = OnConflictStrategy.IGNORE)
    fun add(notification: Notification)
+10 −2
Original line number Diff line number Diff line
@@ -92,8 +92,16 @@ class Repository(private val sharedPrefs: SharedPreferences, private val databas
        return notificationDao.listDeletedWithAttachments()
    }

    fun getDeletedNotificationsWithIcons(): List<Notification> {
        return notificationDao.listDeletedWithIcons()
    fun getActiveIconUris(): Set<String> {
        return notificationDao.listActiveIconUris().toSet()
    }

    fun getDeletedIconUris(): Set<String> {
        return notificationDao.listDeletedIconUris().toSet()
    }

    fun clearIconUri(uri: String) {
        notificationDao.clearIconUri(uri)
    }

    fun getNotificationsLiveData(subscriptionId: Long): LiveData<List<Notification>> {
+5 −3
Original line number Diff line number Diff line
@@ -14,11 +14,12 @@ import io.heckel.ntfy.R
import io.heckel.ntfy.app.Application
import io.heckel.ntfy.db.*
import io.heckel.ntfy.util.Log
import io.heckel.ntfy.util.stringToHash
import io.heckel.ntfy.util.sha256
import okhttp3.OkHttpClient
import okhttp3.Request
import okhttp3.Response
import java.io.File
import java.util.Date
import java.util.concurrent.TimeUnit

class DownloadIconWorker(private val context: Context, params: WorkerParameters) : Worker(context, params) {
@@ -45,7 +46,8 @@ class DownloadIconWorker(private val context: Context, params: WorkerParameters)
        icon = notification.icon ?: return Result.failure()
        try {
            val iconFile = createIconFile(icon)
            if (!iconFile.exists()) {
            val yesterdayTimestamp = Date().time - 1000*60*60*24 // now Unix timestamp - 24 hours
            if (!iconFile.exists() || iconFile.lastModified() < yesterdayTimestamp) {
                downloadIcon(iconFile)
            } else {
                Log.d(TAG, "Loading icon from cache: ${icon.url}")
@@ -152,7 +154,7 @@ class DownloadIconWorker(private val context: Context, params: WorkerParameters)
        if (!iconDir.exists() && !iconDir.mkdirs()) {
            throw Exception("Cannot create cache directory for icons: $iconDir")
        }
        val hash = stringToHash(icon.url)
        val hash = icon.url.sha256()
        return File(iconDir, hash)
    }

+1 −5
Original line number Diff line number Diff line
@@ -50,11 +50,7 @@ class NotificationParser {
                )
            }
        } else null
        val icon: Icon? = if (message.icon != null) {
            Icon(
                url = message.icon
            )
        } else null
        val icon: Icon? = if (message.icon != null) Icon(url = message.icon) else null
        val notification = Notification(
            id = message.id,
            subscriptionId = subscriptionId,
+2 −3
Original line number Diff line number Diff line
@@ -471,9 +471,8 @@ fun copyToClipboard(context: Context, notification: Notification) {
        .show()
}

fun stringToHash(s: String): String {
    val bytes = s.toByteArray();
fun String.sha256(): String {
    val md = MessageDigest.getInstance("SHA-256")
    val digest = md.digest(bytes)
    val digest = md.digest(this.toByteArray())
    return digest.fold("") { str, it -> str + "%02x".format(it) }
}
 No newline at end of file
Loading