com.github.insanusmokrassar.AutoPostTelegramBot.base.database.tables.PostsMessagesTable.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of AutoPostTelegramBot Show documentation
Show all versions of AutoPostTelegramBot Show documentation
It is base library for creating smart bot for simple management of channels posts
package com.github.insanusmokrassar.AutoPostTelegramBot.base.database.tables
import com.github.insanusmokrassar.AutoPostTelegramBot.base.models.PostMessage
import com.github.insanusmokrassar.AutoPostTelegramBot.extraSmallBroadcastCapacity
import com.github.insanusmokrassar.AutoPostTelegramBot.largeBroadcastCapacity
import com.github.insanusmokrassar.AutoPostTelegramBot.utils.NewDefaultCoroutineScope
import com.github.insanusmokrassar.TelegramBotAPI.types.MessageIdentifier
import kotlinx.coroutines.channels.BroadcastChannel
import kotlinx.coroutines.launch
import org.jetbrains.exposed.sql.*
import org.jetbrains.exposed.sql.transactions.transaction
typealias PostIdToMessagesIds = Pair>
val PostsMessagesTableScope = NewDefaultCoroutineScope()
object PostsMessagesTable : Table() {
val newMessagesOfPost = BroadcastChannel(largeBroadcastCapacity)
@Deprecated("This channel is not determine post id", ReplaceWith("removedMessageOfPost"))
val removeMessageOfPost = BroadcastChannel(extraSmallBroadcastCapacity)
val removedMessagesOfPost = BroadcastChannel(extraSmallBroadcastCapacity)
val removedMessageOfPost = BroadcastChannel(extraSmallBroadcastCapacity)
private val messageId = long("messageId").primaryKey()
private val mediaGroupId = text("mediaGroupId").nullable()
private val postId = integer("postId")
fun getMessagesOfPost(postId: Int): List {
return transaction {
select {
PostsMessagesTable.postId.eq(postId)
}.map {
PostMessage(
it[messageId].toLong(),
it[mediaGroupId]
)
}
}
}
fun findPostByMessageId(messageId: MessageIdentifier): Int? {
return transaction {
select {
[email protected](messageId)
}.firstOrNull() ?.get(postId)
}
}
fun addMessagesToPost(postId: Int, vararg messages: PostMessage) {
transaction {
messages.map {
message ->
insert {
it[PostsMessagesTable.postId] = postId
it[messageId] = message.messageId
it[mediaGroupId] = message.mediaGroupId
}
message.messageId
}.let {
PostsMessagesTableScope.launch {
newMessagesOfPost.send(PostIdToMessagesIds(postId, it))
}
}
}
}
@Deprecated("This method will be deprecated in near releases", ReplaceWith("removePostMessage"))
fun removeMessageOfPost(messageId: MessageIdentifier) {
transaction {
if (deleteWhere { PostsMessagesTable.messageId.eq(messageId) } > 0) {
PostsMessagesTableScope.launch {
removeMessageOfPost.send(messageId)
}
}
}
}
fun removePostMessages(postId: Int) {
getMessagesOfPost(postId).let {
transaction {
it.mapNotNull {
postMessage ->
if (removePostMessage(postId, postMessage.messageId)) {
postMessage.messageId
} else {
null
}
}
}
}.also {
if (it.isNotEmpty()) {
PostsMessagesTableScope.launch {
removedMessagesOfPost.send(postId to it)
}
}
}
}
fun removePostMessage(postId: Int, messageId: MessageIdentifier): Boolean {
return transaction {
if (deleteWhere { PostsMessagesTable.messageId.eq(messageId) } > 0) {
PostsMessagesTableScope.launch {
removedMessageOfPost.send(postId to messageId)
}
true
} else {
false
}
}
}
}