Skip to content

Commit

Permalink
feat: обработка миграции чата в супергруппу
Browse files Browse the repository at this point in the history
  • Loading branch information
Djaler committed Feb 24, 2023
1 parent 7183f5a commit 2ea4b65
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -1,33 +1,37 @@
package com.github.djaler.evilbot.handlers

import com.github.djaler.evilbot.handlers.base.CommonMessageHandler
import com.github.djaler.evilbot.handlers.base.MessageHandler
import com.github.djaler.evilbot.service.ChatService
import com.github.djaler.evilbot.service.UserService
import dev.inmo.tgbotapi.extensions.utils.asChatEventMessage
import dev.inmo.tgbotapi.extensions.utils.asPublicChat
import dev.inmo.tgbotapi.types.message.abstracts.CommonMessage
import dev.inmo.tgbotapi.types.message.ChatEvents.MigratedToSupergroup
import dev.inmo.tgbotapi.types.message.abstracts.FromUserMessage
import dev.inmo.tgbotapi.types.message.abstracts.Message
import org.springframework.stereotype.Component
import org.springframework.transaction.annotation.Transactional

@Component
class UpdateStatisticHandler(
private val chatService: ChatService,
private val userService: UserService
) : CommonMessageHandler() {
override val order = 0
) : MessageHandler() {

@Transactional
override suspend fun handleMessage(message: CommonMessage<*>): Boolean {
if (message !is FromUserMessage) {
return false
}
override val order = 0

override suspend fun handleMessage(message: Message): Boolean {
val chat = message.chat.asPublicChat() ?: return false

val (chatEntity, _) = chatService.getOrCreateChatFrom(chat)
val (userEntity, _) = userService.getOrCreateUserFrom(message.user)
val migratedToSupergroup = message.asChatEventMessage()?.chatEvent as? MigratedToSupergroup
if (migratedToSupergroup != null) {
chatService.updateChatId(chat.id, migratedToSupergroup.migratedFrom)
}

if (message is FromUserMessage) {
val (chatEntity, _) = chatService.getOrCreateChatFrom(chat)
val (userEntity, _) = userService.getOrCreateUserFrom(message.user)

userService.registerMessageInStatistic(userEntity, chatEntity)
userService.registerMessageInStatistic(userEntity, chatEntity)
}

return false
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package com.github.djaler.evilbot.repository

import com.github.djaler.evilbot.entity.Chat
import dev.inmo.tgbotapi.types.Identifier
import org.springframework.data.jpa.repository.JpaRepository
import org.springframework.data.jpa.repository.Modifying
import org.springframework.data.jpa.repository.Query
import org.springframework.transaction.annotation.Transactional

interface ChatRepository : JpaRepository<Chat, Short> {
fun findByTelegramId(telegramId: Long): Chat?
Expand All @@ -18,4 +21,9 @@ interface ChatRepository : JpaRepository<Chat, Short> {
""", nativeQuery = true
)
fun findChatsForCrowdSourcing(): List<Chat>

@Query("update Chat c set c.telegramId=:newChatId where c.telegramId=:oldChatId")
@Modifying
@Transactional
fun updateChatId(oldChatId: Identifier, newChatId: Identifier)
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,8 @@ class ChatService(
fun getChatsLeftFor(duration: Duration): List<Short> {
return chatHistoryRepository.findChatsLeftBefore(LocalDateTime.now().minus(duration))
}

fun updateChatId(oldChatId: ChatId, newChatId: ChatId) {
chatRepository.updateChatId(oldChatId.chatId, newChatId.chatId)
}
}

0 comments on commit 2ea4b65

Please sign in to comment.