fix: keep user data up to date
This commit is contained in:
parent
05fc97a933
commit
6e6429db82
1 changed files with 53 additions and 10 deletions
|
@ -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"
|
||||||
|
@ -130,7 +129,7 @@ object CommentsAPI {
|
||||||
body.add("parent_comment_id", it.toString())
|
body.add("parent_comment_id", it.toString())
|
||||||
}
|
}
|
||||||
val request = requestBuilder()
|
val request = requestBuilder()
|
||||||
val json = try{
|
val json = try {
|
||||||
request.post(url, requestBody = body.build())
|
request.post(url, requestBody = body.build())
|
||||||
} catch (e: IOException) {
|
} catch (e: IOException) {
|
||||||
snackString("Failed to comment")
|
snackString("Failed to comment")
|
||||||
|
@ -168,7 +167,7 @@ object CommentsAPI {
|
||||||
suspend fun deleteComment(commentId: Int): Boolean {
|
suspend fun deleteComment(commentId: Int): Boolean {
|
||||||
val url = "$address/comments/$commentId"
|
val url = "$address/comments/$commentId"
|
||||||
val request = requestBuilder()
|
val request = requestBuilder()
|
||||||
val json = try{
|
val json = try {
|
||||||
request.delete(url)
|
request.delete(url)
|
||||||
} catch (e: IOException) {
|
} catch (e: IOException) {
|
||||||
snackString("Failed to delete comment")
|
snackString("Failed to delete comment")
|
||||||
|
@ -203,7 +202,7 @@ object CommentsAPI {
|
||||||
suspend fun banUser(userId: String): Boolean {
|
suspend fun banUser(userId: String): Boolean {
|
||||||
val url = "$address/ban/$userId"
|
val url = "$address/ban/$userId"
|
||||||
val request = requestBuilder()
|
val request = requestBuilder()
|
||||||
val json = try{
|
val json = try {
|
||||||
request.post(url)
|
request.post(url)
|
||||||
} catch (e: IOException) {
|
} catch (e: IOException) {
|
||||||
snackString("Failed to ban user")
|
snackString("Failed to ban user")
|
||||||
|
@ -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)
|
||||||
|
@ -225,7 +229,7 @@ object CommentsAPI {
|
||||||
.add("reportedId", reportedId)
|
.add("reportedId", reportedId)
|
||||||
.build()
|
.build()
|
||||||
val request = requestBuilder()
|
val request = requestBuilder()
|
||||||
val json = try{
|
val json = try {
|
||||||
request.post(url, requestBody = body)
|
request.post(url, requestBody = body)
|
||||||
} catch (e: IOException) {
|
} catch (e: IOException) {
|
||||||
snackString("Failed to report comment")
|
snackString("Failed to report comment")
|
||||||
|
@ -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")
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue