1
0
mirror of https://github.com/InsanusMokrassar/TelegramBotAPI.git synced 2024-06-02 07:55:25 +00:00
tgbotapi/tgbotapi.utils/src/commonMain/kotlin/dev/inmo/tgbotapi/extensions/utils/extensions/Same.kt

129 lines
4.7 KiB
Kotlin
Raw Normal View History

2022-07-10 18:53:20 +00:00
package dev.inmo.tgbotapi.extensions.utils.extensions
2023-10-11 07:07:07 +00:00
import dev.inmo.tgbotapi.abstracts.WithPreviewChat
2023-03-16 13:29:35 +00:00
import dev.inmo.tgbotapi.extensions.utils.usernameChatOrNull
import dev.inmo.tgbotapi.extensions.utils.whenUsernameChat
import dev.inmo.tgbotapi.types.ChatIdentifier
import dev.inmo.tgbotapi.types.MessageId
import dev.inmo.tgbotapi.types.MessageThreadId
import dev.inmo.tgbotapi.types.Username
import dev.inmo.tgbotapi.types.chat.Chat
2024-01-07 09:52:49 +00:00
import dev.inmo.tgbotapi.types.message.abstracts.AccessibleMessage
2023-03-16 13:29:35 +00:00
import dev.inmo.tgbotapi.types.threadId
import dev.inmo.tgbotapi.utils.extensions.threadIdOrNull
2023-03-16 13:29:35 +00:00
/**
* @return true in case if [this] message is placed in the chat with id == [chatId]
*/
@Suppress("NOTHING_TO_INLINE")
2023-10-11 07:07:07 +00:00
inline fun WithPreviewChat.sameChat(chatId: ChatIdentifier) =
chat.id == chatId || (chatId is Username && chat.whenUsernameChat {
it.username == chatId
} ?: false)
2023-03-16 13:29:35 +00:00
/**
* @return true in case if [this] message is placed in the [chat]
*/
@Suppress("NOTHING_TO_INLINE")
2023-10-11 07:07:07 +00:00
inline fun WithPreviewChat.sameChat(chat: Chat) =
sameChat(chat.id) || chat.usernameChatOrNull()?.username?.let { sameChat(it) } ?: false
2022-07-10 18:53:20 +00:00
2022-07-10 18:57:13 +00:00
/**
* @return true in case if [this] message is placed in the same chat that [other]
*/
@Suppress("NOTHING_TO_INLINE")
2024-01-07 09:52:49 +00:00
inline fun WithPreviewChat.sameChat(other: AccessibleMessage) = sameChat(other.chat)
2023-03-16 13:29:35 +00:00
/**
2024-01-07 09:52:49 +00:00
* @return true in case if [this] message is from the same chat (with id == [chatId]) and [this] [AccessibleMessage.messageId]
2023-03-16 13:29:35 +00:00
* equal [messageId] identifier
*/
@Suppress("NOTHING_TO_INLINE")
2024-01-07 09:52:49 +00:00
inline fun AccessibleMessage.sameMessage(
2023-03-16 13:29:35 +00:00
chatId: ChatIdentifier,
messageId: MessageId
) = sameChat(chatId) && this.messageId == messageId
/**
2024-01-07 09:52:49 +00:00
* @return true in case if [this] message is from the same [chat] and [this] [AccessibleMessage.messageId] equal [messageId]
2023-03-16 13:29:35 +00:00
* identifier
*/
@Suppress("NOTHING_TO_INLINE")
2024-01-07 09:52:49 +00:00
inline fun AccessibleMessage.sameMessage(
2023-03-16 13:29:35 +00:00
chat: Chat,
messageId: MessageId
) = sameChat(chat) && this.messageId == messageId
2022-07-10 18:57:13 +00:00
2022-07-10 18:53:20 +00:00
/**
* @return true in case if [this] message is the same as [other]. The same here means that these messages from one chat
2024-01-07 09:52:49 +00:00
* and have equal [AccessibleMessage.messageId] identifier
2022-07-10 18:53:20 +00:00
*/
@Suppress("NOTHING_TO_INLINE")
2024-01-07 09:52:49 +00:00
inline fun AccessibleMessage.sameMessage(other: AccessibleMessage) = sameMessage(other.chat, other.messageId)
/**
* Thread is the same thing that topic
*
2023-03-16 13:29:35 +00:00
* @return true in case if [this] message is in the chat [chatId] and topic [threadId]. The same here means that these
2024-01-07 09:52:49 +00:00
* messages from one chat and have equal [AccessibleMessage.threadIdOrNull] identifier
*/
@Suppress("NOTHING_TO_INLINE")
2024-01-07 09:52:49 +00:00
inline fun AccessibleMessage.sameTopic(
2023-03-16 13:29:35 +00:00
chatId: ChatIdentifier,
threadId: MessageThreadId? = chatId.threadId
) = sameChat(chatId) && threadIdOrNull == threadId
/**
* Thread is the same thing that topic
*
2023-03-16 13:29:35 +00:00
* @return true in case if [this] message is in the chat [chatId] and topic [threadId]. The same here means that these
2024-01-07 09:52:49 +00:00
* messages from one chat and have equal [AccessibleMessage.threadIdOrNull] identifier
2023-03-16 13:29:35 +00:00
*/
@Suppress("NOTHING_TO_INLINE")
2024-01-07 09:52:49 +00:00
inline fun AccessibleMessage.sameThread(
2023-03-16 13:29:35 +00:00
chatId: ChatIdentifier,
threadId: MessageThreadId? = chatId.threadId
) = sameTopic(chatId, threadId)
/**
* Thread is the same thing that topic
*
* @return true in case if [this] message is from the [chat] and topic [threadId]. The same here means that these
2024-01-07 09:52:49 +00:00
* messages from one chat and have equal [AccessibleMessage.threadIdOrNull] identifier
*/
@Suppress("NOTHING_TO_INLINE")
2024-01-07 09:52:49 +00:00
inline fun AccessibleMessage.sameTopic(
2023-03-16 13:29:35 +00:00
chat: Chat,
threadId: MessageThreadId? = chat.id.threadId
) = sameTopic(chat.id, threadId)
2023-03-16 11:23:39 +00:00
2023-03-16 13:29:35 +00:00
/**
* Thread is the same thing that topic
*
* @return true in case if [this] message is from the [chat] and topic [threadId]. The same here means that these
2024-01-07 09:52:49 +00:00
* messages from one chat and have equal [AccessibleMessage.threadIdOrNull] identifier
2023-03-16 13:29:35 +00:00
*/
2023-03-16 11:23:39 +00:00
@Suppress("NOTHING_TO_INLINE")
2024-01-07 09:52:49 +00:00
inline fun AccessibleMessage.sameThread(
2023-03-16 13:29:35 +00:00
chat: Chat,
threadId: MessageThreadId? = chat.id.threadId
) = sameThread(chat.id, threadId)
2023-03-16 11:23:39 +00:00
2023-03-16 13:29:35 +00:00
/**
* Thread is the same thing that topic
*
* @return true in case if [this] message is from the same chat and topic as [other]. The same here means that these
2024-01-07 09:52:49 +00:00
* messages from one chat and have equal [AccessibleMessage.threadIdOrNull] identifier
2023-03-16 13:29:35 +00:00
*/
@Suppress("NOTHING_TO_INLINE")
2024-01-07 09:52:49 +00:00
inline fun AccessibleMessage.sameTopic(other: AccessibleMessage) = sameTopic(other.chat, other.threadIdOrNull)
2023-03-16 13:29:35 +00:00
/**
* Thread is the same thing that topic
*
* @return true in case if [this] message is in the same topic as the [other]. The same here means that these messages
2024-01-07 09:52:49 +00:00
* from one chat and have equal [AccessibleMessage.threadIdOrNull] identifier
2023-03-16 13:29:35 +00:00
*/
2023-03-16 11:23:39 +00:00
@Suppress("NOTHING_TO_INLINE")
2024-01-07 09:52:49 +00:00
inline fun AccessibleMessage.sameThread(other: AccessibleMessage) = sameTopic(other)