Alert user if API is not enabled. Store some settings in db

This commit is contained in:
Matthieu 2021-02-05 14:48:12 +01:00
parent 9bf62832d3
commit cf3021c26a
8 changed files with 56 additions and 32 deletions

View File

@ -178,11 +178,10 @@ class LoginActivity : BaseActivity() {
return@coroutineScope failedRegistration(getString(R.string.instance_error))
}
//TODO here check for api being activated, if not show dialog
val domain: String = try {
if (nodeInfo.hasInstanceEndpointInfo()) {
storeInstance(db, nodeInfo)
nodeInfo.metadata?.config?.site?.url!!
nodeInfo.metadata?.config?.site?.url
} else {
val instance: Instance = try {
pixelfedAPI.instance()
@ -192,18 +191,16 @@ class LoginActivity : BaseActivity() {
return@coroutineScope failedRegistration(getString(R.string.instance_error))
}
storeInstance(db, nodeInfo = null, instance = instance)
instance.uri ?: return@coroutineScope failedRegistration(getString(R.string.instance_error))
instance.uri
}
} catch (e: IllegalArgumentException){
return@coroutineScope failedRegistration(getString(R.string.instance_error))
}
} catch (e: IllegalArgumentException){ null }
?: return@coroutineScope failedRegistration(getString(R.string.instance_error))
preferences.edit().putString("domain", domain).apply()
preferences.edit().putString("domain", normalizeDomain(domain)).apply()
if (!nodeInfo.software?.name.orEmpty().contains("pixelfed")) {
val builder = AlertDialog.Builder(this@LoginActivity)
builder.apply {
AlertDialog.Builder(this@LoginActivity).apply {
setMessage(R.string.instance_not_pixelfed_warning)
setPositiveButton(R.string.instance_not_pixelfed_continue) { _, _ ->
promptOAuth(normalizedDomain, clientId)
@ -212,13 +209,18 @@ class LoginActivity : BaseActivity() {
loadingAnimation(false)
wipeSharedSettings()
}
}
// Create the AlertDialog
builder.show()
}.show()
} else if (nodeInfo.metadata?.config?.features?.mobile_apis != true) {
AlertDialog.Builder(this@LoginActivity).apply {
setMessage(R.string.api_not_enabled_dialog)
setNegativeButton(android.R.string.ok) { _, _ ->
loadingAnimation(false)
wipeSharedSettings()
}
}.show()
} else {
promptOAuth(normalizedDomain, clientId)
}
}

View File

@ -61,15 +61,16 @@ class MainActivity : BaseActivity() {
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
TraceDroidEmailSender.sendStackTraces("contact@pixeldroid.org", this)
//get the currently active user
user = db.userDao().getActiveUser()
//Check if we have logged in and gotten an access token
if (user == null) {
launchActivity(LoginActivity(), firstTime = true)
finish()
} else {
TraceDroidEmailSender.sendStackTraces("contact@pixeldroid.org", this)
setupDrawer()
val tabs: List<() -> Fragment> = listOf(

View File

@ -32,6 +32,7 @@ import com.h.pixeldroid.utils.BaseActivity
import com.h.pixeldroid.utils.api.PixelfedAPI
import com.h.pixeldroid.utils.api.objects.Attachment
import com.h.pixeldroid.utils.api.objects.Instance
import com.h.pixeldroid.utils.db.entities.InstanceDatabaseEntity.Companion.DEFAULT_MAX_TOOT_CHARS
import com.h.pixeldroid.utils.db.entities.UserDatabaseEntity
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.disposables.Disposable
@ -93,7 +94,7 @@ class PostCreationActivity : BaseActivity() {
}
thisInstances.first().maxStatusChars
} else {
Instance.DEFAULT_MAX_TOOT_CHARS
DEFAULT_MAX_TOOT_CHARS
}
accessToken = user?.accessToken.orEmpty()

View File

@ -1,5 +1,7 @@
package com.h.pixeldroid.utils.api.objects
import com.h.pixeldroid.utils.db.entities.InstanceDatabaseEntity.Companion.DEFAULT_MAX_TOOT_CHARS
data class Instance (
val description: String?,
val email: String?,
@ -9,10 +11,4 @@ data class Instance (
val title: String?,
val uri: String?,
val version: String?
) {
companion object {
// Default max number of chars for Mastodon: used when their is no other value supplied by
// either NodeInfo or the instance endpoint
const val DEFAULT_MAX_TOOT_CHARS = 500
}
}
)

View File

@ -20,7 +20,7 @@ import com.h.pixeldroid.utils.api.objects.Notification
PublicFeedStatusDatabaseEntity::class,
Notification::class
],
version = 2
version = 3
)
@TypeConverters(Converters::class)
abstract class AppDatabase : RoomDatabase() {

View File

@ -5,6 +5,10 @@ import com.h.pixeldroid.utils.db.entities.UserDatabaseEntity
import com.h.pixeldroid.utils.api.objects.Account
import com.h.pixeldroid.utils.api.objects.Instance
import com.h.pixeldroid.utils.api.objects.NodeInfo
import com.h.pixeldroid.utils.db.entities.InstanceDatabaseEntity.Companion.DEFAULT_ALBUM_LIMIT
import com.h.pixeldroid.utils.db.entities.InstanceDatabaseEntity.Companion.DEFAULT_MAX_PHOTO_SIZE
import com.h.pixeldroid.utils.db.entities.InstanceDatabaseEntity.Companion.DEFAULT_MAX_TOOT_CHARS
import com.h.pixeldroid.utils.db.entities.InstanceDatabaseEntity.Companion.DEFAULT_MAX_VIDEO_SIZE
import com.h.pixeldroid.utils.normalizeDomain
import java.lang.IllegalArgumentException
@ -29,15 +33,19 @@ fun addUser(db: AppDatabase, account: Account, instance_uri: String, activeUser:
fun storeInstance(db: AppDatabase, nodeInfo: NodeInfo?, instance: Instance? = null) {
val dbInstance: InstanceDatabaseEntity = nodeInfo?.run {
InstanceDatabaseEntity(
uri = normalizeDomain(metadata?.config?.site?.url!!),
title = metadata.config.site.name!!,
maxStatusChars = metadata.config.uploader?.max_caption_length!!.toInt(),
uri = normalizeDomain(metadata?.config?.site?.url!!),
title = metadata.config.site.name!!,
maxStatusChars = metadata.config.uploader?.max_caption_length!!.toInt(),
maxPhotoSize = metadata.config.uploader.max_photo_size?.toIntOrNull() ?: DEFAULT_MAX_PHOTO_SIZE,
//Pixelfed doesn't distinguish between max photo and video size
maxVideoSize = metadata.config.uploader.max_photo_size?.toIntOrNull() ?: DEFAULT_MAX_VIDEO_SIZE,
albumLimit = metadata.config.uploader.album_limit?.toIntOrNull() ?: DEFAULT_ALBUM_LIMIT
)
} ?: instance?.run {
InstanceDatabaseEntity(
uri = normalizeDomain(uri.orEmpty()),
title = title.orEmpty(),
maxStatusChars = max_toot_chars?.toInt() ?: Instance.DEFAULT_MAX_TOOT_CHARS,
uri = normalizeDomain(uri.orEmpty()),
title = title.orEmpty(),
maxStatusChars = max_toot_chars?.toInt() ?: DEFAULT_MAX_TOOT_CHARS,
)
} ?: throw IllegalArgumentException("Cannot store instance where both are null")

View File

@ -8,6 +8,21 @@ import com.h.pixeldroid.utils.api.objects.Instance
data class InstanceDatabaseEntity (
@PrimaryKey var uri: String,
var title: String,
var maxStatusChars: Int = Instance.DEFAULT_MAX_TOOT_CHARS,
var maxStatusChars: Int = DEFAULT_MAX_TOOT_CHARS,
// Per-file file-size limit in KB. Defaults to 15000 (15MB). Default limit for Mastodon is 8MB
var maxPhotoSize: Int = DEFAULT_MAX_PHOTO_SIZE,
// Mastodon has different file limits for videos, default of 40MB
var maxVideoSize: Int = DEFAULT_MAX_VIDEO_SIZE,
// How many photos can go into an album. Default limit for Pixelfed and Mastodon is 4
var albumLimit: Int = DEFAULT_ALBUM_LIMIT,
) {
companion object{
// Default max number of chars for Mastodon: used when their is no other value supplied by
// either NodeInfo or the instance endpoint
const val DEFAULT_MAX_TOOT_CHARS = 500
)
const val DEFAULT_MAX_PHOTO_SIZE = 8000
const val DEFAULT_MAX_VIDEO_SIZE = 40000
const val DEFAULT_ALBUM_LIMIT = 4
}
}

View File

@ -195,5 +195,6 @@ Following"</item>
<string name="mascot_description">Image showing a red panda, Pixelfed\'s mascot, using a phone</string>
<string name="save_before_returning">Save your edits?</string>
<string name="no_cancel_edit">No, cancel edit</string>
<string name="api_not_enabled_dialog">The API is not activated on this instance. Contact your administrator to ask them to activate it.</string>
</resources>