Skip to content

Commit

Permalink
Merge pull request #738 from InsanusMokrassar/7.0.1
Browse files Browse the repository at this point in the history
7.0.1
  • Loading branch information
InsanusMokrassar authored Mar 16, 2023
2 parents 741d808 + b688fa5 commit 0484861
Show file tree
Hide file tree
Showing 7 changed files with 161 additions and 12 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
# TelegramBotAPI changelog

## 7.0.1

* `Core`:
* New interface `WithChat` which contains `chat` field
* `Message` now inherits `WithChat`
* `ChatMemberUpdated` now inherits `WithChat`
* `Utils`:
* Improvements in `same`-notations

## 7.0.0

This update contains support of [Telegram Bot API 6.6](https://core.telegram.org/bots/api-changelog#march-9-2023)
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ kotlin.incremental=true
kotlin.incremental.js=true

library_group=dev.inmo
library_version=7.0.0
library_version=7.0.1
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package dev.inmo.tgbotapi.abstracts

import dev.inmo.tgbotapi.types.chat.Chat

/**
* All inheritors of this interface have [chat] field and related to this [chat]
*/
interface WithChat {
val chat: Chat
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package dev.inmo.tgbotapi.types.chat.member

import dev.inmo.tgbotapi.abstracts.WithChat
import dev.inmo.tgbotapi.abstracts.WithUser
import dev.inmo.tgbotapi.types.*
import dev.inmo.tgbotapi.types.chat.Chat
import dev.inmo.tgbotapi.types.chat.User
Expand All @@ -9,9 +11,9 @@ import kotlinx.serialization.Serializable
@Serializable
data class ChatMemberUpdated(
@SerialName(chatField)
val chat: Chat,
override val chat: Chat,
@SerialName(fromField)
val user: User,
override val user: User,
@SerialName(dateField)
val date: TelegramDate,
@SerialName(oldChatMemberField)
Expand All @@ -20,4 +22,4 @@ data class ChatMemberUpdated(
val newChatMemberState: ChatMember,
@SerialName(inviteLinkField)
val inviteLink: ChatInviteLink? = null
)
) : WithChat, WithUser
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package dev.inmo.tgbotapi.types.message.abstracts

import com.soywiz.klock.DateTime
import dev.inmo.tgbotapi.abstracts.WithChat
import dev.inmo.tgbotapi.utils.internal.ClassCastsIncluded
import dev.inmo.tgbotapi.types.MessageId
import dev.inmo.tgbotapi.types.chat.Chat
Expand All @@ -11,9 +12,8 @@ import kotlinx.serialization.encoding.Decoder
import kotlinx.serialization.encoding.Encoder

@ClassCastsIncluded
interface Message {
interface Message : WithChat {
val messageId: MessageId
val chat: Chat
val date: DateTime
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package dev.inmo.tgbotapi.extensions.utils.extensions

import dev.inmo.tgbotapi.abstracts.FromUser
import dev.inmo.tgbotapi.abstracts.WithChat
import dev.inmo.tgbotapi.types.ChatIdentifier
import dev.inmo.tgbotapi.types.UserId
import dev.inmo.tgbotapi.types.chat.Chat
import dev.inmo.tgbotapi.types.chat.User
import dev.inmo.tgbotapi.types.message.abstracts.Message
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.filter
import kotlinx.coroutines.flow.transform

/**
* Will pass only those [T] which have [sameChat] as [chatId]
*/
@Suppress("NOTHING_TO_INLINE")
inline fun <T : WithChat> Flow<T>.fromChat(chatId: ChatIdentifier): Flow<T> = filter { it.sameChat(chatId) }

/**
* Will pass only those [T] which have [sameChat] as [chatId]
*/
@Suppress("NOTHING_TO_INLINE")
inline fun <T : WithChat> Flow<T>.fromChat(chat: Chat): Flow<T> = fromChat(chat.id)

/**
* @return [Flow] with the [FromUser.user] field [User.id] the same as [userId]
*/
@Suppress("NOTHING_TO_INLINE")
inline fun <T : FromUser> Flow<T>.fromUser(userId: UserId): Flow<T> = filter { it.user.id == userId }

/**
* @return [Flow] with the [FromUser.user] field [User.id] the same as [user] [User.id]
*/
@Suppress("NOTHING_TO_INLINE")
inline fun <T : FromUser> Flow<T>.fromUser(user: User): Flow<T> = fromUser(user.id)

Original file line number Diff line number Diff line change
@@ -1,29 +1,120 @@
package dev.inmo.tgbotapi.extensions.utils.extensions

import dev.inmo.tgbotapi.abstracts.WithChat
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
import dev.inmo.tgbotapi.types.message.abstracts.Message
import dev.inmo.tgbotapi.types.threadId
import dev.inmo.tgbotapi.utils.extensions.threadIdOrNull

/**
* @return true in case if [this] message is placed in the chat with id == [chatId]
*/
@Suppress("NOTHING_TO_INLINE")
inline fun WithChat.sameChat(chatId: ChatIdentifier) = chat.id == chatId || (chatId is Username && chat.whenUsernameChat {
it.username == chatId
} ?: false)

/**
* @return true in case if [this] message is placed in the [chat]
*/
@Suppress("NOTHING_TO_INLINE")
inline fun WithChat.sameChat(chat: Chat) = sameChat(chat.id) || chat.usernameChatOrNull() ?.username ?.let { sameChat(it) } ?: false

/**
* @return true in case if [this] message is placed in the same chat that [other]
*/
@Suppress("NOTHING_TO_INLINE")
inline fun Message.sameChat(other: Message) = chat.id == other.chat.id
inline fun WithChat.sameChat(other: Message) = sameChat(other.chat)

/**
* @return true in case if [this] message is from the same chat (with id == [chatId]) and [this] [Message.messageId]
* equal [messageId] identifier
*/
@Suppress("NOTHING_TO_INLINE")
inline fun Message.sameMessage(
chatId: ChatIdentifier,
messageId: MessageId
) = sameChat(chatId) && this.messageId == messageId

/**
* @return true in case if [this] message is from the same [chat] and [this] [Message.messageId] equal [messageId]
* identifier
*/
@Suppress("NOTHING_TO_INLINE")
inline fun Message.sameMessage(
chat: Chat,
messageId: MessageId
) = sameChat(chat) && this.messageId == messageId

/**
* @return true in case if [this] message is the same as [other]. The same here means that these messages from one chat
* and have equal [Message.messageId] identifier
*/
@Suppress("NOTHING_TO_INLINE")
inline fun Message.sameMessage(other: Message) = sameChat(other) && messageId == other.messageId
inline fun Message.sameMessage(other: Message) = sameMessage(other.chat, other.messageId)

/**
* 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
* from one chat and have equal [Message.threadIdOrNull] identifier
* @return true in case if [this] message is in the chat [chatId] and topic [threadId]. The same here means that these
* messages from one chat and have equal [Message.threadIdOrNull] identifier
*/
@Suppress("NOTHING_TO_INLINE")
inline fun Message.sameTopic(
chatId: ChatIdentifier,
threadId: MessageThreadId? = chatId.threadId
) = sameChat(chatId) && threadIdOrNull == threadId

/**
* Thread is the same thing that topic
*
* @return true in case if [this] message is in the chat [chatId] and topic [threadId]. The same here means that these
* messages from one chat and have equal [Message.threadIdOrNull] identifier
*/
@Suppress("NOTHING_TO_INLINE")
inline fun Message.sameThread(
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
* messages from one chat and have equal [Message.threadIdOrNull] identifier
*/
@Suppress("NOTHING_TO_INLINE")
inline fun Message.sameTopic(
chat: Chat,
threadId: MessageThreadId? = chat.id.threadId
) = sameTopic(chat.id, 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
* messages from one chat and have equal [Message.threadIdOrNull] identifier
*/
@Suppress("NOTHING_TO_INLINE")
inline fun Message.sameThread(
chat: Chat,
threadId: MessageThreadId? = chat.id.threadId
) = sameThread(chat.id, threadId)

/**
* 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
* messages from one chat and have equal [Message.threadIdOrNull] identifier
*/
@Suppress("NOTHING_TO_INLINE")
inline fun Message.sameTopic(other: Message) = sameChat(other) && threadIdOrNull == other.threadIdOrNull
inline fun Message.sameTopic(other: Message) = sameTopic(other.chat, other.threadIdOrNull)

/**
* Thread is the same thing that topic
Expand All @@ -32,4 +123,4 @@ inline fun Message.sameTopic(other: Message) = sameChat(other) && threadIdOrNull
* from one chat and have equal [Message.threadIdOrNull] identifier
*/
@Suppress("NOTHING_TO_INLINE")
inline fun Message.sameThread(other: Message) = sameChat(other) && threadIdOrNull == other.threadIdOrNull
inline fun Message.sameThread(other: Message) = sameTopic(other)

0 comments on commit 0484861

Please sign in to comment.