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

59 lines
3.6 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 {
2019-12-11 04:07:27 +01:00
let userHexEncodedPublicKey = OWSIdentityManager.shared().identityKeyPair()!.hexEncodedPublicKey
2019-10-15 01:29:41 +02:00
var publicChat: LokiPublicChat?
2019-12-11 04:07:27 +01:00
var userLinkedDeviceHexEncodedPublicKeys: Set<String>!
OWSPrimaryStorage.shared().dbReadConnection.read { transaction in
2019-10-15 01:29:41 +02:00
publicChat = LokiDatabaseUtilities.getPublicChat(for: threadID, in: transaction)
2019-12-11 04:07:27 +01:00
userLinkedDeviceHexEncodedPublicKeys = LokiDatabaseUtilities.getLinkedDeviceHexEncodedPublicKeys(for: userHexEncodedPublicKey, in: transaction)
}
2019-10-11 05:27:31 +02:00
var string = string
let regex = try! NSRegularExpression(pattern: "@[0-9a-fA-F]*", options: [])
2019-12-10 00:12:51 +01:00
let knownHexEncodedPublicKeys = LokiAPI.userHexEncodedPublicKeyCache[threadID] ?? [] // Should always be populated at this point
2019-12-11 04:07:27 +01:00
var mentions: [(range: NSRange, hexEncodedPublicKey: String)] = []
2019-10-11 05:27:31 +02:00
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-12-10 00:12:51 +01:00
if knownHexEncodedPublicKeys.contains(hexEncodedPublicKey) {
var displayName: String?
if hexEncodedPublicKey == userHexEncodedPublicKey {
displayName = OWSProfileManager.shared().localProfileName()
2019-10-11 05:27:31 +02:00
} else {
2019-10-15 01:29:41 +02:00
if let publicChat = publicChat {
2019-12-10 00:12:51 +01:00
displayName = DisplayNameUtilities.getPublicChatDisplayName(for: hexEncodedPublicKey, in: publicChat.channel, on: publicChat.server)
} else {
2019-12-10 00:12:51 +01:00
displayName = DisplayNameUtilities.getPrivateChatDisplayName(for: hexEncodedPublicKey)
2019-10-11 05:27:31 +02:00
}
}
2019-12-10 00:12:51 +01:00
if let displayName = displayName {
string = (string as NSString).replacingCharacters(in: match.range, with: "@\(displayName)")
2019-12-11 04:07:27 +01:00
mentions.append((range: NSRange(location: match.range.location, length: displayName.count + 1), hexEncodedPublicKey: hexEncodedPublicKey)) // + 1 to include the @
2019-12-10 00:12:51 +01:00
matchEnd = match.range.location + displayName.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
}
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
2019-12-11 04:07:27 +01:00
guard userLinkedDeviceHexEncodedPublicKeys.contains(mention.hexEncodedPublicKey) else { return }
result.addAttribute(.foregroundColor, value: Colors.accent, range: mention.range)
result.addAttribute(.font, value: UIFont.boldSystemFont(ofSize: Values.mediumFontSize), range: mention.range)
2019-10-11 05:27:31 +02:00
}
return result
}
}