1
0
mirror of https://github.com/InsanusMokrassar/TelegramBotAPI.git synced 2024-11-25 19:48:43 +00:00

add representations of all chat invite link functionality

This commit is contained in:
InsanusMokrassar 2021-03-10 22:46:56 +06:00
parent 233d893b5a
commit 40d702a311
6 changed files with 248 additions and 0 deletions

View File

@ -0,0 +1,20 @@
package dev.inmo.tgbotapi.requests.chat.abstracts
import com.soywiz.klock.DateTime
import dev.inmo.tgbotapi.requests.abstracts.SimpleRequest
import dev.inmo.tgbotapi.types.*
import kotlinx.serialization.DeserializationStrategy
interface ChatInviteLinkRequest : SimpleRequest<BotInviteLink> {
val chatId: ChatId
override val resultDeserializer: DeserializationStrategy<BotInviteLink>
get() = BotInviteLink.serializer()
}
interface KnownChatInviteLinkRequest : ChatInviteLinkRequest {
val inviteLink: String
}
interface EditChatInviteLinkRequest : ChatInviteLinkRequest {
val expireDate: DateTime?
val membersLimit: MembersLimit?
}

View File

@ -0,0 +1,31 @@
package dev.inmo.tgbotapi.requests.chat.invite_links
import com.soywiz.klock.*
import dev.inmo.tgbotapi.requests.chat.abstracts.EditChatInviteLinkRequest
import dev.inmo.tgbotapi.types.*
import kotlinx.serialization.*
@Serializable
data class CreateChatInviteLink(
@SerialName(chatIdField)
override val chatId: ChatId,
@SerialName(expireDateField)
private val expirationUnixTimeStamp: UnixTimeStamp? = null,
@SerialName(memberLimitField)
override val membersLimit: MembersLimit? = null
) : EditChatInviteLinkRequest {
override val expireDate: DateTime?
get() = expirationUnixTimeStamp ?.seconds ?.let { DateTime(it.milliseconds) }
override val requestSerializer: SerializationStrategy<*>
get() = serializer()
override fun method(): String = "createChatInviteLink"
}
fun CreateChatInviteLink(
chatId: ChatId,
expireDate: DateTime,
membersLimit: MembersLimit? = null
): CreateChatInviteLink = CreateChatInviteLink(
chatId, expireDate.unixMillisDouble.milliseconds.seconds.toLong(), membersLimit
)

View File

@ -0,0 +1,35 @@
package dev.inmo.tgbotapi.requests.chat.invite_links
import com.soywiz.klock.*
import dev.inmo.tgbotapi.requests.chat.abstracts.EditChatInviteLinkRequest
import dev.inmo.tgbotapi.requests.chat.abstracts.KnownChatInviteLinkRequest
import dev.inmo.tgbotapi.types.*
import kotlinx.serialization.*
@Serializable
data class EditChatInviteLink(
@SerialName(chatIdField)
override val chatId: ChatId,
@SerialName(inviteLinkField)
override val inviteLink: String,
@SerialName(expireDateField)
private val expirationUnixTimeStamp: UnixTimeStamp? = null,
@SerialName(memberLimitField)
override val membersLimit: MembersLimit? = null
) : EditChatInviteLinkRequest, KnownChatInviteLinkRequest {
override val expireDate: DateTime?
get() = expirationUnixTimeStamp ?.seconds ?.let { DateTime(it.milliseconds) }
override val requestSerializer: SerializationStrategy<*>
get() = serializer()
override fun method(): String = "editChatInviteLink"
}
fun EditChatInviteLink(
chatId: ChatId,
inviteLink: String,
expireDate: DateTime,
membersLimit: MembersLimit? = null
): EditChatInviteLink = EditChatInviteLink(
chatId, inviteLink, expireDate.unixMillisDouble.milliseconds.seconds.toLong(), membersLimit
)

View File

@ -0,0 +1,20 @@
package dev.inmo.tgbotapi.requests.chat.invite_links
import com.soywiz.klock.*
import dev.inmo.tgbotapi.requests.chat.abstracts.EditChatInviteLinkRequest
import dev.inmo.tgbotapi.requests.chat.abstracts.KnownChatInviteLinkRequest
import dev.inmo.tgbotapi.types.*
import kotlinx.serialization.*
@Serializable
data class RevokeChatInviteLink(
@SerialName(chatIdField)
override val chatId: ChatId,
@SerialName(inviteLinkField)
override val inviteLink: String
) : KnownChatInviteLinkRequest {
override val requestSerializer: SerializationStrategy<*>
get() = serializer()
override fun method(): String = "revokeChatInviteLink"
}

View File

@ -0,0 +1,133 @@
package dev.inmo.tgbotapi.types
import com.soywiz.klock.*
import kotlinx.serialization.*
import kotlinx.serialization.descriptors.SerialDescriptor
import kotlinx.serialization.encoding.Decoder
import kotlinx.serialization.encoding.Encoder
import kotlinx.serialization.json.JsonObject
@Serializable
private data class RawChatInviteLink(
@SerialName(inviteLinkField)
val inviteLink: String,
@SerialName(creatorField)
val creator: User,
@SerialName(isPrimaryField)
val isPrimary: Boolean,
@SerialName(isRevokedField)
val isRevoked: Boolean,
@SerialName(expireDateField)
val expirationDateTime: UnixTimeStamp? = null,
@SerialName(memberLimitField)
val membersLimit: MembersLimit ?= null
)
private fun ChatInviteLink.toRawChatInviteLink() = RawChatInviteLink(
inviteLink,
creator,
isPrimary,
isRevoked,
expirationDateTime ?.unixMillis ?.milliseconds ?.seconds ?.toLong(),
membersLimit
)
@Serializable(ChatInviteLinkSerializer::class)
sealed class ChatInviteLink {
abstract val inviteLink: String
abstract val creator: User
abstract val isPrimary: Boolean
abstract val isRevoked: Boolean
abstract val expirationDateTime: DateTime?
abstract val membersLimit: MembersLimit?
}
@Serializable
data class BotInviteLink(
@SerialName(inviteLinkField)
override val inviteLink: String,
@SerialName(creatorField)
override val creator: Bot,
@SerialName(isRevokedField)
override val isRevoked: Boolean = false,
@SerialName(expireDateField)
private val expireDate: UnixTimeStamp? = null,
@SerialName(memberLimitField)
override val membersLimit: MembersLimit? = null
) : ChatInviteLink() {
override val isPrimary: Boolean
get() = false
override val expirationDateTime: DateTime?
get() {
return DateTime(expireDate ?.seconds ?.milliseconds ?: return null)
}
}
@Serializable
data class PrimaryInviteLink(
@SerialName(inviteLinkField)
override val inviteLink: String,
@SerialName(creatorField)
override val creator: User,
@SerialName(isRevokedField)
override val isRevoked: Boolean = false,
@SerialName(expireDateField)
private val expireDate: UnixTimeStamp? = null,
@SerialName(memberLimitField)
override val membersLimit: MembersLimit? = null
) : ChatInviteLink() {
override val isPrimary: Boolean
get() = true
override val expirationDateTime: DateTime?
get() {
return DateTime(expireDate ?.seconds ?.milliseconds ?: return null)
}
}
@Serializable
data class CommonInviteLink(
@SerialName(inviteLinkField)
override val inviteLink: String,
@SerialName(creatorField)
override val creator: User,
@SerialName(isRevokedField)
override val isRevoked: Boolean = false,
@SerialName(expireDateField)
private val expireDate: UnixTimeStamp? = null,
@SerialName(memberLimitField)
override val membersLimit: MembersLimit? = null
) : ChatInviteLink() {
override val isPrimary: Boolean
get() = false
override val expirationDateTime: DateTime?
get() {
return DateTime(expireDate ?.seconds ?.milliseconds ?: return null)
}
}
@Serializer(ChatInviteLink::class)
object ChatInviteLinkSerializer : KSerializer<ChatInviteLink> {
override val descriptor: SerialDescriptor
get() = RawChatInviteLink.serializer().descriptor
override fun deserialize(decoder: Decoder): ChatInviteLink {
val deserializedRaw = RawChatInviteLink.serializer().deserialize(decoder)
return deserializedRaw.run {
when {
creator is Bot -> BotInviteLink(
inviteLink, creator, isRevoked, expirationDateTime, membersLimit
)
deserializedRaw.isPrimary -> PrimaryInviteLink(
inviteLink, creator, isRevoked, expirationDateTime, membersLimit
)
else -> CommonInviteLink(
inviteLink, creator, isRevoked, expirationDateTime, membersLimit
)
}
}
}
override fun serialize(encoder: Encoder, value: ChatInviteLink) {
RawChatInviteLink.serializer().serialize(encoder, value.toRawChatInviteLink())
}
}

View File

@ -26,10 +26,12 @@ typealias FoursquareId = String
typealias FoursquareType = String
typealias GooglePlaceId = String
typealias GooglePlaceType = String
typealias MembersLimit = Int
typealias Seconds = Int
typealias MilliSeconds = Long
typealias LongSeconds = Long
typealias UnixTimeStamp = LongSeconds
typealias Meters = Float
typealias Degrees = Int
@ -76,6 +78,8 @@ val explanationLimit = 0 .. 200
val openPeriodPollSecondsLimit = 5 .. 600
val membersLimit = 1 .. 99999
// Made as lazy for correct work in K/JS
val telegramInlineModeGifPermittedMimeTypes by lazy {
listOf(
@ -164,6 +168,10 @@ const val linkedChatIdField = "linked_chat_id"
const val horizontalAccuracyField = "horizontal_accuracy"
const val revokeMessagesField = "revoke_messages"
const val messageAutoDeleteTimeField = "message_auto_delete_time"
const val isPrimaryField = "is_primary"
const val isRevokedField = "is_revoked"
const val expireDateField = "expire_date"
const val memberLimitField = "member_limit"
const val requestContactField = "request_contact"
const val requestLocationField = "request_location"
@ -296,6 +304,7 @@ const val payField = "pay"
const val permissionsField = "permissions"
const val typeField = "type"
const val valueField = "value"
const val creatorField = "creator"
const val pointField = "point"
const val xShiftField = "x_shift"