Filter bio entities

This commit is contained in:
Ali
2020-10-28 22:31:45 +04:00
parent 08296d1eb0
commit 56a4beb174
8 changed files with 35 additions and 18 deletions

View File

@@ -85,12 +85,13 @@ public struct EnabledEntityTypes: OptionSet {
public static let command = EnabledEntityTypes(rawValue: 1 << 0)
public static let mention = EnabledEntityTypes(rawValue: 1 << 1)
public static let hashtag = EnabledEntityTypes(rawValue: 1 << 2)
public static let url = EnabledEntityTypes(rawValue: 1 << 3)
public static let allUrl = EnabledEntityTypes(rawValue: 1 << 3)
public static let phoneNumber = EnabledEntityTypes(rawValue: 1 << 4)
public static let timecode = EnabledEntityTypes(rawValue: 1 << 5)
public static let external = EnabledEntityTypes(rawValue: 1 << 6)
public static let internalUrl = EnabledEntityTypes(rawValue: 1 << 7)
public static let all: EnabledEntityTypes = [.command, .mention, .hashtag, .url, .phoneNumber]
public static let all: EnabledEntityTypes = [.command, .mention, .hashtag, .allUrl, .phoneNumber]
}
private func commitEntity(_ utf16: String.UTF16View, _ type: CurrentEntityType, _ range: Range<String.UTF16View.Index>, _ enabledTypes: EnabledEntityTypes, _ entities: inout [MessageTextEntity], mediaDuration: Double? = nil) {
@@ -160,11 +161,11 @@ public func generateTextEntities(_ text: String, enabledTypes: EnabledEntityType
let utf16 = text.utf16
var detector: NSDataDetector?
if enabledTypes.contains(.phoneNumber) && enabledTypes.contains(.url) {
if enabledTypes.contains(.phoneNumber) && (enabledTypes.contains(.allUrl) || enabledTypes.contains(.internalUrl)) {
detector = dataAndPhoneNumberDetector
} else if enabledTypes.contains(.phoneNumber) {
detector = phoneNumberDetector
} else if enabledTypes.contains(.url) {
} else if enabledTypes.contains(.allUrl) || enabledTypes.contains(.internalUrl) {
detector = dataDetector
}
@@ -179,6 +180,21 @@ public func generateTextEntities(_ text: String, enabledTypes: EnabledEntityType
if let lowerBound = lowerBound, let upperBound = upperBound {
let type: MessageTextEntityType
if result.resultType == NSTextCheckingResult.CheckingType.link {
if !enabledTypes.contains(.allUrl) && enabledTypes.contains(.internalUrl) {
guard let url = result.url else {
return
}
if url.scheme != "tg" {
guard let host = url.host?.lowercased() else {
return
}
if host == "telegram.org" || host == "t.me" {
} else {
return
}
}
}
type = .Url
} else {
type = .PhoneNumber