43 lines
1.3 KiB
Kotlin
43 lines
1.3 KiB
Kotlin
|
|
/*
|
||
|
|
* Created by sweetbread
|
||
|
|
* Copyright (c) 2025. All rights reserved.
|
||
|
|
*/
|
||
|
|
|
||
|
|
package ru.risdeveau.pixeldragon.repo
|
||
|
|
|
||
|
|
import android.util.Log
|
||
|
|
import org.json.JSONObject
|
||
|
|
import ru.risdeveau.pixeldragon.api.getUserProfile
|
||
|
|
import ru.risdeveau.pixeldragon.db.cacheDb
|
||
|
|
import ru.risdeveau.pixeldragon.db.isExpired
|
||
|
|
import ru.risdeveau.pixeldragon.db.toDomain
|
||
|
|
import ru.risdeveau.pixeldragon.db.toEntity
|
||
|
|
|
||
|
|
class User (
|
||
|
|
val id: String,
|
||
|
|
val name: String?,
|
||
|
|
val avatarUrl: String?,
|
||
|
|
val attrs: JSONObject
|
||
|
|
) {
|
||
|
|
companion object {
|
||
|
|
suspend fun getById(id: String): User? {
|
||
|
|
val cachedUser = cacheDb.userDoa().getById(id)
|
||
|
|
if ((cachedUser == null) or (cachedUser?.isExpired() == true)) {
|
||
|
|
val userProfile = getUserProfile(id)
|
||
|
|
if (userProfile == null) {
|
||
|
|
Log.i("User.getById", "User $id not found")
|
||
|
|
return null
|
||
|
|
}
|
||
|
|
val user = User(
|
||
|
|
id,
|
||
|
|
userProfile.displayName,
|
||
|
|
userProfile.avatarUrl,
|
||
|
|
userProfile.other
|
||
|
|
)
|
||
|
|
cacheDb.userDoa().insert(user.toEntity())
|
||
|
|
return user
|
||
|
|
}
|
||
|
|
return cachedUser!!.toDomain()
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|