From 9e140aefa6fb35190424f4489b63b529037b6e79 Mon Sep 17 00:00:00 2001 From: davidoskky Date: Sun, 13 Apr 2025 19:53:01 +0200 Subject: [PATCH] Add filters only with selfoss >= 2.19 Source filters have been implemented in 2014 (commit 849957ca) but there is no reliable way to determine whether the selfoss instance does support source filters. --- .../readerforselfossv2/rest/SelfossApi.kt | 95 +++++++++++++++++-- 1 file changed, 87 insertions(+), 8 deletions(-) diff --git a/shared/src/commonMain/kotlin/bou/amine/apps/readerforselfossv2/rest/SelfossApi.kt b/shared/src/commonMain/kotlin/bou/amine/apps/readerforselfossv2/rest/SelfossApi.kt index 6eafbba..90a29c8 100644 --- a/shared/src/commonMain/kotlin/bou/amine/apps/readerforselfossv2/rest/SelfossApi.kt +++ b/shared/src/commonMain/kotlin/bou/amine/apps/readerforselfossv2/rest/SelfossApi.kt @@ -642,14 +642,13 @@ class SelfossApi( ): SuccessResponse = maybeResponse( if (appSettingsService.getApiVersion() > 1) { - createSource("tags[]", title, url, spout, tags, filter) + createSource(title, url, spout, tags, filter) } else { - createSource("tags", title, url, spout, tags, filter) + createSourceOld(title, url, spout, tags) }, ) private suspend fun createSource( - tagsParamName: String, title: String, url: String, spout: String, @@ -668,7 +667,47 @@ class SelfossApi( append("url", url) append("spout", spout) append("filter", filter) - append(tagsParamName, tags) + append("tags[]", tags) + }, + block = { + if (appSettingsService + .getBasicUserName() + .isNotEmpty() && + appSettingsService.getBasicPassword().isNotEmpty() + ) { + headers { + append( + HttpHeaders.Authorization, + constructBasicAuthValue( + BasicAuthCredentials( + username = appSettingsService.getBasicUserName(), + password = appSettingsService.getBasicPassword(), + ), + ), + ) + } + } + }, + ) + + private suspend fun createSourceOld( + title: String, + url: String, + spout: String, + tags: String, + ): HttpResponse? = + client.tryToSubmitForm( + url = url("/source"), + formParameters = + Parameters.build { + if (!shouldHavePostLogin()) { + append("username", appSettingsService.getUserName()) + append("password", appSettingsService.getPassword()) + } + append("title", title) + append("url", url) + append("spout", spout) + append("tags", tags) }, block = { if (appSettingsService @@ -701,15 +740,14 @@ class SelfossApi( ): SuccessResponse = maybeResponse( if (appSettingsService.getApiVersion() > 1) { - updateSource(id, "tags[]", title, url, spout, tags, filter) + updateSource(id, title, url, spout, tags, filter) } else { - updateSource(id, "tags", title, url, spout, tags, filter) + updateSourceOld(id, title, url, spout, tags) }, ) private suspend fun updateSource( id: Int, - tagsParamName: String, title: String, url: String, spout: String, @@ -727,7 +765,7 @@ class SelfossApi( append("title", title) append("url", url) append("spout", spout) - append(tagsParamName, tags) + append("tags[]", tags) append("filter", filter) }, block = { @@ -751,6 +789,47 @@ class SelfossApi( }, ) + private suspend fun updateSourceOld( + id: Int, + title: String, + url: String, + spout: String, + tags: String, + ): HttpResponse? = + client.tryToSubmitForm( + url = url("/source/$id"), + formParameters = + Parameters.build { + if (!shouldHavePostLogin()) { + append("username", appSettingsService.getUserName()) + append("password", appSettingsService.getPassword()) + } + append("title", title) + append("url", url) + append("spout", spout) + append("tags", tags) + }, + block = { + if (appSettingsService + .getBasicUserName() + .isNotEmpty() && + appSettingsService.getBasicPassword().isNotEmpty() + ) { + headers { + append( + HttpHeaders.Authorization, + constructBasicAuthValue( + BasicAuthCredentials( + username = appSettingsService.getBasicUserName(), + password = appSettingsService.getBasicPassword(), + ), + ), + ) + } + } + }, + ) + suspend fun deleteSource(id: Int): SuccessResponse = maybeResponse( client.tryToDelete(url("/source/$id")) {