From 9c81eb44a192699367850ac659cb1da937dab3ab Mon Sep 17 00:00:00 2001 From: Michael Kirk Date: Sat, 21 Apr 2018 13:45:45 -0400 Subject: [PATCH] Replace remaining UI usage of `interaction.description` // FREEBIE --- .../Cells/ConversationViewCell.h | 3 +- .../Cells/ConversationViewCell.m | 4 +- .../Cells/OWSContactOffersCell.m | 2 +- .../ConversationView/Cells/OWSMessageCell.m | 2 +- .../Cells/OWSSystemMessageCell.m | 35 +++++--- .../Cells/OWSUnreadIndicatorCell.m | 2 +- .../ConversationViewController.m | 4 +- .../ViewControllers/DebugUI/DebugUIMessages.m | 8 +- Signal/src/environment/NotificationsManager.m | 90 ++++++++++--------- .../NoopNotificationsManager.swift | 4 +- .../src/Messages/OWSIdentityManager.m | 4 +- .../src/Messages/OWSMessageDecrypter.m | 23 +++-- SignalServiceKit/src/Messages/TSCall.h | 2 +- SignalServiceKit/src/Messages/TSCall.m | 12 +-- .../src/Protocols/NotificationsProtocol.h | 11 ++- 15 files changed, 114 insertions(+), 92 deletions(-) diff --git a/Signal/src/ViewControllers/ConversationView/Cells/ConversationViewCell.h b/Signal/src/ViewControllers/ConversationView/Cells/ConversationViewCell.h index 0f9a58664..097669b62 100644 --- a/Signal/src/ViewControllers/ConversationView/Cells/ConversationViewCell.h +++ b/Signal/src/ViewControllers/ConversationView/Cells/ConversationViewCell.h @@ -13,6 +13,7 @@ NS_ASSUME_NONNULL_BEGIN @class TSMessage; @class TSOutgoingMessage; @class TSQuotedMessage; +@class YapDatabaseReadTransaction; @protocol ConversationViewCellDelegate @@ -71,7 +72,7 @@ NS_ASSUME_NONNULL_BEGIN // The width of the collection view. @property (nonatomic) int contentWidth; -- (void)loadForDisplay; +- (void)loadForDisplayWithTransaction:(YapDatabaseReadTransaction *)transaction; - (CGSize)cellSizeForViewWidth:(int)viewWidth contentWidth:(int)contentWidth; diff --git a/Signal/src/ViewControllers/ConversationView/Cells/ConversationViewCell.m b/Signal/src/ViewControllers/ConversationView/Cells/ConversationViewCell.m index 6235989d4..e82863d8c 100644 --- a/Signal/src/ViewControllers/ConversationView/Cells/ConversationViewCell.m +++ b/Signal/src/ViewControllers/ConversationView/Cells/ConversationViewCell.m @@ -1,5 +1,5 @@ // -// Copyright (c) 2017 Open Whisper Systems. All rights reserved. +// Copyright (c) 2018 Open Whisper Systems. All rights reserved. // #import "ConversationViewCell.h" @@ -19,7 +19,7 @@ NS_ASSUME_NONNULL_BEGIN self.contentWidth = 0; } -- (void)loadForDisplay +- (void)loadForDisplayWithTransaction:(YapDatabaseReadTransaction *)transaction { OWSFail(@"%@ This method should be overridden.", self.logTag); } diff --git a/Signal/src/ViewControllers/ConversationView/Cells/OWSContactOffersCell.m b/Signal/src/ViewControllers/ConversationView/Cells/OWSContactOffersCell.m index a86655a7b..6402fbbf6 100644 --- a/Signal/src/ViewControllers/ConversationView/Cells/OWSContactOffersCell.m +++ b/Signal/src/ViewControllers/ConversationView/Cells/OWSContactOffersCell.m @@ -85,7 +85,7 @@ NS_ASSUME_NONNULL_BEGIN return NSStringFromClass([self class]); } -- (void)loadForDisplay +- (void)loadForDisplayWithTransaction:(YapDatabaseReadTransaction *)transaction { OWSAssert(self.viewItem); OWSAssert([self.viewItem.interaction isKindOfClass:[OWSContactOffersInteraction class]]); diff --git a/Signal/src/ViewControllers/ConversationView/Cells/OWSMessageCell.m b/Signal/src/ViewControllers/ConversationView/Cells/OWSMessageCell.m index 49b6b817b..6c19879c3 100644 --- a/Signal/src/ViewControllers/ConversationView/Cells/OWSMessageCell.m +++ b/Signal/src/ViewControllers/ConversationView/Cells/OWSMessageCell.m @@ -150,7 +150,7 @@ NS_ASSUME_NONNULL_BEGIN #pragma mark - Load -- (void)loadForDisplay +- (void)loadForDisplayWithTransaction:(YapDatabaseReadTransaction *)transaction { OWSAssert(self.viewItem); OWSAssert(self.viewItem.interaction); diff --git a/Signal/src/ViewControllers/ConversationView/Cells/OWSSystemMessageCell.m b/Signal/src/ViewControllers/ConversationView/Cells/OWSSystemMessageCell.m index d1d6a48b9..4381b6633 100644 --- a/Signal/src/ViewControllers/ConversationView/Cells/OWSSystemMessageCell.m +++ b/Signal/src/ViewControllers/ConversationView/Cells/OWSSystemMessageCell.m @@ -73,7 +73,7 @@ NS_ASSUME_NONNULL_BEGIN return NSStringFromClass([self class]); } -- (void)loadForDisplay +- (void)loadForDisplayWithTransaction:(YapDatabaseReadTransaction *)transaction { OWSAssert(self.viewItem); @@ -83,7 +83,7 @@ NS_ASSUME_NONNULL_BEGIN self.imageView.image = [icon imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate]; self.imageView.tintColor = [self iconColorForInteraction:interaction]; self.titleLabel.textColor = [self textColor]; - [self applyTitleForInteraction:interaction label:self.titleLabel]; + [self applyTitleForInteraction:interaction label:self.titleLabel transaction:transaction]; [self setNeedsLayout]; } @@ -162,7 +162,9 @@ NS_ASSUME_NONNULL_BEGIN return result; } -- (void)applyTitleForInteraction:(TSInteraction *)interaction label:(UILabel *)label +- (void)applyTitleForInteraction:(TSInteraction *)interaction + label:(UILabel *)label + transaction:(YapDatabaseReadTransaction *)transaction { OWSAssert(interaction); OWSAssert(label); @@ -170,22 +172,24 @@ NS_ASSUME_NONNULL_BEGIN // TODO: Should we move the copy generation into this view? if ([interaction isKindOfClass:[TSErrorMessage class]]) { - label.text = interaction.description; + TSErrorMessage *errorMessage = (TSErrorMessage *)interaction; + label.text = [errorMessage previewTextWithTransaction:transaction]; } else if ([interaction isKindOfClass:[TSInfoMessage class]]) { - if ([interaction isKindOfClass:[OWSVerificationStateChangeMessage class]]) { - OWSVerificationStateChangeMessage *message = (OWSVerificationStateChangeMessage *)interaction; - BOOL isVerified = message.verificationState == OWSVerificationStateVerified; + TSInfoMessage *infoMessage = (TSInfoMessage *)interaction; + if ([infoMessage isKindOfClass:[OWSVerificationStateChangeMessage class]]) { + OWSVerificationStateChangeMessage *verificationMessage = (OWSVerificationStateChangeMessage *)infoMessage; + BOOL isVerified = verificationMessage.verificationState == OWSVerificationStateVerified; NSString *displayName = - [[Environment current].contactsManager displayNameForPhoneIdentifier:message.recipientId]; + [[Environment current].contactsManager displayNameForPhoneIdentifier:verificationMessage.recipientId]; NSString *titleFormat = (isVerified - ? (message.isLocalChange + ? (verificationMessage.isLocalChange ? NSLocalizedString(@"VERIFICATION_STATE_CHANGE_FORMAT_VERIFIED_LOCAL", @"Format for info message indicating that the verification state was verified on " @"this device. Embeds {{user's name or phone number}}.") : NSLocalizedString(@"VERIFICATION_STATE_CHANGE_FORMAT_VERIFIED_OTHER_DEVICE", @"Format for info message indicating that the verification state was verified on " @"another device. Embeds {{user's name or phone number}}.")) - : (message.isLocalChange + : (verificationMessage.isLocalChange ? NSLocalizedString(@"VERIFICATION_STATE_CHANGE_FORMAT_NOT_VERIFIED_LOCAL", @"Format for info message indicating that the verification state was unverified on " @"this device. Embeds {{user's name or phone number}}.") @@ -194,10 +198,11 @@ NS_ASSUME_NONNULL_BEGIN @"another device. Embeds {{user's name or phone number}}."))); label.text = [NSString stringWithFormat:titleFormat, displayName]; } else { - label.text = interaction.description; + label.text = [infoMessage previewTextWithTransaction:transaction]; } } else if ([interaction isKindOfClass:[TSCall class]]) { - label.text = interaction.description; + TSCall *call = (TSCall *)interaction; + label.text = [call previewTextWithTransaction:transaction]; } else { OWSFail(@"Unknown interaction type: %@", [interaction class]); label.text = nil; @@ -266,7 +271,11 @@ NS_ASSUME_NONNULL_BEGIN result.height += self.topVMargin; result.height += self.bottomVMargin; - [self applyTitleForInteraction:interaction label:self.titleLabel]; + // FIXME pass in transaction from the uiDBConnection. + [[TSYapDatabaseObject dbReadConnection] readWithBlock:^(YapDatabaseReadTransaction *_Nonnull transaction) { + [self applyTitleForInteraction:interaction label:self.titleLabel transaction:transaction]; + }]; + CGFloat maxTitleWidth = (viewWidth - ([self hMargin] * 2.f + [self hSpacing] + [self iconSize])); CGSize titleSize = [self.titleLabel sizeThatFits:CGSizeMake(maxTitleWidth, CGFLOAT_MAX)]; diff --git a/Signal/src/ViewControllers/ConversationView/Cells/OWSUnreadIndicatorCell.m b/Signal/src/ViewControllers/ConversationView/Cells/OWSUnreadIndicatorCell.m index 88ca54b48..13ceeb817 100644 --- a/Signal/src/ViewControllers/ConversationView/Cells/OWSUnreadIndicatorCell.m +++ b/Signal/src/ViewControllers/ConversationView/Cells/OWSUnreadIndicatorCell.m @@ -84,7 +84,7 @@ NS_ASSUME_NONNULL_BEGIN return NSStringFromClass([self class]); } -- (void)loadForDisplay +- (void)loadForDisplayWithTransaction:(YapDatabaseReadTransaction *)transaction { OWSAssert(self.viewItem); OWSAssert([self.viewItem.interaction isKindOfClass:[TSUnreadIndicatorInteraction class]]); diff --git a/Signal/src/ViewControllers/ConversationView/ConversationViewController.m b/Signal/src/ViewControllers/ConversationView/ConversationViewController.m index 9c7bdb86d..6531cc68f 100644 --- a/Signal/src/ViewControllers/ConversationView/ConversationViewController.m +++ b/Signal/src/ViewControllers/ConversationView/ConversationViewController.m @@ -4757,7 +4757,9 @@ typedef enum : NSUInteger { } cell.contentWidth = self.layout.contentWidth; - [cell loadForDisplay]; + [self.uiDatabaseConnection readWithBlock:^(YapDatabaseReadTransaction *_Nonnull transaction) { + [cell loadForDisplayWithTransaction:transaction]; + }]; return cell; } diff --git a/Signal/src/ViewControllers/DebugUI/DebugUIMessages.m b/Signal/src/ViewControllers/DebugUI/DebugUIMessages.m index b050f9e8b..ef3ffc1dd 100644 --- a/Signal/src/ViewControllers/DebugUI/DebugUIMessages.m +++ b/Signal/src/ViewControllers/DebugUI/DebugUIMessages.m @@ -3223,7 +3223,7 @@ NS_ASSUME_NONNULL_BEGIN TSContactThread *contactThread = [TSContactThread getOrCreateThreadWithContactId:phoneNumber.toE164]; [self sendFakeMessages:messageCount thread:contactThread]; - DDLogError(@"Create fake thread: %@, interactions: %zd", + DDLogError(@"Create fake thread: %@, interactions: %tu", phoneNumber.toE164, contactThread.numberOfInteractions); }]; @@ -3251,7 +3251,7 @@ NS_ASSUME_NONNULL_BEGIN [self sendFakeMessages:batchSize thread:thread isTextOnly:isTextOnly transaction:transaction]; }]; remainder -= batchSize; - DDLogInfo(@"%@ sendFakeMessages %zd / %zd", self.logTag, counter - remainder, counter); + DDLogInfo(@"%@ sendFakeMessages %td / %tu", self.logTag, counter - remainder, counter); } }); } @@ -3263,7 +3263,7 @@ NS_ASSUME_NONNULL_BEGIN isTextOnly:(BOOL)isTextOnly transaction:(YapDatabaseReadWriteTransaction *)transaction { - DDLogInfo(@"%@ sendFakeMessages: %zd", self.logTag, counter); + DDLogInfo(@"%@ sendFakeMessages: %tu", self.logTag, counter); for (NSUInteger i = 0; i < counter; i++) { NSString *randomText = [self randomText]; @@ -3411,7 +3411,7 @@ NS_ASSUME_NONNULL_BEGIN { OWSAssert(thread); - DDLogInfo(@"%@ injectIncomingMessageInThread: %zd", self.logTag, counter); + DDLogInfo(@"%@ injectIncomingMessageInThread: %tu", self.logTag, counter); NSString *randomText = [self randomText]; NSString *text = [[[@(counter) description] stringByAppendingString:@" "] stringByAppendingString:randomText]; diff --git a/Signal/src/environment/NotificationsManager.m b/Signal/src/environment/NotificationsManager.m index a09ab3b65..1bd2f6f74 100644 --- a/Signal/src/environment/NotificationsManager.m +++ b/Signal/src/environment/NotificationsManager.m @@ -18,6 +18,7 @@ #import #import #import +#import @interface NotificationsManager () @@ -200,51 +201,56 @@ #pragma mark - Signal Messages -- (void)notifyUserForErrorMessage:(TSErrorMessage *)message inThread:(TSThread *)thread { +- (void)notifyUserForErrorMessage:(TSErrorMessage *)message + thread:(TSThread *)thread + transaction:(YapDatabaseReadWriteTransaction *)transaction +{ OWSAssert(message); OWSAssert(thread); - dispatch_async(dispatch_get_main_queue(), ^{ - if (thread.isMuted) { - return; - } - - BOOL shouldPlaySound = [self shouldPlaySoundForNotification]; - - NSString *messageDescription = message.description; - - if (([UIApplication sharedApplication].applicationState != UIApplicationStateActive) && messageDescription) { - UILocalNotification *notification = [[UILocalNotification alloc] init]; - notification.userInfo = @{ Signal_Thread_UserInfo_Key : thread.uniqueId }; - if (shouldPlaySound) { - OWSSound sound = [OWSSounds notificationSoundForThread:thread]; - notification.soundName = [OWSSounds filenameForSound:sound]; - } - - NSString *alertBodyString = @""; - - NSString *authorName = [thread name]; - switch (self.notificationPreviewType) { - case NotificationNamePreview: - case NotificationNameNoPreview: - alertBodyString = [NSString stringWithFormat:@"%@: %@", authorName, messageDescription]; - break; - case NotificationNoNameNoPreview: - alertBodyString = messageDescription; - break; - } - notification.alertBody = alertBodyString; - - [[PushManager sharedManager] presentNotification:notification checkForCancel:NO]; - } else { - if (shouldPlaySound && [Environment.preferences soundInForeground]) { - OWSSound sound = [OWSSounds notificationSoundForThread:thread]; - SystemSoundID soundId = [OWSSounds systemSoundIDForSound:sound quiet:YES]; - // Vibrate, respect silent switch, respect "Alert" volume, not media volume. - AudioServicesPlayAlertSound(soundId); - } - } - }); + NSString *messageText = [message previewTextWithTransaction:transaction]; + + [transaction + addCompletionQueue:nil + completionBlock:^() { + if (thread.isMuted) { + return; + } + + BOOL shouldPlaySound = [self shouldPlaySoundForNotification]; + + if (([UIApplication sharedApplication].applicationState != UIApplicationStateActive) && messageText) { + UILocalNotification *notification = [[UILocalNotification alloc] init]; + notification.userInfo = @{ Signal_Thread_UserInfo_Key : thread.uniqueId }; + if (shouldPlaySound) { + OWSSound sound = [OWSSounds notificationSoundForThread:thread]; + notification.soundName = [OWSSounds filenameForSound:sound]; + } + + NSString *alertBodyString = @""; + + NSString *authorName = [thread name]; + switch (self.notificationPreviewType) { + case NotificationNamePreview: + case NotificationNameNoPreview: + alertBodyString = [NSString stringWithFormat:@"%@: %@", authorName, messageText]; + break; + case NotificationNoNameNoPreview: + alertBodyString = messageText; + break; + } + notification.alertBody = alertBodyString; + + [[PushManager sharedManager] presentNotification:notification checkForCancel:NO]; + } else { + if (shouldPlaySound && [Environment.preferences soundInForeground]) { + OWSSound sound = [OWSSounds notificationSoundForThread:thread]; + SystemSoundID soundId = [OWSSounds systemSoundIDForSound:sound quiet:YES]; + // Vibrate, respect silent switch, respect "Alert" volume, not media volume. + AudioServicesPlayAlertSound(soundId); + } + } + }]; } - (void)notifyUserForIncomingMessage:(TSIncomingMessage *)message diff --git a/SignalMessaging/environment/NoopNotificationsManager.swift b/SignalMessaging/environment/NoopNotificationsManager.swift index 865e47c98..a801a7d8d 100644 --- a/SignalMessaging/environment/NoopNotificationsManager.swift +++ b/SignalMessaging/environment/NoopNotificationsManager.swift @@ -7,11 +7,11 @@ import SignalServiceKit @objc public class NoopNotificationsManager: NSObject, NotificationsProtocol { - public func notifyUser(for incomingMessage: TSIncomingMessage!, in thread: TSThread!, contactsManager: ContactsManagerProtocol!, transaction: YapDatabaseReadTransaction!) { + public func notifyUser(for incomingMessage: TSIncomingMessage, in thread: TSThread, contactsManager: ContactsManagerProtocol, transaction: YapDatabaseReadTransaction) { owsFail("\(self.logTag) in \(#function).") } - public func notifyUser(for error: TSErrorMessage!, in thread: TSThread!) { + public func notifyUser(for error: TSErrorMessage, thread: TSThread, transaction: YapDatabaseReadWriteTransaction) { Logger.warn("\(self.logTag) in \(#function), skipping notification for: \(error.description)") } } diff --git a/SignalServiceKit/src/Messages/OWSIdentityManager.m b/SignalServiceKit/src/Messages/OWSIdentityManager.m index ed76aa21c..78e0b60eb 100644 --- a/SignalServiceKit/src/Messages/OWSIdentityManager.m +++ b/SignalServiceKit/src/Messages/OWSIdentityManager.m @@ -545,7 +545,9 @@ NSString *const kNSNotificationName_IdentityStateDidChange = @"kNSNotificationNa [message saveWithTransaction:transaction]; } - [[TextSecureKitEnv sharedEnv].notificationsManager notifyUserForErrorMessage:errorMessage inThread:contactThread]; + [[TextSecureKitEnv sharedEnv].notificationsManager notifyUserForErrorMessage:errorMessage + thread:contactThread + transaction:transaction]; } - (void)enqueueSyncMessageForVerificationStateForRecipientId:(NSString *)recipientId diff --git a/SignalServiceKit/src/Messages/OWSMessageDecrypter.m b/SignalServiceKit/src/Messages/OWSMessageDecrypter.m index 220b36983..dffbad90e 100644 --- a/SignalServiceKit/src/Messages/OWSMessageDecrypter.m +++ b/SignalServiceKit/src/Messages/OWSMessageDecrypter.m @@ -269,8 +269,10 @@ NS_ASSUME_NONNULL_BEGIN exception.name, exception.reason); - __block TSErrorMessage *errorMessage; + [self.dbConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) { + TSErrorMessage *errorMessage; + if ([exception.name isEqualToString:NoSessionException]) { OWSProdErrorWEnvelope([OWSAnalyticsEvents messageManagerErrorNoSession], envelope); errorMessage = [TSErrorMessage missingSessionWithEnvelope:envelope withTransaction:transaction]; @@ -298,18 +300,21 @@ NS_ASSUME_NONNULL_BEGIN } OWSAssert(errorMessage); - [errorMessage saveWithTransaction:transaction]; + if (errorMessage != nil) { + [errorMessage saveWithTransaction:transaction]; + [self notifyUserForErrorMessage:errorMessage envelope:envelope transaction:transaction]; + } }]; - - if (errorMessage != nil) { - [self notifyForErrorMessage:errorMessage withEnvelope:envelope]; - } } -- (void)notifyForErrorMessage:(TSErrorMessage *)errorMessage withEnvelope:(OWSSignalServiceProtosEnvelope *)envelope +- (void)notifyUserForErrorMessage:(TSErrorMessage *)errorMessage + envelope:(OWSSignalServiceProtosEnvelope *)envelope + transaction:(YapDatabaseReadWriteTransaction *)transaction { - TSThread *contactThread = [TSContactThread getOrCreateThreadWithContactId:envelope.source]; - [[TextSecureKitEnv sharedEnv].notificationsManager notifyUserForErrorMessage:errorMessage inThread:contactThread]; + TSThread *contactThread = [TSContactThread getOrCreateThreadWithContactId:envelope.source transaction:transaction]; + [[TextSecureKitEnv sharedEnv].notificationsManager notifyUserForErrorMessage:errorMessage + thread:contactThread + transaction:transaction]; } @end diff --git a/SignalServiceKit/src/Messages/TSCall.h b/SignalServiceKit/src/Messages/TSCall.h index bcadbaf8b..fdca7d4d1 100644 --- a/SignalServiceKit/src/Messages/TSCall.h +++ b/SignalServiceKit/src/Messages/TSCall.h @@ -20,7 +20,7 @@ typedef enum { RPRecentCallTypeIncomingDeclined } RPRecentCallType; -@interface TSCall : TSInteraction +@interface TSCall : TSInteraction @property (nonatomic, readonly) RPRecentCallType callType; diff --git a/SignalServiceKit/src/Messages/TSCall.m b/SignalServiceKit/src/Messages/TSCall.m index cc49de57a..20d805c09 100644 --- a/SignalServiceKit/src/Messages/TSCall.m +++ b/SignalServiceKit/src/Messages/TSCall.m @@ -69,11 +69,7 @@ NSUInteger TSCallCurrentSchemaVersion = 1; - (NSString *)previewTextWithTransaction:(YapDatabaseReadTransaction *)transaction { - return [self previewText]; -} - -- (NSString *)previewText -{ + // We don't actually use the `transaction` but other sibling classes do. switch (_callType) { case RPRecentCallTypeIncoming: return NSLocalizedString(@"INCOMING_CALL", @""); @@ -93,12 +89,6 @@ NSUInteger TSCallCurrentSchemaVersion = 1; } } -- (NSString *)description -{ - OWSFail(@"%@ in %s verify this isnt exposed in the UI", self.logTag, __PRETTY_FUNCTION__); - return [self previewText]; -} - #pragma mark - OWSReadTracking - (uint64_t)expireStartedAt diff --git a/SignalServiceKit/src/Protocols/NotificationsProtocol.h b/SignalServiceKit/src/Protocols/NotificationsProtocol.h index 183cec078..892c80eed 100644 --- a/SignalServiceKit/src/Protocols/NotificationsProtocol.h +++ b/SignalServiceKit/src/Protocols/NotificationsProtocol.h @@ -1,11 +1,14 @@ // -// Copyright (c) 2017 Open Whisper Systems. All rights reserved. +// Copyright (c) 2018 Open Whisper Systems. All rights reserved. // +NS_ASSUME_NONNULL_BEGIN + @class TSErrorMessage; @class TSIncomingMessage; @class TSThread; @class YapDatabaseReadTransaction; + @protocol ContactsManagerProtocol; @protocol NotificationsProtocol @@ -15,6 +18,10 @@ contactsManager:(id)contactsManager transaction:(YapDatabaseReadTransaction *)transaction; -- (void)notifyUserForErrorMessage:(TSErrorMessage *)error inThread:(TSThread *)thread; +- (void)notifyUserForErrorMessage:(TSErrorMessage *)error + thread:(TSThread *)thread + transaction:(YapDatabaseReadWriteTransaction *)transaction; @end + +NS_ASSUME_NONNULL_END