fix: keep user data up to date

This commit is contained in:
rebelonion 2024-02-29 18:52:27 -06:00
parent 05fc97a933
commit 6e6429db82

View file

@ -21,7 +21,6 @@ import okhttp3.OkHttpClient
import okio.IOException import okio.IOException
import uy.kohesive.injekt.Injekt import uy.kohesive.injekt.Injekt
import uy.kohesive.injekt.api.get import uy.kohesive.injekt.api.get
import java.io.File
object CommentsAPI { object CommentsAPI {
val address: String = "https://1224665.xyz:443" val address: String = "https://1224665.xyz:443"
@ -216,7 +215,12 @@ object CommentsAPI {
return res return res
} }
suspend fun reportComment(commentId: Int, username: String, mediaTitle: String, reportedId: String): Boolean { suspend fun reportComment(
commentId: Int,
username: String,
mediaTitle: String,
reportedId: String
): Boolean {
val url = "$address/report/$commentId" val url = "$address/report/$commentId"
val body = FormBody.Builder() val body = FormBody.Builder()
.add("username", username) .add("username", username)
@ -259,13 +263,38 @@ object CommentsAPI {
return parsed return parsed
} }
private suspend fun getUserDetails(client: OkHttpClient? = null): User? {
val url = "$address/user"
val request = if (client != null) requestBuilder(client) else requestBuilder()
val json = try {
request.get(url)
} catch (e: IOException) {
return null
}
if (json.code == 200) {
val parsed = try {
Json.decodeFromString<UserResponse>(json.text)
} catch (e: Exception) {
e.printStackTrace()
return null
}
isBanned = parsed.user.isBanned ?: false
isAdmin = parsed.user.isAdmin ?: false
isMod = parsed.user.isMod ?: false
totalVotes = parsed.user.totalVotes
return parsed.user
}
return null
}
suspend fun fetchAuthToken(client: OkHttpClient? = null) { suspend fun fetchAuthToken(client: OkHttpClient? = null) {
if (authToken != null) return if (authToken != null) return
val MAX_RETRIES = 5 val MAX_RETRIES = 5
val tokenLifetime: Long = 1000 * 60 * 60 * 24 * 6 // 6 days val tokenLifetime: Long = 1000 * 60 * 60 * 24 * 6 // 6 days
val tokenExpiry = PrefManager.getVal<Long>(PrefName.CommentTokenExpiry) val tokenExpiry = PrefManager.getVal<Long>(PrefName.CommentTokenExpiry)
if (tokenExpiry < System.currentTimeMillis() + tokenLifetime) { if (tokenExpiry < System.currentTimeMillis() + tokenLifetime) {
val commentResponse = PrefManager.getNullableVal<AuthResponse>(PrefName.CommentAuthResponse, null) val commentResponse =
PrefManager.getNullableVal<AuthResponse>(PrefName.CommentAuthResponse, null)
if (commentResponse != null) { if (commentResponse != null) {
authToken = commentResponse.authToken authToken = commentResponse.authToken
userId = commentResponse.user.id userId = commentResponse.user.id
@ -273,8 +302,9 @@ object CommentsAPI {
isAdmin = commentResponse.user.isAdmin ?: false isAdmin = commentResponse.user.isAdmin ?: false
isMod = commentResponse.user.isMod ?: false isMod = commentResponse.user.isMod ?: false
totalVotes = commentResponse.user.totalVotes totalVotes = commentResponse.user.totalVotes
return if (getUserDetails(client) != null) return
} }
} }
val url = "$address/authenticate" val url = "$address/authenticate"
val token = PrefManager.getVal(PrefName.AnilistToken, null as String?) ?: return val token = PrefManager.getVal(PrefName.AnilistToken, null as String?) ?: return
@ -290,7 +320,10 @@ object CommentsAPI {
return return
} }
PrefManager.setVal(PrefName.CommentAuthResponse, parsed) PrefManager.setVal(PrefName.CommentAuthResponse, parsed)
PrefManager.setVal(PrefName.CommentTokenExpiry, System.currentTimeMillis() + tokenLifetime) PrefManager.setVal(
PrefName.CommentTokenExpiry,
System.currentTimeMillis() + tokenLifetime
)
authToken = parsed.authToken authToken = parsed.authToken
userId = parsed.user.id userId = parsed.user.id
isBanned = parsed.user.isBanned ?: false isBanned = parsed.user.isBanned ?: false
@ -311,7 +344,11 @@ object CommentsAPI {
snackString("Failed to login after multiple attempts") snackString("Failed to login after multiple attempts")
} }
private suspend fun authRequest(token: String, url: String, client: OkHttpClient? = null): NiceResponse { private suspend fun authRequest(
token: String,
url: String,
client: OkHttpClient? = null
): NiceResponse {
val body: FormBody = FormBody.Builder() val body: FormBody = FormBody.Builder()
.add("token", token) .add("token", token)
.build() .build()
@ -387,6 +424,12 @@ data class AuthResponse(
} }
} }
@Serializable
data class UserResponse(
@SerialName("user")
val user: User
)
@Serializable @Serializable
data class User( data class User(
@SerialName("user_id") @SerialName("user_id")