This commit is contained in:
Niels Andriesse 2019-10-09 11:16:10 +11:00
parent 4d5ca1ce66
commit ea3da42faf
4 changed files with 22 additions and 6 deletions

View File

@ -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;

View File

@ -524,7 +524,7 @@ typedef enum : NSUInteger {
userInfo:nil
repeats:YES];
[LKAPI populateUserIDCacheIfNeededFor:thread.uniqueId];
[LKAPI populateUserIDCacheIfNeededFor:thread.uniqueId in:nil];
}
- (void)dealloc

View File

@ -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<String> = []
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
}

View File

@ -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