mirror of
https://github.com/InsanusMokrassar/TelegramBotAPI.git
synced 2024-11-22 00:03:48 +00:00
add CloudStorage support
This commit is contained in:
parent
ccfb774f33
commit
1b1da33882
@ -177,6 +177,11 @@ val stickerKeywordLengthLimit = 0 .. 64
|
|||||||
|
|
||||||
const val botActionActualityTime: Seconds = 5
|
const val botActionActualityTime: Seconds = 5
|
||||||
|
|
||||||
|
val cloudStorageKeyLimit = 1 .. 128
|
||||||
|
val cloudStorageValueLimit = 0 .. 4096
|
||||||
|
val cloudStorageKeyRegex = Regex("[A-Za-z0-9_-]{${cloudStorageKeyLimit.first},${cloudStorageKeyLimit.last}}")
|
||||||
|
val cloudStorageValueRegex = Regex(".{${cloudStorageValueLimit.first},${cloudStorageValueLimit.last}}")
|
||||||
|
|
||||||
// Made as lazy for correct work in K/JS
|
// Made as lazy for correct work in K/JS
|
||||||
val telegramInlineModeGifPermittedMimeTypes by lazy {
|
val telegramInlineModeGifPermittedMimeTypes by lazy {
|
||||||
listOf(
|
listOf(
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
package dev.inmo.tgbotapi.webapps
|
package dev.inmo.tgbotapi.webapps
|
||||||
|
|
||||||
import dev.inmo.tgbotapi.utils.TelegramAPIUrlsKeeper
|
import dev.inmo.tgbotapi.utils.TelegramAPIUrlsKeeper
|
||||||
|
import dev.inmo.tgbotapi.webapps.cloud.CloudStorage
|
||||||
import dev.inmo.tgbotapi.webapps.haptic.HapticFeedback
|
import dev.inmo.tgbotapi.webapps.haptic.HapticFeedback
|
||||||
import dev.inmo.tgbotapi.webapps.invoice.InvoiceClosedInfo
|
import dev.inmo.tgbotapi.webapps.invoice.InvoiceClosedInfo
|
||||||
import dev.inmo.tgbotapi.webapps.popup.*
|
import dev.inmo.tgbotapi.webapps.popup.*
|
||||||
@ -49,6 +50,9 @@ external class WebApp {
|
|||||||
@JsName("HapticFeedback")
|
@JsName("HapticFeedback")
|
||||||
val hapticFeedback: HapticFeedback
|
val hapticFeedback: HapticFeedback
|
||||||
|
|
||||||
|
@JsName("CloudStorage")
|
||||||
|
val cloudStorage: CloudStorage
|
||||||
|
|
||||||
internal fun onEvent(type: String, callback: () -> Unit)
|
internal fun onEvent(type: String, callback: () -> Unit)
|
||||||
@JsName("onEvent")
|
@JsName("onEvent")
|
||||||
internal fun onEventWithViewportChangedData(type: String, callback: (ViewportChangedData) -> Unit)
|
internal fun onEventWithViewportChangedData(type: String, callback: (ViewportChangedData) -> Unit)
|
||||||
|
@ -0,0 +1,85 @@
|
|||||||
|
package dev.inmo.tgbotapi.webapps.cloud
|
||||||
|
|
||||||
|
external interface CloudStorage {
|
||||||
|
fun setItem(
|
||||||
|
key: CloudStorageKey,
|
||||||
|
value: CloudStorageValue,
|
||||||
|
callback: (e: Error?, success: Boolean?) -> Unit = definedExternally
|
||||||
|
): CloudStorage
|
||||||
|
fun getItem(
|
||||||
|
key: CloudStorageKey,
|
||||||
|
callback: (e: Error?, value: CloudStorageValue?) -> Unit
|
||||||
|
): CloudStorage
|
||||||
|
fun getItems(
|
||||||
|
key: Array<CloudStorageKey>,
|
||||||
|
callback: (e: Error?, values: Array<CloudStorageValue>?) -> Unit
|
||||||
|
): CloudStorage
|
||||||
|
fun removeItem(
|
||||||
|
key: CloudStorageKey,
|
||||||
|
callback: (e: Error?, success: Boolean?) -> Unit
|
||||||
|
): CloudStorage
|
||||||
|
fun removeItems(
|
||||||
|
key: Array<CloudStorageKey>,
|
||||||
|
callback: (e: Error?, success: Boolean?) -> Unit
|
||||||
|
): CloudStorage
|
||||||
|
fun getKeys(
|
||||||
|
callback: (e: Error?, success: Array<CloudStorageKey>?) -> Unit
|
||||||
|
): CloudStorage
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun <T> resultsToResult(e: Error?, v: T?): Result<T> = when {
|
||||||
|
e != null -> Result.failure(e)
|
||||||
|
v != null -> Result.success(v)
|
||||||
|
else -> Result.failure(IllegalStateException("Both value and e"))
|
||||||
|
}
|
||||||
|
|
||||||
|
fun CloudStorage.set(
|
||||||
|
key: String,
|
||||||
|
value: String,
|
||||||
|
callback: (result: Result<Boolean>) -> Unit = {}
|
||||||
|
) = setItem(CloudStorageKey(key), CloudStorageValue(value)) { e, v -> callback(resultsToResult(e, v)) }
|
||||||
|
|
||||||
|
fun CloudStorage.get(
|
||||||
|
key: String,
|
||||||
|
callback: (result: Result<CloudStorageValue>) -> Unit
|
||||||
|
) = getItem(CloudStorageKey(key)) { e, v -> callback(resultsToResult(e, v)) }
|
||||||
|
|
||||||
|
fun CloudStorage.get(
|
||||||
|
key: String,
|
||||||
|
key2: String,
|
||||||
|
vararg otherKeys: String,
|
||||||
|
callback: (result: Result<Array<CloudStorageValue>>) -> Unit
|
||||||
|
) = getItems(
|
||||||
|
Array(2 + otherKeys.size) {
|
||||||
|
when (it) {
|
||||||
|
0 -> CloudStorageKey(key)
|
||||||
|
1 -> CloudStorageKey(key2)
|
||||||
|
else -> CloudStorageKey(otherKeys[it - 2])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
) { e, v -> callback(resultsToResult(e, v)) }
|
||||||
|
|
||||||
|
fun CloudStorage.remove(
|
||||||
|
key: String,
|
||||||
|
callback: (result: Result<Boolean>) -> Unit
|
||||||
|
) = removeItem(CloudStorageKey(key)) { e, v -> callback(resultsToResult(e, v)) }
|
||||||
|
|
||||||
|
fun CloudStorage.remove(
|
||||||
|
key: String,
|
||||||
|
key2: String,
|
||||||
|
vararg otherKeys: String,
|
||||||
|
callback: (result: Result<Boolean>) -> Unit
|
||||||
|
) = removeItems(
|
||||||
|
Array(2 + otherKeys.size) {
|
||||||
|
when (it) {
|
||||||
|
0 -> CloudStorageKey(key)
|
||||||
|
1 -> CloudStorageKey(key2)
|
||||||
|
else -> CloudStorageKey(otherKeys[it - 2])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
) { e, v -> callback(resultsToResult(e, v)) }
|
||||||
|
|
||||||
|
fun CloudStorage.keys(
|
||||||
|
callback: (result: Result<Array<CloudStorageKey>>) -> Unit
|
||||||
|
) = getKeys { e, v -> callback(resultsToResult(e, v)) }
|
||||||
|
|
@ -0,0 +1,13 @@
|
|||||||
|
package dev.inmo.tgbotapi.webapps.cloud
|
||||||
|
|
||||||
|
import dev.inmo.tgbotapi.types.cloudStorageKeyRegex
|
||||||
|
import kotlinx.serialization.Serializable
|
||||||
|
|
||||||
|
@Serializable
|
||||||
|
value class CloudStorageKey(val key: String) {
|
||||||
|
init {
|
||||||
|
require(key.matches(cloudStorageKeyRegex)) {
|
||||||
|
"'$key' must pass $cloudStorageKeyRegex in case you wish to use it as key for cloud storage operations"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,14 @@
|
|||||||
|
package dev.inmo.tgbotapi.webapps.cloud
|
||||||
|
|
||||||
|
import dev.inmo.tgbotapi.types.cloudStorageKeyRegex
|
||||||
|
import dev.inmo.tgbotapi.types.cloudStorageValueRegex
|
||||||
|
import kotlinx.serialization.Serializable
|
||||||
|
|
||||||
|
@Serializable
|
||||||
|
value class CloudStorageValue(val value: String) {
|
||||||
|
init {
|
||||||
|
require(value.matches(cloudStorageValueRegex)) {
|
||||||
|
"'$value' must pass $cloudStorageValueRegex in case you wish to use it as key for cloud storage operations"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user