com.github.insanusmokrassar.AutoPostTelegramBot.plugins.scheduler.PostsSchedulesTable.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.plugins.scheduler
import com.github.insanusmokrassar.AutoPostTelegramBot.base.database.tables.PostsTable
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 kotlinx.coroutines.channels.*
import kotlinx.coroutines.launch
import org.jetbrains.exposed.sql.*
import org.jetbrains.exposed.sql.transactions.transaction
import org.joda.time.DateTime
typealias PostIdPostTime = Pair
private val PostsSchedulesTableScope = NewDefaultCoroutineScope(4)
class PostsSchedulesTable : Table() {
private val postId = integer("postId").primaryKey()
private val postTime = datetime("postTime")
val postTimeRegisteredChannel = BroadcastChannel(Channel.CONFLATED)
val postTimeChangedChannel = BroadcastChannel(Channel.CONFLATED)
val postTimeRemovedChannel = BroadcastChannel(Channel.CONFLATED)
init {
PostsTable.postRemovedChannel.subscribe {
unregisterPost(it)
}
}
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 {
registeredPostsTimes().map {
(postId, _) ->
postId
}.minus(
value.first.getPluginLinks(value.second)
).forEach {
value.first.registerLink(it, value.second)
}
lastEnableSubscription = postTimeRegisteredChannel.subscribe {
value.first.registerLink(it.first, value.second)
}
lastDisableSubscription = postTimeRemovedChannel.subscribe {
value.first.unregisterLink(it, value.second)
}
}
}
fun postTime(postId: Int): DateTime? {
return transaction {
select {
[email protected](postId)
}.firstOrNull() ?.get(postTime)
}
}
fun registerPostTime(postId: Int, postTime: DateTime) {
transaction {
postTime(postId) ?.also {
update(
{
[email protected](postId)
}
) {
it[[email protected]] = postTime
}
PostsSchedulesTableScope.launch {
postTimeChangedChannel.send(postId to postTime)
}
} ?:also {
insert {
it[[email protected]] = postId
it[[email protected]] = postTime
}
PostsSchedulesTableScope.launch {
postTimeRegisteredChannel.send(postId to postTime)
}
}
}
}
fun unregisterPost(postId: Int) {
transaction {
deleteWhere {
[email protected](postId)
} > 0
}.also {
if (it) {
PostsSchedulesTableScope.launch {
postTimeRemovedChannel.send(postId)
}
}
}
}
fun registeredPostsTimes(): List {
return transaction { selectAll().map { it[postId] to it[postTime] } }
}
fun registeredPostsTimes(period: Pair): List {
return transaction {
select {
postTime.between(period.first, period.second)
}.map {
it[postId] to it[postTime]
}
}
}
fun nearPost(): PostIdPostTime? {
return transaction {
selectAll().sortedBy {
it[postTime]
}.firstOrNull() ?.let {
it[postId] to it[postTime]
}
}
}
}