improve null safety of instance info (#4226)

according to crash logs there are seem to be some instances that don't
always return the expected json, so lets be extra safe here

```
Exception java.lang.NullPointerException:
  at com.keylesspalace.tusky.components.instanceinfo.InstanceInfoRepository$getInstanceInfo$2.invokeSuspend (InstanceInfoRepository.kt:67)
  at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith (ContinuationImpl.kt:33)
  at kotlinx.coroutines.DispatchedTask.run (DispatchedTask.kt:108)
  at kotlinx.coroutines.internal.LimitedDispatcher$Worker.run (LimitedDispatcher.java:115)
  at kotlinx.coroutines.scheduling.TaskImpl.run (Tasks.kt:103)
  at kotlinx.coroutines.scheduling.CoroutineScheduler.runSafely (CoroutineScheduler.java:584)
  at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.executeTask (CoroutineScheduler.kt:793)
  at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.runWorker (CoroutineScheduler.kt:697)
  at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.run (CoroutineScheduler.kt:684)
```
This commit is contained in:
Konrad Pozniak 2024-01-28 19:08:44 +01:00 committed by GitHub
parent 0c2b8b114b
commit d66866648e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 13 additions and 13 deletions

View file

@ -64,17 +64,17 @@ class InstanceInfoRepository @Inject constructor(
{ instance -> { instance ->
val instanceEntity = InstanceInfoEntity( val instanceEntity = InstanceInfoEntity(
instance = instanceName, instance = instanceName,
maximumTootCharacters = instance.configuration.statuses?.maxCharacters ?: DEFAULT_CHARACTER_LIMIT, maximumTootCharacters = instance.configuration?.statuses?.maxCharacters ?: DEFAULT_CHARACTER_LIMIT,
maxPollOptions = instance.configuration.polls?.maxOptions ?: DEFAULT_MAX_OPTION_COUNT, maxPollOptions = instance.configuration?.polls?.maxOptions ?: DEFAULT_MAX_OPTION_COUNT,
maxPollOptionLength = instance.configuration.polls?.maxCharactersPerOption ?: DEFAULT_MAX_OPTION_LENGTH, maxPollOptionLength = instance.configuration?.polls?.maxCharactersPerOption ?: DEFAULT_MAX_OPTION_LENGTH,
minPollDuration = instance.configuration.polls?.minExpirationSeconds ?: DEFAULT_MIN_POLL_DURATION, minPollDuration = instance.configuration?.polls?.minExpirationSeconds ?: DEFAULT_MIN_POLL_DURATION,
maxPollDuration = instance.configuration.polls?.maxExpirationSeconds ?: DEFAULT_MAX_POLL_DURATION, maxPollDuration = instance.configuration?.polls?.maxExpirationSeconds ?: DEFAULT_MAX_POLL_DURATION,
charactersReservedPerUrl = instance.configuration.statuses?.charactersReservedPerUrl ?: DEFAULT_CHARACTERS_RESERVED_PER_URL, charactersReservedPerUrl = instance.configuration?.statuses?.charactersReservedPerUrl ?: DEFAULT_CHARACTERS_RESERVED_PER_URL,
version = instance.version, version = instance.version,
videoSizeLimit = instance.configuration.mediaAttachments?.videoSizeLimitBytes?.toInt() ?: DEFAULT_VIDEO_SIZE_LIMIT, videoSizeLimit = instance.configuration?.mediaAttachments?.videoSizeLimitBytes?.toInt() ?: DEFAULT_VIDEO_SIZE_LIMIT,
imageSizeLimit = instance.configuration.mediaAttachments?.imageSizeLimitBytes?.toInt() ?: DEFAULT_IMAGE_SIZE_LIMIT, imageSizeLimit = instance.configuration?.mediaAttachments?.imageSizeLimitBytes?.toInt() ?: DEFAULT_IMAGE_SIZE_LIMIT,
imageMatrixLimit = instance.configuration.mediaAttachments?.imagePixelCountLimit?.toInt() ?: DEFAULT_IMAGE_MATRIX_LIMIT, imageMatrixLimit = instance.configuration?.mediaAttachments?.imagePixelCountLimit?.toInt() ?: DEFAULT_IMAGE_MATRIX_LIMIT,
maxMediaAttachments = instance.configuration.statuses?.maxMediaAttachments ?: DEFAULT_MAX_MEDIA_ATTACHMENTS, maxMediaAttachments = instance.configuration?.statuses?.maxMediaAttachments ?: DEFAULT_MAX_MEDIA_ATTACHMENTS,
maxFields = instance.pleroma?.metadata?.fieldLimits?.maxFields, maxFields = instance.pleroma?.metadata?.fieldLimits?.maxFields,
maxFieldNameLength = instance.pleroma?.metadata?.fieldLimits?.nameLength, maxFieldNameLength = instance.pleroma?.metadata?.fieldLimits?.nameLength,
maxFieldValueLength = instance.pleroma?.metadata?.fieldLimits?.valueLength maxFieldValueLength = instance.pleroma?.metadata?.fieldLimits?.valueLength

View file

@ -39,7 +39,7 @@ class LoginWebViewViewModel @Inject constructor(
viewModelScope.launch { viewModelScope.launch {
api.getInstance().fold( api.getInstance().fold(
{ instance -> { instance ->
instanceRules.value = instance.rules.map { rule -> rule.text } instanceRules.value = instance.rules.orEmpty().map { rule -> rule.text }
}, },
{ throwable -> { throwable ->
if (throwable.isHttpNotFound()) { if (throwable.isHttpNotFound()) {

View file

@ -11,10 +11,10 @@ data class Instance(
// val usage: Usage, // val usage: Usage,
// val thumbnail: Thumbnail, // val thumbnail: Thumbnail,
// val languages: List<String>, // val languages: List<String>,
val configuration: Configuration, val configuration: Configuration?,
// val registrations: Registrations, // val registrations: Registrations,
// val contact: Contact, // val contact: Contact,
val rules: List<Rule>, val rules: List<Rule>?,
val pleroma: PleromaConfiguration? val pleroma: PleromaConfiguration?
) { ) {
data class Usage(val users: Users) { data class Usage(val users: Users) {