From 17b751d22a360fd70cbc291f7b090678f75525aa Mon Sep 17 00:00:00 2001 From: Matthew Chen Date: Sun, 9 Apr 2017 20:53:45 -0400 Subject: [PATCH] Create block offer when non-contacts send you a message. // FREEBIE --- src/Contacts/ContactsUpdater.h | 1 + src/Messages/Interactions/TSErrorMessage.h | 6 ++- src/Messages/Interactions/TSErrorMessage.m | 20 ++++---- src/Messages/OWSMessageSender.m | 1 + .../OWSUnknownContactBlockOfferMessage.h | 19 ++++++++ .../OWSUnknownContactBlockOfferMessage.m | 46 +++++++++++++++++++ 6 files changed, 83 insertions(+), 10 deletions(-) create mode 100644 src/Messages/OWSUnknownContactBlockOfferMessage.h create mode 100644 src/Messages/OWSUnknownContactBlockOfferMessage.m diff --git a/src/Contacts/ContactsUpdater.h b/src/Contacts/ContactsUpdater.h index bb04863be..255052e93 100644 --- a/src/Contacts/ContactsUpdater.h +++ b/src/Contacts/ContactsUpdater.h @@ -36,6 +36,7 @@ NS_ASSUME_NONNULL_BEGIN - (void)updateSignalContactIntersectionWithABContacts:(NSArray *)abContacts success:(void (^)())success failure:(void (^)(NSError *error))failure; + @end NS_ASSUME_NONNULL_END diff --git a/src/Messages/Interactions/TSErrorMessage.h b/src/Messages/Interactions/TSErrorMessage.h index b03c7f3f6..7504400a2 100644 --- a/src/Messages/Interactions/TSErrorMessage.h +++ b/src/Messages/Interactions/TSErrorMessage.h @@ -1,5 +1,6 @@ -// Created by Frederic Jacobs on 12/11/14. -// Copyright (c) 2014 Open Whisper Systems. All rights reserved. +// +// Copyright (c) 2017 Open Whisper Systems. All rights reserved. +// #import "OWSSignalServiceProtos.pb.h" #import "TSMessage.h" @@ -15,6 +16,7 @@ typedef NS_ENUM(int32_t, TSErrorMessageType) { TSErrorMessageDuplicateMessage, TSErrorMessageInvalidVersion, TSErrorMessageNonBlockingIdentityChange, + TSErrorMessageUnknownContactBlockOffer, }; -(instancetype)initWithCoder:(NSCoder *)coder NS_DESIGNATED_INITIALIZER; diff --git a/src/Messages/Interactions/TSErrorMessage.m b/src/Messages/Interactions/TSErrorMessage.m index 3c6c555c8..748abc847 100644 --- a/src/Messages/Interactions/TSErrorMessage.m +++ b/src/Messages/Interactions/TSErrorMessage.m @@ -1,9 +1,5 @@ // -// TSErrorMessage.m -// TextSecureKit -// -// Created by Frederic Jacobs on 12/11/14. -// Copyright (c) 2014 Open Whisper Systems. All rights reserved. +// Copyright (c) 2017 Open Whisper Systems. All rights reserved. // #import "TSErrorMessage.h" @@ -36,8 +32,14 @@ } _errorType = errorMessageType; - // TODO Move this out of model class. - [[TextSecureKitEnv sharedEnv].notificationsManager notifyUserForErrorMessage:self inThread:thread]; + // TODO: Move this out of model class. + // + // For now, dispatch async to ensure we're not inside a transaction + // and thereby avoid deadlock. + TSErrorMessage *errorMessage = self; + dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ + [[TextSecureKitEnv sharedEnv].notificationsManager notifyUserForErrorMessage:errorMessage inThread:thread]; + }); return self; } @@ -47,7 +49,7 @@ failedMessageType:(TSErrorMessageType)errorMessageType { TSContactThread *contactThread = - [TSContactThread getOrCreateThreadWithContactId:envelope.source transaction:transaction]; + [TSContactThread getOrCreateThreadWithContactId:envelope.source transaction:transaction relay:nil]; return [self initWithTimestamp:envelope.timestamp inThread:contactThread failedMessageType:errorMessageType]; } @@ -68,6 +70,8 @@ return NSLocalizedString(@"ERROR_MESSAGE_WRONG_TRUSTED_IDENTITY_KEY", @""); case TSErrorMessageNonBlockingIdentityChange: return NSLocalizedString(@"ERROR_MESSAGE_NON_BLOCKING_IDENTITY_CHANGE", @""); + case TSErrorMessageUnknownContactBlockOffer: + return NSLocalizedString(@"UNKNOWN_CONTACT_BLOCK_OFFER", nil); default: return NSLocalizedString(@"ERROR_MESSAGE_UNKNOWN_ERROR", @""); break; diff --git a/src/Messages/OWSMessageSender.m b/src/Messages/OWSMessageSender.m index b9553fdc5..efd0e0284 100644 --- a/src/Messages/OWSMessageSender.m +++ b/src/Messages/OWSMessageSender.m @@ -1231,6 +1231,7 @@ NSString *const OWSMessageSenderRateLimitedException = @"RateLimitedException"; DDLogWarn(@"%@ Got exception: %@", self.tag, exception); [self.dbConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) { + // TODO: This error message is never created? TSErrorMessage *errorMessage; if (message.groupMetaMessage == TSGroupMessageNone) { diff --git a/src/Messages/OWSUnknownContactBlockOfferMessage.h b/src/Messages/OWSUnknownContactBlockOfferMessage.h new file mode 100644 index 000000000..7d9099dd9 --- /dev/null +++ b/src/Messages/OWSUnknownContactBlockOfferMessage.h @@ -0,0 +1,19 @@ +// +// Copyright (c) 2017 Open Whisper Systems. All rights reserved. +// + +#import "TSErrorMessage.h" + +NS_ASSUME_NONNULL_BEGIN + +@interface OWSUnknownContactBlockOfferMessage : TSErrorMessage + ++ (instancetype)unknownContactBlockOfferMessage:(uint64_t)timestamp + thread:(TSThread *)thread + contactId:(NSString *)contactId; + +@property (nonatomic, readonly) NSString *contactId; + +@end + +NS_ASSUME_NONNULL_END diff --git a/src/Messages/OWSUnknownContactBlockOfferMessage.m b/src/Messages/OWSUnknownContactBlockOfferMessage.m new file mode 100644 index 000000000..92f62b6a8 --- /dev/null +++ b/src/Messages/OWSUnknownContactBlockOfferMessage.m @@ -0,0 +1,46 @@ +// +// Copyright (c) 2017 Open Whisper Systems. All rights reserved. +// + +#import "OWSUnknownContactBlockOfferMessage.h" + +NS_ASSUME_NONNULL_BEGIN + +@interface OWSUnknownContactBlockOfferMessage () + +@property (nonatomic) NSString *contactId; + +@end + +#pragma mark - + +@implementation OWSUnknownContactBlockOfferMessage + ++ (instancetype)unknownContactBlockOfferMessage:(uint64_t)timestamp + thread:(TSThread *)thread + contactId:(NSString *)contactId +{ + return [[OWSUnknownContactBlockOfferMessage alloc] initWithTimestamp:timestamp thread:thread contactId:contactId]; +} + +- (instancetype)initWithTimestamp:(uint64_t)timestamp thread:(TSThread *)thread contactId:(NSString *)contactId +{ + self = [super initWithTimestamp:timestamp inThread:thread failedMessageType:TSErrorMessageUnknownContactBlockOffer]; + + if (self) { + _contactId = contactId; + } + + return self; +} + +- (nullable NSDate *)receiptDateForSorting +{ + // Always use date, since we're creating these interactions after the fact + // and back-dating them. + return self.date; +} + +@end + +NS_ASSUME_NONNULL_END