Skip to content

Commit 3fa0afa

Browse files
committed
Message Filters: emit AyuGram-style <type>N</type> tag for regex matching
ForkRegexMessageFilters.matches(message:) now appends a trailing <type>N</type> tag (approximating AyuGram Android's MessageObject.TYPE_* constants) so patterns like <type>1</type> (photo) ported from AyuGram Android continue to work here. Co-authored-by: D3C0Y <decoder-dev@users.noreply.github.com>
1 parent 0698fcd commit 3fa0afa

1 file changed

Lines changed: 53 additions & 1 deletion

File tree

‎submodules/TelegramUIPreferences/Sources/ForkExtrasSettings.swift‎

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import Foundation
22
import TelegramCore
33
import SwiftSignalKit
4+
import Postbox
45

56
public enum ForkTranslationBackend: String, Codable {
67
case `default`
@@ -493,7 +494,9 @@ public enum ForkRegexMessageFilters {
493494
}
494495

495496
/// Match the same useful payload AyuGram exposes to filters: message text plus inline-button
496-
/// titles and links. The resulting input is still bounded by `matches` before ICU sees it.
497+
/// titles and links, plus a trailing `<type>N</type>` tag (AyuGram Android's
498+
/// `MessageObject.TYPE_*` constants). The resulting input is still bounded by `matches`
499+
/// before ICU sees it.
497500
public static func matches(message: EngineMessage, regexes: [NSRegularExpression]? = nil) -> Bool {
498501
var input = message.text
499502
for attribute in message.attributes {
@@ -518,8 +521,57 @@ public enum ForkRegexMessageFilters {
518521
}
519522
}
520523
}
524+
input.append("\n<type>")
525+
input.append(String(ayuMessageTypeConstant(for: message)))
526+
input.append("</type>")
521527
return matches(input, regexes: regexes)
522528
}
529+
530+
/// Approximate AyuGram Android `MessageObject.TYPE_*` constant for `<type>N</type>` filters.
531+
/// Not a byte-for-byte port of `MessageObject.updateMessageType()` — just enough of the common
532+
/// cases (photo/video/round-video/voice/gif/sticker/music/file/geo/contact/poll) that
533+
/// AyuGram-style `<type>N</type>` patterns copied from another client remain useful here.
534+
private static func ayuMessageTypeConstant(for message: EngineMessage) -> Int {
535+
for media in message.media {
536+
if media is TelegramMediaImage {
537+
return 1 // TYPE_PHOTO
538+
}
539+
if let file = media as? TelegramMediaFile {
540+
if file.isVoice {
541+
return 2 // TYPE_VOICE
542+
}
543+
if file.isInstantVideo {
544+
return 5 // TYPE_ROUND_VIDEO
545+
}
546+
if file.isVideo {
547+
return 3 // TYPE_VIDEO
548+
}
549+
if file.isAnimated {
550+
return 8 // TYPE_GIF
551+
}
552+
if file.isAnimatedSticker {
553+
return 15 // TYPE_ANIMATED_STICKER
554+
}
555+
if file.isSticker {
556+
return 13 // TYPE_STICKER
557+
}
558+
if file.isMusic {
559+
return 14 // TYPE_MUSIC
560+
}
561+
return 9 // TYPE_FILE
562+
}
563+
if media is TelegramMediaMap {
564+
return 4 // TYPE_GEO
565+
}
566+
if media is TelegramMediaContact {
567+
return 12 // TYPE_CONTACT
568+
}
569+
if media is TelegramMediaPoll {
570+
return 17 // TYPE_POLL
571+
}
572+
}
573+
return 0 // TYPE_TEXT
574+
}
523575
}
524576

525577
/// NSE-readable flags (App Group / standard UserDefaults). AccountManager is not available in NSE.

0 commit comments

Comments
 (0)