diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/requests/chat/abstracts/ChatInviteLinkRequest.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/requests/chat/abstracts/ChatInviteLinkRequest.kt new file mode 100644 index 0000000000..afbcbd72f8 --- /dev/null +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/requests/chat/abstracts/ChatInviteLinkRequest.kt @@ -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 { + val chatId: ChatId + + override val resultDeserializer: DeserializationStrategy + get() = BotInviteLink.serializer() +} +interface KnownChatInviteLinkRequest : ChatInviteLinkRequest { + val inviteLink: String +} +interface EditChatInviteLinkRequest : ChatInviteLinkRequest { + val expireDate: DateTime? + val membersLimit: MembersLimit? +} diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/requests/chat/invite_links/CreateChatInviteLink.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/requests/chat/invite_links/CreateChatInviteLink.kt new file mode 100644 index 0000000000..32ff101b0e --- /dev/null +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/requests/chat/invite_links/CreateChatInviteLink.kt @@ -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 +) diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/requests/chat/invite_links/EditChatInviteLink.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/requests/chat/invite_links/EditChatInviteLink.kt new file mode 100644 index 0000000000..2e6c7e2bce --- /dev/null +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/requests/chat/invite_links/EditChatInviteLink.kt @@ -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 +) diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/requests/chat/invite_links/RevokeChatInviteLink.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/requests/chat/invite_links/RevokeChatInviteLink.kt new file mode 100644 index 0000000000..9d14035c37 --- /dev/null +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/requests/chat/invite_links/RevokeChatInviteLink.kt @@ -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" +} diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/ChatInviteLink.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/ChatInviteLink.kt new file mode 100644 index 0000000000..cba15be9b9 --- /dev/null +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/ChatInviteLink.kt @@ -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 { + 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()) + } +} diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/Common.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/Common.kt index ff8adabedd..74d2f356b0 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/Common.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/types/Common.kt @@ -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"