mirror of
https://github.com/recloudstream/cloudstream.git
synced 2024-08-15 01:53:11 +00:00
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.app
|
||||||
import com.lagradost.cloudstream3.utils.Coroutines.ioSafe
|
import com.lagradost.cloudstream3.utils.Coroutines.ioSafe
|
||||||
import com.lagradost.cloudstream3.utils.Coroutines.main
|
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
|
object VotingApi { // please do not cheat the votes lol
|
||||||
private const val LOGKEY = "VotingApi"
|
private const val LOGKEY = "VotingApi"
|
||||||
|
@ -64,7 +67,8 @@ object VotingApi { // please do not cheat the votes lol
|
||||||
}
|
}
|
||||||
|
|
||||||
private suspend fun createBucket(pluginUrl: String) {
|
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")
|
Log.d(LOGKEY, "Requesting: $url")
|
||||||
app.get(url)
|
app.get(url)
|
||||||
}
|
}
|
||||||
|
@ -74,33 +78,47 @@ object VotingApi { // please do not cheat the votes lol
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private val voteLock = Mutex()
|
||||||
suspend fun vote(pluginUrl: String, requestType: VoteType): Int {
|
suspend fun vote(pluginUrl: String, requestType: VoteType): Int {
|
||||||
|
// Prevent multiple requests at the same time.
|
||||||
|
voteLock.withLock {
|
||||||
if (!canVote(pluginUrl)) {
|
if (!canVote(pluginUrl)) {
|
||||||
main {
|
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)
|
return getVotes(pluginUrl)
|
||||||
}
|
}
|
||||||
val savedType: VoteType = getKey("cs3-votes/${transformUrl(pluginUrl)}") ?: VoteType.NONE
|
|
||||||
var newType: VoteType = requestType
|
val savedType: VoteType =
|
||||||
var changeValue = 0
|
getKey("cs3-votes/${transformUrl(pluginUrl)}") ?: VoteType.NONE
|
||||||
if (requestType == savedType) {
|
|
||||||
newType = VoteType.NONE
|
val newType = if (requestType == savedType) VoteType.NONE else requestType
|
||||||
changeValue = -requestType.value
|
val changeValue = if (requestType == savedType) {
|
||||||
|
-requestType.value
|
||||||
} else if (savedType == VoteType.NONE) {
|
} else if (savedType == VoteType.NONE) {
|
||||||
changeValue = requestType.value
|
requestType.value
|
||||||
} else if (savedType != requestType) {
|
} else if (savedType != requestType) {
|
||||||
changeValue = -savedType.value + requestType.value
|
-savedType.value + requestType.value
|
||||||
}
|
} else 0
|
||||||
val url = "${apiDomain}/update/cs3-votes/${transformUrl(pluginUrl)}?amount=${changeValue}"
|
|
||||||
|
// 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")
|
Log.d(LOGKEY, "Requesting: $url")
|
||||||
val res = app.get(url).parsedSafe<Result>()?.value
|
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
|
votesCache[pluginUrl] = res
|
||||||
}
|
}
|
||||||
return res ?: 0
|
return res ?: 0
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private data class Result(
|
private data class Result(
|
||||||
val value: Int?
|
val value: Int?
|
||||||
|
|
Loading…
Reference in a new issue