Dynamically show user names on public chat.

This fixes the issue where identicons weren't matching up to the one on desktop
pull/45/head
Mikunj 6 years ago
parent 2bda269a45
commit 0d2d1cf101

@ -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..<endIndex]))"
let id = group.id.data(using: String.Encoding.utf8)!
let x1 = SSKProtoGroupContext.builder(id: id, type: .deliver)
x1.setName(group.displayName)
@ -65,11 +66,14 @@ public final class LokiGroupChatPoller : NSObject {
let x3 = SSKProtoContent.builder()
x3.setDataMessage(try! x2.build())
let x4 = SSKProtoEnvelope.builder(type: .ciphertext, timestamp: message.timestamp)
x4.setSource(senderDisplayName)
x4.setSource(senderHexEncodedPublicKey)
x4.setSourceDevice(OWSDevicePrimaryDeviceId)
x4.setContent(try! x3.build().serializedData())
let storage = OWSPrimaryStorage.shared()
storage.dbReadWriteConnection.readWrite { transaction in
// Set the display name of the user
transaction.setObject(senderDisplayName, forKey: senderHexEncodedPublicKey, inCollection: group.id)
SSKEnvironment.shared.messageManager.throws_processEnvelope(try! x4.build(), plaintextData: try! x3.build().serializedData(), wasReceivedByUD: false, transaction: transaction)
}
}

@ -21,6 +21,8 @@
#import <SignalServiceKit/TSIncomingMessage.h>
#import <SignalServiceKit/TSOutgoingMessage.h>
#import <SignalServiceKit/TSThread.h>
#import <SignalServiceKit/TSGroupThread.h>
#import <SignalServiceKit/TSGroupModel.h>
#import <YapDatabase/YapDatabase.h>
#import <YapDatabase/YapDatabaseAutoView.h>
#import <YapDatabase/YapDatabaseViewChange.h>
@ -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

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

Loading…
Cancel
Save