From 11f4e2f8f6865778ea563e61ddaa9fcaf8cf64da Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Orzech?= Date: Thu, 19 Mar 2026 15:33:42 +0100 Subject: [PATCH] fix: extract avatar from post authors instead of /users/me/ (404) Ghost Admin API /users/me/ returns 404. Instead, extract profile_image from the first post's authors array which is already fetched on every refresh. --- .../microblog/data/model/GhostModels.kt | 3 +- .../swoosh/microblog/ui/feed/FeedViewModel.kt | 32 ++++++++----------- 2 files changed, 15 insertions(+), 20 deletions(-) diff --git a/app/src/main/java/com/swoosh/microblog/data/model/GhostModels.kt b/app/src/main/java/com/swoosh/microblog/data/model/GhostModels.kt index 263e1c8..243e47c 100644 --- a/app/src/main/java/com/swoosh/microblog/data/model/GhostModels.kt +++ b/app/src/main/java/com/swoosh/microblog/data/model/GhostModels.kt @@ -59,7 +59,8 @@ data class GhostTag( data class Author( val id: String, - val name: String? + val name: String?, + val profile_image: String? = null ) // --- Local Database Entity --- diff --git a/app/src/main/java/com/swoosh/microblog/ui/feed/FeedViewModel.kt b/app/src/main/java/com/swoosh/microblog/ui/feed/FeedViewModel.kt index 49c1691..92d3e63 100644 --- a/app/src/main/java/com/swoosh/microblog/ui/feed/FeedViewModel.kt +++ b/app/src/main/java/com/swoosh/microblog/ui/feed/FeedViewModel.kt @@ -114,25 +114,18 @@ class FeedViewModel(application: Application) : AndroidViewModel(application) { fun refreshAccountsList() { _accounts.value = accountManager.getAccounts() _activeAccount.value = accountManager.getActiveAccount() - // Fetch avatar if missing for active account - val active = _activeAccount.value - if (active != null && active.avatarUrl == null) { - viewModelScope.launch { - try { - val service = ApiClient.getService( - active.blogUrl, - apiKeyProvider = { active.apiKey } - ) - val response = service.getCurrentUser() - val avatarUrl = response.body()?.users?.firstOrNull()?.profile_image - if (avatarUrl != null) { - accountManager.updateAccount(id = active.id, avatarUrl = avatarUrl) - _activeAccount.value = accountManager.getActiveAccount() - _accounts.value = accountManager.getAccounts() - } - } catch (_: Exception) { /* best-effort */ } - } - } + } + + /** Extract avatar from post authors and save to account if missing */ + private fun tryUpdateAvatarFromPosts(posts: List) { + val active = _activeAccount.value ?: return + if (active.avatarUrl != null) return + val avatarUrl = posts.firstNotNullOfOrNull { post -> + post.authors?.firstOrNull()?.profile_image + } ?: return + accountManager.updateAccount(id = active.id, avatarUrl = avatarUrl) + _activeAccount.value = accountManager.getActiveAccount() + _accounts.value = accountManager.getAccounts() } private fun observeLocalPosts() { @@ -307,6 +300,7 @@ class FeedViewModel(application: Application) : AndroidViewModel(application) { onSuccess = { response -> remotePosts = response.posts.map { it.toFeedPost() } hasMorePages = response.meta?.pagination?.next != null + tryUpdateAvatarFromPosts(response.posts) mergePosts() _uiState.update { it.copy(isRefreshing = false) } },