All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.insanusmokrassar.AutoPostTelegramBot.plugins.rating.database.PostsLikesMessagesTable.kt Maven / Gradle / Ivy

Go to download

It is base library for creating smart bot for simple management of channels posts

There is a newer version: 1.7.0
Show newest version
package com.github.insanusmokrassar.AutoPostTelegramBot.plugins.rating.database

import com.github.insanusmokrassar.AutoPostTelegramBot.base.database.tables.PostIdMessageId
import com.github.insanusmokrassar.AutoPostTelegramBot.base.plugins.PluginName
import com.github.insanusmokrassar.AutoPostTelegramBot.plugins.base.PostsUsedTable
import com.github.insanusmokrassar.AutoPostTelegramBot.utils.NewDefaultCoroutineScope
import com.github.insanusmokrassar.AutoPostTelegramBot.utils.extensions.subscribe
import com.github.insanusmokrassar.TelegramBotAPI.types.MessageIdentifier
import kotlinx.coroutines.channels.*
import kotlinx.coroutines.launch
import org.jetbrains.exposed.sql.*
import org.jetbrains.exposed.sql.transactions.transaction

private val PostsLikesMessagesTableScope = NewDefaultCoroutineScope()

class PostsLikesMessagesTable(
    private val postsLikesTable: PostsLikesTable
) : Table() {
    val ratingMessageRegisteredChannel = BroadcastChannel(Channel.CONFLATED)
    val ratingMessageUnregisteredChannel = BroadcastChannel(Channel.CONFLATED)

    private val postId = integer("postId").primaryKey()
    private val messageId = long("messageId")

    private lateinit var lastEnableSubscription: ReceiveChannel
    private lateinit var lastDisableSubscription: ReceiveChannel

    internal var postsUsedTablePluginName: Pair? = null
        set(value) {
            field ?.also {
                lastEnableSubscription.cancel()
                lastDisableSubscription.cancel()
            }
            field = value
            value ?.also {
                getEnabledPostsIdAndRatings().map {
                    (postId, _) ->
                    postId
                }.minus(
                    value.first.getPluginLinks(value.second)
                ).forEach {
                    value.first.registerLink(it, value.second)
                }
                lastEnableSubscription = ratingMessageRegisteredChannel.subscribe {
                    value.first.registerLink(it.first, value.second)
                }
                lastDisableSubscription = ratingMessageUnregisteredChannel.subscribe {
                    value.first.unregisterLink(it, value.second)
                }
            }
        }

    fun messageIdByPostId(postId: Int): MessageIdentifier? {
        return transaction {
            select {
                [email protected](postId)
            }.firstOrNull() ?.get(messageId)
        }
    }

    fun postIdByMessageId(messageId: MessageIdentifier): Int? {
        return transaction {
            select {
                [email protected](messageId)
            }.firstOrNull() ?.let {
                it[postId]
            }
        }
    }

    fun enableLikes(postId: Int, messageId: MessageIdentifier): Boolean {
        return transaction {
            if (messageIdByPostId(postId) == null) {
                insert {
                    it[[email protected]] = postId
                    it[[email protected]] = messageId
                }
                PostsLikesMessagesTableScope.launch {
                    ratingMessageRegisteredChannel.send(
                        PostIdMessageId(
                            postId,
                            messageId
                        )
                    )
                }
                true
            } else {
                false
            }
        }
    }

    fun disableLikes(postId: Int) {
        transaction {
            (deleteWhere {
                [email protected](postId)
            } > 0).also {
                if (it) {
                    PostsLikesMessagesTableScope.launch {
                        ratingMessageUnregisteredChannel.send(postId)
                    }
                }
            }
        }
    }

    fun getEnabledPostsIdAndRatings(): List {
        return transaction {
            selectAll().mapNotNull {
                it[postId]
            }.map {
                it to postsLikesTable.getPostRating(it)
            }
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy