Explicitly failing for non selfoss rss files.

This commit is contained in:
aminecmi
2022-12-18 20:41:17 +01:00
parent 3b0028164b
commit f42ae97326
21 changed files with 133 additions and 27 deletions

View File

@ -8,13 +8,19 @@ import bou.amine.apps.readerforselfossv2.rest.SelfossApi
import bou.amine.apps.readerforselfossv2.service.AppSettingsService
import bou.amine.apps.readerforselfossv2.utils.*
import io.github.aakira.napier.Napier
import io.ktor.client.call.*
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.launch
class Repository(private val api: SelfossApi, private val appSettingsService: AppSettingsService, val isConnectionAvailable: MutableStateFlow<Boolean>, private val db: ReaderForSelfossDB) {
class Repository(
private val api: SelfossApi,
private val appSettingsService: AppSettingsService,
val isConnectionAvailable: MutableStateFlow<Boolean>,
private val db: ReaderForSelfossDB
) {
var items = ArrayList<SelfossModel.Item>()
var connectionMonitored = false
@ -146,7 +152,8 @@ class Repository(private val api: SelfossApi, private val appSettingsService: Ap
}
suspend fun getTags(): List<SelfossModel.Tag> {
val isDatabaseEnabled = appSettingsService.isItemCachingEnabled() || !appSettingsService.isUpdateSourcesEnabled()
val isDatabaseEnabled =
appSettingsService.isItemCachingEnabled() || !appSettingsService.isUpdateSourcesEnabled()
return if (isNetworkAvailable() && !fetchedTags) {
val apiTags = api.tags()
if (apiTags.success && apiTags.data != null && isDatabaseEnabled) {
@ -178,7 +185,8 @@ class Repository(private val api: SelfossApi, private val appSettingsService: Ap
}
suspend fun getSources(): ArrayList<SelfossModel.Source> {
val isDatabaseEnabled = appSettingsService.isItemCachingEnabled() || !appSettingsService.isUpdateSourcesEnabled()
val isDatabaseEnabled =
appSettingsService.isItemCachingEnabled() || !appSettingsService.isUpdateSourcesEnabled()
return if (isNetworkAvailable() && !fetchedSources) {
val apiSources = api.sources()
if (apiSources.success && apiSources.data != null && isDatabaseEnabled) {
@ -380,18 +388,35 @@ class Repository(private val api: SelfossApi, private val appSettingsService: Ap
return result
}
suspend fun shouldBeSelfossInstance(): Pair<Boolean, Boolean> {
var fetchFailed = true
var showSelfossOnlyModal = false
if (isNetworkAvailable()) {
try {
// Trying to fetch one item, and check someone is trying to use the app with
// a random rss feed, that would throw a NoTransformationFoundException
fetchFailed = !api.getItemsWithoutCatch().success
} catch (e: NoTransformationFoundException) {
showSelfossOnlyModal = true
} catch (e: Throwable) {
Napier.e(e.stackTraceToString(), tag = "RepositoryImpl.shouldBeSelfossInstance")
}
}
return Pair(fetchFailed, showSelfossOnlyModal)
}
suspend fun logout() {
if (isNetworkAvailable()) {
try {
val response = api.logout()
if (response.isSuccess) {
if (!response.isSuccess) {
Napier.e("Couldn't logout.", tag = "RepositoryImpl.logout")
}
} catch (cause: Throwable) {
Napier.e(cause.stackTraceToString(), tag = "RepositoryImpl.logout")
} finally {
appSettingsService.clearAll()
}
appSettingsService.clearAll()
} else {
appSettingsService.clearAll()
}
@ -456,11 +481,29 @@ class Repository(private val api: SelfossApi, private val appSettingsService: Ap
private fun getDBItems(): List<ITEM> = db.itemsQueries.items().executeAsList()
private fun insertDBAction(articleid: String, read: Boolean = false, unread: Boolean = false, starred: Boolean = false, unstarred: Boolean = false) =
private fun insertDBAction(
articleid: String,
read: Boolean = false,
unread: Boolean = false,
starred: Boolean = false,
unstarred: Boolean = false
) =
db.actionsQueries.insertAction(articleid, read, unread, starred, unstarred)
private fun updateDBItem(item: SelfossModel.Item) =
db.itemsQueries.updateItem(item.datetime, item.title.getHtmlDecoded(), item.content, item.unread, item.starred, item.thumbnail, item.icon, item.link, item.sourcetitle, item.tags.joinToString(","), item.id.toString())
db.itemsQueries.updateItem(
item.datetime,
item.title.getHtmlDecoded(),
item.content,
item.unread,
item.starred,
item.thumbnail,
item.icon,
item.link,
item.sourcetitle,
item.tags.joinToString(","),
item.id.toString()
)
suspend fun tryToCacheItemsAndGetNewOnes(): List<SelfossModel.Item> {
try {

View File

@ -47,7 +47,7 @@ class SelfossApi(private val appSettingsService: AppSettingsService) {
}
install(HttpCookies)
install(HttpRequestRetry) {
maxRetries = 2
maxRetries = 1
retryIf { _, response ->
response.status == HttpStatusCode.Forbidden && shouldHavePostLogin() && hasLoginInfo()
}
@ -134,6 +134,16 @@ class SelfossApi(private val appSettingsService: AppSettingsService) {
parameter("offset", offset)
})
suspend fun getItemsWithoutCatch(): StatusAndData<List<SelfossModel.Item>> =
bodyOrFailure(client.get(url("/items")) {
if (!shouldHavePostLogin()) {
parameter("username", appSettingsService.getUserName())
parameter("password", appSettingsService.getPassword())
}
parameter("type", "all")
parameter("items", 1)
})
suspend fun stats(): StatusAndData<SelfossModel.Stats> =
bodyOrFailure(client.tryToGet(url("/stats")) {
if (!shouldHavePostLogin()) {