From 79b0ff7da747110e8f86737721938dc38d145697 Mon Sep 17 00:00:00 2001 From: nielsandriesse Date: Tue, 15 Sep 2020 09:48:20 +1000 Subject: [PATCH] Add utility for handling display names --- .../MessageDetailViewController.swift | 6 ++- SignalMessaging/Views/ContactCellView.m | 50 +++---------------- .../contacts/SystemContactsFetcher.swift | 26 ---------- .../Utilities/DisplayNameUtilities2.swift | 28 +++++++++++ .../src/Loki/Utilities/GeneralUtilities.swift | 5 +- .../OWSIncomingSentMessageTranscript.m | 1 - 6 files changed, 42 insertions(+), 74 deletions(-) create mode 100644 SignalServiceKit/src/Loki/Utilities/DisplayNameUtilities2.swift diff --git a/Signal/src/ViewControllers/MessageDetailViewController.swift b/Signal/src/ViewControllers/MessageDetailViewController.swift index c333df72a..293ba46ba 100644 --- a/Signal/src/ViewControllers/MessageDetailViewController.swift +++ b/Signal/src/ViewControllers/MessageDetailViewController.swift @@ -222,7 +222,11 @@ class MessageDetailViewController: OWSViewController, MediaGalleryDataSourceDele // Sender? if let incomingMessage = message as? TSIncomingMessage { let senderId = incomingMessage.authorId - let senderName = contactsManager.contactOrProfileName(forPhoneIdentifier: senderId) + let threadID = thread.uniqueId! + var senderName: String! + try! Storage.writeSync { transaction in + senderName = DisplayNameUtilities2.getDisplayName(for: senderId, inThreadWithID: threadID, using: transaction) + } rows.append(valueRow(name: NSLocalizedString("MESSAGE_METADATA_VIEW_SENDER", comment: "Label for the 'sender' field of the 'message metadata' view."), value: senderName)) diff --git a/SignalMessaging/Views/ContactCellView.m b/SignalMessaging/Views/ContactCellView.m index 9c06751d7..f3d04abec 100644 --- a/SignalMessaging/Views/ContactCellView.m +++ b/SignalMessaging/Views/ContactCellView.m @@ -211,52 +211,18 @@ const CGFloat kContactCellAvatarTextMargin = 12; - (void)updateAvatar { - if (self.thread.isGroupThread) { - NSMutableArray *sortedUsers = @[].mutableCopy; - NSSet *users = LKMentionsManager.userPublicKeyCache[self.thread.uniqueId]; - if (users != nil) { - for (NSString *user in users) { - [sortedUsers addObject:user]; - } - } - sortedUsers = [sortedUsers sortedArrayUsingSelector:@selector(compare:)].mutableCopy; - self.profilePictureView.hexEncodedPublicKey = (sortedUsers.count > 0) ? sortedUsers[0] : @""; - self.profilePictureView.isRSSFeed = ((TSGroupThread *)self.thread).isRSSFeed; - } else { - self.profilePictureView.hexEncodedPublicKey = self.thread.contactIdentifier; - } - [self.profilePictureView update]; + [self.profilePictureView updateForThread:self.thread]; } - (void)updateProfileName { - OWSContactsManager *contactsManager = self.contactsManager; - if (contactsManager == nil) { - OWSFailDebug(@"contactsManager should not be nil"); - self.nameLabel.text = self.recipientId; - return; - } - - NSString *recipientId = self.recipientId; - if (recipientId.length == 0) { - OWSFailDebug(@"recipientId should not be nil"); - self.nameLabel.text = nil; - return; - } - - if ([contactsManager hasNameInSystemContactsForRecipientId:recipientId]) { - // Don't display profile name when we have a veritas name in system Contacts - self.nameLabel.text = nil; - } else { - - BOOL isNoteToSelf = (!self.thread.isGroupThread && [self.thread.contactIdentifier isEqualToString:self.tsAccountManager.localNumber]); - if (isNoteToSelf) { - self.nameLabel.text = NSLocalizedString(@"NOTE_TO_SELF", @"Label for 1:1 conversation with yourself."); - } else { - self.nameLabel.text = [contactsManager formattedProfileNameForRecipientId:recipientId]; - } - } - + NSString *publicKey = self.recipientId; + NSString *threadID = self.thread.uniqueId; + __block NSString *displayName = nil; + [LKStorage writeSyncWithBlock:^(YapDatabaseReadWriteTransaction *transaction) { + displayName = [LKDisplayNameUtilities2 getDisplayNameForPublicKey:publicKey threadID:threadID transaction:transaction]; + } error:nil]; + self.nameLabel.text = displayName; [self.nameLabel setNeedsLayout]; } diff --git a/SignalMessaging/contacts/SystemContactsFetcher.swift b/SignalMessaging/contacts/SystemContactsFetcher.swift index 4056c0876..65cba8433 100644 --- a/SignalMessaging/contacts/SystemContactsFetcher.swift +++ b/SignalMessaging/contacts/SystemContactsFetcher.swift @@ -232,32 +232,6 @@ public class SystemContactsFetcher: NSObject { switch authorizationStatus { case .notDetermined: return completion(nil) - // Loki: Original code - // ======== -// if CurrentAppContext().isInBackground() { -// Logger.error("do not request contacts permission when app is in background") -// completion(nil) -// return -// } -// self.contactStoreAdapter.requestAccess { (granted, error) in -// if let error = error { -// Logger.error("error fetching contacts: \(error)") -// completion(error) -// return -// } -// -// guard granted else { -// // This case should have been caught by the error guard a few lines up. -// owsFailDebug("declined contact access.") -// completion(nil) -// return -// } -// -// DispatchQueue.main.async { -// self.updateContacts(completion: completion) -// } -// } - // ======== case .authorized: self.updateContacts(completion: completion) case .denied, .restricted: diff --git a/SignalServiceKit/src/Loki/Utilities/DisplayNameUtilities2.swift b/SignalServiceKit/src/Loki/Utilities/DisplayNameUtilities2.swift new file mode 100644 index 000000000..e5ed3c41e --- /dev/null +++ b/SignalServiceKit/src/Loki/Utilities/DisplayNameUtilities2.swift @@ -0,0 +1,28 @@ + +@objc(LKDisplayNameUtilities2) +public final class DisplayNameUtilities2 : NSObject { + + private override init() { } + + @objc(getDisplayNameForPublicKey:threadID:transaction:) + public static func getDisplayName(for publicKey: String, inThreadWithID threadID: String, using transaction: YapDatabaseReadWriteTransaction) -> String { + // Case 1: The public key belongs to the user themselves + if publicKey == getUserHexEncodedPublicKey() { return SSKEnvironment.shared.profileManager.localProfileName() ?? publicKey } + // Case 2: The given thread is an open group + var openGroup: PublicChat? = nil + Storage.read { transaction in + openGroup = LokiDatabaseUtilities.getPublicChat(for: threadID, in: transaction) + } + if let openGroup = openGroup { + var displayName: String? = nil + Storage.read { transaction in + displayName = transaction.object(forKey: publicKey, inCollection: openGroup.id) as! String? + } + if let displayName = displayName { return displayName } + } + // Case 3: The given thread is a closed group or a one-to-one conversation + // FIXME: The line below opens a write transaction under certain circumstances. We should move away from this and towards passing + // a write transaction into this function. + return SSKEnvironment.shared.profileManager.profileNameForRecipient(withID: publicKey) ?? publicKey + } +} diff --git a/SignalServiceKit/src/Loki/Utilities/GeneralUtilities.swift b/SignalServiceKit/src/Loki/Utilities/GeneralUtilities.swift index e2b7cab5b..62fee4eb1 100644 --- a/SignalServiceKit/src/Loki/Utilities/GeneralUtilities.swift +++ b/SignalServiceKit/src/Loki/Utilities/GeneralUtilities.swift @@ -1,9 +1,6 @@ public func getUserHexEncodedPublicKey() -> String { - // In some cases like deleting an account - // the backgroud fetch was not stopped - // so the identityKeyPair can be nil - if let keyPair = OWSIdentityManager.shared().identityKeyPair() { + if let keyPair = OWSIdentityManager.shared().identityKeyPair() { // Can be nil under some circumstances return keyPair.hexEncodedPublicKey } return "" diff --git a/SignalServiceKit/src/Messages/DeviceSyncing/OWSIncomingSentMessageTranscript.m b/SignalServiceKit/src/Messages/DeviceSyncing/OWSIncomingSentMessageTranscript.m index 68db0f8fe..3a0b2abe5 100644 --- a/SignalServiceKit/src/Messages/DeviceSyncing/OWSIncomingSentMessageTranscript.m +++ b/SignalServiceKit/src/Messages/DeviceSyncing/OWSIncomingSentMessageTranscript.m @@ -49,7 +49,6 @@ NS_ASSUME_NONNULL_BEGIN // Skip the other processing for recipient updates. } else { if (self.dataMessage.group) { - // TODO: Figure out if this is correct _thread = [TSGroupThread getOrCreateThreadWithGroupId:_dataMessage.group.id groupType:closedGroup transaction:transaction]; } else { _thread = [TSContactThread getOrCreateThreadWithContactId:_recipientId transaction:transaction];