Add utility for handling display names

This commit is contained in:
nielsandriesse 2020-09-15 09:48:20 +10:00
parent aa2757f2f8
commit 79b0ff7da7
6 changed files with 42 additions and 74 deletions

View file

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

View file

@ -211,52 +211,18 @@ const CGFloat kContactCellAvatarTextMargin = 12;
- (void)updateAvatar
{
if (self.thread.isGroupThread) {
NSMutableArray<NSString *> *sortedUsers = @[].mutableCopy;
NSSet<NSString *> *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];
}

View file

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

View file

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

View file

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

View file

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