From fc7f5f2cf94bc697e536bc28841f0c1a995da759 Mon Sep 17 00:00:00 2001 From: Koitharu Date: Sat, 28 Jun 2025 07:02:51 +0300 Subject: [PATCH] Fix captcha notification dismissing --- .../core/exceptions/resolve/CaptchaHandler.kt | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/app/src/main/kotlin/org/koitharu/kotatsu/core/exceptions/resolve/CaptchaHandler.kt b/app/src/main/kotlin/org/koitharu/kotatsu/core/exceptions/resolve/CaptchaHandler.kt index 7c37cc9e3..d263da65e 100644 --- a/app/src/main/kotlin/org/koitharu/kotatsu/core/exceptions/resolve/CaptchaHandler.kt +++ b/app/src/main/kotlin/org/koitharu/kotatsu/core/exceptions/resolve/CaptchaHandler.kt @@ -77,7 +77,7 @@ class CaptchaHandler @Inject constructor( override fun onReceive(context: Context?, intent: Intent?) { val sourceName = intent?.getStringExtra(AppRouter.KEY_SOURCE) ?: return goAsync { - discard(MangaSource(sourceName)) + handleException(MangaSource(sourceName), exception = null, notify = false) } } }, @@ -86,10 +86,10 @@ class CaptchaHandler @Inject constructor( ) } - suspend fun handle(exception: CloudFlareException): Boolean = handleException(exception.source, exception) + suspend fun handle(exception: CloudFlareException): Boolean = handleException(exception.source, exception, true) suspend fun discard(source: MangaSource) { - handleException(source, null) + handleException(source, null, true) } override fun onError(request: ImageRequest, result: ErrorResult) { @@ -98,23 +98,25 @@ class CaptchaHandler @Inject constructor( if (e is CloudFlareException && request.extras[ignoreCaptchaKey] != true) { val scope = request.lifecycle?.coroutineScope ?: processLifecycleScope scope.launch { - handleException(e.source, e) + handleException(e.source, e, true) } } } private suspend fun handleException( source: MangaSource, - exception: CloudFlareException? + exception: CloudFlareException?, + notify: Boolean ): Boolean = withContext(Dispatchers.Default) { if (source == UnknownMangaSource) { return@withContext false } mutex.withLock { + var removedException: CloudFlareProtectedException? = null if (exception is CloudFlareProtectedException) { exceptionMap[source] = exception } else { - exceptionMap.remove(source) + removedException = exceptionMap.remove(source) } val dao = databaseProvider.get().getSourcesDao() dao.setCfState(source.name, exception?.state ?: CloudFlareHelper.PROTECTION_NOT_DETECTED) @@ -126,7 +128,10 @@ class CaptchaHandler @Inject constructor( }.mapNotNull { exceptionMap[it] } - if (exceptions.isNotEmpty() && context.checkNotificationPermission(CHANNEL_ID)) { + if (notify && context.checkNotificationPermission(CHANNEL_ID)) { + if (removedException != null) { + NotificationManagerCompat.from(context).cancel(TAG, removedException.source.hashCode()) + } notify(exceptions) } }