53 Commits
0.0.8 ... 0.4.0

Author SHA1 Message Date
6df4546b81 update sample files 2023-10-31 20:29:06 +06:00
15bd013eaa Update libs.versions.toml 2023-09-30 07:28:37 +06:00
39b607c4e7 Update gradle.properties 2023-09-26 16:11:33 +06:00
98f3e2a461 Update libs.versions.toml 2023-09-26 16:11:06 +06:00
0d31d90efd Merge pull request #17 from InsanusMokrassar/0.3.0
0.3.0
2023-08-21 11:44:07 +06:00
0ce202a5f6 fill changelog 2023-08-20 15:59:05 +06:00
077f8c30a6 update dependencies 2023-08-20 15:57:10 +06:00
1e9559a2c9 Update config.json 2023-08-13 00:11:04 +06:00
feef8efee1 Merge pull request #16 from InsanusMokrassar/0.2.3
0.2.3
2023-08-13 00:04:27 +06:00
54eb7515d3 small fixes in repos 2023-08-13 00:02:19 +06:00
1bb12bee0e fix of build 2023-08-12 23:58:28 +06:00
467525e48d fill changelog 2023-08-12 23:52:53 +06:00
29e5a04135 update dependencies && add opportunity to use several targetChatIds instead of one 2023-08-12 23:50:56 +06:00
6eb43055a7 0.2.3 2023-08-12 23:31:30 +06:00
57eebb61d5 0.2.2 2023-05-10 11:05:14 +06:00
87957dba30 update dependenies. 0.2.1 2023-05-07 19:05:30 +06:00
20148c02f0 0.2.0 2023-04-25 00:48:39 +06:00
e17cfa1c7c update dependencies 2023-04-20 00:41:19 +06:00
0a5ffee808 improvements 2023-04-06 15:52:44 +06:00
847b285ce3 Merge pull request #15 from InsanusMokrassar/0.1.1
0.1.1
2023-03-30 12:14:07 +06:00
c449457d86 update krontab 2023-03-18 14:11:40 +06:00
1b3a632d7b replace OfferTemplate 2023-03-18 14:05:09 +06:00
ebfa79cf64 add OfferTemplate and change autoschedule command 2023-03-18 13:59:37 +06:00
e59c7b0f7e fixes 2023-03-18 13:48:26 +06:00
7a4fb05bfb add publishing_autoschedule 2023-03-18 13:12:05 +06:00
7bc7bf6e8c update dependencies 2023-03-18 12:35:18 +06:00
c64faf75d0 start 0.1.1 2023-03-18 12:28:35 +06:00
ee80d8a3a1 Merge pull request #14 from InsanusMokrassar/0.1.0
0.1.0
2023-03-14 23:58:49 +06:00
22c94a4c43 update base image 2023-03-12 22:56:20 +06:00
c6bcfc0068 upgrade version retieving from gradle.properties 2023-03-12 22:49:34 +06:00
8a648cb066 fixes in docker publishing script 2023-03-12 22:41:41 +06:00
345a156334 fixes 2023-03-12 22:38:21 +06:00
7fb7f923f7 Update libs.versions.toml 2023-03-12 17:37:52 +06:00
3b858a3c00 Update libs.versions.toml 2023-03-09 12:00:57 +06:00
f09e80b8bd fixes for building 2023-03-05 23:30:22 +06:00
fea25743d5 Update libs.versions.toml 2023-03-05 00:55:00 +06:00
86183f5f74 Update libs.versions.toml 2023-02-28 14:25:45 +06:00
bc8d0b26bd start 0.1.0 2023-02-28 14:23:35 +06:00
b05844737b Merge pull request #13 from InsanusMokrassar/0.0.10
0.0.10
2023-02-21 22:13:19 +06:00
d1b597d2c9 add deploy workflow 2023-02-21 22:11:51 +06:00
aef864d5fd update dependencies 2023-02-21 22:08:11 +06:00
f438ede791 Update libs.versions.toml 2023-02-11 18:33:47 +06:00
dc5833c407 fixes 2023-01-18 23:39:56 +06:00
2e7a1b83c5 update microutils 2023-01-18 23:23:22 +06:00
b603fa8822 add caches 2023-01-18 23:22:54 +06:00
8206131425 start 0.0.10 2023-01-18 22:51:01 +06:00
12d3d5eeea Merge pull request #12 from InsanusMokrassar/0.0.9
0.0.9
2023-01-05 22:37:07 +06:00
2c335b43ab add changelog 2023-01-05 20:12:51 +06:00
2b84c224ec add publishing settings 2023-01-05 20:11:59 +06:00
46adc04a9b Update libs.versions.toml 2023-01-02 10:26:13 +06:00
cffc6a62c2 update dependencies 2022-12-20 10:00:24 +06:00
d6b684a17e start 0.0.9 2022-12-20 09:11:59 +06:00
5cd3a6fb35 Merge pull request #11 from InsanusMokrassar/0.0.8
0.0.8
2022-12-15 11:00:27 +06:00
60 changed files with 853 additions and 209 deletions

View File

@@ -1,16 +0,0 @@
name: Build
on: [push]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Set up JDK 11
uses: actions/setup-java@v1
with:
java-version: 11
- name: Build with Gradle
run: ./gradlew build

27
.github/workflows/build_and_publish.yml vendored Normal file
View File

@@ -0,0 +1,27 @@
name: Build
on: [push]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Set up JDK 11
uses: actions/setup-java@v1
with:
java-version: 11
- name: Rewrite version
run: |
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
mv gradle.properties.tmp gradle.properties
- name: Build
run: ./gradlew build
- name: Publish
continue-on-error: true
run: ./gradlew publishAllPublicationsToGiteaRepository
env:
GITEA_TOKEN: ${{ secrets.GITEA_TOKEN }}

28
.github/workflows/docker-publish.yml vendored Normal file
View File

@@ -0,0 +1,28 @@
name: Docker
on: [push]
jobs:
publishing:
runs-on: ubuntu-latest
permissions:
contents: read
packages: write
steps:
- name: Checkout repository
uses: actions/checkout@v2
- name: Rewrite version
run: |
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
rm gradle.properties
mv gradle.properties.tmp gradle.properties
fi
- name: Log into registry
uses: docker/login-action@28218f9b04b4f3f62068d7b6ce6ca5b26e35336c
with:
username: ${{ secrets.DOCKER_LOGIN }}
password: ${{ secrets.DOCKER_PASSWORD }}
- name: Deploy
run: ./gradlew build && cd ./runner && ./nonsudo_deploy.sh

61
CHANGELOG.md Normal file
View File

@@ -0,0 +1,61 @@
# PlaguPoster
## 0.3.0
* `Versions`:
* `tgbotapi`: `9.1.0`
* `plagubot`: `7.1.0`
* `plagubot-plugins`: `0.14.0`
## 0.2.3
* Add opportunity to use several target chat ids
* Update dependencies
## 0.2.2
* `GarbageCollector`:
* Now on start will all clearing job done
## 0.2.1
* `Versions`:
* `kotlin`: `1.8.21`
* `tgbotapi`: `7.1.2`
* `plagubot`: `5.1.2`
* `microutils`: `0.18.1`
* `kslog`: `1.1.1`
* `plagubot.plugins`: `0.11.2`
* `psql`: `42.6.0`
## 0.2.0
* `Versions`:
* `tgbotapi`: `7.1.0`
* `plagubot`: `5.1.0`
* `krontab`: `1.0.0`
* `plagubot.plugins`: `0.11.0`
## 0.1.2
* `Versions`:
* `kotlin`: `1.8.20`
* `plagubot`: `5.0.2`
* `microutils`: `0.17.8`
* `kslog`: `1.1.1`
* `plagubot.plugins`: `0.10.2`
* `psql`: `42.6.0`
## 0.1.1
* Update dependencies
* `Triggers`
* `SelectorWithTimer`
* Opportunity to get schedule of posts using `publishing_autoschedule` command
## 0.0.10
## 0.0.9
* Update dependencies

View File

@@ -23,4 +23,4 @@ allprojects {
}
apply from: "./extensions.gradle"
// apply from: "./github_release.gradle"
apply from: "./github_release.gradle"

24
changelog_parser.sh Normal file
View File

@@ -0,0 +1,24 @@
#!/bin/bash
function parse() {
version="$1"
while IFS= read -r line && [ -z "`echo "$line" | grep -e "^#\+ $version"`" ]
do
: # do nothing
done
while IFS= read -r line && [ -z "`echo "$line" | grep -e "^#\+"`" ]
do
echo "$line"
done
}
version="$1"
file="$2"
if [ -n "$file" ]; then
parse "$version" < "$file"
else
parse "$version"
fi

View File

@@ -11,7 +11,9 @@ kotlin {
dependencies {
api libs.tgbotapi
api libs.microutils.repos.common
api libs.microutils.repos.cache
api libs.kslog
api libs.microutils.koin
}
}
jvmMain {

View File

@@ -1,6 +1,5 @@
package dev.inmo.plaguposter.common
import dev.inmo.tgbotapi.types.ChatId
import dev.inmo.tgbotapi.types.FullChatIdentifierSerializer
import dev.inmo.tgbotapi.types.IdChatIdentifier
import kotlinx.serialization.SerialName
@@ -10,14 +9,26 @@ import kotlinx.serialization.Serializable
data class ChatConfig(
@SerialName("targetChat")
@Serializable(FullChatIdentifierSerializer::class)
val targetChatId: IdChatIdentifier,
val targetChatId: IdChatIdentifier? = null,
@SerialName("sourceChat")
@Serializable(FullChatIdentifierSerializer::class)
val sourceChatId: IdChatIdentifier,
@SerialName("cacheChat")
@Serializable(FullChatIdentifierSerializer::class)
val cacheChatId: IdChatIdentifier
val cacheChatId: IdChatIdentifier,
@SerialName("targetChats")
val targetChatIds: List<@Serializable(FullChatIdentifierSerializer::class) IdChatIdentifier> = emptyList(),
) {
val allTargetChatIds by lazy {
listOfNotNull(targetChatId) + targetChatIds
}
init {
require(targetChatId != null || targetChatIds.isNotEmpty()) {
"One of fields, 'targetChat' or 'targetChats' should be presented"
}
}
fun check(chatId: IdChatIdentifier) = when (chatId) {
targetChatId,
sourceChatId,

View File

@@ -2,13 +2,14 @@ package dev.inmo.plaguposter.common
import dev.inmo.tgbotapi.extensions.behaviour_builder.filters.CommonMessageFilterExcludeMediaGroups
import dev.inmo.tgbotapi.extensions.behaviour_builder.utils.SimpleFilter
import dev.inmo.tgbotapi.extensions.utils.contentMessageOrNull
import dev.inmo.tgbotapi.extensions.utils.textContentOrNull
import dev.inmo.tgbotapi.extensions.utils.withContentOrNull
import dev.inmo.tgbotapi.types.BotCommand
import dev.inmo.tgbotapi.types.message.abstracts.*
import dev.inmo.tgbotapi.types.message.content.TextContent
import dev.inmo.tgbotapi.types.message.textsources.BotCommandTextSource
val FirstSourceIsCommandsFilter = SimpleFilter<Message> {
it is ContentMessage<*> && it.content.textContentOrNull() ?.textSources ?.firstOrNull {
it is BotCommandTextSource
} != null
it.contentMessageOrNull() ?.withContentOrNull<TextContent>() ?.content ?.textSources ?.firstOrNull() is BotCommandTextSource
}

View File

@@ -1,6 +1,6 @@
package dev.inmo.plaguposter.common
import com.soywiz.klock.DateTime
import korlibs.time.DateTime
import kotlinx.serialization.KSerializer
import kotlinx.serialization.Serializer
import kotlinx.serialization.builtins.serializer
@@ -8,7 +8,6 @@ import kotlinx.serialization.descriptors.SerialDescriptor
import kotlinx.serialization.encoding.Decoder
import kotlinx.serialization.encoding.Encoder
@Serializer(DateTime::class)
object DateTimeSerializer : KSerializer<DateTime> {
override val descriptor: SerialDescriptor = Double.serializer().descriptor
override fun deserialize(decoder: Decoder): DateTime = DateTime(decoder.decodeDouble())

View File

@@ -0,0 +1,16 @@
package dev.inmo.plaguposter.common
import org.koin.core.Koin
import org.koin.core.module.Module
import org.koin.core.qualifier.named
import org.koin.core.scope.Scope
val Scope.useCache: Boolean
get() = getOrNull(named("useCache")) ?: false
val Koin.useCache: Boolean
get() = getOrNull(named("useCache")) ?: false
fun Module.useCache(useCache: Boolean) {
single(named("useCache")) { useCache }
}

View File

@@ -1,6 +1,5 @@
package dev.inmo.plaguposter.common
import dev.inmo.kslog.common.i
import dev.inmo.kslog.common.iS
import dev.inmo.kslog.common.logger
import dev.inmo.plagubot.Plugin
@@ -10,6 +9,8 @@ import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
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
@@ -18,12 +19,14 @@ object CommonPlugin : Plugin {
private val Log = logger
override fun Module.setupDI(database: Database, params: JsonObject) {
single { CoroutineScope(Dispatchers.Default + SupervisorJob()) }
val useCache = (params["useCache"] as? JsonPrimitive) ?.booleanOrNull ?: true
useCache(useCache)
}
override suspend fun BehaviourContext.setupBotPlugin(koin: Koin) {
val config = koin.get<ChatConfig>()
Log.iS { "Target chat info: ${getChat(config.targetChatId)}" }
Log.iS { "Target chats info: ${config.allTargetChatIds.map { getChat(it) }.joinToString()}" }
Log.iS { "Source chat info: ${getChat(config.sourceChatId)}" }
Log.iS { "Cache chat info: ${getChat(config.cacheChatId)}" }
}

View File

@@ -20,6 +20,6 @@ allprojects {
defaultAndroidSettingsPresetPath = "${rootProject.projectDir.absolutePath}/defaultAndroidSettings.gradle"
// publishGradlePath = "${rootProject.projectDir.absolutePath}/publish.gradle"
publishGradlePath = "${rootProject.projectDir.absolutePath}/publish.gradle"
}
}

31
github_release.gradle Normal file
View File

@@ -0,0 +1,31 @@
private String getCurrentVersionChangelog() {
OutputStream changelogDataOS = new ByteArrayOutputStream()
exec {
commandLine 'chmod', "+x", './changelog_parser.sh'
}
exec {
standardOutput = changelogDataOS
commandLine './changelog_parser.sh', "${project.version}", 'CHANGELOG.md'
}
return changelogDataOS.toString().trim()
}
if (new File(projectDir, "secret.gradle").exists()) {
apply from: './secret.gradle'
apply plugin: "com.github.breadmoirai.github-release"
githubRelease {
token "${project.property('GITHUB_RELEASE_TOKEN')}"
owner "InsanusMokrassar"
repo "PlaguPoster"
tagName "v${project.version}"
releaseName "${project.version}"
targetCommitish "${project.version}"
body getCurrentVersionChangelog()
}
}

View File

@@ -10,4 +10,4 @@ android.enableJetifier=true
# Project data
group=dev.inmo
version=0.0.8
version=0.4.0

View File

@@ -1,19 +1,18 @@
[versions]
kotlin = "1.7.22"
kotlin-serialization = "1.4.1"
kotlin = "1.8.22"
kotlin-serialization = "1.5.1"
plagubot = "3.2.1"
tgbotapi = "4.2.1"
microutils = "0.16.1"
kslog = "0.5.4"
krontab = "0.8.5"
tgbotapi-libraries = "0.6.5"
plagubot-plugins = "0.6.4"
plagubot = "7.2.1"
tgbotapi = "9.2.1"
microutils = "0.19.9"
kslog = "1.1.2"
krontab = "2.1.2"
plagubot-plugins = "0.15.1"
dokka = "1.7.20"
dokka = "1.8.20"
psql = "42.5.0"
psql = "42.6.0"
[libraries]

View File

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

View File

@@ -1,7 +1,7 @@
project.version = "$version"
project.group = "$group"
// apply from: "$publishGradlePath"
apply from: "$publishGradlePath"
kotlin {
jvm {

View File

@@ -1,7 +1,7 @@
project.version = "$version"
project.group = "$group"
// apply from: "$publishGradlePath"
apply from: "$publishGradlePath"
kotlin {
js (IR) {

View File

@@ -1,7 +1,7 @@
project.version = "$version"
project.group = "$group"
// apply from: "$publishGradlePath"
apply from: "$publishGradlePath"
kotlin {
jvm()

View File

@@ -4,6 +4,9 @@ import com.benasher44.uuid.uuid4
import dev.inmo.micro_utils.coroutines.runCatchingSafely
import dev.inmo.micro_utils.coroutines.subscribeSafelyWithoutExceptions
import dev.inmo.micro_utils.koin.getAllDistinct
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.deleteById
import dev.inmo.micro_utils.repos.id
import dev.inmo.micro_utils.repos.set
@@ -12,6 +15,7 @@ import dev.inmo.micro_utils.repos.value
import dev.inmo.plagubot.Plugin
import dev.inmo.plaguposter.common.ChatConfig
import dev.inmo.plaguposter.common.UnsuccessfulSymbol
import dev.inmo.plaguposter.common.useCache
import dev.inmo.plaguposter.posts.models.PostId
import dev.inmo.plaguposter.posts.panel.repos.PostsMessages
import dev.inmo.plaguposter.posts.repo.PostsRepo
@@ -93,7 +97,12 @@ object Plugin : Plugin {
val chatsConfig = koin.get<ChatConfig>()
val config = koin.getOrNull<Config>() ?: Config()
val api = koin.get<PanelButtonsAPI>()
val postsMessages = PostsMessages(koin.get(), koin.get())
val basePostsMessages = PostsMessages(koin.get(), koin.get())
val postsMessages = if (koin.useCache) {
basePostsMessages.cached(FullKVCache(), koin.get())
} else {
basePostsMessages
}
postsRepo.newObjectsFlow.subscribeSafelyWithoutExceptions(this) {
val firstContent = it.content.first()

View File

@@ -1,6 +1,6 @@
package dev.inmo.plaguposter.posts.models
import com.soywiz.klock.DateTime
import korlibs.time.DateTime
import dev.inmo.plaguposter.common.DateTimeSerializer
import dev.inmo.tgbotapi.types.ChatId
import kotlinx.serialization.Serializable

View File

@@ -1,6 +1,6 @@
package dev.inmo.plaguposter.posts.repo
import com.soywiz.klock.DateTime
import korlibs.time.DateTime
import dev.inmo.micro_utils.repos.ReadCRUDRepo
import dev.inmo.plaguposter.posts.models.*
import dev.inmo.tgbotapi.types.ChatId

View File

@@ -18,7 +18,7 @@ class PostPublisher(
private val bot: TelegramBot,
private val postsRepo: PostsRepo,
private val cachingChatId: IdChatIdentifier,
private val targetChatId: IdChatIdentifier,
private val targetChatIds: List<IdChatIdentifier>,
private val deleteAfterPosting: Boolean = true
) {
suspend fun publish(postId: PostId) {
@@ -38,17 +38,19 @@ class PostPublisher(
sortedMessagesContents.forEach { (_, contents) ->
contents.singleOrNull() ?.also {
runCatching {
bot.copyMessage(targetChatId, it.chatId, it.messageId)
}.onFailure { _ ->
targetChatIds.forEach { targetChatId ->
runCatching {
bot.forwardMessage(
it.chatId,
targetChatId,
it.messageId
)
}.onSuccess {
bot.copyMessage(targetChatId, it)
bot.copyMessage(targetChatId, it.chatId, it.messageId)
}.onFailure { _ ->
runCatching {
bot.forwardMessage(
it.chatId,
targetChatId,
it.messageId
)
}.onSuccess {
bot.copyMessage(targetChatId, it)
}
}
}
return@forEach
@@ -57,17 +59,23 @@ class PostPublisher(
it.order to (bot.forwardMessage(toChatId = cachingChatId, fromChatId = it.chatId, messageId = it.messageId).contentMessageOrNull() ?: return@mapNotNull null)
}.sortedBy { it.first }.mapNotNull { (_, forwardedMessage) ->
forwardedMessage.withContentOrNull<MediaGroupPartContent>() ?: null.also { _ ->
bot.copyMessage(targetChatId, forwardedMessage)
targetChatIds.forEach { targetChatId ->
bot.copyMessage(targetChatId, forwardedMessage)
}
}
}
resultContents.singleOrNull() ?.also {
bot.copyMessage(targetChatId, it)
targetChatIds.forEach { targetChatId ->
bot.copyMessage(targetChatId, it)
}
return@forEach
} ?: resultContents.chunked(mediaCountInMediaGroup.last).forEach {
bot.send(
targetChatId,
it.map { it.content.toMediaGroupMemberTelegramMedia() }
)
targetChatIds.forEach { targetChatId ->
bot.send(
targetChatId,
it.map { it.content.toMediaGroupMemberTelegramMedia() }
)
}
}
}

View File

@@ -4,6 +4,7 @@ import dev.inmo.kslog.common.logger
import dev.inmo.kslog.common.w
import dev.inmo.micro_utils.coroutines.runCatchingSafely
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.plaguposter.common.SuccessfulSymbol
@@ -13,6 +14,8 @@ import dev.inmo.plaguposter.common.ChatConfig
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.plaguposter.common.useCache
import dev.inmo.plaguposter.posts.cached.CachedPostsRepo
import dev.inmo.plaguposter.posts.repo.*
import dev.inmo.plaguposter.posts.sending.PostPublisher
import dev.inmo.tgbotapi.extensions.api.delete
@@ -26,7 +29,6 @@ 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 {
@Serializable
@@ -44,14 +46,19 @@ object Plugin : Plugin {
}
single { get<Json>().decodeFromJsonElement(Config.serializer(), configJson) }
single { get<Config>().chats }
single { ExposedPostsRepo(database) } binds arrayOf(
PostsRepo::class,
ReadPostsRepo::class,
WritePostsRepo::class,
)
single { ExposedPostsRepo(database) }
singleWithBinds<PostsRepo> {
val base = get<ExposedPostsRepo>()
if (useCache) {
CachedPostsRepo(base, get())
} else {
base
}
}
single {
val config = get<Config>()
PostPublisher(get(), get(), config.chats.cacheChatId, config.chats.targetChatId, config.deleteAfterPublishing)
PostPublisher(get(), get(), config.chats.cacheChatId, config.chats.allTargetChatIds, config.deleteAfterPublishing)
}
}

View File

@@ -0,0 +1,50 @@
package dev.inmo.plaguposter.posts.cached
import korlibs.time.DateTime
import dev.inmo.micro_utils.pagination.FirstPagePagination
import dev.inmo.micro_utils.pagination.firstPageWithOneElementPagination
import dev.inmo.micro_utils.pagination.utils.doForAllWithNextPaging
import dev.inmo.micro_utils.repos.CRUDRepo
import dev.inmo.micro_utils.repos.cache.cache.FullKVCache
import dev.inmo.micro_utils.repos.cache.full.FullCRUDCacheRepo
import dev.inmo.plaguposter.posts.models.NewPost
import dev.inmo.plaguposter.posts.models.PostContentInfo
import dev.inmo.plaguposter.posts.models.PostId
import dev.inmo.plaguposter.posts.models.RegisteredPost
import dev.inmo.plaguposter.posts.repo.PostsRepo
import dev.inmo.tgbotapi.types.IdChatIdentifier
import dev.inmo.tgbotapi.types.MessageIdentifier
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.flow.Flow
class CachedPostsRepo(
private val parentRepo: PostsRepo,
private val scope: CoroutineScope,
private val kvCache: FullKVCache<PostId, RegisteredPost> = FullKVCache()
) : PostsRepo, CRUDRepo<RegisteredPost, PostId, NewPost> by FullCRUDCacheRepo(
parentRepo,
kvCache,
scope,
skipStartInvalidate = false,
{ it.id }
) {
override val removedPostsFlow: Flow<RegisteredPost> by parentRepo::removedPostsFlow
override suspend fun getIdByChatAndMessage(chatId: IdChatIdentifier, messageId: MessageIdentifier): PostId? {
doForAllWithNextPaging(firstPageWithOneElementPagination) {
kvCache.values(it).also {
it.results.forEach {
return it.takeIf {
it.content.any { it.chatId == chatId && it.messageId == messageId }
} ?.id ?: return@forEach
}
}
}
return null
}
override suspend fun getPostCreationTime(postId: PostId): DateTime? = getById(postId) ?.created
override suspend fun getFirstMessageInfo(postId: PostId): PostContentInfo? = getById(postId) ?.content ?.firstOrNull()
}

View File

@@ -1,7 +1,7 @@
package dev.inmo.plaguposter.posts.exposed
import com.benasher44.uuid.uuid4
import com.soywiz.klock.DateTime
import korlibs.time.DateTime
import dev.inmo.micro_utils.repos.KeyValuesRepo
import dev.inmo.micro_utils.repos.UpdatedValuePair
import dev.inmo.micro_utils.repos.exposed.AbstractExposedCRUDRepo

View File

@@ -18,85 +18,95 @@ import dev.inmo.tgbotapi.extensions.behaviour_builder.BehaviourContextWithFSM
import dev.inmo.tgbotapi.extensions.behaviour_builder.expectations.*
import dev.inmo.tgbotapi.extensions.behaviour_builder.strictlyOn
import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.*
import dev.inmo.tgbotapi.extensions.utils.extensions.raw.text
import dev.inmo.tgbotapi.extensions.utils.extensions.sameChat
import dev.inmo.tgbotapi.extensions.utils.extensions.sameMessage
import dev.inmo.tgbotapi.extensions.utils.formatting.buildEntities
import dev.inmo.tgbotapi.extensions.utils.textContentOrNull
import dev.inmo.tgbotapi.extensions.utils.types.buttons.*
import dev.inmo.tgbotapi.types.message.abstracts.ContentMessage
import dev.inmo.tgbotapi.types.message.content.MediaGroupContent
import dev.inmo.tgbotapi.types.message.content.MessageContent
import dev.inmo.tgbotapi.utils.regular
import kotlinx.coroutines.async
import kotlinx.coroutines.flow.*
import kotlinx.serialization.Serializable
import org.koin.core.Koin
@Serializable
object Plugin : Plugin {
@Serializable
data class Config(
val useInlineFinishingOpportunity: Boolean = true
)
override suspend fun BehaviourContextWithFSM<State>.setupBotPlugin(koin: Koin) {
val config = koin.get<ChatConfig>()
val postsRepo = koin.get<PostsRepo>()
strictlyOn {state: RegistrationState.InProcess ->
strictlyOn { state: RegistrationState.InProcess ->
val buttonUuid = "finish"
val messageToDelete = send(
state.context,
dev.inmo.tgbotapi.utils.buildEntities {
if (state.messages.isNotEmpty()) {
regular("Your message(s) has been registered. You may send new ones or push \"Finish\" to finalize your post")
val suggestionMessageDeferred = async {
send(
state.context,
dev.inmo.tgbotapi.utils.buildEntities {
if (state.messages.isNotEmpty()) {
regular("Your message(s) has been registered. You may send new ones or push \"Finish\" to finalize your post")
} else {
regular("Ok, send me your messages for new post")
}
},
replyMarkup = if (state.messages.isNotEmpty()) {
flatInlineKeyboard {
dataButton(
"Finish",
buttonUuid
)
}
} else {
regular("Ok, send me your messages for new post")
null
}
},
replyMarkup = if (state.messages.isNotEmpty()) {
flatInlineKeyboard {
dataButton(
"Finish",
buttonUuid
)
}
} else {
null
}
)
)
}
val newMessagesInfo = firstOf {
firstOf {
add {
listOf(
waitContentMessage().filter {
it.chat.id == state.context && it.content.textContentOrNull() ?.text != "/finish_post"
}.take(1).first()
)
val receivedMessage = waitAnyContentMessage().filter {
it.sameChat(state.context)
}.first()
when {
receivedMessage.content.textContentOrNull() ?.text == "/finish_post" -> {
val messageToDelete = suggestionMessageDeferred.await()
edit(messageToDelete, "Ok, finishing your request")
RegistrationState.Finish(
state.context,
state.messages
)
}
else -> {
RegistrationState.InProcess(
state.context,
state.messages + PostContentInfo.fromMessage(receivedMessage)
).also {
runCatchingSafely {
suggestionMessageDeferred.cancel()
}
runCatchingSafely {
delete(suggestionMessageDeferred.await())
}
}
}
}
}
add {
val messageToDelete = suggestionMessageDeferred.await()
val finishPressed = waitMessageDataCallbackQuery().filter {
it.message.sameMessage(messageToDelete) && it.data == buttonUuid
}.first()
emptyList<ContentMessage<MessageContent>>()
}
add {
val finishPressed = waitTextMessage().filter {
it.sameChat(messageToDelete) && it.content.text == "/finish_post"
}.first()
emptyList<ContentMessage<MessageContent>>()
}
}.ifEmpty {
edit(messageToDelete, "Ok, finishing your request")
return@strictlyOn RegistrationState.Finish(
state.context,
state.messages
)
}.flatMap {
PostContentInfo.fromMessage(it)
}
RegistrationState.InProcess(
state.context,
state.messages + newMessagesInfo
).also {
delete(messageToDelete)
edit(messageToDelete, "Ok, finishing your request")
RegistrationState.Finish(
state.context,
state.messages
)
}
}
}
@@ -109,12 +119,12 @@ object Plugin : Plugin {
null
}
onCommand("start_post", initialFilter = { it.chat.id == config.sourceChatId }) {
onCommand("start_post", initialFilter = { it.sameChat(config.sourceChatId) }) {
startChain(RegistrationState.InProcess(it.chat.id, emptyList()))
}
onContentMessage(
initialFilter = { it.chat.id == config.sourceChatId && !FirstSourceIsCommandsFilter(it) }
initialFilter = { it.sameChat(config.sourceChatId) && !FirstSourceIsCommandsFilter(it) }
) {
startChain(RegistrationState.Finish(it.chat.id, PostContentInfo.fromMessage(it)))
}

82
publish.gradle Normal file
View File

@@ -0,0 +1,82 @@
apply plugin: 'maven-publish'
task javadocsJar(type: Jar) {
classifier = 'javadoc'
}
publishing {
publications.all {
artifact javadocsJar
pom {
description = "${project.name}"
name = "${project.name}"
url = "https://github.com/InsanusMokrassar/PlaguPoster"
scm {
developerConnection = "scm:git:[fetch=]https://github.com/InsanusMokrassar/PlaguPoster.git[push=]https://github.com/InsanusMokrassar/PlaguPoster.git"
url = "https://github.com/InsanusMokrassar/PlaguPoster.git"
}
developers {
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)
}
}
}
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')
}
}
}
}
}
}
if (project.hasProperty("signing.gnupg.keyName")) {
apply plugin: 'signing'
signing {
useGpgCmd()
sign publishing.publications
}
task signAll {
tasks.withType(Sign).forEach {
dependsOn(it)
}
}
}

1
publish.kpsb Normal file
View File

@@ -0,0 +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"}}}

View File

@@ -1,18 +1,19 @@
package dev.inmo.plaguposter.ratings.gc
import com.soywiz.klock.milliseconds
import com.soywiz.klock.seconds
import korlibs.time.DateTime
import korlibs.time.seconds
import dev.inmo.krontab.KrontabTemplate
import dev.inmo.krontab.toSchedule
import dev.inmo.krontab.utils.asFlow
import dev.inmo.krontab.utils.asFlowWithDelays
import dev.inmo.micro_utils.coroutines.runCatchingSafely
import dev.inmo.micro_utils.coroutines.subscribeSafelyWithoutExceptions
import dev.inmo.micro_utils.repos.*
import dev.inmo.plagubot.Plugin
import dev.inmo.plaguposter.posts.models.PostId
import dev.inmo.plaguposter.posts.repo.PostsRepo
import dev.inmo.plaguposter.ratings.models.Rating
import dev.inmo.plaguposter.ratings.repo.RatingsRepo
import dev.inmo.tgbotapi.extensions.behaviour_builder.BehaviourContext
import dev.inmo.tgbotapi.types.MilliSeconds
import dev.inmo.tgbotapi.types.Seconds
import kotlinx.serialization.Serializable
import kotlinx.serialization.json.*
@@ -43,21 +44,35 @@ object Plugin : Plugin {
val config = koin.get<Config>()
config.immediateDrop ?.let { toDrop ->
ratingsRepo.onNewValue.subscribeSafelyWithoutExceptions(this) {
if (it.value <= toDrop) {
postsRepo.deleteById(it.id)
suspend fun checkAndOptionallyDrop(postId: PostId, rating: Rating) {
if (rating <= toDrop) {
postsRepo.deleteById(postId)
}
}
ratingsRepo.getAll().forEach {
runCatchingSafely {
checkAndOptionallyDrop(it.key, it.value)
}
}
ratingsRepo.onNewValue.subscribeSafelyWithoutExceptions(this) {
checkAndOptionallyDrop(it.first, it.second)
}
}
config.autoclear ?.let { autoclear ->
autoclear.autoClearKrontab.toSchedule().asFlow().subscribeSafelyWithoutExceptions(scope) {
val dropCreatedBefore = it - (autoclear.skipPostAge ?: 0).seconds
suspend fun doAutoClear() {
val dropCreatedBefore = DateTime.now() - (autoclear.skipPostAge ?: 0).seconds
ratingsRepo.getPostsWithRatingLessEq(autoclear.rating).keys.forEach {
if ((postsRepo.getPostCreationTime(it) ?: return@forEach) < dropCreatedBefore) {
postsRepo.deleteById(it)
}
}
}
runCatchingSafely {
doAutoClear()
}
autoclear.autoClearKrontab.toSchedule().asFlowWithDelays().subscribeSafelyWithoutExceptions(scope) {
doAutoClear()
}
}
}
}

View File

@@ -1,6 +1,6 @@
package dev.inmo.plaguposter.ratings.selector
import com.soywiz.klock.DateTime
import korlibs.time.DateTime
import dev.inmo.plaguposter.posts.models.PostId
import dev.inmo.plaguposter.posts.repo.PostsRepo
import dev.inmo.plaguposter.ratings.repo.RatingsRepo
@@ -11,11 +11,11 @@ class DefaultSelector (
private val ratingsRepo: RatingsRepo,
private val postsRepo: PostsRepo
) : Selector {
override suspend fun take(n: Int, now: DateTime): List<PostId> {
override suspend fun take(n: Int, now: DateTime, exclude: List<PostId>): List<PostId> {
val result = mutableListOf<PostId>()
do {
val selected = config.active(now.time) ?.rating ?.select(ratingsRepo, postsRepo, result, now) ?: break
val selected = config.active(now.time) ?.rating ?.select(ratingsRepo, postsRepo, result + exclude, now) ?: break
result.add(selected)
} while (result.size < n)

View File

@@ -1,8 +1,8 @@
package dev.inmo.plaguposter.ratings.selector
import com.soywiz.klock.DateTime
import korlibs.time.DateTime
import dev.inmo.plaguposter.posts.models.PostId
interface Selector {
suspend fun take(n: Int = 1, now: DateTime = DateTime.now()): List<PostId>
suspend fun take(n: Int = 1, now: DateTime = DateTime.now(), exclude: List<PostId> = emptyList()): List<PostId>
}

View File

@@ -1,7 +1,7 @@
package dev.inmo.plaguposter.ratings.selector.models
import com.soywiz.klock.DateTime
import com.soywiz.klock.seconds
import korlibs.time.DateTime
import korlibs.time.seconds
import dev.inmo.micro_utils.pagination.FirstPagePagination
import dev.inmo.micro_utils.pagination.Pagination
import dev.inmo.micro_utils.pagination.utils.getAllByWithNextPaging

View File

@@ -1,7 +1,7 @@
package dev.inmo.plaguposter.ratings.selector.models
import com.soywiz.klock.DateTime
import com.soywiz.klock.Time
import korlibs.time.DateTime
import korlibs.time.Time
import kotlinx.serialization.Serializable
@Serializable

View File

@@ -1,6 +1,6 @@
package dev.inmo.plaguposter.ratings.selector.models
import com.soywiz.klock.*
import korlibs.time.*
import kotlinx.serialization.*
import kotlinx.serialization.builtins.serializer
import kotlinx.serialization.descriptors.SerialDescriptor

View File

@@ -1,6 +1,9 @@
package dev.inmo.plaguposter.ratings.source.buttons
import com.soywiz.klock.DateFormat
import korlibs.time.DateFormat
import dev.inmo.kslog.common.TagLogger
import dev.inmo.kslog.common.d
import dev.inmo.kslog.common.i
import dev.inmo.micro_utils.coroutines.runCatchingSafely
import dev.inmo.micro_utils.pagination.FirstPagePagination
import dev.inmo.micro_utils.pagination.Pagination
@@ -63,6 +66,7 @@ suspend fun RatingsRepo.buildRatingButtons(
postCreationTimeFormat: DateFormat = defaultPostCreationTimeFormat
): InlineKeyboardMarkup {
val postsByRatings = getPosts(rating .. rating, true).keys.paginate(pagination)
TagLogger("RatingsButtonsBuilder").i { postsByRatings.results }
return inlineKeyboard {
if (postsByRatings.pagesNumber > 1) {
row {
@@ -75,7 +79,7 @@ suspend fun RatingsRepo.buildRatingButtons(
}
}
}
postsByRatings.results.chunked(rowSize).map {
postsByRatings.results.chunked(rowSize).forEach {
row {
it.forEach { postId ->
val firstMessageInfo = postsRepo.getFirstMessageInfo(postId) ?: return@forEach

View File

@@ -0,0 +1,14 @@
package dev.inmo.plaguposter.ratings.source.repos
import dev.inmo.micro_utils.repos.KeyValueRepo
import dev.inmo.micro_utils.repos.cache.cache.FullKVCache
import dev.inmo.micro_utils.repos.cache.full.fullyCached
import dev.inmo.plaguposter.common.ShortMessageInfo
import dev.inmo.tgbotapi.types.PollIdentifier
import kotlinx.coroutines.CoroutineScope
class CachedPollsToMessagesInfoRepo(
private val repo: PollsToMessagesInfoRepo,
private val scope: CoroutineScope,
private val kvCache: FullKVCache<PollIdentifier, ShortMessageInfo> = FullKVCache()
) : PollsToMessagesInfoRepo, KeyValueRepo<PollIdentifier, ShortMessageInfo> by repo.fullyCached(kvCache, scope)

View File

@@ -0,0 +1,14 @@
package dev.inmo.plaguposter.ratings.source.repos
import dev.inmo.micro_utils.repos.KeyValueRepo
import dev.inmo.micro_utils.repos.cache.cache.FullKVCache
import dev.inmo.micro_utils.repos.cache.full.fullyCached
import dev.inmo.plaguposter.posts.models.PostId
import dev.inmo.tgbotapi.types.PollIdentifier
import kotlinx.coroutines.CoroutineScope
class CachedPollsToPostsIdsRepo(
private val repo: PollsToPostsIdsRepo,
private val scope: CoroutineScope,
private val kvCache: FullKVCache<PollIdentifier, PostId> = FullKVCache()
) : PollsToPostsIdsRepo, KeyValueRepo<PollIdentifier, PostId> by repo.fullyCached(kvCache, scope)

View File

@@ -67,8 +67,29 @@ object Plugin : Plugin {
get<Json>().decodeFromJsonElement(Config.serializer(), params["ratingsPolls"] ?: error("Unable to load config for rating polls in $params"))
}
single<RatingsVariants>(ratingVariantsQualifier) { get<Config>().variants }
single<PollsToPostsIdsRepo> { ExposedPollsToPostsIdsRepo(database) }
single<PollsToMessagesInfoRepo> { ExposedPollsToMessagesInfoRepo(database) }
single { ExposedPollsToPostsIdsRepo(database) }
single<PollsToPostsIdsRepo> {
val base = get<ExposedPollsToPostsIdsRepo>()
if (useCache) {
CachedPollsToPostsIdsRepo(base, get())
} else {
base
}
}
single { ExposedPollsToMessagesInfoRepo(database) }
single<PollsToMessagesInfoRepo> {
val base = get<ExposedPollsToMessagesInfoRepo>()
if (useCache) {
CachedPollsToMessagesInfoRepo(base, get())
} else {
base
}
}
single<VariantTransformer> {
val ratingsSettings = get<RatingsVariants>(ratingVariantsQualifier)
VariantTransformer {

View File

@@ -0,0 +1,61 @@
package dev.inmo.plaguposter.ratings.repo
import dev.inmo.micro_utils.pagination.utils.doForAllWithNextPaging
import dev.inmo.micro_utils.repos.KeyValueRepo
import dev.inmo.micro_utils.repos.cache.cache.FullKVCache
import dev.inmo.micro_utils.repos.cache.full.FullKeyValueCacheRepo
import dev.inmo.plaguposter.posts.models.PostId
import dev.inmo.plaguposter.ratings.models.Rating
import kotlinx.coroutines.CoroutineScope
class CachedRatingsRepo(
private val base: RatingsRepo,
private val scope: CoroutineScope,
private val kvCache: FullKVCache<PostId, Rating> = FullKVCache()
) : RatingsRepo, KeyValueRepo<PostId, Rating> by FullKeyValueCacheRepo(base, kvCache, scope) {
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()
}
override suspend fun getPostsWithRatingGreaterEq(
then: Rating,
reversed: Boolean,
count: Int?,
exclude: List<PostId>
): Map<PostId, Rating> = getPosts(
then .. Rating(Double.MAX_VALUE),
reversed,
count,
exclude
)
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
)
}

View File

@@ -1,8 +1,10 @@
package dev.inmo.plaguposter.ratings
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.plaguposter.common.useCache
import dev.inmo.plaguposter.posts.exposed.ExposedPostsRepo
import dev.inmo.plaguposter.posts.repo.PostsRepo
import dev.inmo.plaguposter.ratings.exposed.ExposedRatingsRepo
@@ -16,11 +18,16 @@ import org.koin.dsl.binds
object Plugin : Plugin {
override fun Module.setupDI(database: Database, params: JsonObject) {
single { ExposedRatingsRepo(database) } binds arrayOf(
RatingsRepo::class,
ReadRatingsRepo::class,
WriteRatingsRepo::class,
)
single { ExposedRatingsRepo(database) }
singleWithBinds<RatingsRepo> {
val base = get<ExposedRatingsRepo>()
if (useCache) {
CachedRatingsRepo(base, get())
} else {
base
}
}
}
override suspend fun BehaviourContext.setupBotPlugin(koin: Koin) {

View File

@@ -1,4 +1,4 @@
FROM adoptopenjdk/openjdk11
FROM bellsoft/liberica-openjdk-alpine:19
USER 1000

View File

@@ -12,6 +12,7 @@
"dev.inmo.plaguposter.ratings.Plugin",
"dev.inmo.plaguposter.ratings.source.Plugin",
"dev.inmo.plaguposter.ratings.selector.Plugin",
"dev.inmo.plaguposter.ratings.gc.Plugin",
"dev.inmo.plaguposter.triggers.selector_with_timer.Plugin",
"dev.inmo.plagubot.plugins.inline.queries.Plugin",
"dev.inmo.plaguposter.triggers.command.Plugin",
@@ -25,16 +26,18 @@
"chats": {
"targetChat": 12345678,
"cacheChat": 12345678,
"sourceChat": 12345678
"sourceChat": 12345678,
"targetChats": [12345678],
"_note": "You must set targetChat or targetChats with at least one object"
}
},
"ratingsPolls": {
"variants": {
"Круть": 2,
"Ок": 1,
"Не ок": -1,
"Совсем не ок": -2,
"Посмотреть результаты": 0
"Cool": 2,
"Ok": 1,
"Not ok": -1,
"Inappropriate": -2,
"Results": 0
},
"autoAttach": true,
"ratingOfferText": "What do you think about it?"
@@ -72,5 +75,13 @@
},
"publish_command": {
"panelButtonText": "Publish"
},
"gc": {
"autoclear": {
"rating": -1,
"autoClearKrontab": "0 0 0 * *",
"skipPostAge": 86400
},
"immediateDrop": -6
}
}

View File

@@ -14,8 +14,8 @@ function assert_success() {
}
app=plaguposter
version="`grep ../gradle.properties -e "^version=" | grep -e "[0-9.]*" -o`"
server=docker.io/insanusmokrassar
version="`grep ../gradle.properties -e "^version=" | sed -e "s/version=\(.*\)/\1/"`"
server=insanusmokrassar
assert_success ../gradlew build
assert_success sudo docker build -t $app:"$version" .

25
runner/nonsudo_deploy.sh Executable file
View File

@@ -0,0 +1,25 @@
#!/bin/bash
function send_notification() {
echo "$1"
}
function assert_success() {
"${@}"
local status=${?}
if [ ${status} -ne 0 ]; then
send_notification "### Error ${status} at: ${BASH_LINENO[*]} ###"
exit ${status}
fi
}
app=plaguposter
version="`grep ../gradle.properties -e "^version=" | sed -e "s/version=\(.*\)/\1/"`"
server=insanusmokrassar
assert_success ../gradlew build
assert_success docker build -t $app:"$version" .
assert_success docker tag $app:"$version" $server/$app:$version
assert_success docker tag $app:"$version" $server/$app:latest
assert_success docker push $server/$app:$version
assert_success docker push $server/$app:latest

View File

@@ -0,0 +1,23 @@
version: "3.4"
services:
plaguposter_postgres:
image: postgres:15.4-bullseye
container_name: "plaguposter_postgres"
restart: "unless-stopped"
environment:
POSTGRES_USER: "test"
POSTGRES_PASSWORD: "test"
POSTGRES_DB: "test"
volumes:
- "./db/:/var/lib/postgresql/data"
ports:
- "127.0.0.1:8091:5432"
plaguposter:
image: insanusmokrassar/plaguposter:latest
container_name: "plaguposter"
restart: "unless-stopped"
volumes:
- "./config.json:/config.json"
depends_on:
- "plaguposter_postgres"

View File

@@ -11,7 +11,6 @@ String[] includes = [
":ratings:gc",
":triggers:command",
":triggers:selector_with_timer",
":triggers:selector_with_scheduling",
":triggers:timer",
":triggers:timer:disablers:ratings",
":triggers:timer:disablers:autoposts",

View File

@@ -1,16 +0,0 @@
plugins {
id "org.jetbrains.kotlin.multiplatform"
id "org.jetbrains.kotlin.plugin.serialization"
}
apply from: "$mppProjectWithSerializationPresetPath"
kotlin {
sourceSets {
commonMain {
dependencies {
api project(":plaguposter.common")
}
}
}
}

View File

@@ -1 +0,0 @@
package dev.inmo.plaguposter.triggers.selector_with_scheduling

View File

@@ -1,11 +0,0 @@
package dev.inmo.plaguposter.triggers.selector_with_scheduling
import dev.inmo.plagubot.Plugin
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) {
}
}

View File

@@ -1 +0,0 @@
<manifest package="dev.inmo.plaguposter.triggers.selector_with_scheduling"/>

View File

@@ -1,6 +1,6 @@
package dev.inmo.plaguposter.triggers.selector_with_timer
import com.soywiz.klock.DateTime
import korlibs.time.DateTime
import dev.inmo.plaguposter.posts.models.PostId
fun interface AutopostFilter {

View File

@@ -1,14 +1,43 @@
package dev.inmo.plaguposter.triggers.selector_with_timer
import korlibs.time.DateFormat
import dev.inmo.krontab.KrontabTemplate
import dev.inmo.krontab.toSchedule
import dev.inmo.krontab.utils.asFlow
import dev.inmo.krontab.utils.asFlowWithDelays
import dev.inmo.krontab.utils.asFlowWithoutDelays
import dev.inmo.micro_utils.coroutines.runCatchingSafely
import dev.inmo.micro_utils.coroutines.subscribeSafelyWithoutExceptions
import dev.inmo.micro_utils.koin.singleWithRandomQualifier
import dev.inmo.micro_utils.pagination.FirstPagePagination
import dev.inmo.micro_utils.pagination.Pagination
import dev.inmo.micro_utils.pagination.firstIndex
import dev.inmo.micro_utils.pagination.lastIndexExclusive
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.plaguposter.common.ChatConfig
import dev.inmo.plaguposter.posts.models.PostId
import dev.inmo.plaguposter.posts.repo.ReadPostsRepo
import dev.inmo.plaguposter.posts.sending.PostPublisher
import dev.inmo.plaguposter.ratings.selector.Selector
import dev.inmo.tgbotapi.extensions.api.answers.answer
import dev.inmo.tgbotapi.extensions.api.edit.edit
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 dev.inmo.tgbotapi.extensions.utils.extensions.sameChat
import dev.inmo.tgbotapi.extensions.utils.formatting.makeLinkToMessage
import dev.inmo.tgbotapi.extensions.utils.types.buttons.dataButton
import dev.inmo.tgbotapi.extensions.utils.types.buttons.inlineKeyboard
import dev.inmo.tgbotapi.extensions.utils.types.buttons.urlButton
import dev.inmo.tgbotapi.types.BotCommand
import dev.inmo.tgbotapi.types.buttons.InlineKeyboardMarkup
import dev.inmo.tgbotapi.utils.row
import kotlinx.coroutines.FlowPreview
import kotlinx.coroutines.flow.collectIndexed
import kotlinx.coroutines.flow.take
import kotlinx.serialization.*
import kotlinx.serialization.json.*
import org.jetbrains.exposed.sql.Database
@@ -16,15 +45,21 @@ import org.koin.core.Koin
import org.koin.core.module.Module
object Plugin : Plugin {
@Serializable
private const val pageCallbackDataQueryPrefix = "publishing_autoschedule page"
private const val pageCallbackDataQuerySize = 5
@Serializable
internal data class Config(
@SerialName("krontab")
val krontabTemplate: KrontabTemplate
val krontabTemplate: KrontabTemplate,
val dateTimeFormat: String = "HH:mm:ss, dd.MM.yyyy"
) {
@Transient
val krontab by lazy {
krontabTemplate.toSchedule()
}
@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) }
@@ -35,12 +70,103 @@ object Plugin : Plugin {
val publisher = koin.get<PostPublisher>()
val selector = koin.get<Selector>()
val filters = koin.getAll<AutopostFilter>().distinct()
koin.get<Config>().krontab.asFlow().subscribeSafelyWithoutExceptions(this) { dateTime ->
val chatConfig = koin.get<ChatConfig>()
val postsRepo = koin.get<ReadPostsRepo>()
koin.getOrNull<InlineTemplatesRepo>() ?.apply {
addTemplate(
OfferTemplate(
"Autoschedule buttons",
listOf(
Format(
"/autoschedule_panel"
)
),
"Show autoscheduling publishing info"
)
)
}
val krontab = koin.get<Config>().krontab
val dateTimeFormat = koin.get<Config>().format
krontab.asFlowWithDelays().subscribeSafelyWithoutExceptions(this) { dateTime ->
selector.take(now = dateTime).forEach { postId ->
if (filters.all { it.check(postId, dateTime) }) {
publisher.publish(postId)
}
}
}
suspend fun buildPage(pagination: Pagination = FirstPagePagination(size = pageCallbackDataQuerySize)): InlineKeyboardMarkup {
return inlineKeyboard {
row {
if (pagination.page > 1) {
dataButton("⬅️", "${pageCallbackDataQueryPrefix}0")
}
if (pagination.page > 0) {
dataButton("◀️", "${pageCallbackDataQueryPrefix}${pagination.page - 1}")
}
dataButton("\uD83D\uDD04 ${pagination.page}", "${pageCallbackDataQueryPrefix}${pagination.page}")
dataButton("▶️", "${pageCallbackDataQueryPrefix}${pagination.page + 1}")
}
val selected = mutableListOf<PostId>()
krontab.asFlowWithoutDelays().take(pagination.lastIndexExclusive).collectIndexed { i, dateTime ->
val postId = selector.take(now = dateTime, exclude = selected).firstOrNull() ?.also { postId ->
if (filters.all { it.check(postId, dateTime) }) {
selected.add(postId)
} else {
return@collectIndexed
}
}
val post = postsRepo.getFirstMessageInfo(postId ?: return@collectIndexed)
if (i < pagination.firstIndex || post == null) {
return@collectIndexed
}
row {
urlButton(
dateTime.local.format(dateTimeFormat),
makeLinkToMessage(post.chatId, post.messageId)
)
}
}
}
}
onCommand("autoschedule_panel", initialFilter = { it.sameChat(chatConfig.sourceChatId) }) {
val keyboard = buildPage()
runCatchingSafely {
edit(it, replyMarkup = keyboard) {
+"Your schedule:"
}
}.onFailure { _ ->
send(it.chat, replyMarkup = keyboard) {
+"Your schedule:"
}
}
}
onMessageDataCallbackQuery(
Regex("^$pageCallbackDataQueryPrefix\\d+"),
initialFilter = { it.message.sameChat(chatConfig.sourceChatId) }
) {
val page = it.data.removePrefix(pageCallbackDataQueryPrefix).toIntOrNull() ?: let { _ ->
answer(it)
return@onMessageDataCallbackQuery
}
runCatchingSafely {
edit(
it.message,
replyMarkup = buildPage(Pagination(page, size = pageCallbackDataQuerySize))
)
}.onFailure { _ ->
answer(it)
}
}
}
}

View File

@@ -1,10 +1,10 @@
package dev.inmo.plaguposter.triggers.timer
import com.soywiz.klock.DateFormat
import com.soywiz.klock.DateTime
import com.soywiz.klock.DateTimeTz
import com.soywiz.klock.Month
import com.soywiz.klock.Year
import korlibs.time.DateFormat
import korlibs.time.DateTime
import korlibs.time.DateTimeTz
import korlibs.time.Month
import korlibs.time.Year
import dev.inmo.micro_utils.coroutines.runCatchingSafely
import dev.inmo.micro_utils.repos.unset
import dev.inmo.plaguposter.common.SuccessfulSymbol

View File

@@ -1,7 +1,7 @@
package dev.inmo.plaguposter.triggers.timer
import com.soywiz.klock.DateTime
import com.soywiz.klock.minutes
import korlibs.time.DateTime
import korlibs.time.minutes
fun nearestAvailableTimerTime() = (DateTime.now() + 1.minutes).copyDayOfMonth(
milliseconds = 0,

View File

@@ -1,6 +1,6 @@
package dev.inmo.plaguposter.triggers.timer
import com.soywiz.klock.DateTime
import korlibs.time.DateTime
import dev.inmo.micro_utils.coroutines.launchSafelyWithoutExceptions
import dev.inmo.micro_utils.coroutines.plus
import dev.inmo.micro_utils.coroutines.subscribeSafelyWithoutExceptions

View File

@@ -1,6 +1,6 @@
package dev.inmo.plaguposter.triggers.timer
import com.soywiz.klock.DateTime
import korlibs.time.DateTime
import dev.inmo.micro_utils.repos.KeyValueRepo
import dev.inmo.plaguposter.posts.models.PostId

View File

@@ -1,6 +1,6 @@
package dev.inmo.plaguposter.triggers.timer
import com.soywiz.klock.DateTime
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

View File

@@ -1,6 +1,6 @@
package dev.inmo.plaguposter.triggers.timer.repo
import com.soywiz.klock.DateTime
import korlibs.time.DateTime
import dev.inmo.micro_utils.common.firstNotNull
import dev.inmo.micro_utils.coroutines.subscribeSafelyWithoutExceptions
import dev.inmo.micro_utils.pagination.paginate