forked from recloudstream/cloudstream
Update plugin backend
This commit is contained in:
parent
0f492c2c82
commit
2f44c97b86
1 changed files with 48 additions and 30 deletions
|
@ -10,6 +10,9 @@ import java.security.MessageDigest
|
|||
import com.lagradost.cloudstream3.app
|
||||
import com.lagradost.cloudstream3.utils.Coroutines.ioSafe
|
||||
import com.lagradost.cloudstream3.utils.Coroutines.main
|
||||
import kotlinx.coroutines.delay
|
||||
import kotlinx.coroutines.sync.Mutex
|
||||
import kotlinx.coroutines.sync.withLock
|
||||
|
||||
object VotingApi { // please do not cheat the votes lol
|
||||
private const val LOGKEY = "VotingApi"
|
||||
|
@ -64,7 +67,8 @@ object VotingApi { // please do not cheat the votes lol
|
|||
}
|
||||
|
||||
private suspend fun createBucket(pluginUrl: String) {
|
||||
val url = "${apiDomain}/create?namespace=cs3-votes&key=${transformUrl(pluginUrl)}&value=0&update_lowerbound=-2&update_upperbound=2&enable_reset=0"
|
||||
val url =
|
||||
"${apiDomain}/create?namespace=cs3-votes&key=${transformUrl(pluginUrl)}&value=0&update_lowerbound=-2&update_upperbound=2&enable_reset=0"
|
||||
Log.d(LOGKEY, "Requesting: $url")
|
||||
app.get(url)
|
||||
}
|
||||
|
@ -74,33 +78,47 @@ object VotingApi { // please do not cheat the votes lol
|
|||
return true
|
||||
}
|
||||
|
||||
private val voteLock = Mutex()
|
||||
suspend fun vote(pluginUrl: String, requestType: VoteType): Int {
|
||||
// Prevent multiple requests at the same time.
|
||||
voteLock.withLock {
|
||||
if (!canVote(pluginUrl)) {
|
||||
main {
|
||||
Toast.makeText(context, R.string.extension_install_first, Toast.LENGTH_SHORT).show()
|
||||
Toast.makeText(context, R.string.extension_install_first, Toast.LENGTH_SHORT)
|
||||
.show()
|
||||
}
|
||||
return getVotes(pluginUrl)
|
||||
}
|
||||
val savedType: VoteType = getKey("cs3-votes/${transformUrl(pluginUrl)}") ?: VoteType.NONE
|
||||
var newType: VoteType = requestType
|
||||
var changeValue = 0
|
||||
if (requestType == savedType) {
|
||||
newType = VoteType.NONE
|
||||
changeValue = -requestType.value
|
||||
|
||||
val savedType: VoteType =
|
||||
getKey("cs3-votes/${transformUrl(pluginUrl)}") ?: VoteType.NONE
|
||||
|
||||
val newType = if (requestType == savedType) VoteType.NONE else requestType
|
||||
val changeValue = if (requestType == savedType) {
|
||||
-requestType.value
|
||||
} else if (savedType == VoteType.NONE) {
|
||||
changeValue = requestType.value
|
||||
requestType.value
|
||||
} else if (savedType != requestType) {
|
||||
changeValue = -savedType.value + requestType.value
|
||||
}
|
||||
val url = "${apiDomain}/update/cs3-votes/${transformUrl(pluginUrl)}?amount=${changeValue}"
|
||||
-savedType.value + requestType.value
|
||||
} else 0
|
||||
|
||||
// Pre-emptively set vote key
|
||||
setKey("cs3-votes/${transformUrl(pluginUrl)}", newType)
|
||||
|
||||
val url =
|
||||
"${apiDomain}/update/cs3-votes/${transformUrl(pluginUrl)}?amount=${changeValue}"
|
||||
Log.d(LOGKEY, "Requesting: $url")
|
||||
val res = app.get(url).parsedSafe<Result>()?.value
|
||||
if (res != null) {
|
||||
setKey("cs3-votes/${transformUrl(pluginUrl)}", newType)
|
||||
|
||||
if (res == null) {
|
||||
// "Refund" key if the response is invalid
|
||||
setKey("cs3-votes/${transformUrl(pluginUrl)}", savedType)
|
||||
} else {
|
||||
votesCache[pluginUrl] = res
|
||||
}
|
||||
return res ?: 0
|
||||
}
|
||||
}
|
||||
|
||||
private data class Result(
|
||||
val value: Int?
|
||||
|
|
Loading…
Reference in a new issue