Notify users of corrupt messages.

This commit is contained in:
Matthew Chen 2018-04-17 15:23:05 -04:00
parent 371b247b4f
commit c5981b164b
10 changed files with 98 additions and 1 deletions

View File

@ -206,7 +206,13 @@
transaction:(YapDatabaseReadWriteTransaction *)transaction
{
OWSAssert(message);
OWSAssert(thread);
if (!thread) {
OWSProdLogAndFail(
@"%@ unexpected notification not associated with a thread: %@.", self.logTag, [message class]);
[self notifyUserForThreadlessErrorMessage:message transaction:transaction];
return;
}
NSString *messageText = [message previewTextWithTransaction:transaction];
@ -253,6 +259,40 @@
}];
}
- (void)notifyUserForThreadlessErrorMessage:(TSErrorMessage *)message
transaction:(YapDatabaseReadWriteTransaction *)transaction;
{
OWSAssert(message);
NSString *messageText = [message previewTextWithTransaction:transaction];
[transaction
addCompletionQueue:nil
completionBlock:^() {
BOOL shouldPlaySound = [self shouldPlaySoundForNotification];
if (([UIApplication sharedApplication].applicationState != UIApplicationStateActive) && messageText) {
UILocalNotification *notification = [[UILocalNotification alloc] init];
if (shouldPlaySound) {
OWSSound sound = [OWSSounds globalNotificationSound];
notification.soundName = [OWSSounds filenameForSound:sound];
}
NSString *alertBodyString = messageText;
notification.alertBody = alertBodyString;
[[PushManager sharedManager] presentNotification:notification checkForCancel:NO];
} else {
if (shouldPlaySound && [Environment.preferences soundInForeground]) {
OWSSound sound = [OWSSounds globalNotificationSound];
SystemSoundID soundId = [OWSSounds systemSoundIDForSound:sound quiet:YES];
// Vibrate, respect silent switch, respect "Alert" volume, not media volume.
AudioServicesPlayAlertSound(soundId);
}
}
}];
}
- (void)notifyUserForIncomingMessage:(TSIncomingMessage *)message
inThread:(TSThread *)thread
contactsManager:(id<ContactsManagerProtocol>)contactsManager

View File

@ -279,6 +279,9 @@ NSString *const Signal_Message_MarkAsRead_Identifier = @"Signal_Message_MarkAsRe
- (void)markAllInThreadAsRead:(NSDictionary *)userInfo completionHandler:(void (^)(void))completionHandler
{
NSString *threadId = userInfo[Signal_Thread_UserInfo_Key];
if (!threadId) {
return;
}
TSThread *thread = [TSThread fetchObjectWithUniqueID:threadId];
[OWSPrimaryStorage.dbReadWriteConnection

View File

@ -14,4 +14,8 @@ public class NoopNotificationsManager: NSObject, NotificationsProtocol {
public func notifyUser(for error: TSErrorMessage, thread: TSThread, transaction: YapDatabaseReadWriteTransaction) {
Logger.warn("\(self.logTag) in \(#function), skipping notification for: \(error.description)")
}
public func notifyUser(forThreadlessErrorMessage error: TSErrorMessage, transaction: YapDatabaseReadWriteTransaction) {
Logger.warn("\(self.logTag) in \(#function), skipping notification for: \(error.description)")
}
}

View File

@ -56,6 +56,8 @@ typedef NS_ENUM(int32_t, TSErrorMessageType) {
+ (instancetype)corruptedMessageWithEnvelope:(OWSSignalServiceProtosEnvelope *)envelope
withTransaction:(YapDatabaseReadWriteTransaction *)transaction;
+ (instancetype)corruptedMessageInUnknownThread;
+ (instancetype)invalidVersionWithEnvelope:(OWSSignalServiceProtosEnvelope *)envelope
withTransaction:(YapDatabaseReadWriteTransaction *)transaction;

View File

@ -92,6 +92,11 @@ NSUInteger TSErrorMessageSchemaVersion = 1;
return [self initWithTimestamp:envelope.timestamp inThread:contactThread failedMessageType:errorMessageType];
}
- (instancetype)initWithFailedMessageType:(TSErrorMessageType)errorMessageType
{
return [self initWithTimestamp:[NSDate ows_millisecondTimeStamp] inThread:nil failedMessageType:errorMessageType];
}
- (OWSInteractionType)interactionType
{
return OWSInteractionType_Error;
@ -147,6 +152,11 @@ NSUInteger TSErrorMessageSchemaVersion = 1;
failedMessageType:TSErrorMessageInvalidMessage];
}
+ (instancetype)corruptedMessageInUnknownThread
{
return [[self alloc] initWithFailedMessageType:TSErrorMessageInvalidMessage];
}
+ (instancetype)invalidVersionWithEnvelope:(OWSSignalServiceProtosEnvelope *)envelope
withTransaction:(YapDatabaseReadWriteTransaction *)transaction
{

View File

@ -6,6 +6,7 @@
#import "AppContext.h"
#import "AppReadiness.h"
#import "NSArray+OWS.h"
#import "NotificationsProtocol.h"
#import "OWSBackgroundTask.h"
#import "OWSMessageManager.h"
#import "OWSPrimaryStorage+SessionStore.h"
@ -14,7 +15,9 @@
#import "OWSSignalServiceProtos.pb.h"
#import "OWSStorage.h"
#import "TSDatabaseView.h"
#import "TSErrorMessage.h"
#import "TSYapDatabaseObject.h"
#import "TextSecureKitEnv.h"
#import "Threading.h"
#import <YapDatabase/YapDatabaseAutoView.h>
#import <YapDatabase/YapDatabaseConnection.h>
@ -382,6 +385,9 @@ NSString *const OWSMessageContentJobFinderExtensionGroup = @"OWSMessageContentJo
} @catch (NSException *exception) {
OWSProdLogAndFail(@"%@ Received an invalid envelope: %@", self.logTag, exception.debugDescription);
// TODO: Add analytics.
TSErrorMessage *errorMessage = [TSErrorMessage corruptedMessageInUnknownThread];
[[TextSecureKitEnv sharedEnv].notificationsManager notifyUserForThreadlessErrorMessage:errorMessage
transaction:transaction];
}
[processedJobs addObject:job];

View File

@ -170,6 +170,13 @@ NS_ASSUME_NONNULL_BEGIN
} @catch (NSException *exception) {
OWSProdLogAndFail(@"%@ Received an invalid envelope: %@", self.logTag, exception.debugDescription);
OWSProdFail([OWSAnalyticsEvents messageManagerErrorInvalidProtocolMessage]);
[[OWSPrimaryStorage.sharedManager newDatabaseConnection]
readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) {
TSErrorMessage *errorMessage = [TSErrorMessage corruptedMessageInUnknownThread];
[[TextSecureKitEnv sharedEnv].notificationsManager notifyUserForThreadlessErrorMessage:errorMessage
transaction:transaction];
}];
}
failureBlock();

View File

@ -6,6 +6,7 @@
#import "AppContext.h"
#import "AppReadiness.h"
#import "NSArray+OWS.h"
#import "NotificationsProtocol.h"
#import "OWSBackgroundTask.h"
#import "OWSBatchMessageProcessor.h"
#import "OWSMessageDecrypter.h"
@ -14,7 +15,9 @@
#import "OWSSignalServiceProtos.pb.h"
#import "OWSStorage.h"
#import "TSDatabaseView.h"
#import "TSErrorMessage.h"
#import "TSYapDatabaseObject.h"
#import "TextSecureKitEnv.h"
#import "Threading.h"
#import <YapDatabase/YapDatabaseAutoView.h>
#import <YapDatabase/YapDatabaseConnection.h>
@ -322,6 +325,14 @@ NSString *const OWSMessageDecryptJobFinderExtensionGroup = @"OWSMessageProcessin
} @catch (NSException *exception) {
OWSProdLogAndFail(@"%@ Could not parse proto: %@", self.logTag, exception.debugDescription);
// TODO: Add analytics.
[[OWSPrimaryStorage.sharedManager newDatabaseConnection]
readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) {
TSErrorMessage *errorMessage = [TSErrorMessage corruptedMessageInUnknownThread];
[[TextSecureKitEnv sharedEnv].notificationsManager notifyUserForThreadlessErrorMessage:errorMessage
transaction:transaction];
}];
dispatch_async(self.serialQueue, ^{
completion(NO);
});

View File

@ -8,15 +8,19 @@
#import "Cryptography.h"
#import "NSNotificationCenter+OWS.h"
#import "NSTimer+OWS.h"
#import "NotificationsProtocol.h"
#import "OWSBackgroundTask.h"
#import "OWSMessageManager.h"
#import "OWSMessageReceiver.h"
#import "OWSPrimaryStorage.h"
#import "OWSSignalService.h"
#import "OWSSignalServiceProtos.pb.h"
#import "OWSWebsocketSecurityPolicy.h"
#import "SubProtocol.pb.h"
#import "TSAccountManager.h"
#import "TSConstants.h"
#import "TSErrorMessage.h"
#import "TextSecureKitEnv.h"
#import "Threading.h"
static const CGFloat kSocketHeartbeatPeriodSeconds = 30.f;
@ -403,6 +407,13 @@ NSString *const kNSNotification_SocketManagerStateDidChange = @"kNSNotification_
} @catch (NSException *exception) {
OWSProdLogAndFail(@"%@ Received an invalid envelope: %@", self.logTag, exception.debugDescription);
// TODO: Add analytics.
[[OWSPrimaryStorage.sharedManager newDatabaseConnection] readWriteWithBlock:^(
YapDatabaseReadWriteTransaction *transaction) {
TSErrorMessage *errorMessage = [TSErrorMessage corruptedMessageInUnknownThread];
[[TextSecureKitEnv sharedEnv].notificationsManager notifyUserForThreadlessErrorMessage:errorMessage
transaction:transaction];
}];
}
dispatch_async(dispatch_get_main_queue(), ^{

View File

@ -22,6 +22,9 @@ NS_ASSUME_NONNULL_BEGIN
thread:(TSThread *)thread
transaction:(YapDatabaseReadWriteTransaction *)transaction;
- (void)notifyUserForThreadlessErrorMessage:(TSErrorMessage *)error
transaction:(YapDatabaseReadWriteTransaction *)transaction;
@end
NS_ASSUME_NONNULL_END