diff --git a/src/Messages/Interactions/TSErrorMessage.h b/src/Messages/Interactions/TSErrorMessage.h index 66a4ec71a..b43c18b44 100644 --- a/src/Messages/Interactions/TSErrorMessage.h +++ b/src/Messages/Interactions/TSErrorMessage.h @@ -5,6 +5,8 @@ #import "OWSSignalServiceProtos.pb.h" #import "TSMessage.h" +NS_ASSUME_NONNULL_BEGIN + @interface TSErrorMessage : TSMessage typedef NS_ENUM(int32_t, TSErrorMessageType) { @@ -20,13 +22,19 @@ typedef NS_ENUM(int32_t, TSErrorMessageType) { }; - (instancetype)initWithCoder:(NSCoder *)coder NS_DESIGNATED_INITIALIZER; + - (instancetype)initWithTimestamp:(uint64_t)timestamp inThread:(TSThread *)thread - failedMessageType:(TSErrorMessageType)errorMessageType NS_DESIGNATED_INITIALIZER; + failedMessageType:(TSErrorMessageType)errorMessageType + recipientId:(nullable NSString *)recipientId NS_DESIGNATED_INITIALIZER; - (instancetype)initWithTimestamp:(uint64_t)timestamp inThread:(TSThread *)thread - messageBody:(NSString *)body + failedMessageType:(TSErrorMessageType)errorMessageType; + +- (instancetype)initWithTimestamp:(uint64_t)timestamp + inThread:(nullable TSThread *)thread + messageBody:(nullable NSString *)body attachmentIds:(NSArray *)attachmentIds expiresInSeconds:(uint32_t)expiresInSeconds expireStartedAt:(uint64_t)expireStartedAt NS_UNAVAILABLE; @@ -43,6 +51,11 @@ typedef NS_ENUM(int32_t, TSErrorMessageType) { + (instancetype)missingSessionWithEnvelope:(OWSSignalServiceProtosEnvelope *)envelope withTransaction:(YapDatabaseReadWriteTransaction *)transaction; ++ (instancetype)nonblockingIdentityChangeInThread:(TSThread *)thread recipientId:(NSString *)recipientId; + @property (nonatomic, readonly) TSErrorMessageType errorType; +@property (nullable, nonatomic, readonly) NSString *recipientId; @end + +NS_ASSUME_NONNULL_END diff --git a/src/Messages/Interactions/TSErrorMessage.m b/src/Messages/Interactions/TSErrorMessage.m index 2ffdd3485..a15ab1057 100644 --- a/src/Messages/Interactions/TSErrorMessage.m +++ b/src/Messages/Interactions/TSErrorMessage.m @@ -3,12 +3,16 @@ // #import "TSErrorMessage.h" +#import "ContactsManagerProtocol.h" +#import "NSDate+millisecondTimeStamp.h" #import "NotificationsProtocol.h" #import "TSContactThread.h" #import "TSErrorMessage_privateConstructor.h" #import "TSMessagesManager.h" #import "TextSecureKitEnv.h" +NS_ASSUME_NONNULL_BEGIN + @implementation TSErrorMessage - (instancetype)initWithCoder:(NSCoder *)coder @@ -19,6 +23,14 @@ - (instancetype)initWithTimestamp:(uint64_t)timestamp inThread:(TSThread *)thread failedMessageType:(TSErrorMessageType)errorMessageType +{ + return [self initWithTimestamp:timestamp inThread:thread failedMessageType:errorMessageType recipientId:nil]; +} + +- (instancetype)initWithTimestamp:(uint64_t)timestamp + inThread:(TSThread *)thread + failedMessageType:(TSErrorMessageType)errorMessageType + recipientId:(nullable NSString *)recipientId { self = [super initWithTimestamp:timestamp inThread:thread @@ -32,6 +44,8 @@ } _errorType = errorMessageType; + _recipientId = recipientId; + // TODO: Move this out of model class. // // For now, dispatch async to ensure we're not inside a transaction @@ -68,8 +82,21 @@ return NSLocalizedString(@"ERROR_MESSAGE_INVALID_KEY_EXCEPTION", @""); case TSErrorMessageWrongTrustedIdentityKey: return NSLocalizedString(@"ERROR_MESSAGE_WRONG_TRUSTED_IDENTITY_KEY", @""); - case TSErrorMessageNonBlockingIdentityChange: - return NSLocalizedString(@"ERROR_MESSAGE_NON_BLOCKING_IDENTITY_CHANGE", @""); + case TSErrorMessageNonBlockingIdentityChange: { + if (self.recipientId) { + NSString *messageFormat = NSLocalizedString(@"ERROR_MESSAGE_NON_BLOCKING_IDENTITY_CHANGE_FORMAT", + @"Shown when signal users safety numbers changed, embeds the user's {{name or phone number}}"); + + NSString *recipientDisplayName = + [[TextSecureKitEnv sharedEnv].contactsManager displayNameForPhoneIdentifier:self.recipientId]; + return [NSString stringWithFormat:messageFormat, recipientDisplayName]; + } else { + // recipientId will be nil for legacy errors + return NSLocalizedString( + @"ERROR_MESSAGE_NON_BLOCKING_IDENTITY_CHANGE", @"Shown when signal users safety numbers changed"); + } + break; + } case TSErrorMessageUnknownContactBlockOffer: return NSLocalizedString(@"UNKNOWN_CONTACT_BLOCK_OFFER", nil); default: @@ -109,4 +136,14 @@ [[self alloc] initWithEnvelope:envelope withTransaction:transaction failedMessageType:TSErrorMessageNoSession]; } ++ (instancetype)nonblockingIdentityChangeInThread:(TSThread *)thread recipientId:(NSString *)recipientId +{ + return [[self alloc] initWithTimestamp:[NSDate ows_millisecondTimeStamp] + inThread:thread + failedMessageType:TSErrorMessageNonBlockingIdentityChange + recipientId:recipientId]; +} + @end + +NS_ASSUME_NONNULL_END diff --git a/src/Messages/InvalidKeyMessages/TSInvalidIdentityKeySendingErrorMessage.h b/src/Messages/InvalidKeyMessages/TSInvalidIdentityKeySendingErrorMessage.h index d22a9c017..37c1fa1ae 100644 --- a/src/Messages/InvalidKeyMessages/TSInvalidIdentityKeySendingErrorMessage.h +++ b/src/Messages/InvalidKeyMessages/TSInvalidIdentityKeySendingErrorMessage.h @@ -1,9 +1,5 @@ // -// TSInvalidIdentityKeySendingErrorMessage.h -// Signal -// -// Created by Frederic Jacobs on 15/02/15. -// Copyright (c) 2015 Open Whisper Systems. All rights reserved. +// Copyright (c) 2017 Open Whisper Systems. All rights reserved. // #import "TSInvalidIdentityKeyErrorMessage.h" @@ -24,7 +20,6 @@ extern NSString *TSInvalidRecipientKey; forRecipient:(NSString *)recipientId preKeyBundle:(PreKeyBundle *)preKeyBundle; -@property (nonatomic, readonly) NSString *recipientId; @property (nonatomic, readonly) NSString *messageId; @end diff --git a/src/Messages/InvalidKeyMessages/TSInvalidIdentityKeySendingErrorMessage.m b/src/Messages/InvalidKeyMessages/TSInvalidIdentityKeySendingErrorMessage.m index 10d3ed7b1..363eca908 100644 --- a/src/Messages/InvalidKeyMessages/TSInvalidIdentityKeySendingErrorMessage.m +++ b/src/Messages/InvalidKeyMessages/TSInvalidIdentityKeySendingErrorMessage.m @@ -32,12 +32,12 @@ NSString *TSInvalidRecipientKey = @"TSInvalidRecipientKey"; { self = [super initWithTimestamp:message.timestamp inThread:thread - failedMessageType:TSErrorMessageWrongTrustedIdentityKey]; + failedMessageType:TSErrorMessageWrongTrustedIdentityKey + recipientId:recipientId]; if (self) { _messageId = message.uniqueId; _preKeyBundle = preKeyBundle; - _recipientId = recipientId; } return self; diff --git a/src/Storage/AxolotlStore/TSStorageManager+IdentityKeyStore.m b/src/Storage/AxolotlStore/TSStorageManager+IdentityKeyStore.m index 66b9bcf62..aa3b52ddc 100644 --- a/src/Storage/AxolotlStore/TSStorageManager+IdentityKeyStore.m +++ b/src/Storage/AxolotlStore/TSStorageManager+IdentityKeyStore.m @@ -84,19 +84,12 @@ - (void)createIdentityChangeInfoMessageForRecipientId:(NSString *)recipientId { - UInt64 nowTimestamp = [NSDate ows_millisecondTimeStamp]; - TSContactThread *contactThread = [TSContactThread getOrCreateThreadWithContactId:recipientId]; - [[[TSErrorMessage alloc] initWithTimestamp:nowTimestamp - inThread:contactThread - failedMessageType:TSErrorMessageNonBlockingIdentityChange] save]; + [[TSErrorMessage nonblockingIdentityChangeInThread:contactThread recipientId:recipientId] save]; for (TSGroupThread *groupThread in [TSGroupThread groupThreadsWithRecipientId:recipientId]) { - [[[TSErrorMessage alloc] initWithTimestamp:nowTimestamp - inThread:groupThread - failedMessageType:TSErrorMessageNonBlockingIdentityChange] save]; + [[TSErrorMessage nonblockingIdentityChangeInThread:groupThread recipientId:recipientId] save]; } } - @end