2017-01-24 21:47:41 +01:00
|
|
|
//
|
2018-01-11 15:56:38 +01:00
|
|
|
// Copyright (c) 2018 Open Whisper Systems. All rights reserved.
|
2017-01-24 21:47:41 +01:00
|
|
|
//
|
2015-12-07 03:31:43 +01:00
|
|
|
|
2017-09-14 17:00:30 +02:00
|
|
|
#import "OWSMessageManager.h"
|
2017-11-29 21:27:19 +01:00
|
|
|
#import "AppContext.h"
|
2018-01-26 22:11:05 +01:00
|
|
|
#import "AppReadiness.h"
|
2016-08-26 01:01:35 +02:00
|
|
|
#import "ContactsManagerProtocol.h"
|
|
|
|
#import "MimeTypeUtil.h"
|
2016-12-18 22:08:26 +01:00
|
|
|
#import "NotificationsProtocol.h"
|
2016-10-14 23:00:29 +02:00
|
|
|
#import "OWSAttachmentsProcessor.h"
|
2017-04-03 20:42:04 +02:00
|
|
|
#import "OWSBlockingManager.h"
|
2016-12-18 22:08:26 +01:00
|
|
|
#import "OWSCallMessageHandler.h"
|
2018-05-01 14:52:59 +02:00
|
|
|
#import "OWSContact.h"
|
2017-11-16 15:54:55 +01:00
|
|
|
#import "OWSDevice.h"
|
2016-10-05 17:42:44 +02:00
|
|
|
#import "OWSDisappearingConfigurationUpdateInfoMessage.h"
|
|
|
|
#import "OWSDisappearingMessagesConfiguration.h"
|
|
|
|
#import "OWSDisappearingMessagesJob.h"
|
2017-09-14 17:00:30 +02:00
|
|
|
#import "OWSIdentityManager.h"
|
2017-02-16 00:32:27 +01:00
|
|
|
#import "OWSIncomingMessageFinder.h"
|
2016-08-23 22:38:05 +02:00
|
|
|
#import "OWSIncomingSentMessageTranscript.h"
|
2016-10-14 23:00:29 +02:00
|
|
|
#import "OWSMessageSender.h"
|
2018-01-30 21:05:04 +01:00
|
|
|
#import "OWSMessageUtils.h"
|
2018-03-05 15:30:58 +01:00
|
|
|
#import "OWSPrimaryStorage+SessionStore.h"
|
|
|
|
#import "OWSPrimaryStorage.h"
|
2017-09-15 21:28:44 +02:00
|
|
|
#import "OWSReadReceiptManager.h"
|
2016-10-05 17:42:44 +02:00
|
|
|
#import "OWSRecordTranscriptJob.h"
|
2017-10-02 22:35:24 +02:00
|
|
|
#import "OWSSyncConfigurationMessage.h"
|
2016-08-26 01:01:35 +02:00
|
|
|
#import "OWSSyncContactsMessage.h"
|
2016-08-27 00:07:54 +02:00
|
|
|
#import "OWSSyncGroupsMessage.h"
|
2017-05-05 23:22:56 +02:00
|
|
|
#import "OWSSyncGroupsRequestMessage.h"
|
2017-08-02 19:12:26 +02:00
|
|
|
#import "ProfileManagerProtocol.h"
|
2018-08-31 19:12:40 +02:00
|
|
|
#import "SSKEnvironment.h"
|
2016-04-08 09:38:34 +02:00
|
|
|
#import "TSAccountManager.h"
|
2018-04-05 18:01:53 +02:00
|
|
|
#import "TSAttachment.h"
|
|
|
|
#import "TSAttachmentPointer.h"
|
2018-04-07 02:08:17 +02:00
|
|
|
#import "TSAttachmentStream.h"
|
2016-07-28 01:58:49 +02:00
|
|
|
#import "TSContactThread.h"
|
2015-12-07 03:31:43 +01:00
|
|
|
#import "TSDatabaseView.h"
|
2016-07-28 01:58:49 +02:00
|
|
|
#import "TSGroupModel.h"
|
|
|
|
#import "TSGroupThread.h"
|
2017-09-14 17:00:30 +02:00
|
|
|
#import "TSIncomingMessage.h"
|
2015-12-07 03:31:43 +01:00
|
|
|
#import "TSInfoMessage.h"
|
2016-09-23 22:55:56 +02:00
|
|
|
#import "TSNetworkManager.h"
|
2017-09-14 17:00:30 +02:00
|
|
|
#import "TSOutgoingMessage.h"
|
2018-02-07 18:44:09 +01:00
|
|
|
#import "TSQuotedMessage.h"
|
2018-09-25 19:09:55 +02:00
|
|
|
#import <SignalCoreKit/Cryptography.h>
|
2018-09-21 21:41:10 +02:00
|
|
|
#import <SignalCoreKit/NSDate+OWS.h>
|
|
|
|
#import <SignalCoreKit/NSString+SSK.h>
|
2018-06-07 07:57:59 +02:00
|
|
|
#import <SignalServiceKit/SignalServiceKit-Swift.h>
|
2017-12-19 03:42:50 +01:00
|
|
|
#import <YapDatabase/YapDatabase.h>
|
2015-12-07 03:31:43 +01:00
|
|
|
|
2016-10-05 17:42:44 +02:00
|
|
|
NS_ASSUME_NONNULL_BEGIN
|
|
|
|
|
2017-09-14 17:00:30 +02:00
|
|
|
@interface OWSMessageManager ()
|
2016-10-01 20:42:39 +02:00
|
|
|
|
2018-03-05 15:30:58 +01:00
|
|
|
@property (nonatomic, readonly) OWSPrimaryStorage *primaryStorage;
|
2017-09-14 17:00:30 +02:00
|
|
|
@property (nonatomic, readonly) YapDatabaseConnection *dbConnection;
|
2018-09-20 18:52:43 +02:00
|
|
|
@property (nonatomic, readonly) OWSIncomingMessageFinder *incomingMessageFinder;
|
2016-10-01 20:42:39 +02:00
|
|
|
|
|
|
|
@end
|
|
|
|
|
2017-05-09 20:38:49 +02:00
|
|
|
#pragma mark -
|
|
|
|
|
2017-09-14 17:00:30 +02:00
|
|
|
@implementation OWSMessageManager
|
2015-12-07 03:31:43 +01:00
|
|
|
|
2017-09-14 17:00:30 +02:00
|
|
|
+ (instancetype)sharedManager
|
|
|
|
{
|
2018-09-20 18:52:43 +02:00
|
|
|
OWSAssertDebug(SSKEnvironment.shared.messageManager);
|
2015-12-07 03:31:43 +01:00
|
|
|
|
2018-09-20 18:52:43 +02:00
|
|
|
return SSKEnvironment.shared.messageManager;
|
2016-09-23 22:55:56 +02:00
|
|
|
}
|
|
|
|
|
2018-09-20 18:52:43 +02:00
|
|
|
- (instancetype)initWithPrimaryStorage:(OWSPrimaryStorage *)primaryStorage
|
2016-09-23 22:55:56 +02:00
|
|
|
{
|
2015-12-07 03:31:43 +01:00
|
|
|
self = [super init];
|
|
|
|
|
2016-09-23 22:55:56 +02:00
|
|
|
if (!self) {
|
|
|
|
return self;
|
2015-12-07 03:31:43 +01:00
|
|
|
}
|
|
|
|
|
2018-03-05 15:30:58 +01:00
|
|
|
_primaryStorage = primaryStorage;
|
|
|
|
_dbConnection = primaryStorage.newDatabaseConnection;
|
|
|
|
_incomingMessageFinder = [[OWSIncomingMessageFinder alloc] initWithPrimaryStorage:primaryStorage];
|
2017-04-01 00:36:08 +02:00
|
|
|
|
|
|
|
OWSSingletonAssert();
|
2018-09-06 19:01:24 +02:00
|
|
|
OWSAssertDebug(CurrentAppContext().isMainApp);
|
2017-04-01 00:36:08 +02:00
|
|
|
|
2015-12-07 03:31:43 +01:00
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2018-09-20 18:52:43 +02:00
|
|
|
- (id<OWSCallMessageHandler>)callMessageHandler
|
|
|
|
{
|
|
|
|
OWSAssertDebug(SSKEnvironment.shared.callMessageHandler);
|
|
|
|
|
|
|
|
return SSKEnvironment.shared.callMessageHandler;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (id<ContactsManagerProtocol>)contactsManager
|
|
|
|
{
|
|
|
|
OWSAssertDebug(SSKEnvironment.shared.contactsManager);
|
|
|
|
|
|
|
|
return SSKEnvironment.shared.contactsManager;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (OWSMessageSender *)messageSender
|
|
|
|
{
|
|
|
|
OWSAssertDebug(SSKEnvironment.shared.messageSender);
|
|
|
|
|
|
|
|
return SSKEnvironment.shared.messageSender;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (OWSBlockingManager *)blockingManager
|
|
|
|
{
|
|
|
|
OWSAssertDebug(SSKEnvironment.shared.blockingManager);
|
|
|
|
|
|
|
|
return SSKEnvironment.shared.blockingManager;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (OWSIdentityManager *)identityManager
|
|
|
|
{
|
|
|
|
OWSAssertDebug(SSKEnvironment.shared.identityManager);
|
|
|
|
|
|
|
|
return SSKEnvironment.shared.identityManager;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (TSNetworkManager *)networkManager
|
|
|
|
{
|
|
|
|
OWSAssertDebug(SSKEnvironment.shared.networkManager);
|
|
|
|
|
|
|
|
return SSKEnvironment.shared.networkManager;
|
|
|
|
}
|
|
|
|
|
2017-06-30 18:12:37 +02:00
|
|
|
- (void)startObserving
|
|
|
|
{
|
|
|
|
[[NSNotificationCenter defaultCenter] addObserver:self
|
|
|
|
selector:@selector(yapDatabaseModified:)
|
|
|
|
name:YapDatabaseModifiedNotification
|
2018-03-05 15:30:58 +01:00
|
|
|
object:OWSPrimaryStorage.sharedManager.dbNotificationObject];
|
2017-11-29 17:37:49 +01:00
|
|
|
[[NSNotificationCenter defaultCenter] addObserver:self
|
|
|
|
selector:@selector(yapDatabaseModified:)
|
|
|
|
name:YapDatabaseModifiedExternallyNotification
|
2018-01-11 16:04:03 +01:00
|
|
|
object:nil];
|
2017-06-30 18:12:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)yapDatabaseModified:(NSNotification *)notification
|
|
|
|
{
|
2018-02-14 20:09:27 +01:00
|
|
|
if (AppReadiness.isAppReady) {
|
|
|
|
[OWSMessageUtils.sharedManager updateApplicationBadgeCount];
|
|
|
|
} else {
|
|
|
|
static dispatch_once_t onceToken;
|
|
|
|
dispatch_once(&onceToken, ^{
|
|
|
|
[AppReadiness runNowOrWhenAppIsReady:^{
|
|
|
|
[OWSMessageUtils.sharedManager updateApplicationBadgeCount];
|
|
|
|
}];
|
|
|
|
});
|
|
|
|
}
|
2017-06-30 18:12:37 +02:00
|
|
|
}
|
|
|
|
|
2017-09-13 18:14:22 +02:00
|
|
|
#pragma mark - Blocking
|
2017-04-25 21:42:49 +02:00
|
|
|
|
2018-09-09 20:35:52 +02:00
|
|
|
- (BOOL)isEnvelopeSenderBlocked:(SSKProtoEnvelope *)envelope
|
2016-08-22 22:09:58 +02:00
|
|
|
{
|
2018-09-06 19:01:24 +02:00
|
|
|
OWSAssertDebug(envelope);
|
2017-09-13 18:14:22 +02:00
|
|
|
|
2018-09-20 18:52:43 +02:00
|
|
|
return [self.blockingManager isRecipientIdBlocked:envelope.source];
|
2017-09-13 18:14:22 +02:00
|
|
|
}
|
|
|
|
|
2018-09-09 20:35:52 +02:00
|
|
|
- (BOOL)isDataMessageBlocked:(SSKProtoDataMessage *)dataMessage envelope:(SSKProtoEnvelope *)envelope
|
|
|
|
{
|
2018-09-15 16:17:08 +02:00
|
|
|
OWSAssertDebug(dataMessage);
|
|
|
|
OWSAssertDebug(envelope);
|
2018-09-09 20:35:52 +02:00
|
|
|
|
|
|
|
if (dataMessage.group) {
|
|
|
|
return [self.blockingManager isGroupIdBlocked:dataMessage.group.id];
|
|
|
|
} else {
|
|
|
|
BOOL senderBlocked = [self isEnvelopeSenderBlocked:envelope];
|
|
|
|
|
|
|
|
// If the envelopeSender was blocked, we never should have gotten as far as decrypting the dataMessage.
|
2018-09-15 16:17:08 +02:00
|
|
|
OWSAssertDebug(!senderBlocked);
|
2018-09-09 20:35:52 +02:00
|
|
|
|
|
|
|
return senderBlocked;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-13 18:14:22 +02:00
|
|
|
#pragma mark - message handling
|
|
|
|
|
2018-08-01 16:45:21 +02:00
|
|
|
- (void)processEnvelope:(SSKProtoEnvelope *)envelope
|
2017-09-13 21:35:33 +02:00
|
|
|
plaintextData:(NSData *_Nullable)plaintextData
|
|
|
|
transaction:(YapDatabaseReadWriteTransaction *)transaction
|
2016-08-22 22:09:58 +02:00
|
|
|
{
|
2018-08-30 16:31:01 +02:00
|
|
|
if (!envelope) {
|
|
|
|
OWSFailDebug(@"Missing envelope.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!transaction) {
|
2018-09-11 00:56:22 +02:00
|
|
|
OWSFail(@"Missing transaction.");
|
2018-08-30 16:31:01 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!TSAccountManager.isRegistered) {
|
|
|
|
OWSFailDebug(@"Not registered.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!CurrentAppContext().isMainApp) {
|
|
|
|
OWSFail(@"Not main app.");
|
|
|
|
return;
|
|
|
|
}
|
2017-02-10 01:35:10 +01:00
|
|
|
|
2018-08-27 18:51:32 +02:00
|
|
|
OWSLogInfo(@"handling decrypted envelope: %@", [self descriptionForEnvelope:envelope]);
|
2015-12-07 03:31:43 +01:00
|
|
|
|
2018-02-16 02:45:28 +01:00
|
|
|
if (!envelope.source.isValidE164) {
|
2018-08-27 18:51:32 +02:00
|
|
|
OWSFailDebug(@"incoming envelope has invalid source");
|
2018-02-16 02:45:28 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-09-06 19:01:24 +02:00
|
|
|
OWSAssertDebug(envelope.source.length > 0);
|
2018-09-15 16:17:08 +02:00
|
|
|
OWSAssertDebug(![self isEnvelopeSenderBlocked:envelope]);
|
2015-12-07 03:31:43 +01:00
|
|
|
|
2017-09-13 18:14:22 +02:00
|
|
|
switch (envelope.type) {
|
2018-08-01 16:45:21 +02:00
|
|
|
case SSKProtoEnvelopeTypeCiphertext:
|
|
|
|
case SSKProtoEnvelopeTypePrekeyBundle:
|
2018-08-30 16:31:01 +02:00
|
|
|
if (!plaintextData) {
|
2018-08-27 18:51:32 +02:00
|
|
|
OWSFailDebug(@"missing decrypted data for envelope: %@", [self descriptionForEnvelope:envelope]);
|
2018-08-30 16:31:01 +02:00
|
|
|
return;
|
2017-01-24 21:47:41 +01:00
|
|
|
}
|
2018-08-30 16:31:01 +02:00
|
|
|
[self handleEnvelope:envelope plaintextData:plaintextData transaction:transaction];
|
2017-09-13 18:14:22 +02:00
|
|
|
break;
|
2018-08-01 16:45:21 +02:00
|
|
|
case SSKProtoEnvelopeTypeReceipt:
|
2018-09-06 19:01:24 +02:00
|
|
|
OWSAssertDebug(!plaintextData);
|
2017-09-13 21:35:33 +02:00
|
|
|
[self handleDeliveryReceipt:envelope transaction:transaction];
|
2017-09-13 18:14:22 +02:00
|
|
|
break;
|
2018-04-16 20:48:29 +02:00
|
|
|
// Other messages are just dismissed for now.
|
2018-08-01 16:45:21 +02:00
|
|
|
case SSKProtoEnvelopeTypeKeyExchange:
|
2018-08-27 18:00:28 +02:00
|
|
|
OWSLogWarn(@"Received Key Exchange Message, not supported");
|
2017-09-13 18:14:22 +02:00
|
|
|
break;
|
2018-08-01 16:45:21 +02:00
|
|
|
case SSKProtoEnvelopeTypeUnknown:
|
2018-08-27 18:00:28 +02:00
|
|
|
OWSLogWarn(@"Received an unknown message type");
|
2017-09-13 18:14:22 +02:00
|
|
|
break;
|
|
|
|
default:
|
2018-08-27 18:00:28 +02:00
|
|
|
OWSLogWarn(@"Received unhandled envelope type: %d", (int)envelope.type);
|
2017-09-13 18:14:22 +02:00
|
|
|
break;
|
2016-12-06 03:32:11 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-01 16:45:21 +02:00
|
|
|
- (void)handleDeliveryReceipt:(SSKProtoEnvelope *)envelope transaction:(YapDatabaseReadWriteTransaction *)transaction
|
2017-09-13 18:14:22 +02:00
|
|
|
{
|
2018-08-30 16:31:01 +02:00
|
|
|
if (!envelope) {
|
|
|
|
OWSFailDebug(@"Missing envelope.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!transaction) {
|
2018-09-11 00:56:22 +02:00
|
|
|
OWSFail(@"Missing transaction.");
|
2018-08-30 16:31:01 +02:00
|
|
|
return;
|
|
|
|
}
|
2017-09-13 21:35:33 +02:00
|
|
|
|
2017-09-22 23:23:09 +02:00
|
|
|
// Old-style delivery notices don't include a "delivery timestamp".
|
2017-09-27 20:19:26 +02:00
|
|
|
[self processDeliveryReceiptsFromRecipientId:envelope.source
|
|
|
|
sentTimestamps:@[
|
|
|
|
@(envelope.timestamp),
|
|
|
|
]
|
|
|
|
deliveryTimestamp:nil
|
|
|
|
transaction:transaction];
|
2017-09-22 22:40:44 +02:00
|
|
|
}
|
|
|
|
|
2017-09-27 20:19:26 +02:00
|
|
|
// deliveryTimestamp is an optional parameter, since legacy
|
|
|
|
// delivery receipts don't have a "delivery timestamp". Those
|
|
|
|
// messages repurpose the "timestamp" field to indicate when the
|
|
|
|
// corresponding message was originally sent.
|
|
|
|
- (void)processDeliveryReceiptsFromRecipientId:(NSString *)recipientId
|
|
|
|
sentTimestamps:(NSArray<NSNumber *> *)sentTimestamps
|
|
|
|
deliveryTimestamp:(NSNumber *_Nullable)deliveryTimestamp
|
|
|
|
transaction:(YapDatabaseReadWriteTransaction *)transaction
|
2017-09-22 22:40:44 +02:00
|
|
|
{
|
2018-08-30 16:31:01 +02:00
|
|
|
if (recipientId.length < 1) {
|
|
|
|
OWSFailDebug(@"Empty recipientId.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (sentTimestamps.count < 1) {
|
|
|
|
OWSFailDebug(@"Missing sentTimestamps.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!transaction) {
|
2018-09-11 00:56:22 +02:00
|
|
|
OWSFail(@"Missing transaction.");
|
2018-08-30 16:31:01 +02:00
|
|
|
return;
|
|
|
|
}
|
2017-09-22 22:40:44 +02:00
|
|
|
|
|
|
|
for (NSNumber *nsTimestamp in sentTimestamps) {
|
|
|
|
uint64_t timestamp = [nsTimestamp unsignedLongLongValue];
|
|
|
|
|
|
|
|
NSArray<TSOutgoingMessage *> *messages
|
2018-09-20 22:10:35 +02:00
|
|
|
= (NSArray<TSOutgoingMessage *> *)[TSInteraction interactionsWithTimestamp:timestamp
|
|
|
|
ofClass:[TSOutgoingMessage class]
|
|
|
|
withTransaction:transaction];
|
2017-09-22 22:40:44 +02:00
|
|
|
if (messages.count < 1) {
|
2017-09-27 20:19:26 +02:00
|
|
|
// The service sends delivery receipts for "unpersisted" messages
|
2017-09-22 22:40:44 +02:00
|
|
|
// like group updates, so these errors are expected to a certain extent.
|
2017-09-27 20:19:26 +02:00
|
|
|
//
|
|
|
|
// TODO: persist "early" delivery receipts.
|
2018-08-27 18:51:32 +02:00
|
|
|
OWSLogInfo(@"Missing message for delivery receipt: %llu", timestamp);
|
2017-09-22 22:40:44 +02:00
|
|
|
} else {
|
|
|
|
if (messages.count > 1) {
|
2018-08-27 18:55:37 +02:00
|
|
|
OWSLogInfo(@"More than one message (%lu) for delivery receipt: %llu",
|
2018-07-18 03:08:53 +02:00
|
|
|
(unsigned long)messages.count,
|
2017-11-08 20:04:51 +01:00
|
|
|
timestamp);
|
2017-09-22 22:40:44 +02:00
|
|
|
}
|
|
|
|
for (TSOutgoingMessage *outgoingMessage in messages) {
|
2018-04-23 16:30:51 +02:00
|
|
|
[outgoingMessage updateWithDeliveredRecipient:recipientId
|
|
|
|
deliveryTimestamp:deliveryTimestamp
|
|
|
|
transaction:transaction];
|
2017-09-22 22:40:44 +02:00
|
|
|
}
|
2017-09-22 21:15:04 +02:00
|
|
|
}
|
2017-09-13 21:35:33 +02:00
|
|
|
}
|
2017-09-13 18:14:22 +02:00
|
|
|
}
|
|
|
|
|
2018-08-01 16:45:21 +02:00
|
|
|
- (void)handleEnvelope:(SSKProtoEnvelope *)envelope
|
2017-09-13 21:35:33 +02:00
|
|
|
plaintextData:(NSData *)plaintextData
|
|
|
|
transaction:(YapDatabaseReadWriteTransaction *)transaction
|
2016-12-06 03:32:11 +01:00
|
|
|
{
|
2018-08-30 16:31:01 +02:00
|
|
|
if (!envelope) {
|
|
|
|
OWSFailDebug(@"Missing envelope.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!plaintextData) {
|
|
|
|
OWSFailDebug(@"Missing plaintextData.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!transaction) {
|
2018-09-11 00:56:22 +02:00
|
|
|
OWSFail(@"Missing transaction.");
|
2018-08-30 16:31:01 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (envelope.timestamp < 1) {
|
|
|
|
OWSFailDebug(@"Invalid timestamp.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (envelope.source.length < 1) {
|
|
|
|
OWSFailDebug(@"Missing source.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (envelope.sourceDevice < 1) {
|
|
|
|
OWSFailDebug(@"Invaid source device.");
|
|
|
|
return;
|
|
|
|
}
|
2017-02-16 00:32:27 +01:00
|
|
|
|
|
|
|
BOOL duplicateEnvelope = [self.incomingMessageFinder existsMessageWithTimestamp:envelope.timestamp
|
|
|
|
sourceId:envelope.source
|
2017-09-13 21:35:33 +02:00
|
|
|
sourceDeviceId:envelope.sourceDevice
|
|
|
|
transaction:transaction];
|
2017-02-16 00:32:27 +01:00
|
|
|
if (duplicateEnvelope) {
|
2018-08-27 18:55:37 +02:00
|
|
|
OWSLogInfo(@"Ignoring previously received envelope from %@ with timestamp: %llu",
|
2017-08-28 23:29:25 +02:00
|
|
|
envelopeAddress(envelope),
|
|
|
|
envelope.timestamp);
|
2017-02-16 00:32:27 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-08-03 21:50:27 +02:00
|
|
|
if (envelope.content != nil) {
|
2018-08-02 15:34:50 +02:00
|
|
|
NSError *error;
|
|
|
|
SSKProtoContent *_Nullable contentProto = [SSKProtoContent parseData:plaintextData error:&error];
|
|
|
|
if (error || !contentProto) {
|
2018-08-27 18:51:32 +02:00
|
|
|
OWSFailDebug(@"could not parse proto: %@", error);
|
2018-08-02 15:34:50 +02:00
|
|
|
return;
|
|
|
|
}
|
2018-08-27 18:51:32 +02:00
|
|
|
OWSLogInfo(@"handling content: <Content: %@>", [self descriptionForContent:contentProto]);
|
2017-08-01 19:53:51 +02:00
|
|
|
|
2018-08-08 19:41:12 +02:00
|
|
|
if (contentProto.syncMessage) {
|
2018-08-02 15:34:50 +02:00
|
|
|
[self handleIncomingEnvelope:envelope withSyncMessage:contentProto.syncMessage transaction:transaction];
|
2017-11-16 15:54:55 +01:00
|
|
|
|
|
|
|
[[OWSDeviceManager sharedManager] setHasReceivedSyncMessage];
|
2018-08-08 19:41:12 +02:00
|
|
|
} else if (contentProto.dataMessage) {
|
2018-08-02 15:34:50 +02:00
|
|
|
[self handleIncomingEnvelope:envelope withDataMessage:contentProto.dataMessage transaction:transaction];
|
2018-08-08 19:41:12 +02:00
|
|
|
} else if (contentProto.callMessage) {
|
2018-08-02 15:34:50 +02:00
|
|
|
[self handleIncomingEnvelope:envelope withCallMessage:contentProto.callMessage];
|
2018-08-08 19:41:12 +02:00
|
|
|
} else if (contentProto.nullMessage) {
|
2018-08-27 18:51:32 +02:00
|
|
|
OWSLogInfo(@"Received null message.");
|
2018-08-08 19:41:12 +02:00
|
|
|
} else if (contentProto.receiptMessage) {
|
2018-08-02 15:34:50 +02:00
|
|
|
[self handleIncomingEnvelope:envelope
|
|
|
|
withReceiptMessage:contentProto.receiptMessage
|
|
|
|
transaction:transaction];
|
2016-08-23 22:38:05 +02:00
|
|
|
} else {
|
2018-08-27 18:51:32 +02:00
|
|
|
OWSLogWarn(@"Ignoring envelope. Content with no known payload");
|
2016-08-22 22:09:58 +02:00
|
|
|
}
|
2018-08-03 21:50:27 +02:00
|
|
|
} else if (envelope.legacyMessage != nil) { // DEPRECATED - Remove after all clients have been upgraded.
|
2018-08-02 15:34:50 +02:00
|
|
|
NSError *error;
|
|
|
|
SSKProtoDataMessage *_Nullable dataMessageProto = [SSKProtoDataMessage parseData:plaintextData error:&error];
|
|
|
|
if (error || !dataMessageProto) {
|
2018-08-27 18:51:32 +02:00
|
|
|
OWSFailDebug(@"could not parse proto: %@", error);
|
2018-08-02 15:34:50 +02:00
|
|
|
return;
|
|
|
|
}
|
2018-08-27 18:51:32 +02:00
|
|
|
OWSLogInfo(@"handling message: <DataMessage: %@ />", [self descriptionForDataMessage:dataMessageProto]);
|
2017-08-04 15:33:56 +02:00
|
|
|
|
2018-08-02 15:34:50 +02:00
|
|
|
[self handleIncomingEnvelope:envelope withDataMessage:dataMessageProto transaction:transaction];
|
2016-12-06 03:32:11 +01:00
|
|
|
} else {
|
2018-07-25 22:00:25 +02:00
|
|
|
OWSProdInfoWEnvelope([OWSAnalyticsEvents messageManagerErrorEnvelopeNoActionablePayload], envelope);
|
2015-12-07 03:31:43 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-01 16:45:21 +02:00
|
|
|
- (void)handleIncomingEnvelope:(SSKProtoEnvelope *)envelope
|
2018-08-01 23:13:01 +02:00
|
|
|
withDataMessage:(SSKProtoDataMessage *)dataMessage
|
2017-09-13 21:35:33 +02:00
|
|
|
transaction:(YapDatabaseReadWriteTransaction *)transaction
|
2016-07-31 02:40:14 +02:00
|
|
|
{
|
2018-08-30 16:31:01 +02:00
|
|
|
if (!envelope) {
|
|
|
|
OWSFailDebug(@"Missing envelope.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!dataMessage) {
|
|
|
|
OWSFailDebug(@"Missing dataMessage.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!transaction) {
|
2018-09-11 00:56:22 +02:00
|
|
|
OWSFail(@"Missing transaction.");
|
2018-08-30 16:31:01 +02:00
|
|
|
return;
|
|
|
|
}
|
2017-05-05 23:22:56 +02:00
|
|
|
|
2018-09-09 20:35:52 +02:00
|
|
|
if ([self isDataMessageBlocked:dataMessage envelope:envelope]) {
|
|
|
|
NSString *logMessage = [NSString stringWithFormat:@"Ignoring blocked message from sender: %@", envelope.source];
|
|
|
|
if (dataMessage.group) {
|
|
|
|
logMessage =
|
|
|
|
[logMessage stringByAppendingString:[NSString stringWithFormat:@" in group: %@", dataMessage.group.id]];
|
|
|
|
}
|
2018-09-15 16:17:08 +02:00
|
|
|
OWSLogError(@"%@", logMessage);
|
2018-09-09 20:35:52 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-02-02 16:56:16 +01:00
|
|
|
if (dataMessage.hasTimestamp) {
|
|
|
|
if (dataMessage.timestamp <= 0) {
|
2018-08-30 16:31:01 +02:00
|
|
|
OWSFailDebug(@"Ignoring message with invalid data message timestamp: %@", envelope.source);
|
|
|
|
// TODO: Add analytics.
|
2018-02-02 16:56:16 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
// This prevents replay attacks by the service.
|
|
|
|
if (dataMessage.timestamp != envelope.timestamp) {
|
2018-08-30 16:31:01 +02:00
|
|
|
OWSFailDebug(@"Ignoring message with non-matching data message timestamp: %@", envelope.source);
|
|
|
|
// TODO: Add analytics.
|
2018-02-02 16:56:16 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-04 15:33:56 +02:00
|
|
|
if ([dataMessage hasProfileKey]) {
|
|
|
|
NSData *profileKey = [dataMessage profileKey];
|
2017-09-13 21:35:33 +02:00
|
|
|
NSString *recipientId = envelope.source;
|
2017-08-28 18:03:59 +02:00
|
|
|
if (profileKey.length == kAES256_KeyByteLength) {
|
|
|
|
[self.profileManager setProfileKeyData:profileKey forRecipientId:recipientId];
|
|
|
|
} else {
|
2018-08-27 16:29:51 +02:00
|
|
|
OWSFailDebug(
|
2017-08-28 18:03:59 +02:00
|
|
|
@"Unexpected profile key length:%lu on message from:%@", (unsigned long)profileKey.length, recipientId);
|
|
|
|
}
|
2017-08-04 15:33:56 +02:00
|
|
|
}
|
|
|
|
|
2018-08-08 19:41:12 +02:00
|
|
|
if (dataMessage.group) {
|
2017-10-04 16:06:38 +02:00
|
|
|
TSGroupThread *_Nullable groupThread =
|
2017-10-03 19:41:48 +02:00
|
|
|
[TSGroupThread threadWithGroupId:dataMessage.group.id transaction:transaction];
|
2017-10-04 16:06:38 +02:00
|
|
|
|
2018-09-12 23:06:21 +02:00
|
|
|
if (groupThread) {
|
|
|
|
if (dataMessage.group.type != SSKProtoGroupContextTypeUpdate) {
|
2018-09-13 16:57:35 +02:00
|
|
|
if (![groupThread.groupModel.groupMemberIds containsObject:[TSAccountManager localNumber]]) {
|
2018-09-15 16:17:08 +02:00
|
|
|
OWSLogInfo(@"Ignoring messages for left group.");
|
2018-09-12 23:06:21 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2017-10-04 16:06:38 +02:00
|
|
|
// Unknown group.
|
2018-08-01 23:13:01 +02:00
|
|
|
if (dataMessage.group.type == SSKProtoGroupContextTypeUpdate) {
|
2017-10-04 16:06:38 +02:00
|
|
|
// Accept group updates for unknown groups.
|
2018-08-01 23:13:01 +02:00
|
|
|
} else if (dataMessage.group.type == SSKProtoGroupContextTypeDeliver) {
|
2017-10-04 16:06:38 +02:00
|
|
|
[self sendGroupInfoRequest:dataMessage.group.id envelope:envelope transaction:transaction];
|
2017-10-03 19:41:48 +02:00
|
|
|
return;
|
2017-10-04 16:06:38 +02:00
|
|
|
} else {
|
2018-08-27 18:51:32 +02:00
|
|
|
OWSLogInfo(@"Ignoring group message for unknown group from: %@", envelope.source);
|
2017-08-08 18:16:17 +02:00
|
|
|
return;
|
|
|
|
}
|
2015-12-07 03:31:43 +01:00
|
|
|
}
|
|
|
|
}
|
2017-09-21 20:03:24 +02:00
|
|
|
|
2018-08-01 23:13:01 +02:00
|
|
|
if ((dataMessage.flags & SSKProtoDataMessageFlagsEndSession) != 0) {
|
2017-09-13 21:35:33 +02:00
|
|
|
[self handleEndSessionMessageWithEnvelope:envelope dataMessage:dataMessage transaction:transaction];
|
2018-08-01 23:13:01 +02:00
|
|
|
} else if ((dataMessage.flags & SSKProtoDataMessageFlagsExpirationTimerUpdate) != 0) {
|
2017-09-13 21:35:33 +02:00
|
|
|
[self handleExpirationTimerUpdateMessageWithEnvelope:envelope dataMessage:dataMessage transaction:transaction];
|
2018-08-01 23:13:01 +02:00
|
|
|
} else if ((dataMessage.flags & SSKProtoDataMessageFlagsProfileKeyUpdate) != 0) {
|
2017-09-13 21:35:33 +02:00
|
|
|
[self handleProfileKeyMessageWithEnvelope:envelope dataMessage:dataMessage];
|
2016-10-14 23:00:29 +02:00
|
|
|
} else if (dataMessage.attachments.count > 0) {
|
2017-09-13 21:35:33 +02:00
|
|
|
[self handleReceivedMediaWithEnvelope:envelope dataMessage:dataMessage transaction:transaction];
|
2015-12-07 03:31:43 +01:00
|
|
|
} else {
|
2017-09-13 21:35:33 +02:00
|
|
|
[self handleReceivedTextMessageWithEnvelope:envelope dataMessage:dataMessage transaction:transaction];
|
2017-10-03 19:41:48 +02:00
|
|
|
|
2016-10-14 23:00:29 +02:00
|
|
|
if ([self isDataMessageGroupAvatarUpdate:dataMessage]) {
|
2018-08-27 18:51:32 +02:00
|
|
|
OWSLogVerbose(@"Data message had group avatar attachment");
|
2017-09-13 21:35:33 +02:00
|
|
|
[self handleReceivedGroupAvatarUpdateWithEnvelope:envelope dataMessage:dataMessage transaction:transaction];
|
2016-10-14 23:00:29 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-03 19:41:48 +02:00
|
|
|
- (void)sendGroupInfoRequest:(NSData *)groupId
|
2018-08-01 16:45:21 +02:00
|
|
|
envelope:(SSKProtoEnvelope *)envelope
|
2017-10-03 19:41:48 +02:00
|
|
|
transaction:(YapDatabaseReadWriteTransaction *)transaction
|
|
|
|
{
|
2018-08-30 16:31:01 +02:00
|
|
|
if (!envelope) {
|
|
|
|
OWSFailDebug(@"Missing envelope.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!transaction) {
|
2018-09-11 00:56:22 +02:00
|
|
|
OWSFail(@"Missing transaction.");
|
2018-08-30 16:31:01 +02:00
|
|
|
return;
|
|
|
|
}
|
2017-10-03 19:41:48 +02:00
|
|
|
if (groupId.length < 1) {
|
2018-08-30 16:31:01 +02:00
|
|
|
OWSFailDebug(@"Invalid groupId.");
|
2017-10-03 19:41:48 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-02-07 23:00:45 +01:00
|
|
|
// FIXME: https://github.com/signalapp/Signal-iOS/issues/1340
|
2018-08-27 18:55:37 +02:00
|
|
|
OWSLogInfo(@"Sending group info request: %@", envelopeAddress(envelope));
|
2017-10-03 19:41:48 +02:00
|
|
|
|
|
|
|
NSString *recipientId = envelope.source;
|
|
|
|
|
|
|
|
TSThread *thread = [TSContactThread getOrCreateThreadWithContactId:recipientId transaction:transaction];
|
|
|
|
|
|
|
|
OWSSyncGroupsRequestMessage *syncGroupsRequestMessage =
|
|
|
|
[[OWSSyncGroupsRequestMessage alloc] initWithThread:thread groupId:groupId];
|
2017-11-15 19:21:31 +01:00
|
|
|
[self.messageSender enqueueMessage:syncGroupsRequestMessage
|
2017-10-03 19:41:48 +02:00
|
|
|
success:^{
|
2018-08-27 18:51:32 +02:00
|
|
|
OWSLogWarn(@"Successfully sent Request Group Info message.");
|
2017-10-03 19:41:48 +02:00
|
|
|
}
|
|
|
|
failure:^(NSError *error) {
|
2018-08-27 18:51:32 +02:00
|
|
|
OWSLogError(@"Failed to send Request Group Info message with error: %@", error);
|
2017-10-03 19:41:48 +02:00
|
|
|
}];
|
|
|
|
}
|
|
|
|
|
2017-08-25 23:16:49 +02:00
|
|
|
- (id<ProfileManagerProtocol>)profileManager
|
|
|
|
{
|
2018-09-17 15:27:58 +02:00
|
|
|
return SSKEnvironment.shared.profileManager;
|
2017-08-25 23:16:49 +02:00
|
|
|
}
|
|
|
|
|
2018-08-01 16:45:21 +02:00
|
|
|
- (void)handleIncomingEnvelope:(SSKProtoEnvelope *)envelope
|
2018-08-01 23:13:01 +02:00
|
|
|
withReceiptMessage:(SSKProtoReceiptMessage *)receiptMessage
|
2017-09-22 22:40:44 +02:00
|
|
|
transaction:(YapDatabaseReadWriteTransaction *)transaction
|
2017-09-15 21:28:44 +02:00
|
|
|
{
|
2018-08-30 16:31:01 +02:00
|
|
|
if (!envelope) {
|
|
|
|
OWSFailDebug(@"Missing envelope.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!receiptMessage) {
|
|
|
|
OWSFailDebug(@"Missing receiptMessage.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!transaction) {
|
2018-09-11 00:56:22 +02:00
|
|
|
OWSFail(@"Missing transaction.");
|
2018-08-30 16:31:01 +02:00
|
|
|
return;
|
|
|
|
}
|
2017-09-22 22:40:44 +02:00
|
|
|
|
2018-08-02 15:34:50 +02:00
|
|
|
NSArray<NSNumber *> *sentTimestamps = receiptMessage.timestamp;
|
2017-09-15 21:28:44 +02:00
|
|
|
|
|
|
|
switch (receiptMessage.type) {
|
2018-08-01 23:13:01 +02:00
|
|
|
case SSKProtoReceiptMessageTypeDelivery:
|
2018-08-27 18:51:32 +02:00
|
|
|
OWSLogVerbose(@"Processing receipt message with delivery receipts.");
|
2017-09-27 20:19:26 +02:00
|
|
|
[self processDeliveryReceiptsFromRecipientId:envelope.source
|
|
|
|
sentTimestamps:sentTimestamps
|
|
|
|
deliveryTimestamp:@(envelope.timestamp)
|
|
|
|
transaction:transaction];
|
2017-09-15 21:28:44 +02:00
|
|
|
return;
|
2018-08-01 23:13:01 +02:00
|
|
|
case SSKProtoReceiptMessageTypeRead:
|
2018-08-27 18:51:32 +02:00
|
|
|
OWSLogVerbose(@"Processing receipt message with read receipts.");
|
2017-09-22 22:40:44 +02:00
|
|
|
[OWSReadReceiptManager.sharedManager processReadReceiptsFromRecipientId:envelope.source
|
|
|
|
sentTimestamps:sentTimestamps
|
|
|
|
readTimestamp:envelope.timestamp];
|
2017-09-15 21:28:44 +02:00
|
|
|
break;
|
|
|
|
default:
|
2018-08-27 18:55:37 +02:00
|
|
|
OWSLogInfo(@"Ignoring receipt message of unknown type: %d.", (int)receiptMessage.type);
|
2017-09-15 21:28:44 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-01 16:45:21 +02:00
|
|
|
- (void)handleIncomingEnvelope:(SSKProtoEnvelope *)envelope
|
2018-08-01 23:13:01 +02:00
|
|
|
withCallMessage:(SSKProtoCallMessage *)callMessage
|
2016-12-18 22:08:26 +01:00
|
|
|
{
|
2018-08-30 16:31:01 +02:00
|
|
|
if (!envelope) {
|
|
|
|
OWSFailDebug(@"Missing envelope.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!callMessage) {
|
|
|
|
OWSFailDebug(@"Missing callMessage.");
|
|
|
|
return;
|
|
|
|
}
|
2017-08-04 15:33:56 +02:00
|
|
|
|
|
|
|
if ([callMessage hasProfileKey]) {
|
|
|
|
NSData *profileKey = [callMessage profileKey];
|
2017-09-13 21:35:33 +02:00
|
|
|
NSString *recipientId = envelope.source;
|
2017-08-25 23:16:49 +02:00
|
|
|
[self.profileManager setProfileKeyData:profileKey forRecipientId:recipientId];
|
2017-08-04 15:33:56 +02:00
|
|
|
}
|
|
|
|
|
2017-09-20 17:48:37 +02:00
|
|
|
// By dispatching async, we introduce the possibility that these messages might be lost
|
|
|
|
// if the app exits before this block is executed. This is fine, since the call by
|
|
|
|
// definition will end if the app exits.
|
2017-09-13 21:35:33 +02:00
|
|
|
dispatch_async(dispatch_get_main_queue(), ^{
|
2018-08-08 19:41:12 +02:00
|
|
|
if (callMessage.offer) {
|
2017-09-13 21:35:33 +02:00
|
|
|
[self.callMessageHandler receivedOffer:callMessage.offer fromCallerId:envelope.source];
|
2018-08-08 19:41:12 +02:00
|
|
|
} else if (callMessage.answer) {
|
2017-09-13 21:35:33 +02:00
|
|
|
[self.callMessageHandler receivedAnswer:callMessage.answer fromCallerId:envelope.source];
|
|
|
|
} else if (callMessage.iceUpdate.count > 0) {
|
2018-08-01 23:13:01 +02:00
|
|
|
for (SSKProtoCallMessageIceUpdate *iceUpdate in callMessage.iceUpdate) {
|
2017-09-13 21:35:33 +02:00
|
|
|
[self.callMessageHandler receivedIceUpdate:iceUpdate fromCallerId:envelope.source];
|
|
|
|
}
|
2018-08-08 19:41:12 +02:00
|
|
|
} else if (callMessage.hangup) {
|
2018-08-27 18:51:32 +02:00
|
|
|
OWSLogVerbose(@"Received CallMessage with Hangup.");
|
2017-09-13 21:35:33 +02:00
|
|
|
[self.callMessageHandler receivedHangup:callMessage.hangup fromCallerId:envelope.source];
|
2018-08-08 19:41:12 +02:00
|
|
|
} else if (callMessage.busy) {
|
2017-09-13 21:35:33 +02:00
|
|
|
[self.callMessageHandler receivedBusy:callMessage.busy fromCallerId:envelope.source];
|
|
|
|
} else {
|
|
|
|
OWSProdInfoWEnvelope([OWSAnalyticsEvents messageManagerErrorCallMessageNoActionablePayload], envelope);
|
2016-12-18 22:08:26 +01:00
|
|
|
}
|
2017-09-13 21:35:33 +02:00
|
|
|
});
|
2016-12-18 22:08:26 +01:00
|
|
|
}
|
|
|
|
|
2018-08-01 16:45:21 +02:00
|
|
|
- (void)handleReceivedGroupAvatarUpdateWithEnvelope:(SSKProtoEnvelope *)envelope
|
2018-08-01 23:13:01 +02:00
|
|
|
dataMessage:(SSKProtoDataMessage *)dataMessage
|
2017-09-13 21:35:33 +02:00
|
|
|
transaction:(YapDatabaseReadWriteTransaction *)transaction
|
2016-10-14 23:00:29 +02:00
|
|
|
{
|
2018-08-30 16:31:01 +02:00
|
|
|
if (!envelope) {
|
|
|
|
OWSFailDebug(@"Missing envelope.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!dataMessage) {
|
|
|
|
OWSFailDebug(@"Missing dataMessage.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!transaction) {
|
2018-09-11 00:56:22 +02:00
|
|
|
OWSFail(@"Missing transaction.");
|
2018-08-30 16:31:01 +02:00
|
|
|
return;
|
|
|
|
}
|
2017-09-13 21:35:33 +02:00
|
|
|
|
2017-10-03 19:41:48 +02:00
|
|
|
TSGroupThread *_Nullable groupThread =
|
|
|
|
[TSGroupThread threadWithGroupId:dataMessage.group.id transaction:transaction];
|
|
|
|
if (!groupThread) {
|
2018-08-27 18:51:32 +02:00
|
|
|
OWSFailDebug(@"Missing group for group avatar update");
|
2017-10-03 19:41:48 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-10-14 23:00:29 +02:00
|
|
|
OWSAttachmentsProcessor *attachmentsProcessor =
|
|
|
|
[[OWSAttachmentsProcessor alloc] initWithAttachmentProtos:@[ dataMessage.group.avatar ]
|
2017-09-13 21:35:33 +02:00
|
|
|
networkManager:self.networkManager
|
|
|
|
transaction:transaction];
|
2016-10-14 23:00:29 +02:00
|
|
|
|
|
|
|
if (!attachmentsProcessor.hasSupportedAttachments) {
|
2018-08-27 18:51:32 +02:00
|
|
|
OWSLogWarn(@"received unsupported group avatar envelope");
|
2016-10-14 23:00:29 +02:00
|
|
|
return;
|
2015-12-07 03:31:43 +01:00
|
|
|
}
|
2016-10-14 23:00:29 +02:00
|
|
|
[attachmentsProcessor fetchAttachmentsForMessage:nil
|
2017-09-13 21:35:33 +02:00
|
|
|
transaction:transaction
|
2017-05-10 16:05:01 +02:00
|
|
|
success:^(TSAttachmentStream *attachmentStream) {
|
2016-10-14 23:00:29 +02:00
|
|
|
[groupThread updateAvatarWithAttachmentStream:attachmentStream];
|
|
|
|
}
|
2017-05-10 16:05:01 +02:00
|
|
|
failure:^(NSError *error) {
|
2018-08-27 18:51:32 +02:00
|
|
|
OWSLogError(@"failed to fetch attachments for group avatar sent at: %llu. with error: %@",
|
2016-10-14 23:00:29 +02:00
|
|
|
envelope.timestamp,
|
|
|
|
error);
|
|
|
|
}];
|
|
|
|
}
|
|
|
|
|
2018-08-01 16:45:21 +02:00
|
|
|
- (void)handleReceivedMediaWithEnvelope:(SSKProtoEnvelope *)envelope
|
2018-08-01 23:13:01 +02:00
|
|
|
dataMessage:(SSKProtoDataMessage *)dataMessage
|
2017-09-13 21:35:33 +02:00
|
|
|
transaction:(YapDatabaseReadWriteTransaction *)transaction
|
2016-10-14 23:00:29 +02:00
|
|
|
{
|
2018-08-30 16:31:01 +02:00
|
|
|
if (!envelope) {
|
|
|
|
OWSFailDebug(@"Missing envelope.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!dataMessage) {
|
|
|
|
OWSFailDebug(@"Missing dataMessage.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!transaction) {
|
2018-09-11 00:56:22 +02:00
|
|
|
OWSFail(@"Missing transaction.");
|
2018-08-30 16:31:01 +02:00
|
|
|
return;
|
|
|
|
}
|
2017-09-13 21:35:33 +02:00
|
|
|
|
2017-10-03 19:41:48 +02:00
|
|
|
TSThread *_Nullable thread = [self threadForEnvelope:envelope dataMessage:dataMessage transaction:transaction];
|
|
|
|
if (!thread) {
|
2018-08-27 18:51:32 +02:00
|
|
|
OWSFailDebug(@"ignoring media message for unknown group.");
|
2017-10-03 19:41:48 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-10-14 23:00:29 +02:00
|
|
|
OWSAttachmentsProcessor *attachmentsProcessor =
|
|
|
|
[[OWSAttachmentsProcessor alloc] initWithAttachmentProtos:dataMessage.attachments
|
2017-09-13 21:35:33 +02:00
|
|
|
networkManager:self.networkManager
|
|
|
|
transaction:transaction];
|
2016-10-14 23:00:29 +02:00
|
|
|
if (!attachmentsProcessor.hasSupportedAttachments) {
|
2018-08-27 18:51:32 +02:00
|
|
|
OWSLogWarn(@"received unsupported media envelope");
|
2016-10-14 23:00:29 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-04-06 23:59:42 +02:00
|
|
|
TSIncomingMessage *_Nullable createdMessage = [self handleReceivedEnvelope:envelope
|
|
|
|
withDataMessage:dataMessage
|
|
|
|
attachmentIds:attachmentsProcessor.attachmentIds
|
|
|
|
transaction:transaction];
|
2017-05-10 16:05:01 +02:00
|
|
|
|
|
|
|
if (!createdMessage) {
|
|
|
|
return;
|
|
|
|
}
|
2016-10-14 23:00:29 +02:00
|
|
|
|
2018-08-27 18:51:32 +02:00
|
|
|
OWSLogDebug(@"incoming attachment message: %@", createdMessage.debugDescription);
|
2017-06-13 16:50:42 +02:00
|
|
|
|
2016-10-14 23:00:29 +02:00
|
|
|
[attachmentsProcessor fetchAttachmentsForMessage:createdMessage
|
2017-09-13 21:35:33 +02:00
|
|
|
transaction:transaction
|
2017-05-10 16:05:01 +02:00
|
|
|
success:^(TSAttachmentStream *attachmentStream) {
|
2018-08-27 18:51:32 +02:00
|
|
|
OWSLogDebug(@"successfully fetched attachment: %@ for message: %@", attachmentStream, createdMessage);
|
2016-10-14 23:00:29 +02:00
|
|
|
}
|
2017-05-10 16:05:01 +02:00
|
|
|
failure:^(NSError *error) {
|
2018-08-27 18:51:32 +02:00
|
|
|
OWSLogError(@"failed to fetch attachments for message: %@ with error: %@", createdMessage, error);
|
2016-10-14 23:00:29 +02:00
|
|
|
}];
|
2015-12-07 03:31:43 +01:00
|
|
|
}
|
|
|
|
|
2018-08-01 16:45:21 +02:00
|
|
|
- (void)handleIncomingEnvelope:(SSKProtoEnvelope *)envelope
|
2018-08-01 23:13:01 +02:00
|
|
|
withSyncMessage:(SSKProtoSyncMessage *)syncMessage
|
2017-09-13 21:35:33 +02:00
|
|
|
transaction:(YapDatabaseReadWriteTransaction *)transaction
|
2016-08-23 22:38:05 +02:00
|
|
|
{
|
2018-08-30 16:31:01 +02:00
|
|
|
if (!envelope) {
|
|
|
|
OWSFailDebug(@"Missing envelope.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!syncMessage) {
|
|
|
|
OWSFailDebug(@"Missing syncMessage.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!transaction) {
|
2018-09-11 00:56:22 +02:00
|
|
|
OWSFail(@"Missing transaction.");
|
2018-08-30 16:31:01 +02:00
|
|
|
return;
|
|
|
|
}
|
2017-09-13 21:35:33 +02:00
|
|
|
|
2017-08-04 18:01:45 +02:00
|
|
|
NSString *localNumber = [TSAccountManager localNumber];
|
2017-09-13 21:35:33 +02:00
|
|
|
if (![localNumber isEqualToString:envelope.source]) {
|
2017-08-04 18:01:45 +02:00
|
|
|
// Sync messages should only come from linked devices.
|
2017-09-13 21:35:33 +02:00
|
|
|
OWSProdErrorWEnvelope([OWSAnalyticsEvents messageManagerErrorSyncMessageFromUnknownSource], envelope);
|
2017-08-04 18:01:45 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-08-08 19:41:12 +02:00
|
|
|
if (syncMessage.sent) {
|
2016-08-23 22:38:05 +02:00
|
|
|
OWSIncomingSentMessageTranscript *transcript =
|
2018-04-07 21:26:22 +02:00
|
|
|
[[OWSIncomingSentMessageTranscript alloc] initWithProto:syncMessage.sent
|
|
|
|
transaction:transaction];
|
2016-10-14 23:00:29 +02:00
|
|
|
|
|
|
|
OWSRecordTranscriptJob *recordJob =
|
2017-09-15 21:28:44 +02:00
|
|
|
[[OWSRecordTranscriptJob alloc] initWithIncomingSentMessageTranscript:transcript];
|
2016-10-14 23:00:29 +02:00
|
|
|
|
2018-08-30 16:31:01 +02:00
|
|
|
SSKProtoDataMessage *_Nullable dataMessage = syncMessage.sent.message;
|
|
|
|
if (!dataMessage) {
|
|
|
|
OWSFailDebug(@"Missing dataMessage.");
|
|
|
|
return;
|
|
|
|
}
|
2017-08-04 15:33:56 +02:00
|
|
|
NSString *destination = syncMessage.sent.destination;
|
2017-08-29 15:31:02 +02:00
|
|
|
if (dataMessage && destination.length > 0 && dataMessage.hasProfileKey) {
|
2017-08-04 15:33:56 +02:00
|
|
|
// If we observe a linked device sending our profile key to another
|
|
|
|
// user, we can infer that that user belongs in our profile whitelist.
|
2018-08-08 19:41:12 +02:00
|
|
|
if (dataMessage.group) {
|
2017-08-29 15:31:02 +02:00
|
|
|
[self.profileManager addGroupIdToProfileWhitelist:dataMessage.group.id];
|
|
|
|
} else {
|
|
|
|
[self.profileManager addUserToProfileWhitelist:destination];
|
|
|
|
}
|
2017-08-04 15:33:56 +02:00
|
|
|
}
|
|
|
|
|
2016-10-14 23:00:29 +02:00
|
|
|
if ([self isDataMessageGroupAvatarUpdate:syncMessage.sent.message]) {
|
2017-05-10 16:05:01 +02:00
|
|
|
[recordJob runWithAttachmentHandler:^(TSAttachmentStream *attachmentStream) {
|
2017-10-03 19:41:48 +02:00
|
|
|
[self.dbConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) {
|
|
|
|
TSGroupThread *_Nullable groupThread =
|
|
|
|
[TSGroupThread threadWithGroupId:dataMessage.group.id transaction:transaction];
|
|
|
|
if (!groupThread) {
|
2018-08-27 18:51:32 +02:00
|
|
|
OWSFailDebug(@"ignoring sync group avatar update for unknown group.");
|
2017-10-03 19:41:48 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
[groupThread updateAvatarWithAttachmentStream:attachmentStream transaction:transaction];
|
|
|
|
}];
|
2017-09-13 21:35:33 +02:00
|
|
|
}
|
|
|
|
transaction:transaction];
|
2016-10-14 23:00:29 +02:00
|
|
|
} else {
|
2018-08-27 18:00:28 +02:00
|
|
|
[recordJob
|
|
|
|
runWithAttachmentHandler:^(TSAttachmentStream *attachmentStream) {
|
2018-08-27 18:51:32 +02:00
|
|
|
OWSLogDebug(@"successfully fetched transcript attachment: %@", attachmentStream);
|
2018-08-27 18:00:28 +02:00
|
|
|
}
|
|
|
|
transaction:transaction];
|
2016-10-14 23:00:29 +02:00
|
|
|
}
|
2018-08-08 19:41:12 +02:00
|
|
|
} else if (syncMessage.request) {
|
2018-08-01 23:13:01 +02:00
|
|
|
if (syncMessage.request.type == SSKProtoSyncMessageRequestTypeContacts) {
|
2017-11-21 16:01:55 +01:00
|
|
|
// We respond asynchronously because populating the sync message will
|
|
|
|
// create transactions and it's not practical (due to locking in the OWSIdentityManager)
|
|
|
|
// to plumb our transaction through.
|
|
|
|
//
|
|
|
|
// In rare cases this means we won't respond to the sync request, but that's
|
|
|
|
// acceptable.
|
|
|
|
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
|
|
|
|
OWSSyncContactsMessage *syncContactsMessage =
|
|
|
|
[[OWSSyncContactsMessage alloc] initWithSignalAccounts:self.contactsManager.signalAccounts
|
|
|
|
identityManager:self.identityManager
|
|
|
|
profileManager:self.profileManager];
|
2018-08-07 18:57:48 +02:00
|
|
|
__block NSData *_Nullable syncData;
|
|
|
|
[self.dbConnection readWithBlock:^(YapDatabaseReadTransaction *transaction) {
|
|
|
|
syncData = [syncContactsMessage buildPlainTextAttachmentDataWithTransaction:transaction];
|
|
|
|
}];
|
|
|
|
if (!syncData) {
|
2018-08-27 18:51:32 +02:00
|
|
|
OWSFailDebug(@"Failed to serialize contacts sync message.");
|
2018-08-07 18:57:48 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
DataSource *dataSource = [DataSourceValue dataSourceWithSyncMessageData:syncData];
|
2017-11-21 16:01:55 +01:00
|
|
|
[self.messageSender enqueueTemporaryAttachment:dataSource
|
|
|
|
contentType:OWSMimeTypeApplicationOctetStream
|
|
|
|
inMessage:syncContactsMessage
|
|
|
|
success:^{
|
2018-08-27 18:51:32 +02:00
|
|
|
OWSLogInfo(@"Successfully sent Contacts response syncMessage.");
|
2017-11-21 16:01:55 +01:00
|
|
|
}
|
|
|
|
failure:^(NSError *error) {
|
2018-08-27 18:51:32 +02:00
|
|
|
OWSLogError(@"Failed to send Contacts response syncMessage with error: %@", error);
|
2017-11-21 16:01:55 +01:00
|
|
|
}];
|
|
|
|
});
|
2018-08-01 23:13:01 +02:00
|
|
|
} else if (syncMessage.request.type == SSKProtoSyncMessageRequestTypeGroups) {
|
2018-08-07 19:03:33 +02:00
|
|
|
OWSSyncGroupsMessage *syncGroupsMessage = [[OWSSyncGroupsMessage alloc] init];
|
|
|
|
NSData *_Nullable syncData = [syncGroupsMessage buildPlainTextAttachmentDataWithTransaction:transaction];
|
|
|
|
if (!syncData) {
|
2018-08-27 18:51:32 +02:00
|
|
|
OWSFailDebug(@"Failed to serialize groups sync message.");
|
2018-08-07 19:03:33 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
DataSource *dataSource = [DataSourceValue dataSourceWithSyncMessageData:syncData];
|
|
|
|
[self.messageSender enqueueTemporaryAttachment:dataSource
|
|
|
|
contentType:OWSMimeTypeApplicationOctetStream
|
|
|
|
inMessage:syncGroupsMessage
|
|
|
|
success:^{
|
2018-08-27 18:51:32 +02:00
|
|
|
OWSLogInfo(@"Successfully sent Groups response syncMessage.");
|
2018-08-07 18:57:48 +02:00
|
|
|
}
|
2018-08-07 19:03:33 +02:00
|
|
|
failure:^(NSError *error) {
|
2018-08-27 18:51:32 +02:00
|
|
|
OWSLogError(@"Failed to send Groups response syncMessage with error: %@", error);
|
2018-08-07 19:03:33 +02:00
|
|
|
}];
|
2018-08-01 23:13:01 +02:00
|
|
|
} else if (syncMessage.request.type == SSKProtoSyncMessageRequestTypeBlocked) {
|
2018-08-27 18:51:32 +02:00
|
|
|
OWSLogInfo(@"Received request for block list");
|
2018-09-20 18:52:43 +02:00
|
|
|
[self.blockingManager syncBlockList];
|
2018-08-01 23:13:01 +02:00
|
|
|
} else if (syncMessage.request.type == SSKProtoSyncMessageRequestTypeConfiguration) {
|
2017-10-03 17:11:15 +02:00
|
|
|
BOOL areReadReceiptsEnabled =
|
|
|
|
[[OWSReadReceiptManager sharedManager] areReadReceiptsEnabledWithTransaction:transaction];
|
2017-10-02 22:35:24 +02:00
|
|
|
OWSSyncConfigurationMessage *syncConfigurationMessage =
|
|
|
|
[[OWSSyncConfigurationMessage alloc] initWithReadReceiptsEnabled:areReadReceiptsEnabled];
|
2017-11-15 19:21:31 +01:00
|
|
|
[self.messageSender enqueueMessage:syncConfigurationMessage
|
2017-10-02 22:35:24 +02:00
|
|
|
success:^{
|
2018-08-27 18:51:32 +02:00
|
|
|
OWSLogInfo(@"Successfully sent Configuration response syncMessage.");
|
2017-10-02 22:35:24 +02:00
|
|
|
}
|
|
|
|
failure:^(NSError *error) {
|
2018-08-27 18:51:32 +02:00
|
|
|
OWSLogError(@"Failed to send Configuration response syncMessage with error: %@", error);
|
2017-10-02 22:35:24 +02:00
|
|
|
}];
|
2016-11-22 18:14:39 +01:00
|
|
|
} else {
|
2018-08-27 18:51:32 +02:00
|
|
|
OWSLogWarn(@"ignoring unsupported sync request message");
|
2016-08-26 01:01:35 +02:00
|
|
|
}
|
2018-08-08 19:41:12 +02:00
|
|
|
} else if (syncMessage.blocked) {
|
2017-09-28 16:01:17 +02:00
|
|
|
NSArray<NSString *> *blockedPhoneNumbers = [syncMessage.blocked.numbers copy];
|
|
|
|
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
|
2018-05-11 16:36:40 +02:00
|
|
|
[self.blockingManager setBlockedPhoneNumbers:blockedPhoneNumbers sendSyncMessage:NO];
|
2017-09-13 21:35:33 +02:00
|
|
|
});
|
2016-09-01 16:28:35 +02:00
|
|
|
} else if (syncMessage.read.count > 0) {
|
2018-08-30 16:31:01 +02:00
|
|
|
OWSLogInfo(@"Received %lu read receipt(s)", (unsigned long)syncMessage.read.count);
|
2017-09-26 17:45:22 +02:00
|
|
|
[OWSReadReceiptManager.sharedManager processReadReceiptsFromLinkedDevice:syncMessage.read
|
2018-04-18 02:53:27 +02:00
|
|
|
readTimestamp:envelope.timestamp
|
2017-09-26 17:45:22 +02:00
|
|
|
transaction:transaction];
|
2018-08-08 19:41:12 +02:00
|
|
|
} else if (syncMessage.verified) {
|
2018-08-27 18:51:32 +02:00
|
|
|
OWSLogInfo(@"Received verification state for %@", syncMessage.verified.destination);
|
2018-02-02 20:52:45 +01:00
|
|
|
[self.identityManager processIncomingSyncMessage:syncMessage.verified transaction:transaction];
|
2016-08-23 22:38:05 +02:00
|
|
|
} else {
|
2018-08-27 18:51:32 +02:00
|
|
|
OWSLogWarn(@"Ignoring unsupported sync message.");
|
2016-08-23 22:38:05 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-01 16:45:21 +02:00
|
|
|
- (void)handleEndSessionMessageWithEnvelope:(SSKProtoEnvelope *)envelope
|
2018-08-01 23:13:01 +02:00
|
|
|
dataMessage:(SSKProtoDataMessage *)dataMessage
|
2017-09-13 21:35:33 +02:00
|
|
|
transaction:(YapDatabaseReadWriteTransaction *)transaction
|
2016-08-22 22:09:58 +02:00
|
|
|
{
|
2018-08-30 16:31:01 +02:00
|
|
|
if (!envelope) {
|
|
|
|
OWSFailDebug(@"Missing envelope.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!dataMessage) {
|
|
|
|
OWSFailDebug(@"Missing dataMessage.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!transaction) {
|
2018-09-11 00:56:22 +02:00
|
|
|
OWSFail(@"Missing transaction.");
|
2018-08-30 16:31:01 +02:00
|
|
|
return;
|
|
|
|
}
|
2017-09-13 21:35:33 +02:00
|
|
|
|
|
|
|
TSContactThread *thread = [TSContactThread getOrCreateThreadWithContactId:envelope.source transaction:transaction];
|
|
|
|
|
2018-09-20 22:10:35 +02:00
|
|
|
[[[TSInfoMessage alloc] initWithTimestamp:[NSDate ows_millisecondTimeStamp]
|
|
|
|
inThread:thread
|
|
|
|
messageType:TSInfoMessageTypeSessionDidEnd] saveWithTransaction:transaction];
|
2015-12-07 03:31:43 +01:00
|
|
|
|
2018-03-05 15:30:58 +01:00
|
|
|
[self.primaryStorage deleteAllSessionsForContact:envelope.source protocolContext:transaction];
|
2015-12-07 03:31:43 +01:00
|
|
|
}
|
|
|
|
|
2018-08-01 16:45:21 +02:00
|
|
|
- (void)handleExpirationTimerUpdateMessageWithEnvelope:(SSKProtoEnvelope *)envelope
|
2018-08-01 23:13:01 +02:00
|
|
|
dataMessage:(SSKProtoDataMessage *)dataMessage
|
2017-09-13 21:35:33 +02:00
|
|
|
transaction:(YapDatabaseReadWriteTransaction *)transaction
|
2016-07-29 21:33:25 +02:00
|
|
|
{
|
2018-08-30 16:31:01 +02:00
|
|
|
if (!envelope) {
|
|
|
|
OWSFailDebug(@"Missing envelope.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!dataMessage) {
|
|
|
|
OWSFailDebug(@"Missing dataMessage.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!transaction) {
|
2018-09-11 00:56:22 +02:00
|
|
|
OWSFail(@"Missing transaction.");
|
2018-08-30 16:31:01 +02:00
|
|
|
return;
|
|
|
|
}
|
2017-09-13 21:35:33 +02:00
|
|
|
|
2017-10-03 19:41:48 +02:00
|
|
|
TSThread *_Nullable thread = [self threadForEnvelope:envelope dataMessage:dataMessage transaction:transaction];
|
|
|
|
if (!thread) {
|
2018-08-27 18:51:32 +02:00
|
|
|
OWSFailDebug(@"ignoring expiring messages update for unknown group.");
|
2017-10-03 19:41:48 +02:00
|
|
|
return;
|
|
|
|
}
|
2016-10-05 17:42:44 +02:00
|
|
|
|
|
|
|
OWSDisappearingMessagesConfiguration *disappearingMessagesConfiguration;
|
|
|
|
if (dataMessage.hasExpireTimer && dataMessage.expireTimer > 0) {
|
2018-08-27 18:55:37 +02:00
|
|
|
OWSLogInfo(
|
|
|
|
@"Expiring messages duration turned to %u for thread %@", (unsigned int)dataMessage.expireTimer, thread);
|
2016-10-05 17:42:44 +02:00
|
|
|
disappearingMessagesConfiguration =
|
|
|
|
[[OWSDisappearingMessagesConfiguration alloc] initWithThreadId:thread.uniqueId
|
|
|
|
enabled:YES
|
|
|
|
durationSeconds:dataMessage.expireTimer];
|
|
|
|
} else {
|
2018-08-27 18:51:32 +02:00
|
|
|
OWSLogInfo(@"Expiring messages have been turned off for thread %@", thread);
|
2016-10-05 17:42:44 +02:00
|
|
|
disappearingMessagesConfiguration = [[OWSDisappearingMessagesConfiguration alloc]
|
|
|
|
initWithThreadId:thread.uniqueId
|
|
|
|
enabled:NO
|
|
|
|
durationSeconds:OWSDisappearingMessagesConfigurationDefaultExpirationDuration];
|
|
|
|
}
|
2018-09-06 19:01:24 +02:00
|
|
|
OWSAssertDebug(disappearingMessagesConfiguration);
|
2017-09-13 21:35:33 +02:00
|
|
|
[disappearingMessagesConfiguration saveWithTransaction:transaction];
|
2016-12-02 03:10:15 +01:00
|
|
|
NSString *name = [self.contactsManager displayNameForPhoneIdentifier:envelope.source];
|
2016-10-05 17:42:44 +02:00
|
|
|
OWSDisappearingConfigurationUpdateInfoMessage *message =
|
2018-09-20 22:10:35 +02:00
|
|
|
[[OWSDisappearingConfigurationUpdateInfoMessage alloc] initWithTimestamp:[NSDate ows_millisecondTimeStamp]
|
|
|
|
thread:thread
|
|
|
|
configuration:disappearingMessagesConfiguration
|
|
|
|
createdByRemoteName:name
|
|
|
|
createdInExistingGroup:NO];
|
2017-09-13 21:35:33 +02:00
|
|
|
[message saveWithTransaction:transaction];
|
2015-12-07 03:31:43 +01:00
|
|
|
}
|
|
|
|
|
2018-08-01 16:45:21 +02:00
|
|
|
- (void)handleProfileKeyMessageWithEnvelope:(SSKProtoEnvelope *)envelope
|
2018-08-01 23:13:01 +02:00
|
|
|
dataMessage:(SSKProtoDataMessage *)dataMessage
|
2017-08-28 23:00:55 +02:00
|
|
|
{
|
2018-08-30 16:31:01 +02:00
|
|
|
if (!envelope) {
|
|
|
|
OWSFailDebug(@"Missing envelope.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!dataMessage) {
|
|
|
|
OWSFailDebug(@"Missing dataMessage.");
|
|
|
|
return;
|
|
|
|
}
|
2017-09-13 21:35:33 +02:00
|
|
|
|
|
|
|
NSString *recipientId = envelope.source;
|
2017-08-28 23:00:55 +02:00
|
|
|
if (!dataMessage.hasProfileKey) {
|
2018-08-27 18:55:37 +02:00
|
|
|
OWSFailDebug(@"received profile key message without profile key from: %@", envelopeAddress(envelope));
|
2017-08-28 23:00:55 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
NSData *profileKey = dataMessage.profileKey;
|
|
|
|
if (profileKey.length != kAES256_KeyByteLength) {
|
2018-08-27 18:55:37 +02:00
|
|
|
OWSFailDebug(@"received profile key of unexpected length:%lu from:%@",
|
2017-08-28 23:00:55 +02:00
|
|
|
(unsigned long)profileKey.length,
|
2017-09-13 21:35:33 +02:00
|
|
|
envelopeAddress(envelope));
|
2017-08-28 23:00:55 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-09-17 15:27:58 +02:00
|
|
|
id<ProfileManagerProtocol> profileManager = SSKEnvironment.shared.profileManager;
|
2017-08-28 23:00:55 +02:00
|
|
|
[profileManager setProfileKeyData:profileKey forRecipientId:recipientId];
|
|
|
|
}
|
|
|
|
|
2018-08-01 16:45:21 +02:00
|
|
|
- (void)handleReceivedTextMessageWithEnvelope:(SSKProtoEnvelope *)envelope
|
2018-08-01 23:13:01 +02:00
|
|
|
dataMessage:(SSKProtoDataMessage *)dataMessage
|
2017-09-13 21:35:33 +02:00
|
|
|
transaction:(YapDatabaseReadWriteTransaction *)transaction
|
2016-07-29 21:33:25 +02:00
|
|
|
{
|
2018-08-30 16:31:01 +02:00
|
|
|
if (!envelope) {
|
|
|
|
OWSFailDebug(@"Missing envelope.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!dataMessage) {
|
|
|
|
OWSFailDebug(@"Missing dataMessage.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!transaction) {
|
2018-09-11 00:56:22 +02:00
|
|
|
OWSFail(@"Missing transaction.");
|
2018-08-30 16:31:01 +02:00
|
|
|
return;
|
|
|
|
}
|
2017-09-13 21:35:33 +02:00
|
|
|
|
|
|
|
[self handleReceivedEnvelope:envelope withDataMessage:dataMessage attachmentIds:@[] transaction:transaction];
|
2015-12-07 03:31:43 +01:00
|
|
|
}
|
|
|
|
|
2017-05-05 23:22:56 +02:00
|
|
|
- (void)sendGroupUpdateForThread:(TSGroupThread *)gThread message:(TSOutgoingMessage *)message
|
|
|
|
{
|
2018-08-30 16:31:01 +02:00
|
|
|
if (!gThread) {
|
|
|
|
OWSFailDebug(@"Missing gThread.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!gThread.groupModel) {
|
|
|
|
OWSFailDebug(@"Missing gThread.groupModel.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!message) {
|
|
|
|
OWSFailDebug(@"Missing message.");
|
|
|
|
return;
|
|
|
|
}
|
2017-05-05 23:22:56 +02:00
|
|
|
|
|
|
|
if (gThread.groupModel.groupImage) {
|
2017-09-08 18:51:25 +02:00
|
|
|
NSData *data = UIImagePNGRepresentation(gThread.groupModel.groupImage);
|
2018-07-30 17:00:56 +02:00
|
|
|
DataSource *_Nullable dataSource = [DataSourceValue dataSourceWithData:data fileExtension:@"png"];
|
2018-07-30 16:22:45 +02:00
|
|
|
[self.messageSender enqueueTemporaryAttachment:dataSource
|
2017-05-05 23:22:56 +02:00
|
|
|
contentType:OWSMimeTypeImagePng
|
|
|
|
inMessage:message
|
|
|
|
success:^{
|
2018-08-27 18:51:32 +02:00
|
|
|
OWSLogDebug(@"Successfully sent group update with avatar");
|
2017-05-05 23:22:56 +02:00
|
|
|
}
|
2017-05-10 16:05:01 +02:00
|
|
|
failure:^(NSError *error) {
|
2018-08-27 18:51:32 +02:00
|
|
|
OWSLogError(@"Failed to send group avatar update with error: %@", error);
|
2017-05-05 23:22:56 +02:00
|
|
|
}];
|
|
|
|
} else {
|
2017-11-15 19:21:31 +01:00
|
|
|
[self.messageSender enqueueMessage:message
|
2017-05-05 23:22:56 +02:00
|
|
|
success:^{
|
2018-08-27 18:51:32 +02:00
|
|
|
OWSLogDebug(@"Successfully sent group update");
|
2017-05-05 23:22:56 +02:00
|
|
|
}
|
2017-05-10 16:05:01 +02:00
|
|
|
failure:^(NSError *error) {
|
2018-08-27 18:51:32 +02:00
|
|
|
OWSLogError(@"Failed to send group update with error: %@", error);
|
2017-05-05 23:22:56 +02:00
|
|
|
}];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-01 16:45:21 +02:00
|
|
|
- (void)handleGroupInfoRequest:(SSKProtoEnvelope *)envelope
|
2018-08-01 23:13:01 +02:00
|
|
|
dataMessage:(SSKProtoDataMessage *)dataMessage
|
2017-09-13 21:35:33 +02:00
|
|
|
transaction:(YapDatabaseReadWriteTransaction *)transaction
|
2017-05-10 16:05:01 +02:00
|
|
|
{
|
2018-08-30 16:31:01 +02:00
|
|
|
if (!envelope) {
|
|
|
|
OWSFailDebug(@"Missing envelope.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!dataMessage) {
|
|
|
|
OWSFailDebug(@"Missing dataMessage.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!transaction) {
|
2018-09-11 00:56:22 +02:00
|
|
|
OWSFail(@"Missing transaction.");
|
2018-08-30 16:31:01 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (dataMessage.group.type != SSKProtoGroupContextTypeRequestInfo) {
|
|
|
|
OWSFailDebug(@"Unexpected group message type.");
|
|
|
|
return;
|
|
|
|
}
|
2017-05-10 16:05:01 +02:00
|
|
|
|
2018-08-08 19:41:12 +02:00
|
|
|
NSData *groupId = dataMessage.group ? dataMessage.group.id : nil;
|
2017-05-10 16:05:01 +02:00
|
|
|
if (!groupId) {
|
2018-08-27 16:29:51 +02:00
|
|
|
OWSFailDebug(@"Group info request is missing group id.");
|
2017-05-10 16:05:01 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-08-30 16:31:01 +02:00
|
|
|
OWSLogInfo(@"Received 'Request Group Info' message for group: %@ from: %@", groupId, envelope.source);
|
2017-05-10 16:05:01 +02:00
|
|
|
|
2017-10-03 19:41:48 +02:00
|
|
|
TSGroupThread *_Nullable gThread = [TSGroupThread threadWithGroupId:dataMessage.group.id transaction:transaction];
|
2017-09-13 21:35:33 +02:00
|
|
|
if (!gThread) {
|
2018-08-27 18:51:32 +02:00
|
|
|
OWSLogWarn(@"Unknown group: %@", groupId);
|
2017-09-13 21:35:33 +02:00
|
|
|
return;
|
|
|
|
}
|
2017-05-12 18:39:36 +02:00
|
|
|
|
2017-10-04 16:19:19 +02:00
|
|
|
// Ensure sender is in the group.
|
2017-09-13 21:35:33 +02:00
|
|
|
if (![gThread.groupModel.groupMemberIds containsObject:envelope.source]) {
|
2018-08-27 18:51:32 +02:00
|
|
|
OWSLogWarn(@"Ignoring 'Request Group Info' message for non-member of group. %@ not in %@",
|
2017-09-13 21:35:33 +02:00
|
|
|
envelope.source,
|
|
|
|
gThread.groupModel.groupMemberIds);
|
2017-10-04 16:19:19 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure we are in the group.
|
|
|
|
NSString *localNumber = [TSAccountManager localNumber];
|
|
|
|
if (![gThread.groupModel.groupMemberIds containsObject:localNumber]) {
|
2018-08-27 18:51:32 +02:00
|
|
|
OWSLogWarn(@"Ignoring 'Request Group Info' message for group we no longer belong to.");
|
2017-10-04 16:19:19 +02:00
|
|
|
return;
|
2017-09-13 21:35:33 +02:00
|
|
|
}
|
2017-05-10 16:05:01 +02:00
|
|
|
|
2017-09-13 21:35:33 +02:00
|
|
|
NSString *updateGroupInfo =
|
|
|
|
[gThread.groupModel getInfoStringAboutUpdateTo:gThread.groupModel contactsManager:self.contactsManager];
|
2018-06-13 22:44:22 +02:00
|
|
|
|
|
|
|
uint32_t expiresInSeconds = [gThread disappearingMessagesDurationWithTransaction:transaction];
|
|
|
|
TSOutgoingMessage *message = [TSOutgoingMessage outgoingMessageInThread:gThread
|
2018-08-31 18:43:05 +02:00
|
|
|
groupMetaMessage:TSGroupMetaMessageUpdate
|
2018-06-13 22:44:22 +02:00
|
|
|
expiresInSeconds:expiresInSeconds];
|
|
|
|
|
2017-09-13 21:35:33 +02:00
|
|
|
[message updateWithCustomMessage:updateGroupInfo transaction:transaction];
|
|
|
|
// Only send this group update to the requester.
|
2018-04-24 22:38:35 +02:00
|
|
|
[message updateWithSendingToSingleGroupRecipient:envelope.source transaction:transaction];
|
2017-09-13 21:35:33 +02:00
|
|
|
|
2017-09-21 17:55:25 +02:00
|
|
|
[self sendGroupUpdateForThread:gThread message:message];
|
2017-05-10 16:05:01 +02:00
|
|
|
}
|
|
|
|
|
2018-08-01 16:45:21 +02:00
|
|
|
- (TSIncomingMessage *_Nullable)handleReceivedEnvelope:(SSKProtoEnvelope *)envelope
|
2018-08-01 23:13:01 +02:00
|
|
|
withDataMessage:(SSKProtoDataMessage *)dataMessage
|
2017-05-10 16:05:01 +02:00
|
|
|
attachmentIds:(NSArray<NSString *> *)attachmentIds
|
2017-09-13 21:35:33 +02:00
|
|
|
transaction:(YapDatabaseReadWriteTransaction *)transaction
|
2016-07-29 21:33:25 +02:00
|
|
|
{
|
2018-08-30 16:31:01 +02:00
|
|
|
if (!envelope) {
|
|
|
|
OWSFailDebug(@"Missing envelope.");
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
if (!dataMessage) {
|
|
|
|
OWSFailDebug(@"Missing dataMessage.");
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
if (!transaction) {
|
2018-09-11 00:56:22 +02:00
|
|
|
OWSFail(@"Missing transaction.");
|
2018-08-30 16:31:01 +02:00
|
|
|
return nil;
|
|
|
|
}
|
2017-09-13 21:35:33 +02:00
|
|
|
|
2016-10-05 17:42:44 +02:00
|
|
|
uint64_t timestamp = envelope.timestamp;
|
2016-08-22 22:09:58 +02:00
|
|
|
NSString *body = dataMessage.body;
|
2018-08-08 19:41:12 +02:00
|
|
|
NSData *groupId = dataMessage.group ? dataMessage.group.id : nil;
|
2018-07-13 20:03:28 +02:00
|
|
|
OWSContact *_Nullable contact = [OWSContacts contactForDataMessage:dataMessage transaction:transaction];
|
2015-12-07 03:31:43 +01:00
|
|
|
|
2018-08-01 23:13:01 +02:00
|
|
|
if (dataMessage.group.type == SSKProtoGroupContextTypeRequestInfo) {
|
2017-09-13 21:35:33 +02:00
|
|
|
[self handleGroupInfoRequest:envelope dataMessage:dataMessage transaction:transaction];
|
2017-05-10 16:05:01 +02:00
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
|
2017-10-03 19:41:48 +02:00
|
|
|
if (groupId.length > 0) {
|
|
|
|
NSMutableSet *newMemberIds = [NSMutableSet setWithArray:dataMessage.group.members];
|
2018-02-16 02:45:28 +01:00
|
|
|
for (NSString *recipientId in newMemberIds) {
|
|
|
|
if (!recipientId.isValidE164) {
|
2018-08-27 18:51:32 +02:00
|
|
|
OWSLogVerbose(
|
|
|
|
@"incoming group update has invalid group member: %@", [self descriptionForEnvelope:envelope]);
|
|
|
|
OWSFailDebug(@"incoming group update has invalid group member");
|
2018-02-16 02:45:28 +01:00
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
}
|
2017-10-03 19:41:48 +02:00
|
|
|
|
|
|
|
// Group messages create the group if it doesn't already exist.
|
|
|
|
//
|
|
|
|
// We distinguish between the old group state (if any) and the new group state.
|
|
|
|
TSGroupThread *_Nullable oldGroupThread = [TSGroupThread threadWithGroupId:groupId transaction:transaction];
|
|
|
|
if (oldGroupThread) {
|
2017-10-04 16:06:38 +02:00
|
|
|
// Don't trust other clients; ensure all known group members remain in the
|
|
|
|
// group unless it is a "quit" message in which case we should only remove
|
|
|
|
// the quiting member below.
|
2017-10-03 19:41:48 +02:00
|
|
|
[newMemberIds addObjectsFromArray:oldGroupThread.groupModel.groupMemberIds];
|
|
|
|
}
|
2017-09-13 21:35:33 +02:00
|
|
|
|
|
|
|
switch (dataMessage.group.type) {
|
2018-08-01 23:13:01 +02:00
|
|
|
case SSKProtoGroupContextTypeUpdate: {
|
2017-10-03 19:41:48 +02:00
|
|
|
// Ensures that the thread exists but doesn't update it.
|
|
|
|
TSGroupThread *newGroupThread =
|
|
|
|
[TSGroupThread getOrCreateThreadWithGroupId:groupId transaction:transaction];
|
|
|
|
|
2018-09-28 01:40:51 +02:00
|
|
|
|
|
|
|
uint64_t now = [NSDate ows_millisecondTimeStamp];
|
2017-10-03 19:41:48 +02:00
|
|
|
TSGroupModel *newGroupModel = [[TSGroupModel alloc] initWithTitle:dataMessage.group.name
|
2018-02-13 07:10:29 +01:00
|
|
|
memberIds:newMemberIds.allObjects
|
2017-10-04 16:06:38 +02:00
|
|
|
image:oldGroupThread.groupModel.groupImage
|
2017-10-03 19:41:48 +02:00
|
|
|
groupId:dataMessage.group.id];
|
|
|
|
NSString *updateGroupInfo = [newGroupThread.groupModel getInfoStringAboutUpdateTo:newGroupModel
|
|
|
|
contactsManager:self.contactsManager];
|
|
|
|
newGroupThread.groupModel = newGroupModel;
|
|
|
|
[newGroupThread saveWithTransaction:transaction];
|
|
|
|
|
2018-09-28 01:40:51 +02:00
|
|
|
TSInfoMessage *infoMessage = [[TSInfoMessage alloc] initWithTimestamp:now
|
2018-09-20 22:10:35 +02:00
|
|
|
inThread:newGroupThread
|
|
|
|
messageType:TSInfoMessageTypeGroupUpdate
|
|
|
|
customMessage:updateGroupInfo];
|
2018-09-20 19:19:12 +02:00
|
|
|
[infoMessage saveWithTransaction:transaction];
|
2018-06-14 16:08:38 +02:00
|
|
|
|
2018-09-28 01:40:51 +02:00
|
|
|
if (dataMessage.hasExpireTimer && dataMessage.expireTimer > 0) {
|
|
|
|
[[OWSDisappearingMessagesJob sharedJob]
|
|
|
|
becomeConsistentWithDisappearingDuration:dataMessage.expireTimer
|
|
|
|
thread:newGroupThread
|
|
|
|
appearBeforeTimestamp:now
|
|
|
|
createdByRemoteContactName:nil
|
|
|
|
createdInExistingGroup:YES
|
|
|
|
transaction:transaction];
|
|
|
|
}
|
|
|
|
|
2017-10-04 16:06:38 +02:00
|
|
|
return nil;
|
2017-09-13 21:35:33 +02:00
|
|
|
}
|
2018-08-01 23:13:01 +02:00
|
|
|
case SSKProtoGroupContextTypeQuit: {
|
2017-10-03 19:41:48 +02:00
|
|
|
if (!oldGroupThread) {
|
2018-09-18 22:44:31 +02:00
|
|
|
OWSLogWarn(@"ignoring quit group message from unknown group.");
|
2017-10-03 19:41:48 +02:00
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
[newMemberIds removeObject:envelope.source];
|
|
|
|
oldGroupThread.groupModel.groupMemberIds = [newMemberIds.allObjects mutableCopy];
|
|
|
|
[oldGroupThread saveWithTransaction:transaction];
|
2017-09-13 21:35:33 +02:00
|
|
|
|
2017-10-03 19:41:48 +02:00
|
|
|
NSString *nameString = [self.contactsManager displayNameForPhoneIdentifier:envelope.source];
|
2017-09-13 21:35:33 +02:00
|
|
|
NSString *updateGroupInfo =
|
|
|
|
[NSString stringWithFormat:NSLocalizedString(@"GROUP_MEMBER_LEFT", @""), nameString];
|
2018-09-20 22:10:35 +02:00
|
|
|
[[[TSInfoMessage alloc] initWithTimestamp:[NSDate ows_millisecondTimeStamp]
|
|
|
|
inThread:oldGroupThread
|
|
|
|
messageType:TSInfoMessageTypeGroupUpdate
|
|
|
|
customMessage:updateGroupInfo] saveWithTransaction:transaction];
|
2017-10-04 16:06:38 +02:00
|
|
|
return nil;
|
2017-09-13 21:35:33 +02:00
|
|
|
}
|
2018-08-01 23:13:01 +02:00
|
|
|
case SSKProtoGroupContextTypeDeliver: {
|
2017-10-03 19:41:48 +02:00
|
|
|
if (!oldGroupThread) {
|
2018-08-27 18:51:32 +02:00
|
|
|
OWSFailDebug(@"ignoring deliver group message from unknown group.");
|
2017-10-03 19:41:48 +02:00
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
|
2018-05-02 18:43:23 +02:00
|
|
|
if (body.length == 0 && attachmentIds.count < 1 && !contact) {
|
2018-08-27 18:55:37 +02:00
|
|
|
OWSLogWarn(@"ignoring empty incoming message from: %@ for group: %@ with timestamp: %lu",
|
2017-09-13 21:35:33 +02:00
|
|
|
envelopeAddress(envelope),
|
|
|
|
groupId,
|
|
|
|
(unsigned long)timestamp);
|
2017-10-03 19:41:48 +02:00
|
|
|
return nil;
|
2017-09-13 21:35:33 +02:00
|
|
|
}
|
2017-10-03 19:41:48 +02:00
|
|
|
|
2018-04-07 21:26:22 +02:00
|
|
|
TSQuotedMessage *_Nullable quotedMessage = [TSQuotedMessage quotedMessageForDataMessage:dataMessage
|
|
|
|
thread:oldGroupThread
|
|
|
|
transaction:transaction];
|
2018-02-07 18:44:09 +01:00
|
|
|
|
2018-08-27 18:55:37 +02:00
|
|
|
OWSLogDebug(@"incoming message from: %@ for group: %@ with timestamp: %lu",
|
2017-10-03 19:41:48 +02:00
|
|
|
envelopeAddress(envelope),
|
|
|
|
groupId,
|
|
|
|
(unsigned long)timestamp);
|
2018-02-07 18:44:09 +01:00
|
|
|
|
2017-10-04 16:06:38 +02:00
|
|
|
TSIncomingMessage *incomingMessage =
|
2018-09-20 22:10:35 +02:00
|
|
|
[[TSIncomingMessage alloc] initIncomingMessageWithTimestamp:timestamp
|
|
|
|
inThread:oldGroupThread
|
|
|
|
authorId:envelope.source
|
|
|
|
sourceDeviceId:envelope.sourceDevice
|
|
|
|
messageBody:body
|
|
|
|
attachmentIds:attachmentIds
|
|
|
|
expiresInSeconds:dataMessage.expireTimer
|
|
|
|
quotedMessage:quotedMessage
|
|
|
|
contactShare:contact];
|
2018-02-07 18:44:09 +01:00
|
|
|
|
2017-10-04 16:06:38 +02:00
|
|
|
[self finalizeIncomingMessage:incomingMessage
|
|
|
|
thread:oldGroupThread
|
|
|
|
envelope:envelope
|
|
|
|
transaction:transaction];
|
|
|
|
return incomingMessage;
|
2017-09-13 21:35:33 +02:00
|
|
|
}
|
|
|
|
default: {
|
2018-08-27 18:55:37 +02:00
|
|
|
OWSLogWarn(@"Ignoring unknown group message type: %d", (int)dataMessage.group.type);
|
2017-10-03 19:41:48 +02:00
|
|
|
return nil;
|
2017-09-13 21:35:33 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2018-05-02 18:43:23 +02:00
|
|
|
if (body.length == 0 && attachmentIds.count < 1 && !contact) {
|
2018-08-27 18:55:37 +02:00
|
|
|
OWSLogWarn(@"ignoring empty incoming message from: %@ with timestamp: %lu",
|
2017-09-13 21:35:33 +02:00
|
|
|
envelopeAddress(envelope),
|
|
|
|
(unsigned long)timestamp);
|
2017-10-03 19:41:48 +02:00
|
|
|
return nil;
|
2017-09-13 21:35:33 +02:00
|
|
|
}
|
2017-10-03 19:41:48 +02:00
|
|
|
|
2018-08-27 18:55:37 +02:00
|
|
|
OWSLogDebug(
|
|
|
|
@"incoming message from: %@ with timestamp: %lu", envelopeAddress(envelope), (unsigned long)timestamp);
|
2018-07-13 20:03:28 +02:00
|
|
|
TSContactThread *thread =
|
|
|
|
[TSContactThread getOrCreateThreadWithContactId:envelope.source transaction:transaction];
|
2017-10-04 16:06:38 +02:00
|
|
|
|
2018-04-07 21:26:22 +02:00
|
|
|
TSQuotedMessage *_Nullable quotedMessage = [TSQuotedMessage quotedMessageForDataMessage:dataMessage
|
|
|
|
thread:thread
|
|
|
|
transaction:transaction];
|
2018-02-07 18:44:09 +01:00
|
|
|
|
|
|
|
TSIncomingMessage *incomingMessage =
|
2018-09-20 22:10:35 +02:00
|
|
|
[[TSIncomingMessage alloc] initIncomingMessageWithTimestamp:timestamp
|
|
|
|
inThread:thread
|
|
|
|
authorId:[thread contactIdentifier]
|
|
|
|
sourceDeviceId:envelope.sourceDevice
|
|
|
|
messageBody:body
|
|
|
|
attachmentIds:attachmentIds
|
|
|
|
expiresInSeconds:dataMessage.expireTimer
|
|
|
|
quotedMessage:quotedMessage
|
|
|
|
contactShare:contact];
|
2018-04-07 19:59:49 +02:00
|
|
|
|
2017-10-04 16:06:38 +02:00
|
|
|
[self finalizeIncomingMessage:incomingMessage
|
|
|
|
thread:thread
|
|
|
|
envelope:envelope
|
|
|
|
transaction:transaction];
|
|
|
|
return incomingMessage;
|
2017-09-13 21:35:33 +02:00
|
|
|
}
|
2017-10-04 16:06:38 +02:00
|
|
|
}
|
2015-12-07 03:31:43 +01:00
|
|
|
|
2017-10-04 16:06:38 +02:00
|
|
|
- (void)finalizeIncomingMessage:(TSIncomingMessage *)incomingMessage
|
|
|
|
thread:(TSThread *)thread
|
2018-08-01 16:45:21 +02:00
|
|
|
envelope:(SSKProtoEnvelope *)envelope
|
2017-10-04 16:06:38 +02:00
|
|
|
transaction:(YapDatabaseReadWriteTransaction *)transaction
|
|
|
|
{
|
2018-08-30 16:31:01 +02:00
|
|
|
if (!envelope) {
|
|
|
|
OWSFailDebug(@"Missing envelope.");
|
|
|
|
return;
|
|
|
|
}
|
2017-10-04 16:06:38 +02:00
|
|
|
if (!thread) {
|
2018-08-30 16:31:01 +02:00
|
|
|
OWSFailDebug(@"Missing thread.");
|
2017-10-04 16:06:38 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!incomingMessage) {
|
2018-08-30 16:31:01 +02:00
|
|
|
OWSFailDebug(@"Missing incomingMessage.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!transaction) {
|
2018-09-11 00:56:22 +02:00
|
|
|
OWSFail(@"Missing transaction.");
|
2017-10-04 16:06:38 +02:00
|
|
|
return;
|
|
|
|
}
|
2017-09-13 21:35:33 +02:00
|
|
|
|
2017-10-04 16:06:38 +02:00
|
|
|
[incomingMessage saveWithTransaction:transaction];
|
2017-09-13 21:35:33 +02:00
|
|
|
|
2018-04-18 02:53:27 +02:00
|
|
|
// Any messages sent from the current user - from this device or another - should be automatically marked as read.
|
|
|
|
if ([envelope.source isEqualToString:TSAccountManager.localNumber]) {
|
2017-10-04 16:06:38 +02:00
|
|
|
// Don't send a read receipt for messages sent by ourselves.
|
2018-04-18 02:53:27 +02:00
|
|
|
[incomingMessage markAsReadAtTimestamp:envelope.timestamp sendReadReceipt:NO transaction:transaction];
|
2017-10-04 16:06:38 +02:00
|
|
|
}
|
2016-09-08 20:21:10 +02:00
|
|
|
|
2018-04-07 19:59:49 +02:00
|
|
|
TSQuotedMessage *_Nullable quotedMessage = incomingMessage.quotedMessage;
|
|
|
|
if (quotedMessage && quotedMessage.thumbnailAttachmentPointerId) {
|
|
|
|
// We weren't able to derive a local thumbnail, so we'll fetch the referenced attachment.
|
|
|
|
TSAttachmentPointer *attachmentPointer =
|
|
|
|
[TSAttachmentPointer fetchObjectWithUniqueID:quotedMessage.thumbnailAttachmentPointerId
|
|
|
|
transaction:transaction];
|
|
|
|
|
|
|
|
if ([attachmentPointer isKindOfClass:[TSAttachmentPointer class]]) {
|
|
|
|
OWSAttachmentsProcessor *attachmentProcessor =
|
|
|
|
[[OWSAttachmentsProcessor alloc] initWithAttachmentPointer:attachmentPointer
|
|
|
|
networkManager:self.networkManager];
|
|
|
|
|
2018-08-27 18:55:37 +02:00
|
|
|
OWSLogDebug(@"downloading thumbnail for message: %lu", (unsigned long)incomingMessage.timestamp);
|
2018-04-07 19:59:49 +02:00
|
|
|
[attachmentProcessor fetchAttachmentsForMessage:incomingMessage
|
|
|
|
transaction:transaction
|
2018-08-30 16:31:01 +02:00
|
|
|
success:^(TSAttachmentStream *attachmentStream) {
|
|
|
|
[self.dbConnection asyncReadWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) {
|
|
|
|
[incomingMessage setQuotedMessageThumbnailAttachmentStream:attachmentStream];
|
|
|
|
[incomingMessage saveWithTransaction:transaction];
|
|
|
|
}];
|
2018-04-07 19:59:49 +02:00
|
|
|
}
|
2018-08-30 16:31:01 +02:00
|
|
|
failure:^(NSError *error) {
|
2018-08-27 18:55:37 +02:00
|
|
|
OWSLogWarn(@"failed to fetch thumbnail for message: %lu with error: %@",
|
2018-05-07 18:32:31 +02:00
|
|
|
(unsigned long)incomingMessage.timestamp,
|
2018-04-07 19:59:49 +02:00
|
|
|
error);
|
|
|
|
}];
|
|
|
|
}
|
|
|
|
}
|
2017-10-04 16:06:38 +02:00
|
|
|
|
2018-05-03 17:31:06 +02:00
|
|
|
OWSContact *_Nullable contact = incomingMessage.contactShare;
|
|
|
|
if (contact && contact.avatarAttachmentId) {
|
|
|
|
TSAttachmentPointer *attachmentPointer =
|
|
|
|
[TSAttachmentPointer fetchObjectWithUniqueID:contact.avatarAttachmentId transaction:transaction];
|
|
|
|
|
|
|
|
if (![attachmentPointer isKindOfClass:[TSAttachmentPointer class]]) {
|
2018-08-27 18:12:53 +02:00
|
|
|
OWSFailDebug(@"avatar attachmentPointer was unexpectedly nil");
|
2018-05-03 17:31:06 +02:00
|
|
|
} else {
|
|
|
|
OWSAttachmentsProcessor *attachmentProcessor =
|
|
|
|
[[OWSAttachmentsProcessor alloc] initWithAttachmentPointer:attachmentPointer
|
|
|
|
networkManager:self.networkManager];
|
|
|
|
|
2018-08-27 18:55:37 +02:00
|
|
|
OWSLogDebug(@"downloading contact avatar for message: %lu", (unsigned long)incomingMessage.timestamp);
|
2018-05-03 17:31:06 +02:00
|
|
|
[attachmentProcessor fetchAttachmentsForMessage:incomingMessage
|
|
|
|
transaction:transaction
|
2018-08-30 16:31:01 +02:00
|
|
|
success:^(TSAttachmentStream *attachmentStream) {
|
|
|
|
[self.dbConnection asyncReadWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) {
|
|
|
|
[incomingMessage touchWithTransaction:transaction];
|
|
|
|
}];
|
2018-05-03 17:31:06 +02:00
|
|
|
}
|
2018-08-30 16:31:01 +02:00
|
|
|
failure:^(NSError *error) {
|
2018-08-27 18:55:37 +02:00
|
|
|
OWSLogWarn(@"failed to fetch contact avatar for message: %lu with error: %@",
|
2018-05-07 18:32:31 +02:00
|
|
|
(unsigned long)incomingMessage.timestamp,
|
2018-05-03 17:31:06 +02:00
|
|
|
error);
|
|
|
|
}];
|
|
|
|
}
|
|
|
|
}
|
2017-10-04 16:06:38 +02:00
|
|
|
// In case we already have a read receipt for this new message (this happens sometimes).
|
|
|
|
[OWSReadReceiptManager.sharedManager applyEarlyReadReceiptsForIncomingMessage:incomingMessage
|
|
|
|
transaction:transaction];
|
2016-10-05 17:42:44 +02:00
|
|
|
|
2018-09-28 01:40:51 +02:00
|
|
|
[[OWSDisappearingMessagesJob sharedJob] becomeConsistentWithConfigurationForMessage:incomingMessage
|
|
|
|
contactsManager:self.contactsManager
|
|
|
|
transaction:transaction];
|
|
|
|
|
2017-10-04 16:06:38 +02:00
|
|
|
// Update thread preview in inbox
|
|
|
|
[thread touchWithTransaction:transaction];
|
2016-08-23 22:38:05 +02:00
|
|
|
|
2018-09-17 15:27:58 +02:00
|
|
|
[SSKEnvironment.shared.notificationsManager notifyUserForIncomingMessage:incomingMessage
|
|
|
|
inThread:thread
|
|
|
|
contactsManager:self.contactsManager
|
|
|
|
transaction:transaction];
|
2015-12-07 03:31:43 +01:00
|
|
|
}
|
|
|
|
|
2016-10-14 23:00:29 +02:00
|
|
|
#pragma mark - helpers
|
2015-12-07 03:31:43 +01:00
|
|
|
|
2018-08-01 23:13:01 +02:00
|
|
|
- (BOOL)isDataMessageGroupAvatarUpdate:(SSKProtoDataMessage *)dataMessage
|
2016-10-14 23:00:29 +02:00
|
|
|
{
|
2018-08-30 16:31:01 +02:00
|
|
|
if (!dataMessage) {
|
|
|
|
OWSFailDebug(@"Missing dataMessage.");
|
|
|
|
return NO;
|
|
|
|
}
|
|
|
|
|
2018-08-08 19:41:12 +02:00
|
|
|
return (dataMessage.group != nil && dataMessage.group.type == SSKProtoGroupContextTypeUpdate
|
|
|
|
&& dataMessage.group.avatar != nil);
|
2015-12-07 03:31:43 +01:00
|
|
|
}
|
|
|
|
|
2017-07-25 00:05:26 +02:00
|
|
|
/**
|
|
|
|
* @returns
|
2017-10-03 19:41:48 +02:00
|
|
|
* Group or Contact thread for message, creating a new contact thread if necessary,
|
|
|
|
* but never creating a new group thread.
|
2017-07-25 00:05:26 +02:00
|
|
|
*/
|
2018-08-01 16:45:21 +02:00
|
|
|
- (nullable TSThread *)threadForEnvelope:(SSKProtoEnvelope *)envelope
|
2018-08-01 23:13:01 +02:00
|
|
|
dataMessage:(SSKProtoDataMessage *)dataMessage
|
2017-10-03 19:41:48 +02:00
|
|
|
transaction:(YapDatabaseReadWriteTransaction *)transaction
|
2016-10-05 17:42:44 +02:00
|
|
|
{
|
2018-08-30 16:31:01 +02:00
|
|
|
if (!envelope) {
|
|
|
|
OWSFailDebug(@"Missing envelope.");
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
if (!dataMessage) {
|
|
|
|
OWSFailDebug(@"Missing dataMessage.");
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
if (!transaction) {
|
2018-09-11 00:56:22 +02:00
|
|
|
OWSFail(@"Missing transaction.");
|
2018-08-30 16:31:01 +02:00
|
|
|
return nil;
|
|
|
|
}
|
2017-09-13 21:35:33 +02:00
|
|
|
|
2018-08-08 19:41:12 +02:00
|
|
|
if (dataMessage.group) {
|
2017-10-03 19:41:48 +02:00
|
|
|
NSData *groupId = dataMessage.group.id;
|
2018-09-06 19:01:24 +02:00
|
|
|
OWSAssertDebug(groupId.length > 0);
|
2017-10-03 19:41:48 +02:00
|
|
|
TSGroupThread *_Nullable groupThread = [TSGroupThread threadWithGroupId:groupId transaction:transaction];
|
|
|
|
// This method should only be called from a code path that has already verified
|
|
|
|
// that this is a "known" group.
|
2018-09-06 19:01:24 +02:00
|
|
|
OWSAssertDebug(groupThread);
|
2017-10-03 19:41:48 +02:00
|
|
|
return groupThread;
|
2016-10-05 17:42:44 +02:00
|
|
|
} else {
|
2017-09-13 21:35:33 +02:00
|
|
|
return [TSContactThread getOrCreateThreadWithContactId:envelope.source transaction:transaction];
|
2016-10-05 17:42:44 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-07 03:31:43 +01:00
|
|
|
@end
|
2016-10-05 17:42:44 +02:00
|
|
|
|
|
|
|
NS_ASSUME_NONNULL_END
|