mirror of
https://github.com/InsanusMokrassar/TelegramBotAPI.git
synced 2025-09-16 13:49:26 +00:00
start implementation of superchannels
This commit is contained in:
@@ -6,11 +6,23 @@ import dev.inmo.tgbotapi.types.chat.User
|
||||
* Inheritors of this interface have some [User] as a source of data. For example, any [dev.inmo.tgbotapi.types.queries.callback.CallbackQuery]
|
||||
* have [User] as the source of that query
|
||||
*/
|
||||
interface FromUser : WithUser {
|
||||
interface OptionallyFromUser : OptionallyWithUser {
|
||||
/**
|
||||
* The source [User] of this type
|
||||
*/
|
||||
val from: User
|
||||
val from: User?
|
||||
override val user: User?
|
||||
get() = from
|
||||
}
|
||||
/**
|
||||
* Inheritors of this interface have some [User] as a source of data. For example, any [dev.inmo.tgbotapi.types.queries.callback.CallbackQuery]
|
||||
* have [User] as the source of that query
|
||||
*/
|
||||
interface FromUser : OptionallyFromUser, WithUser {
|
||||
/**
|
||||
* The source [User] of this type
|
||||
*/
|
||||
override val from: User
|
||||
override val user: User
|
||||
get() = from
|
||||
}
|
||||
|
@@ -9,6 +9,14 @@ import dev.inmo.tgbotapi.types.chat.User
|
||||
* @see FromUser
|
||||
*/
|
||||
@ClassCastsIncluded(excludeRegex = ".*Impl")
|
||||
interface WithUser {
|
||||
val user: User
|
||||
interface OptionallyWithUser {
|
||||
val user: User?
|
||||
}
|
||||
/**
|
||||
* All inheritors of this type have [User] in their data as one of the main data
|
||||
*
|
||||
* @see FromUser
|
||||
*/
|
||||
interface WithUser : OptionallyWithUser {
|
||||
override val user: User
|
||||
}
|
||||
|
@@ -10,6 +10,7 @@ value class RawChatId(
|
||||
) {
|
||||
companion object {
|
||||
val DefaultUserId = RawChatId(136817688L) // I do not know why, it is Telegram crutch
|
||||
val FakeUserId = RawChatId(777000L) // Brought with Telegram Bot API 7.9 as backward compatibility value for from field
|
||||
}
|
||||
|
||||
override fun toString(): String {
|
||||
|
@@ -5,11 +5,13 @@ import dev.inmo.tgbotapi.types.*
|
||||
import dev.inmo.tgbotapi.types.buttons.InlineKeyboardMarkup
|
||||
import dev.inmo.tgbotapi.types.chat.CommonBot
|
||||
import dev.inmo.tgbotapi.types.chat.PreviewChannelChat
|
||||
import dev.inmo.tgbotapi.types.chat.User
|
||||
import dev.inmo.tgbotapi.types.message.abstracts.*
|
||||
import dev.inmo.tgbotapi.types.message.content.MessageContent
|
||||
|
||||
data class ChannelContentMessageImpl<T: MessageContent>(
|
||||
override val messageId: MessageId,
|
||||
override val from: User?,
|
||||
override val chat: PreviewChannelChat,
|
||||
override val content: T,
|
||||
override val date: DateTime,
|
||||
@@ -25,6 +27,7 @@ data class ChannelContentMessageImpl<T: MessageContent>(
|
||||
) : ChannelContentMessage<T> {
|
||||
constructor(
|
||||
messageId: MessageId,
|
||||
from: User?,
|
||||
chat: PreviewChannelChat,
|
||||
content: T,
|
||||
date: DateTime,
|
||||
@@ -38,6 +41,6 @@ data class ChannelContentMessageImpl<T: MessageContent>(
|
||||
mediaGroupId: MediaGroupId?,
|
||||
fromOffline: Boolean,
|
||||
) : this(
|
||||
messageId, chat, content, date, editDate, hasProtectedContent, forwardInfo.messageOrigin(), replyTo ?.let { ReplyInfo.Internal(it) }, replyMarkup, senderBot, authorSignature, mediaGroupId, fromOffline
|
||||
messageId, from, chat, content, date, editDate, hasProtectedContent, forwardInfo.messageOrigin(), replyTo ?.let { ReplyInfo.Internal(it) }, replyMarkup, senderBot, authorSignature, mediaGroupId, fromOffline
|
||||
)
|
||||
}
|
||||
|
@@ -37,6 +37,7 @@ import dev.inmo.tgbotapi.types.request.ChatShared
|
||||
import dev.inmo.tgbotapi.types.request.UsersShared
|
||||
import dev.inmo.tgbotapi.types.stories.Story
|
||||
import dev.inmo.tgbotapi.types.venue.Venue
|
||||
import dev.inmo.tgbotapi.utils.isFakeTelegramUser
|
||||
import kotlinx.serialization.SerialName
|
||||
import kotlinx.serialization.Serializable
|
||||
import kotlin.reflect.KClass
|
||||
@@ -160,6 +161,7 @@ internal data class RawMessage(
|
||||
private val giveaway_created: GiveawayCreated? = null,
|
||||
private val giveaway_completed: GiveawayPrivateResults? = null,
|
||||
) {
|
||||
private val checkedFrom = from ?.takeIf { !it.isFakeTelegramUser() }
|
||||
private val content: MessageContent? by lazy {
|
||||
val adaptedCaptionEntities = caption ?.let {
|
||||
(caption_entities ?: emptyList()).asTextSources(caption)
|
||||
@@ -298,14 +300,14 @@ internal data class RawMessage(
|
||||
when (chat) {
|
||||
is PreviewSupergroupChat -> CommonSupergroupEventMessage(
|
||||
messageId,
|
||||
from ?: error("Supergroup events are expected to contain 'from' field"),
|
||||
checkedFrom ?: from ?: error("Supergroup events are expected to contain 'from' field"),
|
||||
chat,
|
||||
chatEvent as? SupergroupEvent ?: throwWrongChatEvent(SupergroupEvent::class, chatEvent),
|
||||
date.asDate
|
||||
)
|
||||
is PreviewGroupChat -> CommonGroupEventMessage(
|
||||
messageId,
|
||||
from ?: error("Supergroup events are expected to contain 'from' field"),
|
||||
checkedFrom ?: from ?: error("Supergroup events are expected to contain 'from' field"),
|
||||
chat,
|
||||
chatEvent as? GroupEvent ?: throwWrongChatEvent(GroupChat::class, chatEvent),
|
||||
date.asDate
|
||||
@@ -337,6 +339,7 @@ internal data class RawMessage(
|
||||
is PreviewPublicChat -> when (chat) {
|
||||
is PreviewChannelChat -> ChannelContentMessageImpl(
|
||||
messageId = messageId,
|
||||
from = checkedFrom ?: from,
|
||||
chat = chat,
|
||||
content = content,
|
||||
date = date.asDate,
|
||||
@@ -396,7 +399,7 @@ internal data class RawMessage(
|
||||
chat = actualForumChat,
|
||||
messageId = messageId,
|
||||
threadId = messageThreadId,
|
||||
from = from ?: error("It is expected that in messages from non anonymous users and channels user must be specified"),
|
||||
from = checkedFrom ?: from ?: error("It is expected that in messages from non anonymous users and channels user must be specified"),
|
||||
date = date.asDate,
|
||||
forwardOrigin = forward_origin,
|
||||
editDate = edit_date ?.asDate,
|
||||
@@ -465,7 +468,7 @@ internal data class RawMessage(
|
||||
null -> CommonGroupContentMessageImpl(
|
||||
chat = chat,
|
||||
messageId = messageId,
|
||||
from = from ?: error("It is expected that in messages from non anonymous users and channels user must be specified"),
|
||||
from = checkedFrom ?: from ?: error("It is expected that in messages from non anonymous users and channels user must be specified"),
|
||||
date = date.asDate,
|
||||
forwardOrigin = forward_origin,
|
||||
editDate = edit_date ?.asDate,
|
||||
@@ -534,7 +537,7 @@ internal data class RawMessage(
|
||||
null -> CommonGroupContentMessageImpl(
|
||||
chat = chat,
|
||||
messageId = messageId,
|
||||
from = from ?: error("It is expected that in messages from non anonymous users and channels user must be specified"),
|
||||
from = checkedFrom ?: from ?: error("It is expected that in messages from non anonymous users and channels user must be specified"),
|
||||
date = date.asDate,
|
||||
forwardOrigin = forward_origin,
|
||||
editDate = edit_date ?.asDate,
|
||||
@@ -552,7 +555,7 @@ internal data class RawMessage(
|
||||
is PreviewPrivateChat -> if (business_connection_id == null) {
|
||||
PrivateContentMessageImpl(
|
||||
messageId = messageId,
|
||||
from = from ?: error("Was detected common message, but owner (sender) of the message was not found"),
|
||||
from = checkedFrom ?: from ?: error("Was detected common message, but owner (sender) of the message was not found"),
|
||||
chat = chat,
|
||||
content = content,
|
||||
date = date.asDate,
|
||||
@@ -569,7 +572,7 @@ internal data class RawMessage(
|
||||
} else {
|
||||
BusinessContentMessageImpl(
|
||||
messageId = messageId,
|
||||
from = from ?: error("Was detected common message, but owner (sender) of the message was not found"),
|
||||
from = checkedFrom ?: from ?: error("Was detected common message, but owner (sender) of the message was not found"),
|
||||
chat = BusinessChatImpl(
|
||||
chat.id.toBusinessChatId(business_connection_id),
|
||||
chat
|
||||
@@ -594,7 +597,7 @@ internal data class RawMessage(
|
||||
PassportMessage(
|
||||
messageId,
|
||||
chat,
|
||||
from ?: error("For passport must be provided user, but got null"),
|
||||
checkedFrom ?: from ?: error("For passport must be provided user, but got null"),
|
||||
date.asDate,
|
||||
passport_data
|
||||
)
|
||||
|
@@ -4,7 +4,7 @@ import dev.inmo.tgbotapi.types.chat.ChannelChat
|
||||
import dev.inmo.tgbotapi.types.chat.PreviewChannelChat
|
||||
import dev.inmo.tgbotapi.types.message.content.MessageContent
|
||||
|
||||
interface ChannelContentMessage<T: MessageContent> : PossiblySentViaBotCommonMessage<T>, SignedMessage, WithSenderChatMessage {
|
||||
interface ChannelContentMessage<T: MessageContent> : PossiblySentViaBotCommonMessage<T>, SignedMessage, WithSenderChatMessage, OptionallyFromUserMessage {
|
||||
override val chat: PreviewChannelChat
|
||||
override val senderChat: PreviewChannelChat
|
||||
get() = chat
|
||||
|
@@ -1,5 +1,8 @@
|
||||
package dev.inmo.tgbotapi.types.message.abstracts
|
||||
|
||||
import dev.inmo.tgbotapi.abstracts.FromUser
|
||||
import dev.inmo.tgbotapi.abstracts.OptionallyFromUser
|
||||
|
||||
interface FromUserMessage : FromUser, AccessibleMessage
|
||||
interface OptionallyFromUserMessage : OptionallyFromUser, AccessibleMessage
|
||||
|
||||
interface FromUserMessage : OptionallyFromUserMessage, FromUser
|
||||
|
@@ -0,0 +1,14 @@
|
||||
package dev.inmo.tgbotapi.utils
|
||||
|
||||
import dev.inmo.tgbotapi.types.RawChatId
|
||||
import dev.inmo.tgbotapi.types.UserId
|
||||
import dev.inmo.tgbotapi.types.chat.CommonUser
|
||||
import dev.inmo.tgbotapi.types.chat.User
|
||||
|
||||
val TelegramFakeUser = CommonUser(
|
||||
id = UserId(RawChatId.FakeUserId),
|
||||
firstName = "Telegram"
|
||||
)
|
||||
|
||||
fun User.isFakeTelegramUser() = id == TelegramFakeUser.id && firstName == TelegramFakeUser.firstName
|
||||
|
@@ -17,6 +17,7 @@ fun <T : MediaGroupPartContent> List<PossiblySentViaBotCommonMessage<T>>.asMedia
|
||||
return when (sourceMessage) {
|
||||
is ChannelContentMessage -> ChannelContentMessageImpl(
|
||||
messageId = sourceMessage.messageId,
|
||||
from = sourceMessage.from,
|
||||
chat = sourceMessage.chat,
|
||||
content = content,
|
||||
date = sourceMessage.date,
|
||||
|
Reference in New Issue
Block a user