preview adding of several sources chats

This commit is contained in:
InsanusMokrassar 2023-12-11 00:19:18 +06:00
parent bb433a6441
commit 250f88e2fe
7 changed files with 26 additions and 19 deletions

View File

@ -12,15 +12,20 @@ data class ChatConfig(
val targetChatId: IdChatIdentifier? = null,
@SerialName("sourceChat")
@Serializable(FullChatIdentifierSerializer::class)
val sourceChatId: IdChatIdentifier,
val sourceChatId: IdChatIdentifier?,
@SerialName("cacheChat")
@Serializable(FullChatIdentifierSerializer::class)
val cacheChatId: IdChatIdentifier,
@SerialName("targetChats")
val targetChatIds: List<@Serializable(FullChatIdentifierSerializer::class) IdChatIdentifier> = emptyList(),
@SerialName("sourceChats")
val sourceChatIds: List<@Serializable(FullChatIdentifierSerializer::class) IdChatIdentifier> = emptyList(),
) {
val allTargetChatIds by lazy {
listOfNotNull(targetChatId) + targetChatIds
(listOfNotNull(targetChatId) + targetChatIds).toSet()
}
val allSourceChatIds by lazy {
(listOfNotNull(sourceChatId) + sourceChatIds).toSet()
}
init {
@ -30,8 +35,8 @@ data class ChatConfig(
}
fun check(chatId: IdChatIdentifier) = when (chatId) {
targetChatId,
sourceChatId,
in allTargetChatIds,
in allSourceChatIds,
cacheChatId -> true
else -> false
}

View File

@ -27,7 +27,7 @@ object CommonPlugin : Plugin {
val config = koin.get<ChatConfig>()
Log.iS { "Target chats info: ${config.allTargetChatIds.map { getChat(it) }.joinToString()}" }
Log.iS { "Source chat info: ${getChat(config.sourceChatId)}" }
Log.iS { "Source chats info: ${config.allSourceChatIds.map { getChat(it) }.joinToString()}" }
Log.iS { "Cache chat info: ${getChat(config.cacheChatId)}" }
}
}

View File

@ -104,10 +104,12 @@ object Plugin : Plugin {
return@forEach
}
runCatching {
send(
chatsConfig.sourceChatId,
chatsConfig.cacheChatId,
"Can't find any messages for post $postId. So, deleting it"
)
}
runCatching {
postsRepo.deleteById(postId)
}

View File

@ -146,7 +146,7 @@ object Plugin : Plugin {
onMessageDataCallbackQuery (
initialFilter = {
it.data.startsWith(PanelButtonsAPI.openGlobalMenuDataPrefix) && it.message.chat.id == chatsConfig.sourceChatId
it.data.startsWith(PanelButtonsAPI.openGlobalMenuDataPrefix) && it.message.chat.id in chatsConfig.allSourceChatIds
}
) {
val postId = it.data.removePrefix(PanelButtonsAPI.openGlobalMenuDataPrefix).let(::PostId)
@ -155,7 +155,7 @@ object Plugin : Plugin {
}
onMessageDataCallbackQuery(
initialFilter = {
it.data.startsWith("delete ") && it.message.chat.id == chatsConfig.sourceChatId
it.data.startsWith("delete ") && it.message.chat.id in chatsConfig.allSourceChatIds
}
) { query ->
val postId = query.data.removePrefix("delete ").let(::PostId)
@ -182,7 +182,7 @@ object Plugin : Plugin {
}
onMessageDataCallbackQuery(
initialFilter = {
it.data.startsWith("refresh ") && it.message.chat.id == chatsConfig.sourceChatId
it.data.startsWith("refresh ") && it.message.chat.id in chatsConfig.allSourceChatIds
}
) { query ->
val postId = query.data.removePrefix("refresh ").let(::PostId)

View File

@ -119,12 +119,12 @@ object Plugin : Plugin {
null
}
onCommand("start_post", initialFilter = { it.sameChat(config.sourceChatId) }) {
onCommand("start_post", initialFilter = { config.allSourceChatIds.any { chatId -> it.sameChat(chatId) } }) {
startChain(RegistrationState.InProcess(it.chat.id, emptyList()))
}
onContentMessage(
initialFilter = { it.sameChat(config.sourceChatId) && !FirstSourceIsCommandsFilter(it) }
initialFilter = { config.allSourceChatIds.any { chatId -> it.sameChat(chatId) } && !FirstSourceIsCommandsFilter(it) }
) {
startChain(RegistrationState.Finish(it.chat.id, PostContentInfo.fromMessage(it)))
}

View File

@ -242,7 +242,7 @@ object Plugin : Plugin {
}
}
onCommand("ratings", requireOnlyCommandInMessage = true) {
if (it.chat.id == chatConfig.sourceChatId) {
if (it.chat.id in chatConfig.allSourceChatIds) {
val ratings = ratingsRepo.postsByRatings().toList().sortedByDescending { it.first }
val textSources = buildEntities {
+ "Ratings amount: " + bold("${ratings.sumOf { it.second.size }}") + "\n\n"
@ -260,8 +260,8 @@ object Plugin : Plugin {
}
}
}
includeRootNavigationButtonsHandler(setOf(chatConfig.sourceChatId), ratingsRepo, postsRepo)
onMessageDataCallbackQuery("ratings_interactive", initialFilter = { it.message.chat.id == chatConfig.sourceChatId }) {
includeRootNavigationButtonsHandler(chatConfig.allSourceChatIds, ratingsRepo, postsRepo)
onMessageDataCallbackQuery("ratings_interactive", initialFilter = { it.message.chat.id in chatConfig.allSourceChatIds }) {
edit(
it.message,
ratingsRepo.buildRootButtons()

View File

@ -151,7 +151,7 @@ object Plugin : Plugin {
}
}
onCommand("autoschedule_panel", initialFilter = { it.sameChat(chatConfig.sourceChatId) }) {
onCommand("autoschedule_panel", initialFilter = { chatConfig.allSourceChatIds.any { chatId -> it.sameChat(chatId) } }) {
val keyboard = buildPage()
runCatchingSafely {
@ -167,7 +167,7 @@ object Plugin : Plugin {
onMessageDataCallbackQuery(
Regex("^$pageCallbackDataQueryPrefix\\d+"),
initialFilter = { it.message.sameChat(chatConfig.sourceChatId) }
initialFilter = { chatConfig.allSourceChatIds.any { sourceChatId -> it.message.sameChat(sourceChatId) } }
) {
val page = it.data.removePrefix(pageCallbackDataQueryPrefix).toIntOrNull() ?: let { _ ->
answer(it)