Skip to content

Commit

Permalink
feat: проверка forward из того же чата при анализе дубликатов
Browse files Browse the repository at this point in the history
  • Loading branch information
Djaler committed Feb 18, 2024
1 parent e690edf commit 46c34d6
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand All @@ -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,
Expand All @@ -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
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 46c34d6

Please sign in to comment.