feat: Handle public instances (#126)
Co-authored-by: davidoskky <davidoskky@hidden.hidden> Co-committed-by: davidoskky <davidoskky@hidden.hidden>
This commit is contained in:
@ -35,17 +35,32 @@ class SelfossModel {
|
||||
)
|
||||
|
||||
@Serializable
|
||||
data class ApiVersion(
|
||||
data class ApiInformation(
|
||||
val version: String?,
|
||||
val apiversion: String?
|
||||
val apiversion: String?,
|
||||
val configuration: ApiConfiguration?
|
||||
) {
|
||||
fun getApiMajorVersion() : Int {
|
||||
fun getApiMajorVersion(): Int {
|
||||
var versionNumber = 0
|
||||
if (apiversion != null) {
|
||||
versionNumber = apiversion.substringBefore(".").toInt()
|
||||
}
|
||||
return versionNumber
|
||||
}
|
||||
|
||||
fun getApiConfiguration() = configuration ?: ApiConfiguration(null, null)
|
||||
}
|
||||
|
||||
@Serializable
|
||||
data class ApiConfiguration(
|
||||
@Serializable(with = BooleanSerializer::class)
|
||||
val publicMode: Boolean?,
|
||||
@Serializable(with = BooleanSerializer::class)
|
||||
val authEnabled: Boolean?
|
||||
) {
|
||||
fun isAuthEnabled() = authEnabled ?: true
|
||||
|
||||
fun isPublicModeEnabled() = publicMode ?: false
|
||||
}
|
||||
|
||||
@Serializable
|
||||
|
@ -439,13 +439,23 @@ class Repository(
|
||||
api.refreshLoginInformation()
|
||||
}
|
||||
|
||||
suspend fun updateApiVersion() {
|
||||
suspend fun updateApiInformation() {
|
||||
val apiMajorVersion = appSettingsService.getApiVersion()
|
||||
|
||||
if (isNetworkAvailable()) {
|
||||
val fetchedVersion = api.version()
|
||||
if (fetchedVersion.success && fetchedVersion.data != null && fetchedVersion.data.getApiMajorVersion() != apiMajorVersion) {
|
||||
appSettingsService.updateApiVersion(fetchedVersion.data.getApiMajorVersion())
|
||||
val fetchedInformation = api.apiInformation()
|
||||
if (fetchedInformation.success && fetchedInformation.data != null) {
|
||||
if (fetchedInformation.data.getApiMajorVersion() != apiMajorVersion) {
|
||||
appSettingsService.updateApiVersion(fetchedInformation.data.getApiMajorVersion())
|
||||
}
|
||||
// Check if we're accessing the instance in public mode
|
||||
// This happens when auth and public mode are enabled but
|
||||
// no credentials are provided to login
|
||||
if (appSettingsService.getUserName().isEmpty()
|
||||
&& fetchedInformation.data.getApiConfiguration().isAuthEnabled()
|
||||
&& fetchedInformation.data.getApiConfiguration().isPublicModeEnabled()) {
|
||||
appSettingsService.updatePublicAccess(true)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -191,7 +191,7 @@ class SelfossApi(private val appSettingsService: AppSettingsService) {
|
||||
}
|
||||
})
|
||||
|
||||
suspend fun version(): StatusAndData<SelfossModel.ApiVersion> =
|
||||
suspend fun apiInformation(): StatusAndData<SelfossModel.ApiInformation> =
|
||||
bodyOrFailure(client.tryToGet(url("/api/about")))
|
||||
|
||||
suspend fun markAsRead(id: String): SuccessResponse =
|
||||
|
@ -7,6 +7,7 @@ class AppSettingsService {
|
||||
|
||||
// Api related
|
||||
private var _apiVersion: Int = -1
|
||||
private var _publicAccess: Boolean? = null
|
||||
private var _baseUrl: String = ""
|
||||
private var _userName: String = ""
|
||||
private var _password: String = ""
|
||||
@ -48,10 +49,32 @@ class AppSettingsService {
|
||||
return _apiVersion
|
||||
}
|
||||
|
||||
|
||||
fun updateApiVersion(apiMajorVersion: Int) {
|
||||
settings.putInt(API_VERSION_MAJOR, apiMajorVersion)
|
||||
refreshApiVersion()
|
||||
}
|
||||
|
||||
private fun refreshApiVersion() {
|
||||
_apiVersion = settings.getInt(API_VERSION_MAJOR, -1)
|
||||
}
|
||||
|
||||
fun getPublicAccess(): Boolean {
|
||||
if (_publicAccess == null) {
|
||||
refreshPublicAccess()
|
||||
}
|
||||
return _publicAccess!!
|
||||
}
|
||||
|
||||
fun updatePublicAccess(publicAccess: Boolean) {
|
||||
settings.putBoolean(API_PUBLIC_ACCESS, publicAccess)
|
||||
refreshPublicAccess()
|
||||
}
|
||||
|
||||
private fun refreshPublicAccess() {
|
||||
_publicAccess = settings.getBoolean(API_PUBLIC_ACCESS, false)
|
||||
}
|
||||
|
||||
fun getBaseUrl(): String {
|
||||
if (_baseUrl.isEmpty()) {
|
||||
refreshBaseUrl()
|
||||
@ -333,6 +356,7 @@ class AppSettingsService {
|
||||
refreshUsername()
|
||||
refreshBaseUrl()
|
||||
refreshApiVersion()
|
||||
refreshPublicAccess()
|
||||
}
|
||||
|
||||
fun refreshUserSettings() {
|
||||
@ -376,11 +400,6 @@ class AppSettingsService {
|
||||
refreshApiSettings()
|
||||
}
|
||||
|
||||
fun updateApiVersion(apiMajorVersion: Int) {
|
||||
settings.putInt(API_VERSION_MAJOR, apiMajorVersion)
|
||||
refreshApiVersion()
|
||||
}
|
||||
|
||||
fun clearAll() {
|
||||
settings.clear()
|
||||
refreshApiSettings()
|
||||
@ -409,6 +428,8 @@ class AppSettingsService {
|
||||
|
||||
const val API_VERSION_MAJOR = "apiVersionMajor"
|
||||
|
||||
const val API_PUBLIC_ACCESS = "apiPublicAccess"
|
||||
|
||||
const val API_ITEMS_NUMBER = "prefer_api_items_number"
|
||||
|
||||
const val API_TIMEOUT = "api_timeout"
|
||||
|
Reference in New Issue
Block a user