45 Commits

Author SHA1 Message Date
194a26fa36 update plagubot version 2026-03-15 21:26:12 +06:00
e190ace1f2 start 0.11.1 2026-03-15 20:58:45 +06:00
0f2a0c24b2 Merge pull request #31 from InsanusMokrassar/0.11.0
0.11.0
2026-03-12 18:01:13 +06:00
a65e7598ba small optimization of panel ratings button 2026-03-12 17:56:05 +06:00
53cc851944 small optimization of panelApi.forceRefresh in onPollUpdates 2026-03-12 17:39:47 +06:00
815391d3f6 add refreshing of panel after poll update 2026-03-12 17:24:48 +06:00
f06d91e38f add templating of panelButtonText for ratings 2026-03-12 17:04:08 +06:00
028f1f48e8 add coloring of inline buttons 2026-03-12 16:31:29 +06:00
18180e67f6 made it buildable again 2026-03-12 16:19:00 +06:00
1091f1141b start 0.11.0 2026-03-12 16:19:00 +06:00
7dbc5ef254 Merge pull request #30 from InsanusMokrassar/0.10.1
0.10.1
2025-08-06 17:52:03 +06:00
380e6e1117 update tgbotapi version 2025-08-06 17:50:56 +06:00
d5d924401b start 0.10.1 2025-08-06 17:50:21 +06:00
53e74745aa Merge pull request #29 from InsanusMokrassar/0.10.0
0.10.0
2025-08-04 14:21:52 +06:00
bae2c67136 update dependencies 2025-08-04 14:21:34 +06:00
c579fb576c start 0.10.0 2025-08-04 14:19:32 +06:00
a1cffd688e fix license section in publish.gradle 2025-06-30 14:41:40 +06:00
5cab93a7fb Merge pull request #28 from InsanusMokrassar/0.9.0
0.9.0
2025-06-27 10:40:03 +06:00
1d200c38e4 update gradle wrapper 2025-06-27 10:38:46 +06:00
9ebadfe1e4 update dependencies and fix build 2025-06-27 10:37:53 +06:00
333f807c97 include nmcp 2025-06-27 10:26:33 +06:00
9ae7238974 update publishing way 2025-06-27 10:25:58 +06:00
275cc13ea7 start 0.9.0 2025-06-27 10:08:56 +06:00
cdcdb4ef00 Merge pull request #27 from InsanusMokrassar/0.8.0
0.8.0
2024-11-06 20:50:52 +06:00
aeb3bf1f3d ratings auto clearup 2024-11-06 20:48:21 +06:00
569f15330c fixes after update 2024-11-06 20:04:54 +06:00
defc83740d update dependencies 2024-09-22 18:53:55 +06:00
66b2b21c1c start 0.8.0 2024-09-22 18:43:21 +06:00
63800ce19c Merge pull request #26 from InsanusMokrassar/0.7.0
0.7.0
2024-09-22 18:42:43 +06:00
c2612ec6e9 Revert "rework to use just Java target"
This reverts commit 0273284023.
2024-09-07 22:58:56 +06:00
0273284023 rework to use just Java target 2024-09-07 22:55:33 +06:00
9af72d5d0f fix build 2024-09-07 22:42:13 +06:00
b879426f9d Update mppJsProject.gradle 2024-08-13 02:11:33 +06:00
18b8ce0d3d Update mppProjectWithSerialization.gradle 2024-08-13 02:10:58 +06:00
a4686a56df update dependencies 2024-08-13 00:08:21 +06:00
e567f5b3b4 update dependencies 2024-07-13 21:39:37 +06:00
3941c1dfed start 0.7.0 2024-07-13 21:28:37 +06:00
0bdd965a4a Merge pull request #25 from InsanusMokrassar/0.6.0
0.6.0
2024-07-10 22:08:34 +06:00
ad967c002a update github ci/cd scripts 2024-06-28 13:28:12 +06:00
e3958aff3f update dependencies 2024-06-28 13:24:13 +06:00
ae6993efba start 0.6.0 2024-06-28 13:09:37 +06:00
991b0ef3d3 improvements in cachedratingsrepo 2024-05-13 21:42:57 +06:00
aa6c62d66e fixes in CachedRatingsRepo 2024-05-13 21:25:13 +06:00
e6a0d67444 improve of autoclear 2024-05-13 19:57:12 +06:00
16bd62da51 Merge pull request #24 from InsanusMokrassar/0.5.6
0.5.6
2024-05-13 19:43:19 +06:00
44 changed files with 453 additions and 246 deletions

View File

@@ -14,6 +14,7 @@ jobs:
java-version: 17
- name: Rewrite version
run: |
printf "\norg.gradle.jvmargs=-Xmx1g -Xms500m\nkotlin.daemon.jvmargs=-Xmx1g -Xms500m\norg.gradle.daemon=false" >> gradle.properties
branch="`echo "${{ github.ref }}" | grep -o "[^/]*$"`"
cat gradle.properties | sed -e "s/^version=\([0-9\.]*\)/version=\1-branch_$branch-build${{ github.run_number }}/" > gradle.properties.tmp
rm gradle.properties

View File

@@ -16,6 +16,7 @@ jobs:
java-version: 17
- name: Rewrite version
run: |
printf "\norg.gradle.jvmargs=-Xmx1g -Xms500m\nkotlin.daemon.jvmargs=-Xmx1g -Xms500m\norg.gradle.daemon=false" >> gradle.properties
branch="`echo "${{ github.ref }}" | grep -o "[^/]*$"`"
if [[ "$branch" != "master" ]]; then
cat gradle.properties | sed -e "s/^version=\([0-9\.]*\)/version=\1-branch_$branch-build${{ github.run_number }}/" > gradle.properties.tmp

1
.gitignore vendored
View File

@@ -1,4 +1,5 @@
.idea
.kotlin/
out/*
*.iml
target

View File

@@ -1,5 +1,37 @@
# PlaguPoster
## 0.11.1
* Update `PlaguBot` dependency to allow using of `proxy` section in config
## 0.11.0
## 0.10.1
* Dependencies update
## 0.10.0
* Dependencies update
## 0.9.0
* Dependencies update
## 0.8.0
* Dependencies update
* `Ratings`:
* Add autoclearing of ratings without target posts each half hours
## 0.7.0
* Dependencies update
## 0.6.0
* Dependencies update
## 0.5.6
* `Ratings`:

View File

@@ -2,23 +2,41 @@ buildscript {
repositories {
google()
mavenCentral()
mavenLocal()
maven { url "https://plugins.gradle.org/m2/" }
mavenLocal()
}
dependencies {
classpath libs.kotlin.gradle.plugin
classpath libs.kotlin.serialization.plugin
classpath libs.kotlin.dokka.plugin
classpath libs.github.release
}
}
plugins {
alias(libs.plugins.nmcp.aggregation)
}
if ((project.hasProperty('SONATYPE_USER') || System.getenv('SONATYPE_USER') != null) && (project.hasProperty('SONATYPE_PASSWORD') || System.getenv('SONATYPE_PASSWORD') != null)) {
nmcpAggregation {
centralPortal {
username = project.hasProperty('SONATYPE_USER') ? project.property('SONATYPE_USER') : System.getenv('SONATYPE_USER')
password = project.hasProperty('SONATYPE_PASSWORD') ? project.property('SONATYPE_PASSWORD') : System.getenv('SONATYPE_PASSWORD')
validationTimeout = Duration.ofHours(4)
publishingType = System.getenv('PUBLISHING_TYPE') != "" ? System.getenv('PUBLISHING_TYPE') : "USER_MANAGED"
}
publishAllProjectsProbablyBreakingProjectIsolation()
}
}
allprojects {
repositories {
mavenLocal()
mavenCentral()
google()
maven { url "https://nexus.inmo.dev/repository/maven-releases/" }
mavenLocal()
}
}

0
changelog_parser.sh Normal file → Executable file
View File

View File

@@ -11,15 +11,14 @@ import kotlinx.coroutines.SupervisorJob
import kotlinx.serialization.json.JsonObject
import kotlinx.serialization.json.JsonPrimitive
import kotlinx.serialization.json.booleanOrNull
import org.jetbrains.exposed.sql.Database
import org.koin.core.Koin
import org.koin.core.module.Module
object CommonPlugin : Plugin {
private val Log = logger
override fun Module.setupDI(database: Database, params: JsonObject) {
override fun Module.setupDI(config: JsonObject) {
single { CoroutineScope(Dispatchers.Default + SupervisorJob()) }
val useCache = (params["useCache"] as? JsonPrimitive) ?.booleanOrNull ?: true
val useCache = (config["useCache"] as? JsonPrimitive) ?.booleanOrNull ?: true
useCache(useCache)
}

View File

@@ -11,21 +11,28 @@ private String getCurrentVersionChangelog() {
return changelogDataOS.toString().trim()
}
if (new File(projectDir, "secret.gradle").exists()) {
apply from: './secret.gradle'
def githubTokenVariableName = "GITHUB_RELEASE_TOKEN"
def githubTokenVariableFromEnv = System.getenv(githubTokenVariableName)
def secretFile = new File(projectDir, "secret.gradle")
if (secretFile.exists() || project.hasProperty(githubTokenVariableName) || (githubTokenVariableFromEnv != "" && githubTokenVariableFromEnv != null)) {
if (secretFile.exists()) {
apply from: './secret.gradle'
}
apply plugin: "com.github.breadmoirai.github-release"
def githubReleaseToken = project.hasProperty(githubTokenVariableName) ? project.property(githubTokenVariableName).toString() : githubTokenVariableFromEnv
githubRelease {
token "${project.property('GITHUB_RELEASE_TOKEN')}"
token githubReleaseToken
owner "InsanusMokrassar"
repo "PlaguPoster"
owner = "InsanusMokrassar"
repo = "PlaguPoster"
tagName "v${project.version}"
releaseName "${project.version}"
targetCommitish "${project.version}"
tagName = "v${project.version}"
releaseName = "${project.version}"
targetCommitish = "${project.version}"
body getCurrentVersionChangelog()
body = getCurrentVersionChangelog()
}
}

View File

@@ -1,5 +1,4 @@
kotlin.code.style=official
org.gradle.jvmargs=-Xmx1024m
org.gradle.parallel=true
kotlin.js.generate.externals=true
kotlin.incremental=true
@@ -10,4 +9,4 @@ android.enableJetifier=true
# Project data
group=dev.inmo
version=0.5.6
version=0.11.1

View File

@@ -1,19 +1,23 @@
[versions]
kotlin = "1.9.23"
kotlin-serialization = "1.6.3"
kotlin = "2.3.10"
kotlin-serialization = "1.10.0"
plagubot = "8.3.0"
tgbotapi = "12.0.1"
microutils = "0.20.45"
kslog = "1.3.3"
krontab = "2.2.9"
plagubot-plugins = "0.18.3"
plagubot = "11.1.0"
tgbotapi = "32.0.0"
microutils = "0.29.1"
kslog = "1.6.0"
krontab = "2.8.0"
plagubot-plugins = "0.25.0"
dokka = "1.9.20"
nmcp = "1.4.4"
dokka = "2.0.0"
psql = "42.6.0"
github-release = "2.5.2"
[libraries]
kotlin = { module = "org.jetbrains.kotlin:kotlin-stdlib", version.ref = "kotlin" }
@@ -42,8 +46,11 @@ psql = { module = "org.postgresql:postgresql", version.ref = "psql" }
kotlin-gradle-plugin = { module = "org.jetbrains.kotlin:kotlin-gradle-plugin", version.ref = "kotlin" }
kotlin-serialization-plugin = { module = "org.jetbrains.kotlin:kotlin-serialization", version.ref = "kotlin" }
kotlin-dokka-plugin = { module = "org.jetbrains.dokka:dokka-gradle-plugin", version.ref = "dokka" }
github-release = { module = "com.github.breadmoirai:github-release", version.ref = "github-release" }
[plugins]
kotlin-multiplatform = { id = "org.jetbrains.kotlin.multiplatform", version.ref = "kotlin" }
kotlin-serialization = { id = "org.jetbrains.kotlin.plugin.serialization", version.ref = "kotlin" }
nmcp-aggregation = { id = "com.gradleup.nmcp.aggregation", version.ref = "nmcp" }

View File

@@ -1,5 +1,5 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.4-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.13-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists

View File

@@ -5,7 +5,6 @@ import dev.inmo.kslog.common.w
import dev.inmo.plagubot.Plugin
import dev.inmo.tgbotapi.extensions.behaviour_builder.BehaviourContext
import kotlinx.serialization.json.JsonObject
import org.jetbrains.exposed.sql.Database
import org.koin.core.Koin
import org.koin.core.module.Module
@@ -14,7 +13,7 @@ private val actualPlugin = dev.inmo.plagubot.plugins.inline.queries.Plugin
object Plugin : Plugin by actualPlugin {
private val log = TagLogger("InlinePlugin")
override fun Module.setupDI(database: Database, params: JsonObject) {
override fun Module.setupDI(params: JsonObject) {
single { actualPlugin }
}

View File

@@ -25,7 +25,6 @@ kotlin {
jsTest {
dependencies {
implementation libs.kotlin.test.js
implementation libs.kotlin.test.junit
}
}
}

View File

@@ -4,7 +4,13 @@ project.group = "$group"
apply from: "$publishGradlePath"
kotlin {
jvm()
jvm {
compilations.main {
kotlinOptions {
jvmTarget = "17"
}
}
}
js (IR) {
browser()
nodejs()
@@ -31,10 +37,14 @@ kotlin {
jsTest {
dependencies {
implementation libs.kotlin.test.js
implementation libs.kotlin.test.junit
}
}
}
}
java {
sourceCompatibility = JavaVersion.VERSION_17
targetCompatibility = JavaVersion.VERSION_17
}

View File

@@ -14,6 +14,7 @@ import dev.inmo.plagubot.Plugin
import dev.inmo.plagubot.plugins.inline.queries.models.Format
import dev.inmo.plagubot.plugins.inline.queries.models.OfferTemplate
import dev.inmo.plagubot.plugins.inline.queries.repos.InlineTemplatesRepo
import dev.inmo.plagubot.registerConfig
import dev.inmo.plaguposter.common.ChatConfig
import dev.inmo.plaguposter.posts.models.NewPost
import dev.inmo.plaguposter.posts.models.PostContentInfo
@@ -32,13 +33,13 @@ import dev.inmo.tgbotapi.extensions.utils.extensions.sameMessage
import dev.inmo.tgbotapi.extensions.utils.types.buttons.dataButton
import dev.inmo.tgbotapi.extensions.utils.types.buttons.flatInlineKeyboard
import dev.inmo.tgbotapi.types.MilliSeconds
import dev.inmo.tgbotapi.types.buttons.KeyboardButtonStyle
import dev.inmo.tgbotapi.utils.bold
import kotlinx.coroutines.*
import kotlinx.coroutines.flow.filter
import kotlinx.coroutines.flow.first
import kotlinx.serialization.Serializable
import kotlinx.serialization.json.*
import org.jetbrains.exposed.sql.Database
import org.koin.core.Koin
import org.koin.core.module.Module
@@ -49,10 +50,8 @@ object Plugin : Plugin {
val throttlingMillis: MilliSeconds = 1000,
val doFullCheck: Boolean = false
)
override fun Module.setupDI(database: Database, params: JsonObject) {
params["messagesChecker"] ?.let { element ->
single { get<Json>().decodeFromJsonElement(Config.serializer(), element) }
}
override fun Module.setupDI(config: JsonObject) {
registerConfig<Config>("messagesChecker") { null }
}
private val gcLogger = KSLog("GarbageCollector")
@@ -150,7 +149,7 @@ object Plugin : Plugin {
message,
text = "Are you sure want to trigger posts garbage collecting?",
replyMarkup = flatInlineKeyboard {
dataButton("Sure", yesData)
dataButton("Sure", yesData, style = KeyboardButtonStyle.Primary)
dataButton("No", noData)
}
)

View File

@@ -1,15 +1,15 @@
package dev.inmo.plaguposter.posts.panel
import com.benasher44.uuid.uuid4
import dev.inmo.micro_utils.coroutines.runCatchingLogging
import dev.inmo.micro_utils.coroutines.runCatchingSafely
import dev.inmo.micro_utils.coroutines.subscribeLoggingDropExceptions
import dev.inmo.micro_utils.coroutines.subscribeSafelyWithoutExceptions
import dev.inmo.micro_utils.koin.getAllDistinct
import dev.inmo.micro_utils.repos.*
import dev.inmo.micro_utils.repos.cache.cache.FullKVCache
import dev.inmo.micro_utils.repos.cache.cached
import dev.inmo.micro_utils.repos.cache.full.cached
import dev.inmo.micro_utils.repos.cache.full.fullyCached
import dev.inmo.plagubot.Plugin
import dev.inmo.plagubot.registerConfig
import dev.inmo.plaguposter.common.ChatConfig
import dev.inmo.plaguposter.common.UnsuccessfulSymbol
import dev.inmo.plaguposter.common.useCache
@@ -34,6 +34,7 @@ import dev.inmo.tgbotapi.types.MessageId
import dev.inmo.tgbotapi.types.ReplyParameters
import dev.inmo.tgbotapi.types.buttons.InlineKeyboardButtons.CallbackDataInlineKeyboardButton
import dev.inmo.tgbotapi.types.buttons.InlineKeyboardMarkup
import dev.inmo.tgbotapi.types.buttons.KeyboardButtonStyle
import dev.inmo.tgbotapi.types.message.ParseMode
import dev.inmo.tgbotapi.utils.bold
import dev.inmo.tgbotapi.utils.buildEntities
@@ -41,7 +42,6 @@ import dev.inmo.tgbotapi.utils.italic
import kotlinx.coroutines.flow.first
import kotlinx.serialization.Serializable
import kotlinx.serialization.json.*
import org.jetbrains.exposed.sql.Database
import org.koin.core.Koin
import org.koin.core.module.Module
@@ -55,10 +55,8 @@ object Plugin : Plugin {
val rootButtonText: String = "◀️",
val refreshButtonText: String? = "\uD83D\uDD04"
)
override fun Module.setupDI(database: Database, params: JsonObject) {
params["panel"] ?.let { element ->
single { get<Json>().decodeFromJsonElement(Config.serializer(), element) }
}
override fun Module.setupDI(params: JsonObject) {
registerConfig<Config>("panel") { null }
single {
val config = getOrNull<Config>() ?: Config()
val builtInButtons = listOfNotNull(
@@ -74,7 +72,8 @@ object Plugin : Plugin {
PanelButtonBuilder {
CallbackDataInlineKeyboardButton(
text,
"refresh ${it.id.string}"
"refresh ${it.id.string}",
style = KeyboardButtonStyle.Primary
)
}
}
@@ -102,7 +101,7 @@ object Plugin : Plugin {
basePostsMessages
}
postsRepo.newObjectsFlow.subscribeSafelyWithoutExceptions(this) {
postsRepo.newObjectsFlow.subscribeLoggingDropExceptions(this) {
val firstContent = it.content.first()
val buttons = api.buttonsBuilders.chunked(config.buttonsPerRow).mapNotNull { row ->
row.mapNotNull { builder ->
@@ -167,7 +166,7 @@ object Plugin : Plugin {
edit(
query.message,
replyMarkup = flatInlineKeyboard {
dataButton("\uD83D\uDDD1", approveData)
dataButton("\uD83D\uDDD1", approveData, style = KeyboardButtonStyle.Danger)
api.RootPanelButtonBuilder.buildButton(post) ?.let(::add)
}
)
@@ -189,7 +188,7 @@ object Plugin : Plugin {
val postId = query.data.removePrefix("refresh ").let(::PostId)
val (chatId, messageId) = postsMessages.get(postId) ?: return@onMessageDataCallbackQuery
runCatchingSafely {
runCatching {
refreshPostMessage(
postId,
chatId,

View File

@@ -8,7 +8,7 @@ import dev.inmo.tgbotapi.types.*
import kotlinx.serialization.builtins.PairSerializer
import kotlinx.serialization.builtins.serializer
import kotlinx.serialization.json.Json
import org.jetbrains.exposed.sql.Database
import org.jetbrains.exposed.v1.jdbc.Database
private val ChatIdToMessageSerializer = PairSerializer(FullChatIdentifierSerializer, MessageId.serializer())

View File

@@ -40,13 +40,13 @@ class PostPublisher(
contents.singleOrNull() ?.also {
targetChatIds.forEach { targetChatId ->
runCatching {
bot.copyMessage(targetChatId, it.chatId, it.messageId)
bot.copyMessage(fromChatId = it.chatId, messageId = it.messageId, toChatId = targetChatId)
}.onFailure { _ ->
runCatching {
bot.forwardMessage(
fromChatId = it.chatId,
toChatId = cachingChatId,
messageId = it.messageId
messageId = it.messageId,
toChatId = cachingChatId
)
}.onSuccess {
bot.copyMessage(targetChatId, it)

View File

@@ -7,6 +7,7 @@ import dev.inmo.micro_utils.coroutines.subscribeSafelyWithoutExceptions
import dev.inmo.micro_utils.koin.singleWithBinds
import dev.inmo.micro_utils.repos.deleteById
import dev.inmo.plagubot.Plugin
import dev.inmo.plagubot.database
import dev.inmo.plaguposter.common.SuccessfulSymbol
import dev.inmo.plaguposter.common.UnsuccessfulSymbol
import dev.inmo.plaguposter.posts.exposed.ExposedPostsRepo
@@ -23,10 +24,9 @@ import dev.inmo.tgbotapi.extensions.api.edit.edit
import dev.inmo.tgbotapi.extensions.api.send.reply
import dev.inmo.tgbotapi.extensions.behaviour_builder.BehaviourContext
import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onCommand
import dev.inmo.tgbotapi.types.message.textsources.regular
import dev.inmo.tgbotapi.types.message.textsources.regularTextSource
import kotlinx.serialization.Serializable
import kotlinx.serialization.json.*
import org.jetbrains.exposed.sql.Database
import org.koin.core.Koin
import org.koin.core.module.Module
@@ -37,8 +37,8 @@ object Plugin : Plugin {
val autoRemoveMessages: Boolean = true,
val deleteAfterPublishing: Boolean = true
)
override fun Module.setupDI(database: Database, params: JsonObject) {
val configJson = params["posts"] ?: this@Plugin.let {
override fun Module.setupDI(config: JsonObject) {
val configJson = config["posts"] ?: this@Plugin.let {
it.logger.w {
"Unable to load posts plugin due to absence of `posts` key in config"
}
@@ -90,9 +90,9 @@ object Plugin : Plugin {
postsRepo.deleteById(postId)
if (postsRepo.contains(postId)) {
edit(it, it.content.textSources + regular(UnsuccessfulSymbol))
edit(it, it.content.textSources + regularTextSource(UnsuccessfulSymbol))
} else {
edit(it, it.content.textSources + regular(SuccessfulSymbol))
edit(it, it.content.textSources + regularTextSource(SuccessfulSymbol))
}
}

View File

@@ -5,7 +5,11 @@ import dev.inmo.micro_utils.repos.KeyValuesRepo
import dev.inmo.micro_utils.repos.exposed.*
import dev.inmo.plaguposter.posts.models.*
import dev.inmo.tgbotapi.types.*
import org.jetbrains.exposed.sql.*
import org.jetbrains.exposed.v1.core.Column
import org.jetbrains.exposed.v1.core.ReferenceOption
import org.jetbrains.exposed.v1.core.ResultRow
import org.jetbrains.exposed.v1.core.Table
import org.jetbrains.exposed.v1.jdbc.Database
internal class ExposedContentInfoRepo(
override val database: Database,

View File

@@ -13,11 +13,19 @@ import dev.inmo.tgbotapi.types.IdChatIdentifier
import dev.inmo.tgbotapi.types.MessageId
import kotlinx.coroutines.flow.*
import kotlinx.serialization.json.Json
import org.jetbrains.exposed.sql.*
import org.jetbrains.exposed.sql.SqlExpressionBuilder.eq
import org.jetbrains.exposed.sql.SqlExpressionBuilder.inList
import org.jetbrains.exposed.sql.statements.*
import org.jetbrains.exposed.sql.transactions.transaction
import org.jetbrains.exposed.v1.core.Op
import org.jetbrains.exposed.v1.core.ResultRow
import org.jetbrains.exposed.v1.core.and
import org.jetbrains.exposed.v1.core.eq
import org.jetbrains.exposed.v1.core.inList
import org.jetbrains.exposed.v1.core.isNull
import org.jetbrains.exposed.v1.core.statements.InsertStatement
import org.jetbrains.exposed.v1.core.statements.UpdateBuilder
import org.jetbrains.exposed.v1.jdbc.Database
import org.jetbrains.exposed.v1.jdbc.deleteWhere
import org.jetbrains.exposed.v1.jdbc.insert
import org.jetbrains.exposed.v1.jdbc.selectAll
import org.jetbrains.exposed.v1.jdbc.transactions.transaction
class ExposedPostsRepo(
override val database: Database
@@ -37,8 +45,8 @@ class ExposedPostsRepo(
override val primaryKey: PrimaryKey = PrimaryKey(idColumn)
override val selectById: ISqlExpressionBuilder.(PostId) -> Op<Boolean> = { idColumn.eq(it.string) }
override val selectByIds: ISqlExpressionBuilder.(List<PostId>) -> Op<Boolean> = { idColumn.inList(it.map { it.string }) }
override val selectById: (PostId) -> Op<Boolean> = { idColumn.eq(it.string) }
override val selectByIds: (List<PostId>) -> Op<Boolean> = { idColumn.inList(it.map { it.string }) }
override val ResultRow.asId: PostId
get() = PostId(get(idColumn))
override val ResultRow.asObject: RegisteredPost
@@ -48,7 +56,7 @@ class ExposedPostsRepo(
id,
DateTime(get(createdColumn)),
with(contentRepo) {
select { postIdColumn.eq(id.string) }.map {
selectAll().where { postIdColumn.eq(id.string) }.map {
it.asObject
}
}
@@ -69,14 +77,14 @@ class ExposedPostsRepo(
id,
DateTime(get(createdColumn)),
with(contentRepo) {
select { postIdColumn.eq(id.string) }.map {
selectAll().where { postIdColumn.eq(id.string) }.map {
it.asObject
}
}
)
}
override fun createAndInsertId(value: NewPost, it: InsertStatement<Number>): PostId {
override fun createAndInsertId(value: NewPost, it: UpdateBuilder<Int>): PostId {
val id = PostId(uuid4().toString())
it[idColumn] = id.string
return id
@@ -104,7 +112,7 @@ class ExposedPostsRepo(
}
}
override fun insert(value: NewPost, it: InsertStatement<Number>) {
override fun insert(value: NewPost, it: UpdateBuilder<Int>) {
super.insert(value, it)
it[createdColumn] = DateTime.now().unixMillis
}
@@ -132,8 +140,8 @@ class ExposedPostsRepo(
}.associateBy { it.id }
val existsIds = posts.keys.toList()
transaction(db = database) {
val deleted = deleteWhere(null, null) {
selectByIds(it, existsIds)
val deleted = deleteWhere {
selectByIds(existsIds)
}
with(contentRepo) {
deleteWhere {
@@ -144,7 +152,7 @@ class ExposedPostsRepo(
existsIds
} else {
existsIds.filter {
select { selectById(it) }.limit(1).none()
selectAll().where { selectById(it) }.limit(1).none()
}
}
}.forEach {
@@ -156,7 +164,7 @@ class ExposedPostsRepo(
override suspend fun getIdByChatAndMessage(chatId: IdChatIdentifier, messageId: MessageId): PostId? {
return transaction(database) {
with(contentRepo) {
select {
selectAll().where {
chatIdColumn.eq(chatId.chatId.long)
.and(chatId.threadId ?.let { threadIdColumn.eq(it.long) } ?: threadIdColumn.isNull())
.and(messageIdColumn.eq(messageId.long))
@@ -166,12 +174,12 @@ class ExposedPostsRepo(
}
override suspend fun getPostCreationTime(postId: PostId): DateTime? = transaction(database) {
select { selectById(postId) }.limit(1).firstOrNull() ?.get(createdColumn) ?.let(::DateTime)
selectAll().where { selectById(postId) }.limit(1).firstOrNull() ?.get(createdColumn) ?.let(::DateTime)
}
override suspend fun getFirstMessageInfo(postId: PostId): PostContentInfo? = transaction(database) {
with(contentRepo) {
select { postIdColumn.eq(postId.string) }.limit(1).firstOrNull() ?.asObject
selectAll().where { postIdColumn.eq(postId.string) }.limit(1).firstOrNull() ?.asObject
}
}
}

View File

@@ -22,6 +22,7 @@ import dev.inmo.tgbotapi.extensions.utils.extensions.sameChat
import dev.inmo.tgbotapi.extensions.utils.extensions.sameMessage
import dev.inmo.tgbotapi.extensions.utils.textContentOrNull
import dev.inmo.tgbotapi.extensions.utils.types.buttons.*
import dev.inmo.tgbotapi.types.buttons.KeyboardButtonStyle
import dev.inmo.tgbotapi.utils.regular
import kotlinx.coroutines.async
import kotlinx.coroutines.flow.*
@@ -56,7 +57,8 @@ object Plugin : Plugin {
flatInlineKeyboard {
dataButton(
"Finish",
buttonUuid
buttonUuid,
style = KeyboardButtonStyle.Primary,
)
}
} else {

View File

@@ -1,3 +1,4 @@
apply plugin: 'maven-publish'
task javadocsJar(type: Jar) {
@@ -19,46 +20,30 @@ publishing {
}
developers {
developer {
id = "InsanusMokrassar"
name = "Aleksei Ovsiannikov"
email = "ovsyannikov.alexey95@gmail.com"
}
developer {
id = "InsanusMokrassar"
name = "Aleksei Ovsiannikov"
email = "ovsyannikov.alexey95@gmail.com"
}
}
licenses {
}
}
repositories {
if (project.hasProperty('GITEA_TOKEN') || System.getenv('GITEA_TOKEN') != null) {
maven {
name = "Gitea"
url = uri("https://git.inmo.dev/api/packages/InsanusMokrassar/maven")
credentials(HttpHeaderCredentials) {
name = "Authorization"
value = project.hasProperty('GITEA_TOKEN') ? project.property('GITEA_TOKEN') : System.getenv('GITEA_TOKEN')
}
authentication {
header(HttpHeaderAuthentication)
}
license {
name = "MIT License"
url = "https://opensource.org/licenses/MIT"
}
}
if ((project.hasProperty('SONATYPE_USER') || System.getenv('SONATYPE_USER') != null) && (project.hasProperty('SONATYPE_PASSWORD') || System.getenv('SONATYPE_PASSWORD') != null)) {
maven {
name = "sonatype"
url = uri("https://oss.sonatype.org/service/local/staging/deploy/maven2/")
credentials {
username = project.hasProperty('SONATYPE_USER') ? project.property('SONATYPE_USER') : System.getenv('SONATYPE_USER')
password = project.hasProperty('SONATYPE_PASSWORD') ? project.property('SONATYPE_PASSWORD') : System.getenv('SONATYPE_PASSWORD')
}
}
}
repositories {
if ((project.hasProperty('INMONEXUS_USER') || System.getenv('INMONEXUS_USER') != null) && (project.hasProperty('INMONEXUS_PASSWORD') || System.getenv('INMONEXUS_PASSWORD') != null)) {
maven {
name = "InmoNexus"
url = uri("https://nexus.inmo.dev/repository/maven-releases/")
credentials {
username = project.hasProperty('INMONEXUS_USER') ? project.property('INMONEXUS_USER') : System.getenv('INMONEXUS_USER')
password = project.hasProperty('INMONEXUS_PASSWORD') ? project.property('INMONEXUS_PASSWORD') : System.getenv('INMONEXUS_PASSWORD')
}
}
}
@@ -67,13 +52,13 @@ publishing {
if (project.hasProperty("signing.gnupg.keyName")) {
apply plugin: 'signing'
signing {
useGpgCmd()
sign publishing.publications
}
task signAll {
tasks.withType(Sign).forEach {
dependsOn(it)

View File

@@ -1 +1 @@
{"licenses":[],"mavenConfig":{"name":"${project.name}","description":"${project.name}","url":"https://github.com/InsanusMokrassar/PlaguPoster","vcsUrl":"https://github.com/InsanusMokrassar/PlaguPoster.git","developers":[{"id":"InsanusMokrassar","name":"Aleksei Ovsiannikov","eMail":"ovsyannikov.alexey95@gmail.com"}],"repositories":[{"name":"Gitea","url":"https://git.inmo.dev/api/packages/InsanusMokrassar/maven","credsType":{"type":"dev.inmo.kmppscriptbuilder.core.models.MavenPublishingRepository.CredentialsType.HttpHeaderCredentials","headerName":"Authorization","headerValueProperty":"GITEA_TOKEN"}},{"name":"sonatype","url":"https://oss.sonatype.org/service/local/staging/deploy/maven2/"}],"gpgSigning":{"type":"dev.inmo.kmppscriptbuilder.core.models.GpgSigning.Optional"}}}
{"licenses":[{"id":"MIT","title":"MIT License","url":"https://opensource.org/licenses/MIT"}],"mavenConfig":{"name":"${project.name}","description":"${project.name}","url":"https://github.com/InsanusMokrassar/PlaguPoster","vcsUrl":"https://github.com/InsanusMokrassar/PlaguPoster.git","developers":[{"id":"InsanusMokrassar","name":"Aleksei Ovsiannikov","eMail":"ovsyannikov.alexey95@gmail.com"}],"repositories":[{"name":"InmoNexus","url":"https://nexus.inmo.dev/repository/maven-releases/"}],"gpgSigning":{"type":"dev.inmo.kmppscriptbuilder.core.models.GpgSigning.Optional"}}}

View File

@@ -11,6 +11,7 @@ kotlin {
dependencies {
api project(":plaguposter.common")
api project(":plaguposter.posts")
api libs.krontab
}
}
}

View File

@@ -5,6 +5,8 @@ import korlibs.time.seconds
import dev.inmo.krontab.KrontabTemplate
import dev.inmo.krontab.toSchedule
import dev.inmo.krontab.utils.asFlowWithDelays
import dev.inmo.kslog.common.KSLog
import dev.inmo.kslog.common.i
import dev.inmo.micro_utils.coroutines.runCatchingSafely
import dev.inmo.micro_utils.coroutines.subscribeSafelyWithoutExceptions
import dev.inmo.micro_utils.koin.singleWithRandomQualifier
@@ -13,6 +15,7 @@ import dev.inmo.plagubot.Plugin
import dev.inmo.plagubot.plugins.inline.queries.models.Format
import dev.inmo.plagubot.plugins.inline.queries.models.OfferTemplate
import dev.inmo.plagubot.plugins.inline.queries.repos.InlineTemplatesRepo
import dev.inmo.plagubot.registerConfig
import dev.inmo.plaguposter.posts.models.PostId
import dev.inmo.plaguposter.posts.repo.PostsRepo
import dev.inmo.plaguposter.ratings.models.Rating
@@ -22,7 +25,6 @@ import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onComman
import dev.inmo.tgbotapi.types.Seconds
import kotlinx.serialization.Serializable
import kotlinx.serialization.json.*
import org.jetbrains.exposed.sql.Database
import org.koin.core.Koin
import org.koin.core.module.Module
@@ -39,8 +41,8 @@ object Plugin : Plugin {
val skipPostAge: Seconds? = null
)
}
override fun Module.setupDI(database: Database, params: JsonObject) {
single { get<Json>().decodeFromJsonElement(Config.serializer(), params["gc"] ?: return@single null) }
override fun Module.setupDI(params: JsonObject) {
registerConfig<Config>("gc") { null }
}
override suspend fun BehaviourContext.setupBotPlugin(koin: Koin) {
@@ -64,14 +66,23 @@ object Plugin : Plugin {
}
}
config.autoclear ?.let { autoclear ->
val autoClearLogger = KSLog("autoclear")
suspend fun doAutoClear() {
autoClearLogger.i { "Start autoclear" }
val dropCreatedBefore = DateTime.now() - (autoclear.skipPostAge ?: 0).seconds
ratingsRepo.getPostsWithRatingLessEq(autoclear.rating).keys.forEach {
autoClearLogger.i { "Posts drop created before: ${dropCreatedBefore.toStringDefault()}" }
val idsToDelete = ratingsRepo.getPostsWithRatingLessEq(autoclear.rating).keys.also {
autoClearLogger.i { "Selected posts by rating: $it" }
}.filter {
val postCreationDateTime = postsRepo.getPostCreationTime(it) ?: (dropCreatedBefore - 1.seconds) // do dropping if post creation time is not available
if (postCreationDateTime < dropCreatedBefore) {
ratingsRepo.unset(it)
postsRepo.deleteById(it)
}
postCreationDateTime < dropCreatedBefore
}
autoClearLogger.i { "Filtered posts by datetime: $idsToDelete" }
if (idsToDelete.isNotEmpty()) {
runCatching { ratingsRepo.unset(idsToDelete) }
autoClearLogger.i { "Ratings dropped" }
runCatching { postsRepo.deleteById(idsToDelete) }
autoClearLogger.i { "Posts dropped" }
}
}
runCatchingSafely {

View File

@@ -4,17 +4,17 @@ import dev.inmo.micro_utils.repos.KeyValueRepo
import dev.inmo.micro_utils.repos.exposed.keyvalue.ExposedKeyValueRepo
import dev.inmo.micro_utils.repos.mappers.withMapper
import dev.inmo.plagubot.Plugin
import dev.inmo.plagubot.registerConfig
import dev.inmo.plaguposter.posts.models.PostId
import dev.inmo.plaguposter.ratings.selector.models.SelectorConfig
import korlibs.time.DateTime
import kotlinx.serialization.json.*
import org.jetbrains.exposed.sql.Database
import org.koin.core.module.Module
import org.koin.core.qualifier.qualifier
object Plugin : Plugin {
override fun Module.setupDI(database: Database, params: JsonObject) {
single { get<Json>().decodeFromJsonElement(SelectorConfig.serializer(), params["selector"] ?: return@single null) }
override fun Module.setupDI(config: JsonObject) {
registerConfig<SelectorConfig>("selector") { null }
single<KeyValueRepo<PostId, DateTime>>(qualifier("latestChosenRepo")) {
ExposedKeyValueRepo(
get(),

View File

@@ -122,7 +122,7 @@ suspend fun BehaviourContext.includeRootNavigationButtonsHandler(
edit(
it.message,
onPageUpdate(SimplePagination(page, size), args.drop(3).toTypedArray()) ?: return@runCatchingSafely
replyMarkup = onPageUpdate(SimplePagination(page, size), args.drop(3).toTypedArray()) ?: return@runCatchingSafely
)
}
@@ -155,7 +155,7 @@ suspend fun BehaviourContext.includeRootNavigationButtonsHandler(
if (prefix == RootButtonsShowRatingData) {
runCatchingSafely {
val rating = ratingRaw.toDoubleOrNull() ?: return@runCatchingSafely
edit(it.message, ratingsRepo.buildRatingButtons(postsRepo, Rating(rating)))
edit(it.message, replyMarkup = ratingsRepo.buildRatingButtons(postsRepo, Rating(rating)))
}
answer(it)

View File

@@ -3,6 +3,8 @@ package dev.inmo.plaguposter.ratings.source
import com.benasher44.uuid.uuid4
import dev.inmo.kslog.common.e
import dev.inmo.kslog.common.logger
import dev.inmo.micro_utils.common.fixed
import dev.inmo.micro_utils.coroutines.runCatchingLogging
import dev.inmo.micro_utils.coroutines.runCatchingSafely
import dev.inmo.micro_utils.coroutines.subscribeSafelyWithoutExceptions
import dev.inmo.micro_utils.pagination.firstPageWithOneElementPagination
@@ -10,10 +12,12 @@ import dev.inmo.micro_utils.repos.id
import dev.inmo.micro_utils.repos.pagination.getAll
import dev.inmo.micro_utils.repos.set
import dev.inmo.plagubot.Plugin
import dev.inmo.plagubot.database
import dev.inmo.plaguposter.common.*
import dev.inmo.plagubot.plugins.inline.queries.models.Format
import dev.inmo.plagubot.plugins.inline.queries.models.OfferTemplate
import dev.inmo.plagubot.plugins.inline.queries.repos.InlineTemplatesRepo
import dev.inmo.plagubot.registerConfig
import dev.inmo.plaguposter.posts.models.PostId
import dev.inmo.plaguposter.posts.panel.PanelButtonBuilder
import dev.inmo.plaguposter.posts.panel.PanelButtonsAPI
@@ -28,6 +32,7 @@ import dev.inmo.plaguposter.ratings.utils.postsByRatings
import dev.inmo.tgbotapi.extensions.api.answers.answer
import dev.inmo.tgbotapi.extensions.api.delete
import dev.inmo.tgbotapi.extensions.api.edit.edit
import dev.inmo.tgbotapi.extensions.api.send.polls.sendRegularPoll
import dev.inmo.tgbotapi.extensions.api.send.reply
import dev.inmo.tgbotapi.extensions.api.send.send
import dev.inmo.tgbotapi.extensions.behaviour_builder.BehaviourContext
@@ -39,20 +44,28 @@ import dev.inmo.tgbotapi.extensions.utils.types.buttons.flatInlineKeyboard
import dev.inmo.tgbotapi.extensions.utils.types.buttons.inlineKeyboard
import dev.inmo.tgbotapi.types.ReplyParameters
import dev.inmo.tgbotapi.types.buttons.InlineKeyboardButtons.CallbackDataInlineKeyboardButton
import dev.inmo.tgbotapi.types.message.textsources.bold
import dev.inmo.tgbotapi.types.message.textsources.regular
import dev.inmo.tgbotapi.types.buttons.KeyboardButtonStyle
import dev.inmo.tgbotapi.types.keyboardButtonRequestUserLimit
import dev.inmo.tgbotapi.types.message.textsources.boldTextSource
import dev.inmo.tgbotapi.types.message.textsources.regularTextSource
import dev.inmo.tgbotapi.types.polls.InputPollOption
import dev.inmo.tgbotapi.types.polls.PollOption
import dev.inmo.tgbotapi.utils.bold
import dev.inmo.tgbotapi.utils.buildEntities
import dev.inmo.tgbotapi.utils.extensions.makeSourceString
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.first
import kotlinx.serialization.Serializable
import kotlinx.serialization.json.*
import org.jetbrains.exposed.sql.Database
import org.koin.core.Koin
import org.koin.core.module.Module
import org.koin.core.qualifier.named
import kotlin.math.roundToInt
object Plugin : Plugin {
private val ratingVariantsQualifier = named("ratingsVariants")
private const val attachedSymbolControlSymbol = "\${attached_symbol}"
private const val currentRatingControlSymbol = "\${rating}"
@Serializable
internal data class Config(
@@ -60,13 +73,13 @@ object Plugin : Plugin {
val variants: RatingsVariants,
val autoAttach: Boolean,
val ratingOfferText: String,
val panelButtonText: String = "Ratings"
val panelButtonText: String = "Ratings $attachedSymbolControlSymbol",
val ratingEnabledStyle: KeyboardButtonStyle? = null,
val ratingDisabledStyle: KeyboardButtonStyle? = null,
)
override fun Module.setupDI(database: Database, params: JsonObject) {
single {
get<Json>().decodeFromJsonElement(Config.serializer(), params["ratingsPolls"] ?: error("Unable to load config for rating polls in $params"))
}
override fun Module.setupDI(params: JsonObject) {
registerConfig<Config>("ratingsPolls")
single<RatingsVariants>(ratingVariantsQualifier) { get<Config>().variants }
single { ExposedPollsToPostsIdsRepo(database) }
@@ -109,12 +122,22 @@ object Plugin : Plugin {
val panelApi = koin.getOrNull<PanelButtonsAPI>()
val chatConfig = koin.get<ChatConfig>()
val panelApiOnPollUpdatesUpdateTrigger: suspend (PostId) -> Unit = if (config.panelButtonText.contains(currentRatingControlSymbol)) {
{
panelApi ?.forceRefresh(it)
}
} else {
{
}
}
onPollUpdates (markerFactory = { it.id }) { poll ->
val postId = pollsToPostsIdsRepo.get(poll.id) ?: return@onPollUpdates
val newRating = poll.options.sumOf {
(variantsTransformer(it.text) ?.double ?.times(it.votes)) ?: 0.0
(variantsTransformer(it.textSources.makeSourceString()) ?.double ?.times(it.votes)) ?: 0.0
}
ratingsRepo.set(postId, Rating(newRating))
panelApiOnPollUpdatesUpdateTrigger(postId)
}
suspend fun attachPoll(postId: PostId): Boolean {
@@ -129,7 +152,9 @@ object Plugin : Plugin {
val sent = send(
content.chatId,
config.ratingOfferText,
config.variants.keys.toList(),
options = config.variants.map {
InputPollOption(it.key)
},
replyParameters = ReplyParameters(content.chatId, content.messageId)
)
pollsToPostsIdsRepo.set(sent.content.poll.id, postId)
@@ -192,17 +217,17 @@ object Plugin : Plugin {
}
if (attachPoll(postId)) {
runCatchingSafely {
runCatchingLogging {
edit(
it,
it.content.textSources + regular(" $SuccessfulSymbol")
it.content.textSources + regularTextSource(" $SuccessfulSymbol")
)
}
} else {
runCatchingSafely {
runCatchingLogging {
edit(
it,
it.content.textSources + regular(" $UnsuccessfulSymbol")
it.content.textSources + regularTextSource(" $UnsuccessfulSymbol")
)
}
}
@@ -227,17 +252,17 @@ object Plugin : Plugin {
if (detachPoll(postId)) {
runCatchingSafely {
runCatchingLogging {
edit(
it,
it.content.textSources + regular(" $SuccessfulSymbol")
it.content.textSources + regularTextSource(" $SuccessfulSymbol")
)
}
} else {
runCatchingSafely {
runCatchingLogging {
edit(
it,
it.content.textSources + regular(" $UnsuccessfulSymbol")
it.content.textSources + regularTextSource(" $UnsuccessfulSymbol")
)
}
}
@@ -265,7 +290,7 @@ object Plugin : Plugin {
onMessageDataCallbackQuery("ratings_interactive", initialFilter = { it.message.chat.id in chatConfig.allSourceChatIds }) {
edit(
it.message,
ratingsRepo.buildRootButtons()
replyMarkup = ratingsRepo.buildRootButtons()
)
}
@@ -289,13 +314,69 @@ object Plugin : Plugin {
panelApi ?.apply {
add(
PanelButtonBuilder {
suspend fun isEnabled() = pollsToPostsIdsRepo.keys(it.id, firstPageWithOneElementPagination).results.any()
val enabled = let { _ ->
var enabled: Boolean? = null
suspend {
enabled ?: (isEnabled().also { enabled = it })
}
}
val rating = let { _ ->
var rating: Rating? = null
suspend {
if (isEnabled()) {
rating ?: ratingsRepo.get(it.id) ?.also { rating = it }
} else {
null
}
}
}
val resultText = config.panelButtonText
.let {
if (it.contains(attachedSymbolControlSymbol)) {
it.replace(
attachedSymbolControlSymbol,
if (enabled()) {
SuccessfulSymbol
} else {
UnsuccessfulSymbol
}
)
} else {
it
}
}
.let {
if (it.contains(currentRatingControlSymbol)) {
val rating = rating()
if (rating != null) {
it.replace(
currentRatingControlSymbol,
rating.double.roundToInt().toString()
)
} else {
it.replace(
currentRatingControlSymbol,
UnsuccessfulSymbol
)
}
} else {
it
}
}
.take(12) // maximal text appropriate for this button
CallbackDataInlineKeyboardButton(
config.panelButtonText + if (pollsToPostsIdsRepo.keys(it.id, firstPageWithOneElementPagination).results.any()) {
SuccessfulSymbol
resultText,
"toggle_ratings ${it.id.string}",
style = if (config.ratingEnabledStyle != null || config.ratingDisabledStyle != null) {
if (enabled()) {
config.ratingEnabledStyle
} else {
config.ratingDisabledStyle
}
} else {
UnsuccessfulSymbol
},
"toggle_ratings ${it.id.string}"
null
}
)
}
)

View File

@@ -4,10 +4,13 @@ import dev.inmo.micro_utils.repos.exposed.initTable
import dev.inmo.micro_utils.repos.exposed.keyvalue.AbstractExposedKeyValueRepo
import dev.inmo.plaguposter.common.ShortMessageInfo
import dev.inmo.tgbotapi.types.*
import org.jetbrains.exposed.sql.*
import org.jetbrains.exposed.sql.SqlExpressionBuilder.eq
import org.jetbrains.exposed.sql.SqlExpressionBuilder.isNull
import org.jetbrains.exposed.sql.statements.*
import org.jetbrains.exposed.v1.core.Op
import org.jetbrains.exposed.v1.core.ResultRow
import org.jetbrains.exposed.v1.core.and
import org.jetbrains.exposed.v1.core.eq
import org.jetbrains.exposed.v1.core.isNull
import org.jetbrains.exposed.v1.core.statements.UpdateBuilder
import org.jetbrains.exposed.v1.jdbc.Database
class ExposedPollsToMessagesInfoRepo(
database: Database
@@ -19,8 +22,8 @@ class ExposedPollsToMessagesInfoRepo(
private val chatIdColumn = long("chat_id")
private val threadIdColumn = long("thread_id").nullable().default(null)
private val messageIdColumn = long("message_id")
override val selectById: ISqlExpressionBuilder.(PollId) -> Op<Boolean> = { keyColumn.eq(it.string) }
override val selectByValue: ISqlExpressionBuilder.(ShortMessageInfo) -> Op<Boolean> = {
override val selectById: (PollId) -> Op<Boolean> = { keyColumn.eq(it.string) }
override val selectByValue: (ShortMessageInfo) -> Op<Boolean> = {
chatIdColumn.eq(it.chatId.chatId.long)
.and(it.chatId.threadId?.let { threadIdColumn.eq(it.long) } ?: threadIdColumn.isNull()).and(
messageIdColumn.eq(it.messageId.long)
@@ -44,7 +47,7 @@ class ExposedPollsToMessagesInfoRepo(
it[messageIdColumn] = v.messageId.long
}
override fun insertKey(k: PollId, v: ShortMessageInfo, it: InsertStatement<Number>) {
override fun insertKey(k: PollId, v: ShortMessageInfo, it: UpdateBuilder<Int>) {
it[keyColumn] = k.string
}
}

View File

@@ -4,16 +4,19 @@ import dev.inmo.micro_utils.repos.exposed.initTable
import dev.inmo.micro_utils.repos.exposed.keyvalue.AbstractExposedKeyValueRepo
import dev.inmo.plaguposter.posts.models.PostId
import dev.inmo.tgbotapi.types.PollId
import org.jetbrains.exposed.sql.*
import org.jetbrains.exposed.sql.statements.*
import org.jetbrains.exposed.v1.core.Op
import org.jetbrains.exposed.v1.core.ResultRow
import org.jetbrains.exposed.v1.core.eq
import org.jetbrains.exposed.v1.core.statements.UpdateBuilder
import org.jetbrains.exposed.v1.jdbc.Database
class ExposedPollsToPostsIdsRepo(
database: Database
) : PollsToPostsIdsRepo, AbstractExposedKeyValueRepo<PollId, PostId>(database, "polls_to_posts") {
override val keyColumn = text("poll_id")
val postIdColumn = text("postId")
override val selectById: ISqlExpressionBuilder.(PollId) -> Op<Boolean> = { keyColumn.eq(it.string) }
override val selectByValue: ISqlExpressionBuilder.(PostId) -> Op<Boolean> = { postIdColumn.eq(it.string) }
override val selectById: (PollId) -> Op<Boolean> = { keyColumn.eq(it.string) }
override val selectByValue: (PostId) -> Op<Boolean> = { postIdColumn.eq(it.string) }
override val ResultRow.asKey: PollId
get() = PollId(get(keyColumn))
override val ResultRow.asObject: PostId
@@ -27,7 +30,7 @@ class ExposedPollsToPostsIdsRepo(
it[postIdColumn] = v.string
}
override fun insertKey(k: PollId, v: PostId, it: InsertStatement<Number>) {
override fun insertKey(k: PollId, v: PostId, it: UpdateBuilder<Int>) {
it[keyColumn] = k.string
}
}

View File

@@ -0,0 +1,18 @@
package dev.inmo.plaguposter.ratings.models
import dev.inmo.krontab.EveryHourScheduler
import dev.inmo.krontab.KrontabTemplate
import dev.inmo.krontab.buildSchedule
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
import kotlinx.serialization.Transient
@Serializable
internal data class RatingsConfig(
@SerialName("manualRecheckKrontab")
val manualRecheckKrontabTemplate: KrontabTemplate = "0 /30 *"
) {
@Transient
val manualRecheckKrontab
get() = buildSchedule(manualRecheckKrontabTemplate)
}

View File

@@ -1,6 +1,8 @@
package dev.inmo.plaguposter.ratings.repo
import dev.inmo.micro_utils.pagination.utils.doForAllWithNextPaging
import dev.inmo.micro_utils.pagination.utils.optionallyReverse
import dev.inmo.micro_utils.pagination.utils.paginate
import dev.inmo.micro_utils.repos.KeyValueRepo
import dev.inmo.micro_utils.repos.MapKeyValueRepo
import dev.inmo.micro_utils.repos.cache.full.FullKeyValueCacheRepo
@@ -13,26 +15,30 @@ class CachedRatingsRepo(
private val scope: CoroutineScope,
private val kvCache: MapKeyValueRepo<PostId, Rating> = MapKeyValueRepo()
) : RatingsRepo, KeyValueRepo<PostId, Rating> by FullKeyValueCacheRepo(base, kvCache, scope) {
private suspend fun getPosts(
reversed: Boolean,
count: Int?,
exclude: List<PostId>,
ratingFilter: (Rating) -> Boolean
): Map<PostId, Rating> {
return kvCache.getAll().filter { (it, rating) ->
it !in exclude && ratingFilter(rating)
}.let {
if (count == null) {
it
} else {
val keys = it.keys.optionallyReverse(reversed).take(count)
keys.associateWith { id -> it.getValue(id) }
}
}
}
override suspend fun getPosts(
range: ClosedRange<Rating>,
reversed: Boolean,
count: Int?,
exclude: List<PostId>
): Map<PostId, Rating> {
val result = mutableMapOf<PostId, Rating>()
doForAllWithNextPaging {
kvCache.keys(it).also {
it.results.forEach {
val rating = get(it) ?: return@forEach
if (it !in exclude && rating in range) {
result[it] = rating
}
}
}
}
return result.toMap()
): Map<PostId, Rating> = getPosts(reversed, count, exclude) {
it in range
}
override suspend fun getPostsWithRatingGreaterEq(
@@ -40,22 +46,16 @@ class CachedRatingsRepo(
reversed: Boolean,
count: Int?,
exclude: List<PostId>
): Map<PostId, Rating> = getPosts(
then .. Rating(Double.MAX_VALUE),
reversed,
count,
exclude
)
): Map<PostId, Rating> = getPosts(reversed, count, exclude) {
it >= then
}
override suspend fun getPostsWithRatingLessEq(
then: Rating,
reversed: Boolean,
count: Int?,
exclude: List<PostId>
): Map<PostId, Rating> = getPosts(
Rating(Double.MIN_VALUE) .. then,
reversed,
count,
exclude
)
): Map<PostId, Rating> = getPosts(reversed, count, exclude) {
it <= then
}
}

View File

@@ -1,21 +1,30 @@
package dev.inmo.plaguposter.ratings
import dev.inmo.krontab.utils.asTzFlowWithDelays
import dev.inmo.kslog.common.TagLogger
import dev.inmo.kslog.common.i
import dev.inmo.micro_utils.coroutines.subscribeSafelyWithoutExceptions
import dev.inmo.micro_utils.koin.singleWithBinds
import dev.inmo.micro_utils.repos.unset
import dev.inmo.plagubot.Plugin
import dev.inmo.plagubot.config
import dev.inmo.plagubot.database
import dev.inmo.plagubot.registerConfig
import dev.inmo.plaguposter.common.useCache
import dev.inmo.plaguposter.posts.repo.PostsRepo
import dev.inmo.plaguposter.ratings.Plugin.setupBotPlugin
import dev.inmo.plaguposter.ratings.exposed.ExposedRatingsRepo
import dev.inmo.plaguposter.ratings.models.RatingsConfig
import dev.inmo.plaguposter.ratings.repo.*
import dev.inmo.tgbotapi.extensions.behaviour_builder.BehaviourContext
import kotlinx.coroutines.CoroutineScope
import kotlinx.serialization.json.*
import org.jetbrains.exposed.sql.Database
import org.koin.core.Koin
import org.koin.core.module.Module
object Plugin : Plugin {
override fun Module.setupDI(database: Database, params: JsonObject) {
private val Log = TagLogger("RatingsPlugin")
override fun Module.setupDI(config: JsonObject) {
single { ExposedRatingsRepo(database) }
singleWithBinds<RatingsRepo> {
val base = get<ExposedRatingsRepo>()
@@ -26,12 +35,24 @@ object Plugin : Plugin {
base
}
}
registerConfig(RatingsConfig.serializer(), "ratings") { RatingsConfig() }
}
override suspend fun BehaviourContext.setupBotPlugin(koin: Koin) {
override suspend fun startPlugin(koin: Koin) {
super.startPlugin(koin)
val config = koin.config<RatingsConfig>()
val scope = koin.get<CoroutineScope>()
val ratingsRepo = koin.get<RatingsRepo>()
koin.get<PostsRepo>().deletedObjectsIdsFlow.subscribeSafelyWithoutExceptions(this) {
val postsRepo = koin.get<PostsRepo>()
postsRepo.deletedObjectsIdsFlow.subscribeSafelyWithoutExceptions(scope) {
ratingsRepo.unset(it)
}
config.manualRecheckKrontab.asTzFlowWithDelays().subscribeSafelyWithoutExceptions(scope) {
Log.i { "Start clearing ratings without registered posts" }
val postsIdsToRemove = ratingsRepo.getAll().keys - postsRepo.getAll().keys
Log.i { "Posts to remove: $postsIdsToRemove" }
ratingsRepo.unset(postsIdsToRemove.toList())
}
}
}

View File

@@ -6,9 +6,17 @@ import dev.inmo.micro_utils.repos.exposed.keyvalue.AbstractExposedKeyValueRepo
import dev.inmo.plaguposter.posts.models.PostId
import dev.inmo.plaguposter.ratings.models.Rating
import dev.inmo.plaguposter.ratings.repo.RatingsRepo
import org.jetbrains.exposed.sql.*
import org.jetbrains.exposed.sql.statements.*
import org.jetbrains.exposed.sql.transactions.transaction
import org.jetbrains.exposed.v1.core.Op
import org.jetbrains.exposed.v1.core.ResultRow
import org.jetbrains.exposed.v1.core.and
import org.jetbrains.exposed.v1.core.eq
import org.jetbrains.exposed.v1.core.greaterEq
import org.jetbrains.exposed.v1.core.lessEq
import org.jetbrains.exposed.v1.core.notInList
import org.jetbrains.exposed.v1.core.statements.UpdateBuilder
import org.jetbrains.exposed.v1.jdbc.Database
import org.jetbrains.exposed.v1.jdbc.Query
import org.jetbrains.exposed.v1.jdbc.transactions.transaction
class ExposedRatingsRepo (
database: Database
@@ -18,8 +26,8 @@ class ExposedRatingsRepo (
) {
override val keyColumn = text("post_id")
val ratingsColumn = double("rating")
override val selectById: ISqlExpressionBuilder.(PostId) -> Op<Boolean> = { keyColumn.eq(it.string) }
override val selectByValue: ISqlExpressionBuilder.(Rating) -> Op<Boolean> = { ratingsColumn.eq(it.double) }
override val selectById: (PostId) -> Op<Boolean> = { keyColumn.eq(it.string) }
override val selectByValue: (Rating) -> Op<Boolean> = { ratingsColumn.eq(it.double) }
override val ResultRow.asKey: PostId
get() = get(keyColumn).let(::PostId)
override val ResultRow.asObject: Rating
@@ -33,7 +41,7 @@ class ExposedRatingsRepo (
it[ratingsColumn] = v.double
}
override fun insertKey(k: PostId, v: Rating, it: InsertStatement<Number>) {
override fun insertKey(k: PostId, v: Rating, it: UpdateBuilder<Int>) {
it[keyColumn] = k.string
}

View File

@@ -15,7 +15,7 @@ function assert_success() {
app=plaguposter
version="`grep ../gradle.properties -e "^version=" | sed -e "s/version=\(.*\)/\1/"`"
server=insanusmokrassar
server=docker.inmo.dev
assert_success ../gradlew build
assert_success sudo docker build -t $app:"$version" .

View File

@@ -41,7 +41,11 @@
"Results": 0
},
"autoAttach": true,
"ratingOfferText": "What do you think about it?"
"ratingOfferText": "What do you think about it?",
"panelButtonText": "Ratings ${attached_symbol}",
"ratingEnabledStyle": "success",
"ratingDisabledStyle": "danger",
"_note": "For panelButtonText you may use two control symbols: ${attached_symbol} to attach icon about enabled state and ${rating} to enable rating showing in button"
},
"selector": {
"items": [

View File

@@ -7,6 +7,7 @@ import dev.inmo.plaguposter.common.SuccessfulSymbol
import dev.inmo.plagubot.plugins.inline.queries.models.Format
import dev.inmo.plagubot.plugins.inline.queries.models.OfferTemplate
import dev.inmo.plagubot.plugins.inline.queries.repos.InlineTemplatesRepo
import dev.inmo.plagubot.registerConfig
import dev.inmo.plaguposter.posts.models.PostId
import dev.inmo.plaguposter.posts.panel.PanelButtonBuilder
import dev.inmo.plaguposter.posts.panel.PanelButtonsAPI
@@ -26,11 +27,10 @@ import dev.inmo.tgbotapi.extensions.utils.types.buttons.flatInlineKeyboard
import dev.inmo.tgbotapi.types.ChatId
import dev.inmo.tgbotapi.types.MessageId
import dev.inmo.tgbotapi.types.buttons.InlineKeyboardButtons.CallbackDataInlineKeyboardButton
import dev.inmo.tgbotapi.types.message.textsources.regular
import dev.inmo.tgbotapi.types.message.textsources.regularTextSource
import kotlinx.coroutines.flow.first
import kotlinx.serialization.Serializable
import kotlinx.serialization.json.*
import org.jetbrains.exposed.sql.Database
import org.koin.core.Koin
import org.koin.core.module.Module
@@ -45,10 +45,8 @@ object Plugin : Plugin {
internal data class Config(
val panelButtonText: String? = "Publish"
)
override fun Module.setupDI(database: Database, params: JsonObject) {
params["publish_command"] ?.let { configJson ->
single { get<Json>().decodeFromJsonElement(Config.serializer(), configJson) }
}
override fun Module.setupDI(params: JsonObject) {
registerConfig<Config>("publish_command") { null }
}
override suspend fun BehaviourContextWithFSM<State>.setupBotPlugin(koin: Koin) {
@@ -91,7 +89,7 @@ object Plugin : Plugin {
edit(
it,
it.content.textSources + regular(SuccessfulSymbol)
it.content.textSources + regularTextSource(SuccessfulSymbol)
)
}
@@ -115,7 +113,7 @@ object Plugin : Plugin {
PanelButtonBuilder {
CallbackDataInlineKeyboardButton(
text,
"publish ${it.id.string}"
"publish ${it.id.string}",
)
}
)

View File

@@ -16,6 +16,7 @@ import dev.inmo.plagubot.Plugin
import dev.inmo.plagubot.plugins.inline.queries.models.Format
import dev.inmo.plagubot.plugins.inline.queries.models.OfferTemplate
import dev.inmo.plagubot.plugins.inline.queries.repos.InlineTemplatesRepo
import dev.inmo.plagubot.registerConfig
import dev.inmo.plaguposter.common.ChatConfig
import dev.inmo.plaguposter.posts.models.PostId
import dev.inmo.plaguposter.posts.repo.ReadPostsRepo
@@ -40,7 +41,6 @@ import kotlinx.coroutines.flow.collectIndexed
import kotlinx.coroutines.flow.take
import kotlinx.serialization.*
import kotlinx.serialization.json.*
import org.jetbrains.exposed.sql.Database
import org.koin.core.Koin
import org.koin.core.module.Module
@@ -62,8 +62,8 @@ object Plugin : Plugin {
@Transient
val format: DateFormat = DateFormat(dateTimeFormat)
}
override fun Module.setupDI(database: Database, params: JsonObject) {
single { get<Json>().decodeFromJsonElement(Config.serializer(), params["timer_trigger"] ?: return@single null) }
override fun Module.setupDI(params: JsonObject) {
registerConfig<Config>("timer_trigger") { null }
}
@OptIn(FlowPreview::class)

View File

@@ -1,21 +1,15 @@
package dev.inmo.plaguposter.triggers.timer.disablers.autoposts
import dev.inmo.micro_utils.coroutines.subscribeSafelyWithoutExceptions
import dev.inmo.micro_utils.koin.singleWithRandomQualifier
import dev.inmo.micro_utils.koin.singleWithRandomQualifierAndBinds
import dev.inmo.micro_utils.pagination.FirstPagePagination
import dev.inmo.micro_utils.repos.unset
import dev.inmo.plagubot.Plugin
import dev.inmo.plaguposter.ratings.repo.RatingsRepo
import dev.inmo.plaguposter.triggers.selector_with_timer.AutopostFilter
import dev.inmo.plaguposter.triggers.timer.TimersRepo
import kotlinx.coroutines.CoroutineScope
import kotlinx.serialization.json.*
import org.jetbrains.exposed.sql.Database
import org.koin.core.module.Module
object Plugin : Plugin {
override fun Module.setupDI(database: Database, params: JsonObject) {
override fun Module.setupDI(config: JsonObject) {
singleWithRandomQualifier<AutopostFilter> {
val timersRepo = get<TimersRepo>()
AutopostFilter { _, dateTime ->

View File

@@ -8,11 +8,10 @@ import dev.inmo.plaguposter.ratings.repo.RatingsRepo
import dev.inmo.plaguposter.triggers.timer.TimersRepo
import kotlinx.coroutines.CoroutineScope
import kotlinx.serialization.json.*
import org.jetbrains.exposed.sql.Database
import org.koin.core.module.Module
object Plugin : Plugin {
override fun Module.setupDI(database: Database, params: JsonObject) {
override fun Module.setupDI(config: JsonObject) {
singleWithRandomQualifier(createdAtStart = true) {
val timersRepo = get<TimersRepo>()
val ratingsRepo = get<RatingsRepo>()

View File

@@ -6,6 +6,7 @@ import dev.inmo.plaguposter.posts.models.RegisteredPost
import dev.inmo.plaguposter.posts.panel.PanelButtonBuilder
import dev.inmo.tgbotapi.types.buttons.InlineKeyboardButtons.CallbackDataInlineKeyboardButton
import dev.inmo.tgbotapi.types.buttons.InlineKeyboardButtons.InlineKeyboardButton
import dev.inmo.tgbotapi.types.buttons.KeyboardButtonStyle
class TimerPanelButton(
private val timersRepo: TimersRepo
@@ -18,7 +19,12 @@ class TimerPanelButton(
return CallbackDataInlineKeyboardButton(
"${ if (publishingTime == null) UnsuccessfulSymbol else SuccessfulSymbol }",
"$timerSetPrefix ${post.id}"
"$timerSetPrefix ${post.id}",
style = if (publishingTime == null) {
null
} else {
KeyboardButtonStyle.Success
}
)
}

View File

@@ -1,7 +1,5 @@
package dev.inmo.plaguposter.triggers.timer
import korlibs.time.DateTime
import dev.inmo.micro_utils.coroutines.runCatchingSafely
import dev.inmo.micro_utils.coroutines.subscribeSafelyWithoutExceptions
import dev.inmo.micro_utils.koin.singleWithRandomQualifierAndBinds
import dev.inmo.micro_utils.repos.set
@@ -9,24 +7,19 @@ import dev.inmo.plagubot.Plugin
import dev.inmo.plaguposter.common.ChatConfig
import dev.inmo.plaguposter.posts.models.PostId
import dev.inmo.plaguposter.posts.panel.PanelButtonsAPI
import dev.inmo.plaguposter.posts.repo.ReadPostsRepo
import dev.inmo.plaguposter.triggers.timer.repo.ExposedTimersRepo
import dev.inmo.tgbotapi.extensions.api.answers.answer
import dev.inmo.tgbotapi.extensions.api.edit.edit
import dev.inmo.tgbotapi.extensions.api.send.reply
import dev.inmo.tgbotapi.extensions.api.send.send
import dev.inmo.tgbotapi.extensions.behaviour_builder.BehaviourContext
import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onCommand
import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onMessageDataCallbackQuery
import kotlinx.coroutines.CoroutineScope
import kotlinx.serialization.json.*
import org.jetbrains.exposed.sql.Database
import org.koin.core.Koin
import org.koin.core.module.Module
import org.koin.dsl.binds
object Plugin : Plugin {
override fun Module.setupDI(database: Database, params: JsonObject) {
override fun Module.setupDI(config: JsonObject) {
single { ExposedTimersRepo(get(), get(), get()) } binds arrayOf(TimersRepo::class)
single(createdAtStart = true) { TimersHandler(get(), get(), get()) }
singleWithRandomQualifierAndBinds { TimerPanelButton(get()) }

View File

@@ -11,15 +11,12 @@ import dev.inmo.plaguposter.posts.models.PostId
import dev.inmo.plaguposter.posts.repo.PostsRepo
import dev.inmo.plaguposter.triggers.timer.TimersRepo
import kotlinx.coroutines.CoroutineScope
import org.jetbrains.exposed.sql.Column
import org.jetbrains.exposed.sql.Database
import org.jetbrains.exposed.sql.ISqlExpressionBuilder
import org.jetbrains.exposed.sql.Op
import org.jetbrains.exposed.sql.ResultRow
import org.jetbrains.exposed.sql.select
import org.jetbrains.exposed.sql.statements.InsertStatement
import org.jetbrains.exposed.sql.statements.UpdateBuilder
import org.jetbrains.exposed.sql.transactions.transaction
import org.jetbrains.exposed.v1.core.Op
import org.jetbrains.exposed.v1.core.ResultRow
import org.jetbrains.exposed.v1.core.eq
import org.jetbrains.exposed.v1.core.statements.UpdateBuilder
import org.jetbrains.exposed.v1.jdbc.Database
import org.jetbrains.exposed.v1.jdbc.transactions.transaction
class ExposedTimersRepo(
database: Database,
@@ -31,8 +28,8 @@ class ExposedTimersRepo(
) {
override val keyColumn = text("post_id")
private val dateTimeColumn = long("date_time")
override val selectById: ISqlExpressionBuilder.(PostId) -> Op<Boolean> = { keyColumn.eq(it.string) }
override val selectByValue: ISqlExpressionBuilder.(DateTime) -> Op<Boolean> = { dateTimeColumn.eq(it.unixMillisLong) }
override val selectById: (PostId) -> Op<Boolean> = { keyColumn.eq(it.string) }
override val selectByValue: (DateTime) -> Op<Boolean> = { dateTimeColumn.eq(it.unixMillisLong) }
override val ResultRow.asKey: PostId
get() = PostId(get(keyColumn))
override val ResultRow.asObject: DateTime
@@ -50,7 +47,7 @@ class ExposedTimersRepo(
it[dateTimeColumn] = v.unixMillisLong
}
override fun insertKey(k: PostId, v: DateTime, it: InsertStatement<Number>) {
override fun insertKey(k: PostId, v: DateTime, it: UpdateBuilder<Int>) {
it[keyColumn] = k.string
}