mirror of
https://github.com/pawelorzech/Swoosh.git
synced 2026-03-31 20:15:41 +00:00
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:
parent
05f5518bdb
commit
11f4e2f8f6
2 changed files with 15 additions and 20 deletions
|
|
@ -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 ---
|
||||
|
|
|
|||
|
|
@ -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<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()
|
||||
}
|
||||
|
||||
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) }
|
||||
},
|
||||
|
|
|
|||
Loading…
Reference in a new issue