Skip to content

Commit

Permalink
игнор reply_message
Browse files Browse the repository at this point in the history
  • Loading branch information
Spliterash committed Jan 23, 2024
1 parent f358282 commit 5a4fded
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -1,27 +1,42 @@
package ru.spliterash.vkVideoUnlocker.message.utils

import jakarta.inject.Singleton
import ru.spliterash.vkVideoUnlocker.longpoll.message.FwdMessage
import ru.spliterash.vkVideoUnlocker.longpoll.message.RootMessage
import ru.spliterash.vkVideoUnlocker.longpoll.message.attachments.AttachmentContainer
import ru.spliterash.vkVideoUnlocker.longpoll.message.hasPing
import ru.spliterash.vkVideoUnlocker.longpoll.message.isPersonalChat
import ru.spliterash.vkVideoUnlocker.story.vkModels.VkStory
import ru.spliterash.vkVideoUnlocker.video.holder.VideoContentHolder
import ru.spliterash.vkVideoUnlocker.video.service.VideoService
import ru.spliterash.vkVideoUnlocker.video.vkModels.VkVideo
import ru.spliterash.vkVideoUnlocker.vk.AttachmentScanner
import ru.spliterash.vkVideoUnlocker.vk.actor.GroupUser
import ru.spliterash.vkVideoUnlocker.vk.api.VkApi
import java.util.function.Predicate
import java.util.regex.Pattern

@Singleton
class MessageUtils(
@GroupUser private val groupUser: VkApi,
private val attachmentScanner: AttachmentScanner,
private val videoService: VideoService,
) {
private val vkUrlPattern = Pattern.compile("(?:https?://)?vk\\.com/(?<attachment>(?:video|wall|story)-?\\d+_\\d+)")
suspend fun scanForVideoContent(root: RootMessage): VideoContentHolder? {
val containerPredicate: Predicate<AttachmentContainer> =
if (root.isPersonalChat() || root.hasPing(groupUser))
Predicate { true }
else
Predicate { it !is FwdMessage }

val attachmentContent = attachmentScanner.scanForAttachment(
root,
listOf(
AttachmentScanner.Checker { it.video },
AttachmentScanner.Checker { it.story }
)
),
containerPredicate
)
if (attachmentContent != null) {
return when (attachmentContent) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,24 @@ import jakarta.inject.Singleton
import ru.spliterash.vkVideoUnlocker.longpoll.message.Attachment
import ru.spliterash.vkVideoUnlocker.longpoll.message.attachments.AttachmentContainer
import ru.spliterash.vkVideoUnlocker.longpoll.message.attachments.AttachmentContent
import java.util.function.Predicate

@Singleton
class AttachmentScanner {
fun <T : AttachmentContent> scanForAttachment(root: AttachmentContainer, checker: Checker<T>): T? {
fun <T : AttachmentContent> scanForAttachment(
root: AttachmentContainer,
checker: Checker<T>,
): T? {
return scanForAttachment(root, listOf(checker)) as T?
}

@Suppress("MemberVisibilityCanBePrivate")
fun scanForAttachment(root: AttachmentContainer, checkers: List<Checker<*>>): AttachmentContent? {
fun scanForAttachment(
root: AttachmentContainer,
checkers: List<Checker<*>>,
allowedContainerChecker: Predicate<AttachmentContainer> = Predicate { true }
): AttachmentContent? {
if (!allowedContainerChecker.test(root)) return null
for (attachment in root.attachments()) {
for (checker in checkers) {
val needle = checker.check(attachment)
Expand All @@ -21,19 +30,19 @@ class AttachmentScanner {
}

if (attachment.wall != null) {
val scanResult = scanForAttachment(attachment.wall, checkers)
val scanResult = scanForAttachment(attachment.wall, checkers, allowedContainerChecker)
if (scanResult != null)
return scanResult
}

if (attachment.wallReply != null) {
val scanResult = scanForAttachment(attachment.wallReply, checkers)
val scanResult = scanForAttachment(attachment.wallReply, checkers, allowedContainerChecker)
if (scanResult != null)
return scanResult
}
}
for (somethingWithAttachments in root.containers()) {
val result = scanForAttachment(somethingWithAttachments, checkers)
val result = scanForAttachment(somethingWithAttachments, checkers, allowedContainerChecker)
if (result != null)
return result
}
Expand Down

0 comments on commit 5a4fded

Please sign in to comment.