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(
|
data class Author(
|
||||||
val id: String,
|
val id: String,
|
||||||
val name: String?
|
val name: String?,
|
||||||
|
val profile_image: String? = null
|
||||||
)
|
)
|
||||||
|
|
||||||
// --- Local Database Entity ---
|
// --- Local Database Entity ---
|
||||||
|
|
|
||||||
|
|
@ -114,25 +114,18 @@ class FeedViewModel(application: Application) : AndroidViewModel(application) {
|
||||||
fun refreshAccountsList() {
|
fun refreshAccountsList() {
|
||||||
_accounts.value = accountManager.getAccounts()
|
_accounts.value = accountManager.getAccounts()
|
||||||
_activeAccount.value = accountManager.getActiveAccount()
|
_activeAccount.value = accountManager.getActiveAccount()
|
||||||
// Fetch avatar if missing for active account
|
}
|
||||||
val active = _activeAccount.value
|
|
||||||
if (active != null && active.avatarUrl == null) {
|
/** Extract avatar from post authors and save to account if missing */
|
||||||
viewModelScope.launch {
|
private fun tryUpdateAvatarFromPosts(posts: List<GhostPost>) {
|
||||||
try {
|
val active = _activeAccount.value ?: return
|
||||||
val service = ApiClient.getService(
|
if (active.avatarUrl != null) return
|
||||||
active.blogUrl,
|
val avatarUrl = posts.firstNotNullOfOrNull { post ->
|
||||||
apiKeyProvider = { active.apiKey }
|
post.authors?.firstOrNull()?.profile_image
|
||||||
)
|
} ?: return
|
||||||
val response = service.getCurrentUser()
|
accountManager.updateAccount(id = active.id, avatarUrl = avatarUrl)
|
||||||
val avatarUrl = response.body()?.users?.firstOrNull()?.profile_image
|
_activeAccount.value = accountManager.getActiveAccount()
|
||||||
if (avatarUrl != null) {
|
_accounts.value = accountManager.getAccounts()
|
||||||
accountManager.updateAccount(id = active.id, avatarUrl = avatarUrl)
|
|
||||||
_activeAccount.value = accountManager.getActiveAccount()
|
|
||||||
_accounts.value = accountManager.getAccounts()
|
|
||||||
}
|
|
||||||
} catch (_: Exception) { /* best-effort */ }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun observeLocalPosts() {
|
private fun observeLocalPosts() {
|
||||||
|
|
@ -307,6 +300,7 @@ class FeedViewModel(application: Application) : AndroidViewModel(application) {
|
||||||
onSuccess = { response ->
|
onSuccess = { response ->
|
||||||
remotePosts = response.posts.map { it.toFeedPost() }
|
remotePosts = response.posts.map { it.toFeedPost() }
|
||||||
hasMorePages = response.meta?.pagination?.next != null
|
hasMorePages = response.meta?.pagination?.next != null
|
||||||
|
tryUpdateAvatarFromPosts(response.posts)
|
||||||
mergePosts()
|
mergePosts()
|
||||||
_uiState.update { it.copy(isRefreshing = false) }
|
_uiState.update { it.copy(isRefreshing = false) }
|
||||||
},
|
},
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue