From 0d2d1cf101fa9f81e587af84191caf9dbb441e32 Mon Sep 17 00:00:00 2001 From: Mikunj Date: Thu, 5 Sep 2019 10:41:14 +1000 Subject: [PATCH] Dynamically show user names on public chat. This fixes the issue where identicons weren't matching up to the one on desktop --- Signal/src/Loki/LokiGroupChatPoller.swift | 6 +++- .../ConversationView/ConversationViewModel.m | 28 +++++++++++++++---- SignalMessaging/Loki/JazzIcon.swift | 13 +++++++-- 3 files changed, 39 insertions(+), 8 deletions(-) diff --git a/Signal/src/Loki/LokiGroupChatPoller.swift b/Signal/src/Loki/LokiGroupChatPoller.swift index 56f43e51f..961325bb5 100644 --- a/Signal/src/Loki/LokiGroupChatPoller.swift +++ b/Signal/src/Loki/LokiGroupChatPoller.swift @@ -50,6 +50,7 @@ public final class LokiGroupChatPoller : NSObject { let endIndex = senderHexEncodedPublicKey.endIndex let cutoffIndex = senderHexEncodedPublicKey.index(endIndex, offsetBy: -8) let senderDisplayName = "\(message.displayName) (...\(senderHexEncodedPublicKey[cutoffIndex.. #import #import +#import +#import #import #import #import @@ -1509,11 +1511,27 @@ static const int kYapDatabaseRangeMaxLength = 25000; } if (shouldShowSenderName) { senderName = [self.contactsManager - attributedContactOrProfileNameForPhoneIdentifier:incomingSenderId - primaryAttributes:[OWSMessageBubbleView - senderNamePrimaryAttributes] - secondaryAttributes:[OWSMessageBubbleView - senderNameSecondaryAttributes]]; + attributedContactOrProfileNameForPhoneIdentifier:incomingSenderId + primaryAttributes:[OWSMessageBubbleView + senderNamePrimaryAttributes] + secondaryAttributes:[OWSMessageBubbleView + senderNameSecondaryAttributes]]; + + if ([self.thread isKindOfClass:[TSGroupThread class]]) { + TSGroupThread *groupThread = (TSGroupThread *)self.thread; + NSData *groupId = groupThread.groupModel.groupId; + NSString *stringGroupId = [[NSString alloc] initWithData:groupId encoding:NSUTF8StringEncoding]; + + if (stringGroupId != nil) { + NSString __block *displayName; + [self.uiDatabaseConnection readWithBlock:^(YapDatabaseReadTransaction *transaction) { + displayName = [transaction objectForKey:incomingSenderId inCollection:stringGroupId]; + }]; + if (displayName != nil) { + senderName = [[NSAttributedString alloc] initWithString:displayName attributes:[OWSMessageBubbleView senderNamePrimaryAttributes]]; + } + } + } } // Show the sender avatar for incoming group messages unless diff --git a/SignalMessaging/Loki/JazzIcon.swift b/SignalMessaging/Loki/JazzIcon.swift index 423816b67..aedbba8a4 100644 --- a/SignalMessaging/Loki/JazzIcon.swift +++ b/SignalMessaging/Loki/JazzIcon.swift @@ -1,6 +1,12 @@ import CryptoSwift +extension String { + func matches(_ regex: String) -> Bool { + return self.range(of: regex, options: .regularExpression, range: nil, locale: nil) != nil + } +} + private class RNG { private var seed: UInt private var initial: UInt @@ -59,8 +65,11 @@ public class JazzIcon { } convenience init(seed: String, colours: [UIColor]? = nil) { - let hash = seed.md5() - guard let number = UInt(hash.substring(to: min(hash.count, 12)), radix: 16) else { + // Ensure we have a correct hash + var hash = seed + if !hash.matches("^[0-9A-Fa-f]+$") || hash.count < 12 { hash = seed.sha512() } + + guard let number = UInt(hash.substring(to: 12), radix: 16) else { owsFailDebug("[JazzIcon] Failed to generate number from seed string: \(seed)") self.init(seed: 1234, colours: colours) return