diff --git a/Signal/src/ViewControllers/ConversationView/Cells/OWSMessageBubbleView.m b/Signal/src/ViewControllers/ConversationView/Cells/OWSMessageBubbleView.m index b6f7c7ac1..511c48594 100644 --- a/Signal/src/ViewControllers/ConversationView/Cells/OWSMessageBubbleView.m +++ b/Signal/src/ViewControllers/ConversationView/Cells/OWSMessageBubbleView.m @@ -717,7 +717,16 @@ NS_ASSUME_NONNULL_BEGIN NSString *userID = [[text substringWithRange:match1.range] stringByReplacingCharactersInRange:NSMakeRange(0, 1) withString:@""]; NSUInteger matchEnd; if ([knownUserIDs containsObject:userID]) { - NSString *userDisplayName = [Environment.shared.contactsManager attributedContactOrProfileNameForPhoneIdentifier:userID primaryFont:font secondaryFont:font].string; + __block NSString *userDisplayName = [Environment.shared.contactsManager attributedContactOrProfileNameForPhoneIdentifier:userID primaryFont:font secondaryFont:font].string; + if ([userDisplayName isEqual:userID]) { + [OWSPrimaryStorage.sharedManager.dbReadConnection readWithBlock:^(YapDatabaseReadTransaction *transaction) { + NSString *collection = [NSString stringWithFormat:@"%@.%llu", LKGroupChatAPI.publicChatServer, LKGroupChatAPI.publicChatServerID]; + NSString *userDisplayNameCandidate = [transaction objectForKey:userID inCollection:collection]; + if (userDisplayNameCandidate != nil) { + userDisplayName = userDisplayNameCandidate; + } + }]; + } text = [text stringByReplacingCharactersInRange:match1.range withString:[NSString stringWithFormat:@"@%@", userDisplayName]]; [mentions addObject:[NSValue valueWithRange:NSMakeRange(match1.range.location, userDisplayName.length + 1)]]; matchEnd = match1.range.location + userDisplayName.length; diff --git a/Signal/src/ViewControllers/ConversationView/ConversationViewController.m b/Signal/src/ViewControllers/ConversationView/ConversationViewController.m index b168cacc9..8c36f16ba 100644 --- a/Signal/src/ViewControllers/ConversationView/ConversationViewController.m +++ b/Signal/src/ViewControllers/ConversationView/ConversationViewController.m @@ -524,7 +524,7 @@ typedef enum : NSUInteger { userInfo:nil repeats:YES]; - [LKAPI populateUserIDCacheIfNeededFor:thread.uniqueId]; + [LKAPI populateUserIDCacheIfNeededFor:thread.uniqueId in:nil]; } - (void)dealloc diff --git a/SignalServiceKit/src/Loki/API/LokiAPI.swift b/SignalServiceKit/src/Loki/API/LokiAPI.swift index c05d840ef..a8a935854 100644 --- a/SignalServiceKit/src/Loki/API/LokiAPI.swift +++ b/SignalServiceKit/src/Loki/API/LokiAPI.swift @@ -306,10 +306,10 @@ public final class LokiAPI : NSObject { } } - @objc public static func populateUserIDCacheIfNeeded(for threadID: String) { + @objc public static func populateUserIDCacheIfNeeded(for threadID: String, in transaction: YapDatabaseReadWriteTransaction? = nil) { guard userIDCache[threadID] == nil else { return } var result: Set = [] - storage.dbReadWriteConnection.readWrite { transaction in + func populate(in transaction: YapDatabaseReadWriteTransaction) { guard let thread = TSThread.fetch(uniqueId: threadID, transaction: transaction) else { return } let interactions = transaction.ext(TSMessageDatabaseViewExtensionName) as! YapDatabaseViewTransaction interactions.enumerateKeysAndObjects(inGroup: threadID) { _, _, object, index, _ in @@ -317,6 +317,13 @@ public final class LokiAPI : NSObject { result.insert(message.authorId) } } + if let transaction = transaction { + populate(in: transaction) + } else { + storage.dbReadWriteConnection.readWrite { transaction in + populate(in: transaction) + } + } result.insert(userHexEncodedPublicKey) userIDCache[threadID] = result } diff --git a/SignalServiceKit/src/Messages/OWSMessageManager.m b/SignalServiceKit/src/Messages/OWSMessageManager.m index 905dbd254..de80090f0 100644 --- a/SignalServiceKit/src/Messages/OWSMessageManager.m +++ b/SignalServiceKit/src/Messages/OWSMessageManager.m @@ -1412,9 +1412,9 @@ NS_ASSUME_NONNULL_BEGIN (unsigned long)timestamp); return nil; } - + // Loki: Cache the user hex encoded public key (for mentions) - [LKAPI populateUserIDCacheIfNeededFor:oldGroupThread.uniqueId]; + [LKAPI populateUserIDCacheIfNeededFor:oldGroupThread.uniqueId in:transaction]; [LKAPI cache:incomingMessage.authorId for:oldGroupThread.uniqueId]; [self finalizeIncomingMessage:incomingMessage