session-ios/Session/Utilities/MentionUtilities.swift

53 lines
3.1 KiB
Swift
Raw Normal View History

2019-10-11 05:27:31 +02:00
@objc(LKMentionUtilities)
public final class MentionUtilities : NSObject {
2021-01-29 01:46:32 +01:00
2019-10-11 05:27:31 +02:00
override private init() { }
2021-01-29 01:46:32 +01:00
2019-10-14 05:40:18 +02:00
@objc public static func highlightMentions(in string: String, threadID: String) -> String {
return highlightMentions(in: string, isOutgoingMessage: false, threadID: threadID, attributes: [:]).string // isOutgoingMessage and attributes are irrelevant
2019-10-11 05:27:31 +02:00
}
2021-01-29 01:46:32 +01:00
2019-10-14 05:40:18 +02:00
@objc public static func highlightMentions(in string: String, isOutgoingMessage: Bool, threadID: String, attributes: [NSAttributedString.Key:Any]) -> NSAttributedString {
2021-03-24 04:36:26 +01:00
let openGroupV2 = Storage.shared.getV2OpenGroup(for: threadID)
OWSPrimaryStorage.shared().dbReadConnection.read { transaction in
2020-08-05 08:55:55 +02:00
MentionsManager.populateUserPublicKeyCacheIfNeeded(for: threadID, in: transaction)
}
2019-10-11 05:27:31 +02:00
var string = string
let regex = try! NSRegularExpression(pattern: "@[0-9a-fA-F]*", options: [])
2020-08-05 05:06:05 +02:00
let knownPublicKeys = MentionsManager.userPublicKeyCache[threadID] ?? [] // Should always be populated at this point
2020-08-05 07:25:57 +02:00
var mentions: [(range: NSRange, publicKey: String)] = []
var outerMatch = regex.firstMatch(in: string, options: .withoutAnchoringBounds, range: NSRange(location: 0, length: string.utf16.count))
2019-10-14 05:40:18 +02:00
while let match = outerMatch {
2020-08-05 05:06:05 +02:00
let publicKey = String((string as NSString).substring(with: match.range).dropFirst()) // Drop the @
2019-10-11 05:27:31 +02:00
let matchEnd: Int
2020-08-05 05:06:05 +02:00
if knownPublicKeys.contains(publicKey) {
2021-05-05 06:22:29 +02:00
let context: Contact.Context = (openGroupV2 != nil) ? .openGroup : .regular
2021-02-26 05:56:41 +01:00
let displayName = Storage.shared.getContact(with: publicKey)?.displayName(for: context)
2019-12-10 00:12:51 +01:00
if let displayName = displayName {
string = (string as NSString).replacingCharacters(in: match.range, with: "@\(displayName)")
2020-08-05 07:25:57 +02:00
mentions.append((range: NSRange(location: match.range.location, length: displayName.utf16.count + 1), publicKey: publicKey)) // + 1 to include the @
matchEnd = match.range.location + displayName.utf16.count
2019-10-11 05:27:31 +02:00
} else {
matchEnd = match.range.location + match.range.length
}
} else {
matchEnd = match.range.location + match.range.length
}
2020-08-05 07:25:57 +02:00
outerMatch = regex.firstMatch(in: string, options: .withoutAnchoringBounds, range: NSRange(location: matchEnd, length: string.utf16.count - matchEnd))
2019-10-11 05:27:31 +02:00
}
let result = NSMutableAttributedString(string: string, attributes: attributes)
mentions.forEach { mention in
2021-01-29 01:46:32 +01:00
let color = isOutgoingMessage ? (isLightMode ? .white : .black) : Colors.accent
2020-09-04 08:55:40 +02:00
result.addAttribute(.foregroundColor, value: color, range: mention.range)
2021-01-29 01:46:32 +01:00
result.addAttribute(.font, value: UIFont.boldSystemFont(ofSize: Values.smallFontSize), range: mention.range)
2019-10-11 05:27:31 +02:00
}
return result
}
2021-07-26 07:43:03 +02:00
public static func isUserMentioned(in string: String) -> Bool {
let userPublicKey = getUserHexEncodedPublicKey()
return string.contains("@\(userPublicKey)")
}
2019-10-11 05:27:31 +02:00
}