2017-01-24 21:47:41 +01:00
|
|
|
//
|
|
|
|
// Copyright (c) 2017 Open Whisper Systems. All rights reserved.
|
|
|
|
//
|
2015-12-07 03:31:43 +01:00
|
|
|
|
2017-09-14 17:00:30 +02:00
|
|
|
#import "OWSMessageManager.h"
|
2016-08-26 01:01:35 +02:00
|
|
|
#import "ContactsManagerProtocol.h"
|
2017-08-28 18:03:59 +02:00
|
|
|
#import "Cryptography.h"
|
2016-08-26 01:01:35 +02:00
|
|
|
#import "MimeTypeUtil.h"
|
2017-09-22 16:30:35 +02:00
|
|
|
#import "NSDate+OWS.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"
|
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"
|
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"
|
2016-04-08 09:38:34 +02:00
|
|
|
#import "TSAccountManager.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"
|
|
|
|
#import "TSStorageManager+SessionStore.h"
|
|
|
|
#import "TSStorageManager.h"
|
2015-12-07 03:31:43 +01:00
|
|
|
#import "TextSecureKitEnv.h"
|
|
|
|
|
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
|
|
|
|
2016-12-18 22:08:26 +01:00
|
|
|
@property (nonatomic, readonly) id<OWSCallMessageHandler> callMessageHandler;
|
2016-10-01 20:42:39 +02:00
|
|
|
@property (nonatomic, readonly) id<ContactsManagerProtocol> contactsManager;
|
2016-10-05 17:42:44 +02:00
|
|
|
@property (nonatomic, readonly) TSStorageManager *storageManager;
|
2016-10-14 23:00:29 +02:00
|
|
|
@property (nonatomic, readonly) OWSMessageSender *messageSender;
|
2017-02-16 00:32:27 +01:00
|
|
|
@property (nonatomic, readonly) OWSIncomingMessageFinder *incomingMessageFinder;
|
2017-04-03 20:42:04 +02:00
|
|
|
@property (nonatomic, readonly) OWSBlockingManager *blockingManager;
|
2017-06-22 03:04:16 +02:00
|
|
|
@property (nonatomic, readonly) OWSIdentityManager *identityManager;
|
2017-09-14 17:00:30 +02:00
|
|
|
@property (nonatomic, readonly) TSNetworkManager *networkManager;
|
|
|
|
@property (nonatomic, readonly) YapDatabaseConnection *dbConnection;
|
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
|
|
|
|
{
|
|
|
|
static OWSMessageManager *sharedMyManager = nil;
|
2015-12-07 03:31:43 +01:00
|
|
|
static dispatch_once_t onceToken;
|
|
|
|
dispatch_once(&onceToken, ^{
|
2016-10-14 23:00:29 +02:00
|
|
|
sharedMyManager = [[self alloc] initDefault];
|
2015-12-07 03:31:43 +01:00
|
|
|
});
|
|
|
|
return sharedMyManager;
|
|
|
|
}
|
|
|
|
|
2016-10-14 23:00:29 +02:00
|
|
|
- (instancetype)initDefault
|
2016-09-23 22:55:56 +02:00
|
|
|
{
|
2016-10-14 23:00:29 +02:00
|
|
|
TSNetworkManager *networkManager = [TSNetworkManager sharedManager];
|
|
|
|
TSStorageManager *storageManager = [TSStorageManager sharedManager];
|
|
|
|
id<ContactsManagerProtocol> contactsManager = [TextSecureKitEnv sharedEnv].contactsManager;
|
2016-12-18 22:08:26 +01:00
|
|
|
id<OWSCallMessageHandler> callMessageHandler = [TextSecureKitEnv sharedEnv].callMessageHandler;
|
2017-06-22 03:04:16 +02:00
|
|
|
OWSIdentityManager *identityManager = [OWSIdentityManager sharedManager];
|
2017-03-30 18:37:22 +02:00
|
|
|
OWSMessageSender *messageSender = [TextSecureKitEnv sharedEnv].messageSender;
|
2017-09-14 17:00:30 +02:00
|
|
|
|
2016-10-14 23:00:29 +02:00
|
|
|
|
|
|
|
return [self initWithNetworkManager:networkManager
|
|
|
|
storageManager:storageManager
|
2016-12-18 22:08:26 +01:00
|
|
|
callMessageHandler:callMessageHandler
|
2016-10-14 23:00:29 +02:00
|
|
|
contactsManager:contactsManager
|
2017-06-22 03:04:16 +02:00
|
|
|
identityManager:identityManager
|
2016-10-14 23:00:29 +02:00
|
|
|
messageSender:messageSender];
|
2016-09-23 22:55:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
- (instancetype)initWithNetworkManager:(TSNetworkManager *)networkManager
|
2016-10-05 17:42:44 +02:00
|
|
|
storageManager:(TSStorageManager *)storageManager
|
2016-12-18 22:08:26 +01:00
|
|
|
callMessageHandler:(id<OWSCallMessageHandler>)callMessageHandler
|
2016-10-01 20:42:39 +02:00
|
|
|
contactsManager:(id<ContactsManagerProtocol>)contactsManager
|
2017-06-22 03:04:16 +02:00
|
|
|
identityManager:(OWSIdentityManager *)identityManager
|
2016-10-14 23:00:29 +02:00
|
|
|
messageSender:(OWSMessageSender *)messageSender
|
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
|
|
|
}
|
|
|
|
|
2016-10-05 17:42:44 +02:00
|
|
|
_storageManager = storageManager;
|
2016-09-23 22:55:56 +02:00
|
|
|
_networkManager = networkManager;
|
2016-12-18 22:08:26 +01:00
|
|
|
_callMessageHandler = callMessageHandler;
|
2016-10-01 20:42:39 +02:00
|
|
|
_contactsManager = contactsManager;
|
2017-06-22 03:04:16 +02:00
|
|
|
_identityManager = identityManager;
|
2016-10-14 23:00:29 +02:00
|
|
|
_messageSender = messageSender;
|
2016-09-23 22:55:56 +02:00
|
|
|
|
2016-10-05 17:42:44 +02:00
|
|
|
_dbConnection = storageManager.newDatabaseConnection;
|
2017-02-16 00:32:27 +01:00
|
|
|
_incomingMessageFinder = [[OWSIncomingMessageFinder alloc] initWithDatabase:storageManager.database];
|
2017-04-03 20:42:04 +02:00
|
|
|
_blockingManager = [OWSBlockingManager sharedManager];
|
2017-04-01 00:36:08 +02:00
|
|
|
|
|
|
|
OWSSingletonAssert();
|
|
|
|
|
2017-06-30 18:12:37 +02:00
|
|
|
[self startObserving];
|
|
|
|
|
2015-12-07 03:31:43 +01:00
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2017-06-30 18:12:37 +02:00
|
|
|
- (void)startObserving
|
|
|
|
{
|
|
|
|
[[NSNotificationCenter defaultCenter] addObserver:self
|
|
|
|
selector:@selector(yapDatabaseModified:)
|
|
|
|
name:YapDatabaseModifiedNotification
|
|
|
|
object:nil];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)yapDatabaseModified:(NSNotification *)notification
|
|
|
|
{
|
|
|
|
[self updateApplicationBadgeCount];
|
|
|
|
}
|
|
|
|
|
2017-09-13 18:14:22 +02:00
|
|
|
#pragma mark - Blocking
|
2017-04-25 21:42:49 +02:00
|
|
|
|
2017-09-13 18:14:22 +02:00
|
|
|
- (BOOL)isEnvelopeBlocked:(OWSSignalServiceProtosEnvelope *)envelope
|
2016-08-22 22:09:58 +02:00
|
|
|
{
|
2017-09-13 18:14:22 +02:00
|
|
|
OWSAssert(envelope);
|
|
|
|
|
2017-09-20 17:48:37 +02:00
|
|
|
return [_blockingManager isRecipientIdBlocked:envelope.source];
|
2017-09-13 18:14:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#pragma mark - message handling
|
|
|
|
|
2017-09-13 21:35:33 +02:00
|
|
|
- (void)processEnvelope:(OWSSignalServiceProtosEnvelope *)envelope
|
|
|
|
plaintextData:(NSData *_Nullable)plaintextData
|
|
|
|
transaction:(YapDatabaseReadWriteTransaction *)transaction
|
2016-08-22 22:09:58 +02:00
|
|
|
{
|
2017-09-13 21:35:33 +02:00
|
|
|
OWSAssert(envelope);
|
|
|
|
OWSAssert(transaction);
|
2017-09-13 18:14:22 +02:00
|
|
|
OWSAssert([TSAccountManager isRegistered]);
|
2017-02-10 01:35:10 +01:00
|
|
|
|
2017-09-20 17:48:37 +02:00
|
|
|
DDLogInfo(@"%@ handling decrypted envelope: %@", self.tag, [self descriptionForEnvelope:envelope]);
|
2015-12-07 03:31:43 +01:00
|
|
|
|
2017-09-13 18:14:22 +02:00
|
|
|
OWSAssert(envelope.source.length > 0);
|
|
|
|
OWSAssert(![self isEnvelopeBlocked:envelope]);
|
2015-12-07 03:31:43 +01:00
|
|
|
|
2017-09-13 18:14:22 +02:00
|
|
|
switch (envelope.type) {
|
|
|
|
case OWSSignalServiceProtosEnvelopeTypeCiphertext:
|
|
|
|
case OWSSignalServiceProtosEnvelopeTypePrekeyBundle:
|
|
|
|
if (plaintextData) {
|
2017-09-13 21:35:33 +02:00
|
|
|
[self handleEnvelope:envelope plaintextData:plaintextData transaction:transaction];
|
2017-09-13 18:14:22 +02:00
|
|
|
} else {
|
|
|
|
OWSFail(
|
|
|
|
@"%@ missing decrypted data for envelope: %@", self.tag, [self descriptionForEnvelope:envelope]);
|
2017-01-24 21:47:41 +01:00
|
|
|
}
|
2017-09-13 18:14:22 +02:00
|
|
|
break;
|
|
|
|
case OWSSignalServiceProtosEnvelopeTypeReceipt:
|
|
|
|
OWSAssert(!plaintextData);
|
2017-09-13 21:35:33 +02:00
|
|
|
[self handleDeliveryReceipt:envelope transaction:transaction];
|
2017-09-13 18:14:22 +02:00
|
|
|
break;
|
|
|
|
// Other messages are just dismissed for now.
|
|
|
|
case OWSSignalServiceProtosEnvelopeTypeKeyExchange:
|
|
|
|
DDLogWarn(@"Received Key Exchange Message, not supported");
|
|
|
|
break;
|
|
|
|
case OWSSignalServiceProtosEnvelopeTypeUnknown:
|
|
|
|
DDLogWarn(@"Received an unknown message type");
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
DDLogWarn(@"Received unhandled envelope type: %d", (int)envelope.type);
|
|
|
|
break;
|
2016-12-06 03:32:11 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-13 18:14:22 +02:00
|
|
|
- (void)handleDeliveryReceipt:(OWSSignalServiceProtosEnvelope *)envelope
|
2017-09-13 21:35:33 +02:00
|
|
|
transaction:(YapDatabaseReadWriteTransaction *)transaction
|
2017-09-13 18:14:22 +02:00
|
|
|
{
|
2017-09-13 21:35:33 +02:00
|
|
|
OWSAssert(envelope);
|
|
|
|
OWSAssert(transaction);
|
|
|
|
|
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
|
|
|
{
|
|
|
|
OWSAssert(recipientId);
|
|
|
|
OWSAssert(sentTimestamps);
|
|
|
|
OWSAssert(transaction);
|
|
|
|
|
|
|
|
for (NSNumber *nsTimestamp in sentTimestamps) {
|
|
|
|
uint64_t timestamp = [nsTimestamp unsignedLongLongValue];
|
|
|
|
|
|
|
|
NSArray<TSOutgoingMessage *> *messages
|
|
|
|
= (NSArray<TSOutgoingMessage *> *)[TSInteraction interactionsWithTimestamp:timestamp
|
|
|
|
ofClass:[TSOutgoingMessage class]
|
|
|
|
withTransaction:transaction];
|
|
|
|
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.
|
2017-09-22 22:40:44 +02:00
|
|
|
DDLogInfo(@"%@ Missing message for delivery receipt: %llu", self.tag, timestamp);
|
|
|
|
} else {
|
|
|
|
if (messages.count > 1) {
|
|
|
|
DDLogInfo(
|
|
|
|
@"%@ More than one message (%zd) for delivery receipt: %llu", self.tag, messages.count, timestamp);
|
|
|
|
}
|
|
|
|
for (TSOutgoingMessage *outgoingMessage in messages) {
|
2017-09-22 23:23:09 +02:00
|
|
|
[outgoingMessage updateWithDeliveredToRecipientId: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
|
|
|
}
|
|
|
|
|
2017-09-13 21:35:33 +02:00
|
|
|
- (void)handleEnvelope:(OWSSignalServiceProtosEnvelope *)envelope
|
|
|
|
plaintextData:(NSData *)plaintextData
|
|
|
|
transaction:(YapDatabaseReadWriteTransaction *)transaction
|
2016-12-06 03:32:11 +01:00
|
|
|
{
|
2017-09-13 21:35:33 +02:00
|
|
|
OWSAssert(envelope);
|
|
|
|
OWSAssert(plaintextData);
|
|
|
|
OWSAssert(transaction);
|
2017-02-22 01:21:09 +01:00
|
|
|
OWSAssert(envelope.hasTimestamp && envelope.timestamp > 0);
|
|
|
|
OWSAssert(envelope.hasSource && envelope.source.length > 0);
|
|
|
|
OWSAssert(envelope.hasSourceDevice && envelope.sourceDevice > 0);
|
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) {
|
2017-08-28 23:29:25 +02:00
|
|
|
DDLogInfo(@"%@ Ignoring previously received envelope from %@ with timestamp: %llu",
|
|
|
|
self.tag,
|
|
|
|
envelopeAddress(envelope),
|
|
|
|
envelope.timestamp);
|
2017-02-16 00:32:27 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-12-06 03:32:11 +01:00
|
|
|
if (envelope.hasContent) {
|
|
|
|
OWSSignalServiceProtosContent *content = [OWSSignalServiceProtosContent parseFromData:plaintextData];
|
2017-04-25 21:42:49 +02:00
|
|
|
DDLogInfo(@"%@ handling content: <Content: %@>", self.tag, [self descriptionForContent:content]);
|
2017-08-01 19:53:51 +02:00
|
|
|
|
2016-12-06 03:32:11 +01:00
|
|
|
if (content.hasSyncMessage) {
|
2017-09-13 21:35:33 +02:00
|
|
|
[self handleIncomingEnvelope:envelope withSyncMessage:content.syncMessage transaction:transaction];
|
2016-12-06 03:32:11 +01:00
|
|
|
} else if (content.hasDataMessage) {
|
2017-09-13 21:35:33 +02:00
|
|
|
[self handleIncomingEnvelope:envelope withDataMessage:content.dataMessage transaction:transaction];
|
2016-12-18 22:08:26 +01:00
|
|
|
} else if (content.hasCallMessage) {
|
|
|
|
[self handleIncomingEnvelope:envelope withCallMessage:content.callMessage];
|
2017-06-22 03:05:21 +02:00
|
|
|
} else if (content.hasNullMessage) {
|
|
|
|
DDLogInfo(@"%@ Received null message.", self.tag);
|
2017-09-15 21:28:44 +02:00
|
|
|
} else if (content.hasReceiptMessage) {
|
2017-09-22 22:40:44 +02:00
|
|
|
[self handleIncomingEnvelope:envelope withReceiptMessage:content.receiptMessage transaction:transaction];
|
2016-08-23 22:38:05 +02:00
|
|
|
} else {
|
2017-02-16 00:32:27 +01:00
|
|
|
DDLogWarn(@"%@ Ignoring envelope. Content with no known payload", self.tag);
|
2016-08-22 22:09:58 +02:00
|
|
|
}
|
2016-12-06 03:32:11 +01:00
|
|
|
} else if (envelope.hasLegacyMessage) { // DEPRECATED - Remove after all clients have been upgraded.
|
|
|
|
OWSSignalServiceProtosDataMessage *dataMessage =
|
|
|
|
[OWSSignalServiceProtosDataMessage parseFromData:plaintextData];
|
2017-08-28 23:00:55 +02:00
|
|
|
DDLogInfo(@"%@ handling message: <DataMessage: %@ />", self.tag, [self descriptionForDataMessage:dataMessage]);
|
2017-08-04 15:33:56 +02:00
|
|
|
|
2017-09-13 21:35:33 +02:00
|
|
|
[self handleIncomingEnvelope:envelope withDataMessage:dataMessage transaction:transaction];
|
2016-12-06 03:32:11 +01:00
|
|
|
} else {
|
2017-08-04 18:01:45 +02:00
|
|
|
OWSProdInfoWEnvelope([OWSAnalyticsEvents messageManagerErrorEnvelopeNoActionablePayload], envelope);
|
2015-12-07 03:31:43 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-13 21:35:33 +02:00
|
|
|
- (void)handleIncomingEnvelope:(OWSSignalServiceProtosEnvelope *)envelope
|
2016-08-22 22:09:58 +02:00
|
|
|
withDataMessage:(OWSSignalServiceProtosDataMessage *)dataMessage
|
2017-09-13 21:35:33 +02:00
|
|
|
transaction:(YapDatabaseReadWriteTransaction *)transaction
|
2016-07-31 02:40:14 +02:00
|
|
|
{
|
2017-09-13 21:35:33 +02:00
|
|
|
OWSAssert(envelope);
|
|
|
|
OWSAssert(dataMessage);
|
|
|
|
OWSAssert(transaction);
|
2017-05-05 23:22:56 +02:00
|
|
|
|
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 {
|
|
|
|
OWSFail(
|
|
|
|
@"Unexpected profile key length:%lu on message from:%@", (unsigned long)profileKey.length, recipientId);
|
|
|
|
}
|
2017-08-04 15:33:56 +02:00
|
|
|
}
|
|
|
|
|
2016-08-22 22:09:58 +02:00
|
|
|
if (dataMessage.hasGroup) {
|
2017-09-13 21:35:33 +02:00
|
|
|
TSGroupModel *emptyModelToFillOutId =
|
|
|
|
[[TSGroupModel alloc] initWithTitle:nil memberIds:nil image:nil groupId:dataMessage.group.id];
|
|
|
|
TSGroupThread *gThread = [TSGroupThread threadWithGroupModel:emptyModelToFillOutId transaction:transaction];
|
|
|
|
BOOL unknownGroup = NO;
|
|
|
|
if (gThread == nil && dataMessage.group.type != OWSSignalServiceProtosGroupContextTypeUpdate) {
|
|
|
|
unknownGroup = YES;
|
|
|
|
}
|
2017-08-08 18:16:17 +02:00
|
|
|
if (unknownGroup) {
|
|
|
|
if (dataMessage.group.type == OWSSignalServiceProtosGroupContextTypeRequestInfo) {
|
2017-09-13 21:35:33 +02:00
|
|
|
DDLogInfo(
|
|
|
|
@"%@ Ignoring group info request for group I don't know about from: %@", self.tag, envelope.source);
|
2017-08-08 18:16:17 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-10-05 17:42:44 +02:00
|
|
|
// FIXME: https://github.com/WhisperSystems/Signal-iOS/issues/1340
|
2017-08-28 23:29:25 +02:00
|
|
|
DDLogInfo(@"%@ Received message from group that I left or don't know about from: %@",
|
2017-05-12 18:39:36 +02:00
|
|
|
self.tag,
|
2017-09-13 21:35:33 +02:00
|
|
|
envelopeAddress(envelope));
|
2017-05-05 23:22:56 +02:00
|
|
|
|
2017-09-13 21:35:33 +02:00
|
|
|
NSString *recipientId = envelope.source;
|
2017-05-05 23:22:56 +02:00
|
|
|
|
2017-09-13 21:35:33 +02:00
|
|
|
TSThread *thread = [TSContactThread getOrCreateThreadWithContactId:recipientId transaction:transaction];
|
2017-05-05 23:22:56 +02:00
|
|
|
|
|
|
|
NSData *groupId = dataMessage.group.id;
|
|
|
|
OWSAssert(groupId);
|
|
|
|
OWSSyncGroupsRequestMessage *syncGroupsRequestMessage =
|
|
|
|
[[OWSSyncGroupsRequestMessage alloc] initWithThread:thread groupId:groupId];
|
|
|
|
[self.messageSender sendMessage:syncGroupsRequestMessage
|
|
|
|
success:^{
|
2017-05-12 18:39:36 +02:00
|
|
|
DDLogWarn(@"%@ Successfully sent Request Group Info message.", self.tag);
|
2017-05-05 23:22:56 +02:00
|
|
|
}
|
|
|
|
failure:^(NSError *error) {
|
|
|
|
DDLogError(@"%@ Failed to send Request Group Info message with error: %@", self.tag, error);
|
|
|
|
}];
|
2017-09-21 20:03:24 +02:00
|
|
|
return;
|
2015-12-07 03:31:43 +01:00
|
|
|
}
|
|
|
|
}
|
2017-09-21 20:03:24 +02:00
|
|
|
|
2016-08-22 22:09:58 +02:00
|
|
|
if ((dataMessage.flags & OWSSignalServiceProtosDataMessageFlagsEndSession) != 0) {
|
2017-09-13 21:35:33 +02:00
|
|
|
[self handleEndSessionMessageWithEnvelope:envelope dataMessage:dataMessage transaction:transaction];
|
2016-10-05 17:42:44 +02:00
|
|
|
} else if ((dataMessage.flags & OWSSignalServiceProtosDataMessageFlagsExpirationTimerUpdate) != 0) {
|
2017-09-13 21:35:33 +02:00
|
|
|
[self handleExpirationTimerUpdateMessageWithEnvelope:envelope dataMessage:dataMessage transaction:transaction];
|
2017-10-02 22:03:17 +02:00
|
|
|
} else if ((dataMessage.flags & OWSSignalServiceProtosDataMessageFlagsProfileKeyUpdate) != 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];
|
2016-10-14 23:00:29 +02:00
|
|
|
if ([self isDataMessageGroupAvatarUpdate:dataMessage]) {
|
|
|
|
DDLogVerbose(@"%@ Data message had group avatar attachment", self.tag);
|
2017-09-13 21:35:33 +02:00
|
|
|
[self handleReceivedGroupAvatarUpdateWithEnvelope:envelope dataMessage:dataMessage transaction:transaction];
|
2016-10-14 23:00:29 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-25 23:16:49 +02:00
|
|
|
- (id<ProfileManagerProtocol>)profileManager
|
|
|
|
{
|
|
|
|
return [TextSecureKitEnv sharedEnv].profileManager;
|
|
|
|
}
|
|
|
|
|
2017-09-15 21:28:44 +02:00
|
|
|
- (void)handleIncomingEnvelope:(OWSSignalServiceProtosEnvelope *)envelope
|
|
|
|
withReceiptMessage:(OWSSignalServiceProtosReceiptMessage *)receiptMessage
|
2017-09-22 22:40:44 +02:00
|
|
|
transaction:(YapDatabaseReadWriteTransaction *)transaction
|
2017-09-15 21:28:44 +02:00
|
|
|
{
|
|
|
|
OWSAssert(envelope);
|
|
|
|
OWSAssert(receiptMessage);
|
2017-09-22 22:40:44 +02:00
|
|
|
OWSAssert(transaction);
|
|
|
|
|
|
|
|
PBArray *messageTimestamps = receiptMessage.timestamp;
|
|
|
|
NSMutableArray<NSNumber *> *sentTimestamps = [NSMutableArray new];
|
|
|
|
for (int i = 0; i < messageTimestamps.count; i++) {
|
|
|
|
UInt64 timestamp = [messageTimestamps uint64AtIndex:i];
|
|
|
|
[sentTimestamps addObject:@(timestamp)];
|
|
|
|
}
|
2017-09-15 21:28:44 +02:00
|
|
|
|
|
|
|
switch (receiptMessage.type) {
|
|
|
|
case OWSSignalServiceProtosReceiptMessageTypeDelivery:
|
2017-09-22 22:40:44 +02:00
|
|
|
DDLogVerbose(@"%@ Processing receipt message with delivery receipts.", self.tag);
|
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;
|
|
|
|
case OWSSignalServiceProtosReceiptMessageTypeRead:
|
|
|
|
DDLogVerbose(@"%@ Processing receipt message with read receipts.", self.tag);
|
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:
|
|
|
|
DDLogInfo(@"%@ Ignoring receipt message of unknown type: %d.", self.tag, (int)receiptMessage.type);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-13 21:35:33 +02:00
|
|
|
- (void)handleIncomingEnvelope:(OWSSignalServiceProtosEnvelope *)envelope
|
2016-12-18 22:08:26 +01:00
|
|
|
withCallMessage:(OWSSignalServiceProtosCallMessage *)callMessage
|
|
|
|
{
|
2017-09-13 21:35:33 +02:00
|
|
|
OWSAssert(envelope);
|
2017-08-04 15:33:56 +02:00
|
|
|
OWSAssert(callMessage);
|
|
|
|
|
|
|
|
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(), ^{
|
|
|
|
if (callMessage.hasOffer) {
|
|
|
|
[self.callMessageHandler receivedOffer:callMessage.offer fromCallerId:envelope.source];
|
|
|
|
} else if (callMessage.hasAnswer) {
|
|
|
|
[self.callMessageHandler receivedAnswer:callMessage.answer fromCallerId:envelope.source];
|
|
|
|
} else if (callMessage.iceUpdate.count > 0) {
|
|
|
|
for (OWSSignalServiceProtosCallMessageIceUpdate *iceUpdate in callMessage.iceUpdate) {
|
|
|
|
[self.callMessageHandler receivedIceUpdate:iceUpdate fromCallerId:envelope.source];
|
|
|
|
}
|
|
|
|
} else if (callMessage.hasHangup) {
|
|
|
|
DDLogVerbose(@"%@ Received CallMessage with Hangup.", self.tag);
|
|
|
|
[self.callMessageHandler receivedHangup:callMessage.hangup fromCallerId:envelope.source];
|
|
|
|
} else if (callMessage.hasBusy) {
|
|
|
|
[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
|
|
|
}
|
|
|
|
|
2016-10-14 23:00:29 +02:00
|
|
|
- (void)handleReceivedGroupAvatarUpdateWithEnvelope:(OWSSignalServiceProtosEnvelope *)envelope
|
|
|
|
dataMessage:(OWSSignalServiceProtosDataMessage *)dataMessage
|
2017-09-13 21:35:33 +02:00
|
|
|
transaction:(YapDatabaseReadWriteTransaction *)transaction
|
2016-10-14 23:00:29 +02:00
|
|
|
{
|
2017-09-13 21:35:33 +02:00
|
|
|
OWSAssert(envelope);
|
|
|
|
OWSAssert(dataMessage);
|
|
|
|
OWSAssert(transaction);
|
|
|
|
|
|
|
|
TSGroupThread *groupThread =
|
|
|
|
[TSGroupThread getOrCreateThreadWithGroupIdData:dataMessage.group.id transaction:transaction];
|
2017-08-04 18:01:45 +02:00
|
|
|
OWSAssert(groupThread);
|
2016-10-14 23:00:29 +02:00
|
|
|
OWSAttachmentsProcessor *attachmentsProcessor =
|
|
|
|
[[OWSAttachmentsProcessor alloc] initWithAttachmentProtos:@[ dataMessage.group.avatar ]
|
|
|
|
timestamp:envelope.timestamp
|
|
|
|
relay:envelope.relay
|
|
|
|
thread:groupThread
|
2017-09-13 21:35:33 +02:00
|
|
|
networkManager:self.networkManager
|
2017-09-13 22:48:38 +02:00
|
|
|
storageManager:self.storageManager
|
2017-09-13 21:35:33 +02:00
|
|
|
transaction:transaction];
|
2016-10-14 23:00:29 +02:00
|
|
|
|
|
|
|
if (!attachmentsProcessor.hasSupportedAttachments) {
|
|
|
|
DDLogWarn(@"%@ received unsupported group avatar envelope", self.tag);
|
|
|
|
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) {
|
2016-10-14 23:00:29 +02:00
|
|
|
DDLogError(@"%@ failed to fetch attachments for group avatar sent at: %llu. with error: %@",
|
|
|
|
self.tag,
|
|
|
|
envelope.timestamp,
|
|
|
|
error);
|
|
|
|
}];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)handleReceivedMediaWithEnvelope:(OWSSignalServiceProtosEnvelope *)envelope
|
|
|
|
dataMessage:(OWSSignalServiceProtosDataMessage *)dataMessage
|
2017-09-13 21:35:33 +02:00
|
|
|
transaction:(YapDatabaseReadWriteTransaction *)transaction
|
2016-10-14 23:00:29 +02:00
|
|
|
{
|
2017-09-13 21:35:33 +02:00
|
|
|
OWSAssert(envelope);
|
|
|
|
OWSAssert(dataMessage);
|
|
|
|
OWSAssert(transaction);
|
|
|
|
|
|
|
|
TSThread *thread = [self threadForEnvelope:envelope dataMessage:dataMessage transaction:transaction];
|
2017-08-04 18:01:45 +02:00
|
|
|
OWSAssert(thread);
|
2016-10-14 23:00:29 +02:00
|
|
|
OWSAttachmentsProcessor *attachmentsProcessor =
|
|
|
|
[[OWSAttachmentsProcessor alloc] initWithAttachmentProtos:dataMessage.attachments
|
|
|
|
timestamp:envelope.timestamp
|
|
|
|
relay:envelope.relay
|
|
|
|
thread:thread
|
2017-09-13 21:35:33 +02:00
|
|
|
networkManager:self.networkManager
|
2017-09-13 22:48:38 +02:00
|
|
|
storageManager:self.storageManager
|
2017-09-13 21:35:33 +02:00
|
|
|
transaction:transaction];
|
2016-10-14 23:00:29 +02:00
|
|
|
if (!attachmentsProcessor.hasSupportedAttachments) {
|
|
|
|
DDLogWarn(@"%@ received unsupported media envelope", self.tag);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-05-10 16:05:01 +02:00
|
|
|
TSIncomingMessage *_Nullable createdMessage =
|
|
|
|
[self handleReceivedEnvelope:envelope
|
|
|
|
withDataMessage:dataMessage
|
2017-09-13 21:35:33 +02:00
|
|
|
attachmentIds:attachmentsProcessor.supportedAttachmentIds
|
|
|
|
transaction:transaction];
|
2017-05-10 16:05:01 +02:00
|
|
|
|
|
|
|
if (!createdMessage) {
|
|
|
|
return;
|
|
|
|
}
|
2016-10-14 23:00:29 +02:00
|
|
|
|
2017-06-13 16:50:42 +02:00
|
|
|
DDLogDebug(@"%@ incoming attachment message: %@", self.tag, createdMessage.debugDescription);
|
|
|
|
|
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) {
|
2016-10-14 23:00:29 +02:00
|
|
|
DDLogDebug(
|
|
|
|
@"%@ successfully fetched attachment: %@ for message: %@", self.tag, attachmentStream, createdMessage);
|
|
|
|
}
|
2017-05-10 16:05:01 +02:00
|
|
|
failure:^(NSError *error) {
|
2016-10-14 23:00:29 +02:00
|
|
|
DDLogError(
|
|
|
|
@"%@ failed to fetch attachments for message: %@ with error: %@", self.tag, createdMessage, error);
|
|
|
|
}];
|
2015-12-07 03:31:43 +01:00
|
|
|
}
|
|
|
|
|
2017-09-13 21:35:33 +02:00
|
|
|
- (void)handleIncomingEnvelope:(OWSSignalServiceProtosEnvelope *)envelope
|
2016-08-23 22:38:05 +02:00
|
|
|
withSyncMessage:(OWSSignalServiceProtosSyncMessage *)syncMessage
|
2017-09-13 21:35:33 +02:00
|
|
|
transaction:(YapDatabaseReadWriteTransaction *)transaction
|
2016-08-23 22:38:05 +02:00
|
|
|
{
|
2017-09-13 21:35:33 +02:00
|
|
|
OWSAssert(envelope);
|
|
|
|
OWSAssert(syncMessage);
|
|
|
|
OWSAssert(transaction);
|
2017-08-04 18:01:45 +02:00
|
|
|
OWSAssert([TSAccountManager isRegistered]);
|
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;
|
|
|
|
}
|
|
|
|
|
2016-08-23 22:38:05 +02:00
|
|
|
if (syncMessage.hasSent) {
|
|
|
|
OWSIncomingSentMessageTranscript *transcript =
|
2017-09-13 21:35:33 +02:00
|
|
|
[[OWSIncomingSentMessageTranscript alloc] initWithProto:syncMessage.sent relay:envelope.relay];
|
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
|
|
|
|
2017-08-04 15:33:56 +02:00
|
|
|
OWSSignalServiceProtosDataMessage *dataMessage = syncMessage.sent.message;
|
|
|
|
OWSAssert(dataMessage);
|
|
|
|
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.
|
2017-08-29 15:31:02 +02:00
|
|
|
if (dataMessage.hasGroup) {
|
|
|
|
[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) {
|
2016-10-14 23:00:29 +02:00
|
|
|
TSGroupThread *groupThread =
|
2017-09-13 21:35:33 +02:00
|
|
|
[TSGroupThread getOrCreateThreadWithGroupIdData:syncMessage.sent.message.group.id
|
|
|
|
transaction:transaction];
|
2017-09-20 17:48:37 +02:00
|
|
|
[groupThread updateAvatarWithAttachmentStream:attachmentStream];
|
2017-09-13 21:35:33 +02:00
|
|
|
}
|
|
|
|
transaction:transaction];
|
2016-10-14 23:00:29 +02:00
|
|
|
} else {
|
2017-05-10 16:05:01 +02:00
|
|
|
[recordJob runWithAttachmentHandler:^(TSAttachmentStream *attachmentStream) {
|
2016-10-14 23:00:29 +02:00
|
|
|
DDLogDebug(@"%@ successfully fetched transcript attachment: %@", self.tag, attachmentStream);
|
2017-09-13 21:35:33 +02:00
|
|
|
}
|
|
|
|
transaction:transaction];
|
2016-10-14 23:00:29 +02:00
|
|
|
}
|
2016-09-01 16:28:35 +02:00
|
|
|
} else if (syncMessage.hasRequest) {
|
2016-08-26 01:01:35 +02:00
|
|
|
if (syncMessage.request.type == OWSSignalServiceProtosSyncMessageRequestTypeContacts) {
|
|
|
|
OWSSyncContactsMessage *syncContactsMessage =
|
2017-08-26 23:45:05 +02:00
|
|
|
[[OWSSyncContactsMessage alloc] initWithSignalAccounts:self.contactsManager.signalAccounts
|
|
|
|
identityManager:self.identityManager
|
|
|
|
profileManager:self.profileManager];
|
2017-09-18 21:02:34 +02:00
|
|
|
DataSource *dataSource =
|
2017-09-08 18:51:25 +02:00
|
|
|
[DataSourceValue dataSourceWithSyncMessage:[syncContactsMessage buildPlainTextAttachmentData]];
|
|
|
|
[self.messageSender sendTemporaryAttachmentData:dataSource
|
|
|
|
contentType:OWSMimeTypeApplicationOctetStream
|
|
|
|
inMessage:syncContactsMessage
|
|
|
|
success:^{
|
|
|
|
DDLogInfo(@"%@ Successfully sent Contacts response syncMessage.", self.tag);
|
|
|
|
}
|
|
|
|
failure:^(NSError *error) {
|
|
|
|
DDLogError(@"%@ Failed to send Contacts response syncMessage with error: %@", self.tag, error);
|
|
|
|
}];
|
2016-08-26 01:01:35 +02:00
|
|
|
} else if (syncMessage.request.type == OWSSignalServiceProtosSyncMessageRequestTypeGroups) {
|
2016-08-27 00:07:54 +02:00
|
|
|
OWSSyncGroupsMessage *syncGroupsMessage = [[OWSSyncGroupsMessage alloc] init];
|
2017-09-18 21:02:34 +02:00
|
|
|
DataSource *dataSource =
|
2017-09-08 18:51:25 +02:00
|
|
|
[DataSourceValue dataSourceWithSyncMessage:[syncGroupsMessage buildPlainTextAttachmentData]];
|
|
|
|
[self.messageSender sendTemporaryAttachmentData:dataSource
|
|
|
|
contentType:OWSMimeTypeApplicationOctetStream
|
|
|
|
inMessage:syncGroupsMessage
|
|
|
|
success:^{
|
|
|
|
DDLogInfo(@"%@ Successfully sent Groups response syncMessage.", self.tag);
|
|
|
|
}
|
|
|
|
failure:^(NSError *error) {
|
|
|
|
DDLogError(@"%@ Failed to send Groups response syncMessage with error: %@", self.tag, error);
|
|
|
|
}];
|
2017-10-02 22:35:24 +02:00
|
|
|
} else if (syncMessage.request.type == OWSSignalServiceProtosSyncMessageRequestTypeBlocked) {
|
|
|
|
// TODO
|
|
|
|
DDLogWarn(@"%@ Received unsupported request for block list", self.tag);
|
|
|
|
} else if (syncMessage.request.type == OWSSignalServiceProtosSyncMessageRequestTypeConfiguration) {
|
|
|
|
BOOL areReadReceiptsEnabled = [[OWSReadReceiptManager sharedManager] areReadReceiptsEnabled];
|
|
|
|
OWSSyncConfigurationMessage *syncConfigurationMessage =
|
|
|
|
[[OWSSyncConfigurationMessage alloc] initWithReadReceiptsEnabled:areReadReceiptsEnabled];
|
|
|
|
[self.messageSender sendMessage:syncConfigurationMessage
|
|
|
|
success:^{
|
|
|
|
DDLogInfo(@"%@ Successfully sent Configuration response syncMessage.", self.tag);
|
|
|
|
}
|
|
|
|
failure:^(NSError *error) {
|
|
|
|
DDLogError(@"%@ Failed to send Configuration response syncMessage with error: %@", self.tag, error);
|
|
|
|
}];
|
2016-11-22 18:14:39 +01:00
|
|
|
} else {
|
|
|
|
DDLogWarn(@"%@ ignoring unsupported sync request message", self.tag);
|
2016-08-26 01:01:35 +02:00
|
|
|
}
|
2017-03-31 02:04:19 +02:00
|
|
|
} else if (syncMessage.hasBlocked) {
|
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), ^{
|
2017-09-13 21:35:33 +02:00
|
|
|
[_blockingManager setBlockedPhoneNumbers:blockedPhoneNumbers sendSyncMessage:NO];
|
|
|
|
});
|
2016-09-01 16:28:35 +02:00
|
|
|
} else if (syncMessage.read.count > 0) {
|
2016-10-05 17:42:44 +02:00
|
|
|
DDLogInfo(@"%@ Received %ld read receipt(s)", self.tag, (u_long)syncMessage.read.count);
|
2016-09-01 16:28:35 +02:00
|
|
|
|
2017-09-26 17:45:22 +02:00
|
|
|
[OWSReadReceiptManager.sharedManager processReadReceiptsFromLinkedDevice:syncMessage.read
|
|
|
|
transaction:transaction];
|
2017-06-22 02:19:23 +02:00
|
|
|
} else if (syncMessage.hasVerified) {
|
|
|
|
DDLogInfo(@"%@ Received verification state for %@", self.tag, syncMessage.verified.destination);
|
2017-09-28 16:01:17 +02:00
|
|
|
[self.identityManager processIncomingSyncMessage:syncMessage.verified];
|
2016-08-23 22:38:05 +02:00
|
|
|
} else {
|
2016-10-05 17:42:44 +02:00
|
|
|
DDLogWarn(@"%@ Ignoring unsupported sync message.", self.tag);
|
2016-08-23 22:38:05 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-13 21:35:33 +02:00
|
|
|
- (void)handleEndSessionMessageWithEnvelope:(OWSSignalServiceProtosEnvelope *)envelope
|
2016-08-22 22:09:58 +02:00
|
|
|
dataMessage:(OWSSignalServiceProtosDataMessage *)dataMessage
|
2017-09-13 21:35:33 +02:00
|
|
|
transaction:(YapDatabaseReadWriteTransaction *)transaction
|
2016-08-22 22:09:58 +02:00
|
|
|
{
|
2017-09-13 21:35:33 +02:00
|
|
|
OWSAssert(envelope);
|
|
|
|
OWSAssert(dataMessage);
|
|
|
|
OWSAssert(transaction);
|
|
|
|
|
|
|
|
TSContactThread *thread = [TSContactThread getOrCreateThreadWithContactId:envelope.source transaction:transaction];
|
|
|
|
|
2017-09-20 17:48:37 +02:00
|
|
|
[[[TSInfoMessage alloc] initWithTimestamp:envelope.timestamp
|
|
|
|
inThread:thread
|
|
|
|
messageType:TSInfoMessageTypeSessionDidEnd] saveWithTransaction:transaction];
|
2015-12-07 03:31:43 +01:00
|
|
|
|
2017-04-07 01:23:55 +02:00
|
|
|
dispatch_async([OWSDispatch sessionStoreQueue], ^{
|
2017-09-20 17:48:37 +02:00
|
|
|
[self.storageManager deleteAllSessionsForContact:envelope.source];
|
2017-04-07 01:23:55 +02:00
|
|
|
});
|
2015-12-07 03:31:43 +01:00
|
|
|
}
|
|
|
|
|
2016-10-05 17:42:44 +02:00
|
|
|
- (void)handleExpirationTimerUpdateMessageWithEnvelope:(OWSSignalServiceProtosEnvelope *)envelope
|
|
|
|
dataMessage:(OWSSignalServiceProtosDataMessage *)dataMessage
|
2017-09-13 21:35:33 +02:00
|
|
|
transaction:(YapDatabaseReadWriteTransaction *)transaction
|
2016-07-29 21:33:25 +02:00
|
|
|
{
|
2017-09-13 21:35:33 +02:00
|
|
|
OWSAssert(envelope);
|
|
|
|
OWSAssert(dataMessage);
|
|
|
|
OWSAssert(transaction);
|
|
|
|
|
|
|
|
TSThread *thread = [self threadForEnvelope:envelope dataMessage:dataMessage transaction:transaction];
|
2016-10-05 17:42:44 +02:00
|
|
|
|
|
|
|
OWSDisappearingMessagesConfiguration *disappearingMessagesConfiguration;
|
|
|
|
if (dataMessage.hasExpireTimer && dataMessage.expireTimer > 0) {
|
|
|
|
DDLogInfo(@"%@ Expiring messages duration turned to %u for thread %@",
|
|
|
|
self.tag,
|
|
|
|
(unsigned int)dataMessage.expireTimer,
|
|
|
|
thread);
|
|
|
|
disappearingMessagesConfiguration =
|
|
|
|
[[OWSDisappearingMessagesConfiguration alloc] initWithThreadId:thread.uniqueId
|
|
|
|
enabled:YES
|
|
|
|
durationSeconds:dataMessage.expireTimer];
|
|
|
|
} else {
|
|
|
|
DDLogInfo(@"%@ Expiring messages have been turned off for thread %@", self.tag, thread);
|
|
|
|
disappearingMessagesConfiguration = [[OWSDisappearingMessagesConfiguration alloc]
|
|
|
|
initWithThreadId:thread.uniqueId
|
|
|
|
enabled:NO
|
|
|
|
durationSeconds:OWSDisappearingMessagesConfigurationDefaultExpirationDuration];
|
|
|
|
}
|
2017-08-04 18:01:45 +02:00
|
|
|
OWSAssert(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 =
|
|
|
|
[[OWSDisappearingConfigurationUpdateInfoMessage alloc] initWithTimestamp:envelope.timestamp
|
|
|
|
thread:thread
|
|
|
|
configuration:disappearingMessagesConfiguration
|
|
|
|
createdByRemoteName:name];
|
2017-09-13 21:35:33 +02:00
|
|
|
[message saveWithTransaction:transaction];
|
2015-12-07 03:31:43 +01:00
|
|
|
}
|
|
|
|
|
2017-09-13 21:35:33 +02:00
|
|
|
- (void)handleProfileKeyMessageWithEnvelope:(OWSSignalServiceProtosEnvelope *)envelope
|
2017-08-28 23:00:55 +02:00
|
|
|
dataMessage:(OWSSignalServiceProtosDataMessage *)dataMessage
|
|
|
|
{
|
2017-09-13 21:35:33 +02:00
|
|
|
OWSAssert(envelope);
|
|
|
|
OWSAssert(dataMessage);
|
|
|
|
|
|
|
|
NSString *recipientId = envelope.source;
|
2017-08-28 23:00:55 +02:00
|
|
|
if (!dataMessage.hasProfileKey) {
|
2017-09-13 21:35:33 +02:00
|
|
|
OWSFail(@"%@ received profile key message without profile key from: %@", self.tag, envelopeAddress(envelope));
|
2017-08-28 23:00:55 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
NSData *profileKey = dataMessage.profileKey;
|
|
|
|
if (profileKey.length != kAES256_KeyByteLength) {
|
2017-08-29 00:03:14 +02:00
|
|
|
OWSFail(@"%@ received profile key of unexpected length:%lu from:%@",
|
2017-08-28 23:00:55 +02:00
|
|
|
self.tag,
|
|
|
|
(unsigned long)profileKey.length,
|
2017-09-13 21:35:33 +02:00
|
|
|
envelopeAddress(envelope));
|
2017-08-28 23:00:55 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
id<ProfileManagerProtocol> profileManager = [TextSecureKitEnv sharedEnv].profileManager;
|
|
|
|
[profileManager setProfileKeyData:profileKey forRecipientId:recipientId];
|
|
|
|
}
|
|
|
|
|
2017-09-13 21:35:33 +02:00
|
|
|
- (void)handleReceivedTextMessageWithEnvelope:(OWSSignalServiceProtosEnvelope *)envelope
|
2016-10-05 17:42:44 +02:00
|
|
|
dataMessage:(OWSSignalServiceProtosDataMessage *)dataMessage
|
2017-09-13 21:35:33 +02:00
|
|
|
transaction:(YapDatabaseReadWriteTransaction *)transaction
|
2016-07-29 21:33:25 +02:00
|
|
|
{
|
2017-09-13 21:35:33 +02:00
|
|
|
OWSAssert(envelope);
|
|
|
|
OWSAssert(dataMessage);
|
|
|
|
OWSAssert(transaction);
|
|
|
|
|
|
|
|
[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
|
|
|
|
{
|
|
|
|
OWSAssert(gThread);
|
2017-08-04 18:01:45 +02:00
|
|
|
OWSAssert(gThread.groupModel);
|
2017-05-05 23:22:56 +02:00
|
|
|
OWSAssert(message);
|
|
|
|
|
|
|
|
if (gThread.groupModel.groupImage) {
|
2017-09-08 18:51:25 +02:00
|
|
|
NSData *data = UIImagePNGRepresentation(gThread.groupModel.groupImage);
|
2017-09-18 21:02:34 +02:00
|
|
|
DataSource *_Nullable dataSource = [DataSourceValue dataSourceWithData:data fileExtension:@"png"];
|
2017-09-08 18:51:25 +02:00
|
|
|
[self.messageSender sendAttachmentData:dataSource
|
2017-05-05 23:22:56 +02:00
|
|
|
contentType:OWSMimeTypeImagePng
|
2017-05-15 15:53:16 +02:00
|
|
|
sourceFilename:nil
|
2017-05-05 23:22:56 +02:00
|
|
|
inMessage:message
|
|
|
|
success:^{
|
|
|
|
DDLogDebug(@"%@ Successfully sent group update with avatar", self.tag);
|
|
|
|
}
|
2017-05-10 16:05:01 +02:00
|
|
|
failure:^(NSError *error) {
|
2017-05-05 23:22:56 +02:00
|
|
|
DDLogError(@"%@ Failed to send group avatar update with error: %@", self.tag, error);
|
|
|
|
}];
|
|
|
|
} else {
|
|
|
|
[self.messageSender sendMessage:message
|
|
|
|
success:^{
|
|
|
|
DDLogDebug(@"%@ Successfully sent group update", self.tag);
|
|
|
|
}
|
2017-05-10 16:05:01 +02:00
|
|
|
failure:^(NSError *error) {
|
2017-05-05 23:22:56 +02:00
|
|
|
DDLogError(@"%@ Failed to send group update with error: %@", self.tag, error);
|
|
|
|
}];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-12 17:38:24 +02:00
|
|
|
- (void)handleGroupInfoRequest:(OWSSignalServiceProtosEnvelope *)envelope
|
|
|
|
dataMessage:(OWSSignalServiceProtosDataMessage *)dataMessage
|
2017-09-13 21:35:33 +02:00
|
|
|
transaction:(YapDatabaseReadWriteTransaction *)transaction
|
2017-05-10 16:05:01 +02:00
|
|
|
{
|
2017-09-13 21:35:33 +02:00
|
|
|
OWSAssert(envelope);
|
|
|
|
OWSAssert(dataMessage);
|
|
|
|
OWSAssert(transaction);
|
2017-05-10 16:05:01 +02:00
|
|
|
OWSAssert(dataMessage.group.type == OWSSignalServiceProtosGroupContextTypeRequestInfo);
|
|
|
|
|
|
|
|
NSData *groupId = dataMessage.hasGroup ? dataMessage.group.id : nil;
|
|
|
|
if (!groupId) {
|
2017-07-21 21:38:44 +02:00
|
|
|
OWSFail(@"Group info request is missing group id.");
|
2017-05-10 16:05:01 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-05-12 18:39:36 +02:00
|
|
|
DDLogWarn(@"%@ Received 'Request Group Info' message for group: %@ from: %@", self.tag, groupId, envelope.source);
|
2017-05-10 16:05:01 +02:00
|
|
|
|
2017-09-13 21:35:33 +02:00
|
|
|
TSGroupModel *emptyModelToFillOutId =
|
|
|
|
[[TSGroupModel alloc] initWithTitle:nil memberIds:nil image:nil groupId:dataMessage.group.id];
|
|
|
|
TSGroupThread *gThread = [TSGroupThread threadWithGroupModel:emptyModelToFillOutId transaction:transaction];
|
|
|
|
if (!gThread) {
|
|
|
|
DDLogWarn(@"%@ Unknown group: %@", self.tag, groupId);
|
|
|
|
return;
|
|
|
|
}
|
2017-05-12 18:39:36 +02:00
|
|
|
|
2017-09-13 21:35:33 +02:00
|
|
|
if (![gThread.groupModel.groupMemberIds containsObject:envelope.source]) {
|
|
|
|
DDLogWarn(@"%@ Ignoring 'Request Group Info' message for non-member of group. %@ not in %@",
|
|
|
|
self.tag,
|
|
|
|
envelope.source,
|
|
|
|
gThread.groupModel.groupMemberIds);
|
|
|
|
}
|
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];
|
|
|
|
TSOutgoingMessage *message = [[TSOutgoingMessage alloc] initWithTimestamp:[NSDate ows_millisecondTimeStamp]
|
|
|
|
inThread:gThread
|
|
|
|
groupMetaMessage:TSGroupMessageUpdate];
|
|
|
|
[message updateWithCustomMessage:updateGroupInfo transaction:transaction];
|
|
|
|
// Only send this group update to the requester.
|
|
|
|
[message updateWithSingleGroupRecipient:envelope.source transaction:transaction];
|
|
|
|
|
2017-09-21 17:55:25 +02:00
|
|
|
[self sendGroupUpdateForThread:gThread message:message];
|
2017-05-10 16:05:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
- (TSIncomingMessage *_Nullable)handleReceivedEnvelope:(OWSSignalServiceProtosEnvelope *)envelope
|
|
|
|
withDataMessage:(OWSSignalServiceProtosDataMessage *)dataMessage
|
|
|
|
attachmentIds:(NSArray<NSString *> *)attachmentIds
|
2017-09-13 21:35:33 +02:00
|
|
|
transaction:(YapDatabaseReadWriteTransaction *)transaction
|
2016-07-29 21:33:25 +02:00
|
|
|
{
|
2017-09-13 21:35:33 +02:00
|
|
|
OWSAssert(envelope);
|
|
|
|
OWSAssert(dataMessage);
|
|
|
|
OWSAssert(transaction);
|
|
|
|
|
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;
|
|
|
|
NSData *groupId = dataMessage.hasGroup ? dataMessage.group.id : nil;
|
2015-12-07 03:31:43 +01:00
|
|
|
|
2016-10-05 17:42:44 +02:00
|
|
|
__block TSIncomingMessage *_Nullable incomingMessage;
|
2016-09-13 16:15:01 +02:00
|
|
|
__block TSThread *thread;
|
2016-08-23 22:38:05 +02:00
|
|
|
|
2017-02-09 00:17:24 +01:00
|
|
|
// Do this outside of a transaction to avoid deadlock
|
|
|
|
OWSAssert([TSAccountManager isRegistered]);
|
|
|
|
NSString *localNumber = [TSAccountManager localNumber];
|
|
|
|
|
2017-05-10 16:05:01 +02:00
|
|
|
if (dataMessage.group.type == OWSSignalServiceProtosGroupContextTypeRequestInfo) {
|
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-09-13 21:35:33 +02:00
|
|
|
if (groupId) {
|
|
|
|
NSMutableArray *uniqueMemberIds = [[[NSSet setWithArray:dataMessage.group.members] allObjects] mutableCopy];
|
|
|
|
TSGroupModel *model = [[TSGroupModel alloc] initWithTitle:dataMessage.group.name
|
|
|
|
memberIds:uniqueMemberIds
|
|
|
|
image:nil
|
|
|
|
groupId:dataMessage.group.id];
|
|
|
|
TSGroupThread *gThread = [TSGroupThread getOrCreateThreadWithGroupModel:model transaction:transaction];
|
|
|
|
|
|
|
|
switch (dataMessage.group.type) {
|
|
|
|
case OWSSignalServiceProtosGroupContextTypeUpdate: {
|
|
|
|
NSString *updateGroupInfo =
|
|
|
|
[gThread.groupModel getInfoStringAboutUpdateTo:model contactsManager:self.contactsManager];
|
|
|
|
gThread.groupModel = model;
|
|
|
|
[gThread saveWithTransaction:transaction];
|
|
|
|
[[[TSInfoMessage alloc] initWithTimestamp:timestamp
|
|
|
|
inThread:gThread
|
|
|
|
messageType:TSInfoMessageTypeGroupUpdate
|
|
|
|
customMessage:updateGroupInfo] saveWithTransaction:transaction];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case OWSSignalServiceProtosGroupContextTypeQuit: {
|
|
|
|
NSString *nameString = [self.contactsManager displayNameForPhoneIdentifier:envelope.source];
|
|
|
|
|
|
|
|
NSString *updateGroupInfo =
|
|
|
|
[NSString stringWithFormat:NSLocalizedString(@"GROUP_MEMBER_LEFT", @""), nameString];
|
|
|
|
NSMutableArray *newGroupMembers = [NSMutableArray arrayWithArray:gThread.groupModel.groupMemberIds];
|
|
|
|
[newGroupMembers removeObject:envelope.source];
|
|
|
|
gThread.groupModel.groupMemberIds = newGroupMembers;
|
|
|
|
|
|
|
|
[gThread saveWithTransaction:transaction];
|
|
|
|
[[[TSInfoMessage alloc] initWithTimestamp:timestamp
|
|
|
|
inThread:gThread
|
|
|
|
messageType:TSInfoMessageTypeGroupUpdate
|
|
|
|
customMessage:updateGroupInfo] saveWithTransaction:transaction];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case OWSSignalServiceProtosGroupContextTypeDeliver: {
|
|
|
|
if (body.length == 0 && attachmentIds.count < 1) {
|
2017-09-14 16:00:34 +02:00
|
|
|
DDLogWarn(@"%@ ignoring empty incoming message from: %@ for group: %@ with timestamp: %lu",
|
2017-09-13 21:35:33 +02:00
|
|
|
self.tag,
|
|
|
|
envelopeAddress(envelope),
|
|
|
|
groupId,
|
|
|
|
(unsigned long)timestamp);
|
|
|
|
} else {
|
2017-09-14 16:00:34 +02:00
|
|
|
DDLogDebug(@"%@ incoming message from: %@ for group: %@ with timestamp: %lu",
|
2017-09-13 21:35:33 +02:00
|
|
|
self.tag,
|
|
|
|
envelopeAddress(envelope),
|
|
|
|
groupId,
|
|
|
|
(unsigned long)timestamp);
|
|
|
|
incomingMessage = [[TSIncomingMessage alloc] initWithTimestamp:timestamp
|
|
|
|
inThread:gThread
|
|
|
|
authorId:envelope.source
|
|
|
|
sourceDeviceId:envelope.sourceDevice
|
|
|
|
messageBody:body
|
|
|
|
attachmentIds:attachmentIds
|
|
|
|
expiresInSeconds:dataMessage.expireTimer];
|
|
|
|
|
|
|
|
[incomingMessage saveWithTransaction:transaction];
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default: {
|
2017-09-14 16:00:34 +02:00
|
|
|
DDLogWarn(@"%@ Ignoring unknown group message type: %d", self.tag, (int)dataMessage.group.type);
|
2017-09-13 21:35:33 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
thread = gThread;
|
|
|
|
} else {
|
|
|
|
if (body.length == 0 && attachmentIds.count < 1) {
|
2017-09-14 16:00:34 +02:00
|
|
|
DDLogWarn(@"%@ ignoring empty incoming message from: %@ with timestamp: %lu",
|
2017-09-13 21:35:33 +02:00
|
|
|
self.tag,
|
|
|
|
envelopeAddress(envelope),
|
|
|
|
(unsigned long)timestamp);
|
|
|
|
} else {
|
2017-09-14 16:00:34 +02:00
|
|
|
DDLogDebug(@"%@ incoming message from: %@ with timestamp: %lu",
|
2017-09-13 21:35:33 +02:00
|
|
|
self.tag,
|
|
|
|
envelopeAddress(envelope),
|
|
|
|
(unsigned long)timestamp);
|
|
|
|
TSContactThread *cThread = [TSContactThread getOrCreateThreadWithContactId:envelope.source
|
|
|
|
transaction:transaction
|
|
|
|
relay:envelope.relay];
|
|
|
|
|
|
|
|
incomingMessage = [[TSIncomingMessage alloc] initWithTimestamp:timestamp
|
|
|
|
inThread:cThread
|
|
|
|
authorId:[cThread contactIdentifier]
|
|
|
|
sourceDeviceId:envelope.sourceDevice
|
|
|
|
messageBody:body
|
|
|
|
attachmentIds:attachmentIds
|
|
|
|
expiresInSeconds:dataMessage.expireTimer];
|
|
|
|
|
|
|
|
[incomingMessage saveWithTransaction:transaction];
|
|
|
|
thread = cThread;
|
|
|
|
}
|
|
|
|
}
|
2015-12-07 03:31:43 +01:00
|
|
|
|
2017-08-28 23:29:25 +02:00
|
|
|
if (thread && incomingMessage) {
|
2017-09-13 21:35:33 +02:00
|
|
|
// Any messages sent from the current user - from this device or another - should be
|
|
|
|
// automatically marked as read.
|
|
|
|
BOOL shouldMarkMessageAsRead = [envelope.source isEqualToString:localNumber];
|
|
|
|
if (shouldMarkMessageAsRead) {
|
|
|
|
// Don't send a read receipt for messages sent by ourselves.
|
|
|
|
[incomingMessage markAsReadWithTransaction:transaction sendReadReceipt:NO updateExpiration:YES];
|
|
|
|
}
|
|
|
|
|
|
|
|
DDLogDebug(@"%@ shouldMarkMessageAsRead: %d (%@)", self.tag, shouldMarkMessageAsRead, envelope.source);
|
|
|
|
|
|
|
|
// Other clients allow attachments to be sent along with body, we want the text displayed as a separate
|
|
|
|
// message
|
|
|
|
if ([attachmentIds count] > 0 && body != nil && body.length > 0) {
|
2017-09-21 17:55:25 +02:00
|
|
|
// We want the text to be displayed under the attachment.
|
2017-09-13 21:35:33 +02:00
|
|
|
uint64_t textMessageTimestamp = timestamp + 1;
|
|
|
|
TSIncomingMessage *textMessage = [[TSIncomingMessage alloc] initWithTimestamp:textMessageTimestamp
|
|
|
|
inThread:thread
|
|
|
|
authorId:envelope.source
|
|
|
|
sourceDeviceId:envelope.sourceDevice
|
|
|
|
messageBody:body
|
|
|
|
attachmentIds:@[]
|
|
|
|
expiresInSeconds:dataMessage.expireTimer];
|
|
|
|
DDLogDebug(@"%@ incoming extra text message: %@", self.tag, incomingMessage.debugDescription);
|
|
|
|
[textMessage saveWithTransaction:transaction];
|
|
|
|
}
|
|
|
|
}
|
2016-09-08 20:21:10 +02:00
|
|
|
|
2017-09-13 21:35:33 +02:00
|
|
|
if (thread && incomingMessage) {
|
2017-09-26 17:45:22 +02:00
|
|
|
// In case we already have a read receipt for this new message (this happens sometimes).
|
2017-09-27 20:05:21 +02:00
|
|
|
[OWSReadReceiptManager.sharedManager applyEarlyReadReceiptsForIncomingMessage:incomingMessage
|
|
|
|
transaction:transaction];
|
2017-09-26 17:45:22 +02:00
|
|
|
|
2017-09-28 16:01:17 +02:00
|
|
|
[OWSDisappearingMessagesJob becomeConsistentWithConfigurationForMessage:incomingMessage
|
|
|
|
contactsManager:self.contactsManager];
|
2016-10-05 17:42:44 +02:00
|
|
|
|
2017-09-28 16:01:17 +02:00
|
|
|
// Update thread preview in inbox
|
|
|
|
[thread touchWithTransaction:transaction];
|
2017-09-13 21:35:33 +02:00
|
|
|
|
2017-09-28 16:01:17 +02:00
|
|
|
[[TextSecureKitEnv sharedEnv].notificationsManager notifyUserForIncomingMessage:incomingMessage
|
|
|
|
inThread:thread
|
2017-09-29 21:49:03 +02:00
|
|
|
contactsManager:self.contactsManager
|
|
|
|
transaction:transaction];
|
2016-09-13 16:15:01 +02:00
|
|
|
}
|
2016-08-23 22:38:05 +02:00
|
|
|
|
|
|
|
return incomingMessage;
|
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
|
|
|
|
2016-10-14 23:00:29 +02:00
|
|
|
- (BOOL)isDataMessageGroupAvatarUpdate:(OWSSignalServiceProtosDataMessage *)dataMessage
|
|
|
|
{
|
2017-09-14 17:00:30 +02:00
|
|
|
return dataMessage.hasGroup && dataMessage.group.type == OWSSignalServiceProtosGroupContextTypeUpdate
|
2016-10-14 23:00:29 +02:00
|
|
|
&& dataMessage.group.hasAvatar;
|
2015-12-07 03:31:43 +01:00
|
|
|
}
|
|
|
|
|
2017-07-25 00:05:26 +02:00
|
|
|
/**
|
|
|
|
* @returns
|
|
|
|
* Group or Contact thread for message, creating a new one if necessary.
|
|
|
|
*/
|
2016-10-05 17:42:44 +02:00
|
|
|
- (TSThread *)threadForEnvelope:(OWSSignalServiceProtosEnvelope *)envelope
|
|
|
|
dataMessage:(OWSSignalServiceProtosDataMessage *)dataMessage
|
2017-09-13 21:35:33 +02:00
|
|
|
transaction:(YapDatabaseReadWriteTransaction *)transaction
|
2016-10-05 17:42:44 +02:00
|
|
|
{
|
2017-09-13 21:35:33 +02:00
|
|
|
OWSAssert(envelope);
|
|
|
|
OWSAssert(dataMessage);
|
|
|
|
OWSAssert(transaction);
|
|
|
|
|
2016-10-05 17:42:44 +02:00
|
|
|
if (dataMessage.hasGroup) {
|
2017-09-13 21:35:33 +02:00
|
|
|
return [TSGroupThread getOrCreateThreadWithGroupIdData:dataMessage.group.id transaction:transaction];
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-14 17:00:30 +02:00
|
|
|
- (NSUInteger)unreadMessagesCount
|
|
|
|
{
|
2015-12-07 03:31:43 +01:00
|
|
|
__block NSUInteger numberOfItems;
|
|
|
|
[self.dbConnection readWithBlock:^(YapDatabaseReadTransaction *transaction) {
|
2017-09-14 17:00:30 +02:00
|
|
|
numberOfItems = [[transaction ext:TSUnreadDatabaseViewExtensionName] numberOfItemsInAllGroups];
|
2015-12-07 03:31:43 +01:00
|
|
|
}];
|
|
|
|
|
|
|
|
return numberOfItems;
|
|
|
|
}
|
|
|
|
|
2017-09-14 17:00:30 +02:00
|
|
|
- (NSUInteger)unreadMessagesCountExcept:(TSThread *)thread
|
|
|
|
{
|
2015-12-07 03:31:43 +01:00
|
|
|
__block NSUInteger numberOfItems;
|
|
|
|
[self.dbConnection readWithBlock:^(YapDatabaseReadTransaction *transaction) {
|
2017-08-16 21:33:46 +02:00
|
|
|
id databaseView = [transaction ext:TSUnreadDatabaseViewExtensionName];
|
|
|
|
OWSAssert(databaseView);
|
|
|
|
numberOfItems = ([databaseView numberOfItemsInAllGroups] - [databaseView numberOfItemsInGroup:thread.uniqueId]);
|
2015-12-07 03:31:43 +01:00
|
|
|
}];
|
|
|
|
|
|
|
|
return numberOfItems;
|
|
|
|
}
|
|
|
|
|
2017-06-30 18:12:37 +02:00
|
|
|
- (void)updateApplicationBadgeCount
|
|
|
|
{
|
|
|
|
NSUInteger numberOfItems = [self unreadMessagesCount];
|
|
|
|
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:numberOfItems];
|
|
|
|
}
|
|
|
|
|
2017-09-14 17:00:30 +02:00
|
|
|
- (NSUInteger)unreadMessagesInThread:(TSThread *)thread
|
|
|
|
{
|
2015-12-07 03:31:43 +01:00
|
|
|
__block NSUInteger numberOfItems;
|
|
|
|
[self.dbConnection readWithBlock:^(YapDatabaseReadTransaction *transaction) {
|
2017-09-14 17:00:30 +02:00
|
|
|
numberOfItems = [[transaction ext:TSUnreadDatabaseViewExtensionName] numberOfItemsInGroup:thread.uniqueId];
|
2015-12-07 03:31:43 +01:00
|
|
|
}];
|
|
|
|
return numberOfItems;
|
|
|
|
}
|
|
|
|
|
2016-10-05 17:42:44 +02:00
|
|
|
#pragma mark - Logging
|
|
|
|
|
|
|
|
+ (NSString *)tag
|
|
|
|
{
|
|
|
|
return [NSString stringWithFormat:@"[%@]", self.class];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSString *)tag
|
|
|
|
{
|
|
|
|
return self.class.tag;
|
|
|
|
}
|
|
|
|
|
2015-12-07 03:31:43 +01:00
|
|
|
@end
|
2016-10-05 17:42:44 +02:00
|
|
|
|
|
|
|
NS_ASSUME_NONNULL_END
|