session-ios/Signal/src/Loki/Utilities/MentionUtilities.swift

55 lines
3.2 KiB
Swift
Raw Normal View History

2019-10-11 05:27:31 +02:00
@objc(LKMentionUtilities)
public final class MentionUtilities : NSObject {
override private init() { }
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
}
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 {
var groupChat: LokiGroupChat?
OWSPrimaryStorage.shared().dbReadConnection.read { transaction in
2019-10-14 05:40:18 +02:00
groupChat = LokiDatabaseUtilities.getGroupChat(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: [])
2019-10-14 05:40:18 +02:00
let knownUserHexEncodedPublicKeys = LokiAPI.userHexEncodedPublicKeyCache[threadID] ?? [] // Should always be populated at this point
2019-10-11 05:27:31 +02:00
var mentions: [NSRange] = []
var outerMatch = regex.firstMatch(in: string, options: .withoutAnchoringBounds, range: NSRange(location: 0, length: string.count))
2019-10-14 05:40:18 +02:00
while let match = outerMatch {
2019-10-11 06:57:24 +02:00
let hexEncodedPublicKey = String((string as NSString).substring(with: match.range).dropFirst()) // Drop the @
2019-10-11 05:27:31 +02:00
let matchEnd: Int
2019-10-11 06:57:24 +02:00
if knownUserHexEncodedPublicKeys.contains(hexEncodedPublicKey) {
2019-10-11 05:27:31 +02:00
var userDisplayName: String?
2019-10-11 06:57:24 +02:00
if hexEncodedPublicKey == OWSIdentityManager.shared().identityKeyPair()!.hexEncodedPublicKey {
2019-10-11 05:27:31 +02:00
userDisplayName = OWSProfileManager.shared().localProfileName()
} else {
if let groupChat = groupChat {
2019-10-14 05:40:18 +02:00
userDisplayName = DisplayNameUtilities.getGroupChatDisplayName(for: hexEncodedPublicKey, in: groupChat.channel, on: groupChat.server)
} else {
2019-10-14 05:40:18 +02:00
userDisplayName = DisplayNameUtilities.getPrivateChatDisplayName(for: hexEncodedPublicKey)
2019-10-11 05:27:31 +02:00
}
}
if let userDisplayName = userDisplayName {
string = (string as NSString).replacingCharacters(in: match.range, with: "@\(userDisplayName)")
mentions.append(NSRange(location: match.range.location, length: userDisplayName.count + 1)) // + 1 to include the @
matchEnd = match.range.location + userDisplayName.count
} else {
matchEnd = match.range.location + match.range.length
}
} else {
matchEnd = match.range.location + match.range.length
}
outerMatch = regex.firstMatch(in: string, options: .withoutAnchoringBounds, range: NSRange(location: matchEnd, length: string.count - matchEnd))
}
let result = NSMutableAttributedString(string: string, attributes: attributes)
mentions.forEach { mention in
let color: UIColor = isOutgoingMessage ? .lokiDarkGray() : .lokiGreen()
result.addAttribute(.backgroundColor, value: color, range: mention)
}
return result
}
}