Make DeleteWorker more resilient

This commit is contained in:
Philipp Heckel 2022-11-19 15:03:37 -05:00
parent 69fdd30a26
commit 4bfc72c1c9
2 changed files with 22 additions and 14 deletions

View file

@ -9,14 +9,12 @@ import io.heckel.ntfy.BuildConfig
import io.heckel.ntfy.db.ATTACHMENT_PROGRESS_DELETED
import io.heckel.ntfy.db.Repository
import io.heckel.ntfy.msg.DownloadIconWorker
import io.heckel.ntfy.ui.DetailAdapter
import io.heckel.ntfy.util.Log
import io.heckel.ntfy.util.fileStat
import io.heckel.ntfy.util.maybeFileStat
import io.heckel.ntfy.util.topicShortUrl
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import java.io.File
import java.util.*
/**
* Deletes notifications marked for deletion and attachments for deleted notifications.
@ -32,9 +30,23 @@ class DeleteWorker(ctx: Context, params: WorkerParameters) : CoroutineWorker(ctx
override suspend fun doWork(): Result {
return withContext(Dispatchers.IO) {
deleteExpiredIcons() // Before notifications, so we will also catch manually deleted notifications
deleteExpiredAttachments() // Before notifications, so we will also catch manually deleted notifications
// Run "expired icons" and "expired attachments" before notifications,
// so we will also catch manually deleted notifications
try {
deleteExpiredIcons()
} catch (e: Exception) {
Log.w(TAG, "Failed to delete expired icons", e)
}
try {
deleteExpiredAttachments()
} catch (e: Exception) {
Log.w(TAG, "Failed to delete expired attachments", e)
}
try {
deleteExpiredNotifications()
} catch (e: Exception) {
Log.w(TAG, "Failed to delete expired notifications", e)
}
return@withContext Result.success()
}
}
@ -69,14 +81,9 @@ class DeleteWorker(ctx: Context, params: WorkerParameters) : CoroutineWorker(ctx
Log.d(TAG, "Deleting icons for deleted notifications")
val repository = Repository.getInstance(applicationContext)
val activeIconUris = repository.getActiveIconUris()
val activeIconFilenames = activeIconUris.mapNotNull {
try {
fileStat(applicationContext, Uri.parse(it)).filename
} catch (e: Exception) {
Log.w(TAG, "Unable to stat file $it", e)
null
}
}.toSet()
val activeIconFilenames = activeIconUris
.mapNotNull { maybeFileStat(applicationContext, it)?.filename }
.toSet()
val iconDir = File(applicationContext.cacheDir, DownloadIconWorker.ICON_CACHE_DIR)
val allIconFilenames = iconDir.listFiles()?.map{ file -> file.name }.orEmpty()
val filenamesToDelete = allIconFilenames.minus(activeIconFilenames)

View file

@ -2,6 +2,7 @@ Bug fixes + maintenance:
* Upgrade Android dependencies to SDK 33 (no ticket)
* Android 13: Ask for permission to post notifications (#508)
* Remove timestamp when copying message text (#471, thanks to @wunter8)
* Fix auto-delete if some icons do not exist anymore (#506)
Additional translations:
* Korean (thanks to @YJSofta0f97461d82447ac)