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.
This commit is contained in:
Paweł Orzech 2026-03-19 15:33:42 +01:00
parent 05f5518bdb
commit 11f4e2f8f6
No known key found for this signature in database
2 changed files with 15 additions and 20 deletions

View file

@ -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 ---

View file

@ -114,26 +114,19 @@ 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) {
}
/** Extract avatar from post authors and save to account if missing */
private fun tryUpdateAvatarFromPosts(posts: List<GhostPost>) {
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()
}
} catch (_: Exception) { /* best-effort */ }
}
}
}
private fun observeLocalPosts() {
localPostsJob?.cancel()
@ -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) }
},