From 46c34d68c59019667fd212b4283de7e79e40f009 Mon Sep 17 00:00:00 2001 From: djaler Date: Sun, 18 Feb 2024 14:03:39 +0300 Subject: [PATCH] =?UTF-8?q?feat:=20=D0=BF=D1=80=D0=BE=D0=B2=D0=B5=D1=80?= =?UTF-8?q?=D0=BA=D0=B0=20forward=20=D0=B8=D0=B7=20=D1=82=D0=BE=D0=B3?= =?UTF-8?q?=D0=BE=20=D0=B6=D0=B5=20=D1=87=D0=B0=D1=82=D0=B0=20=D0=BF=D1=80?= =?UTF-8?q?=D0=B8=20=D0=B0=D0=BD=D0=B0=D0=BB=D0=B8=D0=B7=D0=B5=20=D0=B4?= =?UTF-8?q?=D1=83=D0=B1=D0=BB=D0=B8=D0=BA=D0=B0=D1=82=D0=BE=D0=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../evilbot/handlers/SeenMemeHandler.kt | 34 +++++++++++++++---- .../evilbot/service/DuplicateImageChecker.kt | 6 ++-- 2 files changed, 30 insertions(+), 10 deletions(-) diff --git a/src/main/kotlin/com/github/djaler/evilbot/handlers/SeenMemeHandler.kt b/src/main/kotlin/com/github/djaler/evilbot/handlers/SeenMemeHandler.kt index 03f1828..1a4a0f5 100644 --- a/src/main/kotlin/com/github/djaler/evilbot/handlers/SeenMemeHandler.kt +++ b/src/main/kotlin/com/github/djaler/evilbot/handlers/SeenMemeHandler.kt @@ -1,18 +1,18 @@ package com.github.djaler.evilbot.handlers +import com.github.djaler.evilbot.entity.Chat import com.github.djaler.evilbot.handlers.base.MessageHandler import com.github.djaler.evilbot.service.ChatService import com.github.djaler.evilbot.service.DuplicateImageChecker import dev.inmo.tgbotapi.bot.RequestsExecutor import dev.inmo.tgbotapi.extensions.api.files.downloadFile import dev.inmo.tgbotapi.extensions.api.send.reply -import dev.inmo.tgbotapi.extensions.utils.asContentMessage -import dev.inmo.tgbotapi.extensions.utils.asPhotoContent -import dev.inmo.tgbotapi.extensions.utils.asPublicChat +import dev.inmo.tgbotapi.extensions.utils.* import dev.inmo.tgbotapi.extensions.utils.formatting.makeLinkToMessage import dev.inmo.tgbotapi.types.message.abstracts.Message import dev.inmo.tgbotapi.utils.buildEntities import dev.inmo.tgbotapi.utils.link +import org.apache.logging.log4j.LogManager import org.springframework.stereotype.Component import java.io.ByteArrayInputStream import javax.imageio.ImageIO @@ -23,6 +23,10 @@ class SeenMemeHandler( private val duplicateImageChecker: DuplicateImageChecker, private val chatService: ChatService, ) : MessageHandler() { + companion object { + private val log = LogManager.getLogger() + } + override suspend fun handleMessage(message: Message): Boolean { val chat = message.chat.asPublicChat() ?: return false val imageFile = message.asContentMessage()?.content?.asPhotoContent()?.media ?: return false @@ -33,13 +37,18 @@ class SeenMemeHandler( } val (chatEntity, _) = chatService.getOrCreateChatFrom(chat) - val originalMessageId: Long? = duplicateImageChecker.findDuplicate(image, chatEntity) + val originalMessageInfo = duplicateImageChecker.findDuplicate(image, chatEntity) - if (originalMessageId == null) { + if (originalMessageInfo == null) { duplicateImageChecker.saveHash(image, chatEntity, message.messageId, imageFile.fileId) return false } else { - val messageLink = makeLinkToMessage(message.chat, originalMessageId) ?: return false + log.info("Found duplicate message: $originalMessageInfo") + if (isForwardFromSameChat(message, chatEntity)) { + return false + } + + val messageLink = makeLinkToMessage(message.chat, originalMessageInfo.messageId) ?: return false requestExecutor.reply( message, @@ -50,4 +59,17 @@ class SeenMemeHandler( return true } } + + private fun isForwardFromSameChat(message: Message, chatEntity: Chat): Boolean { + val forwardInfo = message.asPossiblyForwardedMessage()?.forwardInfo + if (forwardInfo == null) { + return false + } + + log.info("Message is forwarded: $forwardInfo") + + val forwardChat = forwardInfo.asForwardFromPublicChatInfo()?.chat ?: return false + val (forwardChatEntity, _) = chatService.getOrCreateChatFrom(forwardChat) + return chatEntity.id == forwardChatEntity.id + } } diff --git a/src/main/kotlin/com/github/djaler/evilbot/service/DuplicateImageChecker.kt b/src/main/kotlin/com/github/djaler/evilbot/service/DuplicateImageChecker.kt index 6d3a999..ebb996c 100644 --- a/src/main/kotlin/com/github/djaler/evilbot/service/DuplicateImageChecker.kt +++ b/src/main/kotlin/com/github/djaler/evilbot/service/DuplicateImageChecker.kt @@ -17,12 +17,10 @@ import javax.imageio.ImageIO class DuplicateImageChecker( private val imageHashRepository: ImageHashRepository, ) { - fun findDuplicate(image: BufferedImage, chat: Chat): MessageIdentifier? { + fun findDuplicate(image: BufferedImage, chat: Chat): ImageHash? { val hash = resizeAndGetHash(image) - val duplicate = imageHashRepository.findByChatIdAndHash(chat.id, hash) - - return duplicate?.messageId + return imageHashRepository.findByChatIdAndHash(chat.id, hash) } fun saveHash(image: BufferedImage, chat: Chat, messageId: MessageIdentifier, fileId: FileId) {