session-ios/src/Messages/TSMessagesManager.m

670 lines
32 KiB
Mathematica
Raw Normal View History

2015-12-07 03:31:43 +01:00
// Created by Frederic Jacobs on 11/11/14.
// Copyright (c) 2014 Open Whisper Systems. All rights reserved.
#import "TSMessagesManager.h"
#import "ContactsManagerProtocol.h"
#import "ContactsUpdater.h"
#import "MimeTypeUtil.h"
2015-12-07 03:31:43 +01:00
#import "NSData+messagePadding.h"
#import "NSDate+millisecondTimeStamp.h"
#import "OWSDisappearingConfigurationUpdateInfoMessage.h"
#import "OWSDisappearingMessagesConfiguration.h"
#import "OWSDisappearingMessagesJob.h"
#import "OWSIncomingSentMessageTranscript.h"
2016-09-01 16:28:35 +02:00
#import "OWSReadReceiptsProcessor.h"
#import "OWSRecordTranscriptJob.h"
#import "OWSSyncContactsMessage.h"
#import "OWSSyncGroupsMessage.h"
#import "TSAccountManager.h"
2015-12-07 03:31:43 +01:00
#import "TSAttachmentStream.h"
#import "TSCall.h"
#import "TSContactThread.h"
2015-12-07 03:31:43 +01:00
#import "TSDatabaseView.h"
#import "TSGroupModel.h"
#import "TSGroupThread.h"
2015-12-07 03:31:43 +01:00
#import "TSInfoMessage.h"
#import "TSInvalidIdentityKeyReceivingErrorMessage.h"
#import "TSMessagesManager+attachments.h"
#import "TSNetworkManager.h"
2015-12-07 03:31:43 +01:00
#import "TSStorageHeaders.h"
#import "TextSecureKitEnv.h"
#import <AxolotlKit/AxolotlExceptions.h>
#import <AxolotlKit/SessionCipher.h>
2015-12-07 03:31:43 +01:00
NS_ASSUME_NONNULL_BEGIN
@interface TSMessagesManager ()
@property (nonatomic, readonly) id<ContactsManagerProtocol> contactsManager;
@property (nonatomic, readonly) TSStorageManager *storageManager;
@end
2015-12-07 03:31:43 +01:00
@implementation TSMessagesManager
+ (instancetype)sharedManager {
static TSMessagesManager *sharedMyManager = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
sharedMyManager = [[self alloc] init];
});
return sharedMyManager;
}
- (instancetype)init
{
return [self initWithNetworkManager:[TSNetworkManager sharedManager]
storageManager:[TSStorageManager sharedManager]
contactsManager:[TextSecureKitEnv sharedEnv].contactsManager
contactsUpdater:[ContactsUpdater sharedUpdater]];
}
- (instancetype)initWithNetworkManager:(TSNetworkManager *)networkManager
storageManager:(TSStorageManager *)storageManager
contactsManager:(id<ContactsManagerProtocol>)contactsManager
contactsUpdater:(ContactsUpdater *)contactsUpdater
{
2015-12-07 03:31:43 +01:00
self = [super init];
if (!self) {
return self;
2015-12-07 03:31:43 +01:00
}
_storageManager = storageManager;
_networkManager = networkManager;
_contactsManager = contactsManager;
_contactsUpdater = contactsUpdater;
_dbConnection = storageManager.newDatabaseConnection;
_disappearingMessagesJob = [[OWSDisappearingMessagesJob alloc] initWithStorageManager:storageManager];
2015-12-07 03:31:43 +01:00
return self;
}
- (void)handleReceivedEnvelope:(OWSSignalServiceProtosEnvelope *)envelope
{
2015-12-07 03:31:43 +01:00
@try {
switch (envelope.type) {
case OWSSignalServiceProtosEnvelopeTypeCiphertext:
[self handleSecureMessage:envelope];
2015-12-07 03:31:43 +01:00
break;
case OWSSignalServiceProtosEnvelopeTypePrekeyBundle:
[self handlePreKeyBundle:envelope];
break;
case OWSSignalServiceProtosEnvelopeTypeReceipt:
DDLogInfo(@"Received a delivery receipt");
[self handleDeliveryReceipt:envelope];
2015-12-07 03:31:43 +01:00
break;
// Other messages are just dismissed for now.
case OWSSignalServiceProtosEnvelopeTypeKeyExchange:
2015-12-07 03:31:43 +01:00
DDLogWarn(@"Received Key Exchange Message, not supported");
break;
case OWSSignalServiceProtosEnvelopeTypeUnknown:
2015-12-07 03:31:43 +01:00
DDLogWarn(@"Received an unknown message type");
break;
default:
2016-09-01 21:42:51 +02:00
DDLogWarn(@"Received unhandled envelope type: %d", (int)envelope.type);
2015-12-07 03:31:43 +01:00
break;
}
} @catch (NSException *exception) {
DDLogWarn(@"Received an incorrectly formatted protocol buffer: %@", exception.debugDescription);
}
}
- (void)handleDeliveryReceipt:(OWSSignalServiceProtosEnvelope *)envelope
{
2015-12-07 03:31:43 +01:00
[self.dbConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) {
TSInteraction *interaction =
[TSInteraction interactionForTimestamp:envelope.timestamp withTransaction:transaction];
if ([interaction isKindOfClass:[TSOutgoingMessage class]]) {
TSOutgoingMessage *outgoingMessage = (TSOutgoingMessage *)interaction;
outgoingMessage.messageState = TSOutgoingMessageStateDelivered;
2015-12-07 03:31:43 +01:00
[outgoingMessage saveWithTransaction:transaction];
}
2015-12-07 03:31:43 +01:00
}];
}
- (void)handleSecureMessage:(OWSSignalServiceProtosEnvelope *)messageEnvelope
{
2015-12-07 03:31:43 +01:00
@synchronized(self) {
TSStorageManager *storageManager = [TSStorageManager sharedManager];
NSString *recipientId = messageEnvelope.source;
int deviceId = messageEnvelope.sourceDevice;
2015-12-07 03:31:43 +01:00
if (![storageManager containsSession:recipientId deviceId:deviceId]) {
[self.dbConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) {
TSErrorMessage *errorMessage =
[TSErrorMessage missingSessionWithEnvelope:messageEnvelope withTransaction:transaction];
[errorMessage saveWithTransaction:transaction];
2015-12-07 03:31:43 +01:00
}];
return;
}
// DEPRECATED - Remove after all clients have been upgraded.
NSData *encryptedData = messageEnvelope.hasContent ? messageEnvelope.content : messageEnvelope.legacyMessage;
if (!encryptedData) {
DDLogError(@"Skipping message envelope which had no encrypted data");
return;
}
2015-12-07 03:31:43 +01:00
NSData *plaintextData;
2015-12-07 03:31:43 +01:00
@try {
WhisperMessage *message = [[WhisperMessage alloc] initWithData:encryptedData];
2015-12-07 03:31:43 +01:00
SessionCipher *cipher = [[SessionCipher alloc] initWithSessionStore:storageManager
preKeyStore:storageManager
signedPreKeyStore:storageManager
identityKeyStore:storageManager
recipientId:recipientId
deviceId:deviceId];
plaintextData = [[cipher decrypt:message] removePadding];
2015-12-07 03:31:43 +01:00
} @catch (NSException *exception) {
[self processException:exception envelope:messageEnvelope];
return;
}
if (messageEnvelope.hasContent) {
OWSSignalServiceProtosContent *content = [OWSSignalServiceProtosContent parseFromData:plaintextData];
if (content.hasSyncMessage) {
[self handleIncomingEnvelope:messageEnvelope withSyncMessage:content.syncMessage];
} else if (content.dataMessage) {
[self handleIncomingEnvelope:messageEnvelope withDataMessage:content.dataMessage];
}
} else if (messageEnvelope.hasLegacyMessage) { // DEPRECATED - Remove after all clients have been upgraded.
OWSSignalServiceProtosDataMessage *dataMessage =
[OWSSignalServiceProtosDataMessage parseFromData:plaintextData];
[self handleIncomingEnvelope:messageEnvelope withDataMessage:dataMessage];
} else {
DDLogWarn(@"Ignoring content that has no dataMessage or syncMessage.");
}
2015-12-07 03:31:43 +01:00
}
}
- (void)handlePreKeyBundle:(OWSSignalServiceProtosEnvelope *)preKeyEnvelope
{
2015-12-07 03:31:43 +01:00
@synchronized(self) {
TSStorageManager *storageManager = [TSStorageManager sharedManager];
NSString *recipientId = preKeyEnvelope.source;
int deviceId = preKeyEnvelope.sourceDevice;
2015-12-07 03:31:43 +01:00
// DEPRECATED - Remove after all clients have been upgraded.
NSData *encryptedData = preKeyEnvelope.hasContent ? preKeyEnvelope.content : preKeyEnvelope.legacyMessage;
if (!encryptedData) {
DDLogError(@"Skipping message envelope which had no encrypted data");
return;
}
2015-12-07 03:31:43 +01:00
NSData *plaintextData;
2015-12-07 03:31:43 +01:00
@try {
PreKeyWhisperMessage *message = [[PreKeyWhisperMessage alloc] initWithData:encryptedData];
2015-12-07 03:31:43 +01:00
SessionCipher *cipher = [[SessionCipher alloc] initWithSessionStore:storageManager
preKeyStore:storageManager
signedPreKeyStore:storageManager
identityKeyStore:storageManager
recipientId:recipientId
deviceId:deviceId];
plaintextData = [[cipher decrypt:message] removePadding];
2015-12-07 03:31:43 +01:00
} @catch (NSException *exception) {
[self processException:exception envelope:preKeyEnvelope];
return;
}
if (preKeyEnvelope.hasContent) {
OWSSignalServiceProtosContent *content = [OWSSignalServiceProtosContent parseFromData:plaintextData];
if (content.hasSyncMessage) {
[self handleIncomingEnvelope:preKeyEnvelope withSyncMessage:content.syncMessage];
} else if (content.dataMessage) {
[self handleIncomingEnvelope:preKeyEnvelope withDataMessage:content.dataMessage];
}
} else if (preKeyEnvelope.hasLegacyMessage) { // DEPRECATED - Remove after all clients have been upgraded.
OWSSignalServiceProtosDataMessage *dataMessage =
[OWSSignalServiceProtosDataMessage parseFromData:plaintextData];
[self handleIncomingEnvelope:preKeyEnvelope withDataMessage:dataMessage];
} else {
DDLogWarn(@"Ignoring content that has no dataMessage or syncMessage.");
}
2015-12-07 03:31:43 +01:00
}
}
- (void)handleIncomingEnvelope:(OWSSignalServiceProtosEnvelope *)incomingEnvelope
withDataMessage:(OWSSignalServiceProtosDataMessage *)dataMessage
{
if (dataMessage.hasGroup) {
2015-12-07 03:31:43 +01:00
__block BOOL ignoreMessage = NO;
[self.dbConnection readWithBlock:^(YapDatabaseReadTransaction *transaction) {
TSGroupModel *emptyModelToFillOutId =
[[TSGroupModel alloc] initWithTitle:nil memberIds:nil image:nil groupId:dataMessage.group.id];
TSGroupThread *gThread = [TSGroupThread threadWithGroupModel:emptyModelToFillOutId transaction:transaction];
if (gThread == nil && dataMessage.group.type != OWSSignalServiceProtosGroupContextTypeUpdate) {
ignoreMessage = YES;
}
2015-12-07 03:31:43 +01:00
}];
if (ignoreMessage) {
// FIXME: https://github.com/WhisperSystems/Signal-iOS/issues/1340
DDLogDebug(@"%@ Received message from group that I left or don't know about, ignoring", self.tag);
2015-12-07 03:31:43 +01:00
return;
}
}
if ((dataMessage.flags & OWSSignalServiceProtosDataMessageFlagsEndSession) != 0) {
DDLogVerbose(@"%@ Received end session message", self.tag);
[self handleEndSessionMessageWithEnvelope:incomingEnvelope dataMessage:dataMessage];
} else if ((dataMessage.flags & OWSSignalServiceProtosDataMessageFlagsExpirationTimerUpdate) != 0) {
DDLogVerbose(@"%@ Received expiration timer update message", self.tag);
[self handleExpirationTimerUpdateMessageWithEnvelope:incomingEnvelope dataMessage:dataMessage];
} else if (dataMessage.attachments.count > 0
|| (dataMessage.hasGroup && dataMessage.group.type == OWSSignalServiceProtosGroupContextTypeUpdate
&& dataMessage.group.hasAvatar)) {
DDLogVerbose(@"%@ Received push media message (attachment) or group with an avatar", self.tag);
[self handleReceivedMediaWithEnvelope:incomingEnvelope dataMessage:dataMessage];
2015-12-07 03:31:43 +01:00
} else {
DDLogVerbose(@"%@ Received data message.", self.tag);
[self handleReceivedTextMessageWithEnvelope:incomingEnvelope dataMessage:dataMessage];
2015-12-07 03:31:43 +01:00
}
}
- (void)handleIncomingEnvelope:(OWSSignalServiceProtosEnvelope *)messageEnvelope
withSyncMessage:(OWSSignalServiceProtosSyncMessage *)syncMessage
{
if (syncMessage.hasSent) {
DDLogInfo(@"%@ Received `sent` syncMessage, recording message transcript.", self.tag);
OWSIncomingSentMessageTranscript *transcript =
[[OWSIncomingSentMessageTranscript alloc] initWithProto:syncMessage.sent relay:messageEnvelope.relay];
[[[OWSRecordTranscriptJob alloc] initWithMessagesManager:self incomingSentMessageTranscript:transcript] run];
2016-09-01 16:28:35 +02:00
} else if (syncMessage.hasRequest) {
if (syncMessage.request.type == OWSSignalServiceProtosSyncMessageRequestTypeContacts) {
DDLogInfo(@"%@ Received request `Contacts` syncMessage.", self.tag);
OWSSyncContactsMessage *syncContactsMessage =
[[OWSSyncContactsMessage alloc] initWithContactsManager:self.contactsManager];
[self sendTemporaryAttachment:[syncContactsMessage buildPlainTextAttachmentData]
contentType:OWSMimeTypeApplicationOctetStream
inMessage:syncContactsMessage
thread:nil
success:^{
DDLogInfo(@"%@ Successfully sent Contacts response syncMessage.", self.tag);
}
failure:^{
DDLogError(@"%@ Failed to send Contacts response syncMessage.", self.tag);
}];
} else if (syncMessage.request.type == OWSSignalServiceProtosSyncMessageRequestTypeGroups) {
DDLogInfo(@"%@ Received request `groups` syncMessage.", self.tag);
OWSSyncGroupsMessage *syncGroupsMessage = [[OWSSyncGroupsMessage alloc] init];
[self sendTemporaryAttachment:[syncGroupsMessage buildPlainTextAttachmentData]
contentType:OWSMimeTypeApplicationOctetStream
inMessage:syncGroupsMessage
thread:nil
success:^{
DDLogInfo(@"%@ Successfully sent Groups response syncMessage.", self.tag);
}
failure:^{
DDLogError(@"%@ Failed to send Groups response syncMessage.", self.tag);
}];
}
2016-09-01 16:28:35 +02:00
} else if (syncMessage.read.count > 0) {
DDLogInfo(@"%@ Received %ld read receipt(s)", self.tag, (u_long)syncMessage.read.count);
2016-09-01 16:28:35 +02:00
OWSReadReceiptsProcessor *readReceiptsProcessor =
[[OWSReadReceiptsProcessor alloc] initWithReadReceiptProtos:syncMessage.read
storageManager:self.storageManager];
2016-09-01 16:28:35 +02:00
[readReceiptsProcessor process];
} else {
DDLogWarn(@"%@ Ignoring unsupported sync message.", self.tag);
}
}
- (void)handleEndSessionMessageWithEnvelope:(OWSSignalServiceProtosEnvelope *)endSessionEnvelope
dataMessage:(OWSSignalServiceProtosDataMessage *)dataMessage
{
2015-12-07 03:31:43 +01:00
[self.dbConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) {
TSContactThread *thread =
[TSContactThread getOrCreateThreadWithContactId:endSessionEnvelope.source transaction:transaction];
uint64_t timeStamp = endSessionEnvelope.timestamp;
if (thread) { // TODO thread should always be nonnull.
[[[TSInfoMessage alloc] initWithTimestamp:timeStamp
inThread:thread
messageType:TSInfoMessageTypeSessionDidEnd] saveWithTransaction:transaction];
}
2015-12-07 03:31:43 +01:00
}];
[[TSStorageManager sharedManager] deleteAllSessionsForContact:endSessionEnvelope.source];
2015-12-07 03:31:43 +01:00
}
- (void)handleExpirationTimerUpdateMessageWithEnvelope:(OWSSignalServiceProtosEnvelope *)envelope
dataMessage:(OWSSignalServiceProtosDataMessage *)dataMessage
{
TSThread *thread = [self threadForEnvelope:envelope dataMessage:dataMessage];
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];
}
[disappearingMessagesConfiguration save];
NSString *name = [self.contactsManager nameStringForPhoneIdentifier:envelope.source];
OWSDisappearingConfigurationUpdateInfoMessage *message =
[[OWSDisappearingConfigurationUpdateInfoMessage alloc] initWithTimestamp:envelope.timestamp
thread:thread
configuration:disappearingMessagesConfiguration
createdByRemoteName:name];
[message save];
2015-12-07 03:31:43 +01:00
}
- (void)handleReceivedTextMessageWithEnvelope:(OWSSignalServiceProtosEnvelope *)textMessageEnvelope
dataMessage:(OWSSignalServiceProtosDataMessage *)dataMessage
{
[self handleReceivedEnvelope:textMessageEnvelope withDataMessage:dataMessage attachmentIds:@[]];
2015-12-07 03:31:43 +01:00
}
- (TSIncomingMessage *)handleReceivedEnvelope:(OWSSignalServiceProtosEnvelope *)envelope
withDataMessage:(OWSSignalServiceProtosDataMessage *)dataMessage
attachmentIds:(NSArray<NSString *> *)attachmentIds
{
uint64_t timestamp = envelope.timestamp;
NSString *body = dataMessage.body;
NSData *groupId = dataMessage.hasGroup ? dataMessage.group.id : nil;
2015-12-07 03:31:43 +01:00
__block TSIncomingMessage *_Nullable incomingMessage;
__block TSThread *thread;
2015-12-07 03:31:43 +01:00
[self.dbConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) {
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];
2015-12-07 03:31:43 +01:00
TSGroupThread *gThread = [TSGroupThread getOrCreateThreadWithGroupModel:model transaction:transaction];
[gThread saveWithTransaction:transaction];
if (dataMessage.group.type == OWSSignalServiceProtosGroupContextTypeUpdate) {
if ([attachmentIds count] == 1) {
NSString *avatarId = attachmentIds[0];
2015-12-07 03:31:43 +01:00
TSAttachment *avatar = [TSAttachment fetchObjectWithUniqueID:avatarId];
if ([avatar isKindOfClass:[TSAttachmentStream class]]) {
TSAttachmentStream *stream = (TSAttachmentStream *)avatar;
if ([stream isImage]) {
model.groupImage = [stream image];
// No need to keep the attachment around after assigning the image.
[stream removeWithTransaction:transaction];
2015-12-07 03:31:43 +01:00
}
}
}
NSString *updateGroupInfo = [gThread.groupModel getInfoStringAboutUpdateTo:model contactsManager:self.contactsManager];
2015-12-07 03:31:43 +01:00
gThread.groupModel = model;
[gThread saveWithTransaction:transaction];
[[[TSInfoMessage alloc] initWithTimestamp:timestamp
2015-12-07 03:31:43 +01:00
inThread:gThread
messageType:TSInfoMessageTypeGroupUpdate
customMessage:updateGroupInfo] saveWithTransaction:transaction];
} else if (dataMessage.group.type == OWSSignalServiceProtosGroupContextTypeQuit) {
NSString *nameString = [self.contactsManager nameStringForPhoneIdentifier:envelope.source];
2015-12-07 03:31:43 +01:00
NSString *updateGroupInfo =
[NSString stringWithFormat:NSLocalizedString(@"GROUP_MEMBER_LEFT", @""), nameString];
NSMutableArray *newGroupMembers = [NSMutableArray arrayWithArray:gThread.groupModel.groupMemberIds];
[newGroupMembers removeObject:envelope.source];
2015-12-07 03:31:43 +01:00
gThread.groupModel.groupMemberIds = newGroupMembers;
[gThread saveWithTransaction:transaction];
[[[TSInfoMessage alloc] initWithTimestamp:timestamp
2015-12-07 03:31:43 +01:00
inThread:gThread
messageType:TSInfoMessageTypeGroupUpdate
customMessage:updateGroupInfo] saveWithTransaction:transaction];
} else {
incomingMessage = [[TSIncomingMessage alloc] initWithTimestamp:timestamp
2015-12-07 03:31:43 +01:00
inThread:gThread
authorId:envelope.source
2015-12-07 03:31:43 +01:00
messageBody:body
attachmentIds:attachmentIds
expiresInSeconds:dataMessage.expireTimer];
2015-12-07 03:31:43 +01:00
[incomingMessage saveWithTransaction:transaction];
}
thread = gThread;
} else {
TSContactThread *cThread = [TSContactThread getOrCreateThreadWithContactId:envelope.source
2015-12-07 03:31:43 +01:00
transaction:transaction
relay:envelope.relay];
2015-12-07 03:31:43 +01:00
incomingMessage = [[TSIncomingMessage alloc] initWithTimestamp:timestamp
2015-12-07 03:31:43 +01:00
inThread:cThread
authorId:[cThread contactIdentifier]
2015-12-07 03:31:43 +01:00
messageBody:body
attachmentIds:attachmentIds
expiresInSeconds:dataMessage.expireTimer];
2015-12-07 03:31:43 +01:00
thread = cThread;
}
if (thread && incomingMessage) {
[incomingMessage saveWithTransaction:transaction];
// Android allows attachments to be sent with body.
if ([attachmentIds count] > 0 && body != nil && ![body isEqualToString:@""]) {
// We want the text to be displayed under the attachment
uint64_t textMessageTimestamp = timestamp + 1;
TSIncomingMessage *textMessage;
2015-12-07 03:31:43 +01:00
if ([thread isGroupThread]) {
TSGroupThread *gThread = (TSGroupThread *)thread;
textMessage = [[TSIncomingMessage alloc] initWithTimestamp:textMessageTimestamp
inThread:gThread
authorId:envelope.source
messageBody:body];
2015-12-07 03:31:43 +01:00
} else {
TSContactThread *cThread = (TSContactThread *)thread;
textMessage = [[TSIncomingMessage alloc] initWithTimestamp:textMessageTimestamp
inThread:cThread
authorId:[cThread contactIdentifier]
messageBody:body];
2015-12-07 03:31:43 +01:00
}
textMessage.expiresInSeconds = dataMessage.expireTimer;
[textMessage saveWithTransaction:transaction];
2015-12-07 03:31:43 +01:00
}
}
}];
2015-12-07 03:31:43 +01:00
if (incomingMessage && thread) {
// In case we already have a read receipt for this new message (happens sometimes).
OWSReadReceiptsProcessor *readReceiptsProcessor =
[[OWSReadReceiptsProcessor alloc] initWithIncomingMessage:incomingMessage
storageManager:self.storageManager];
[readReceiptsProcessor process];
[self becomeConsistentWithDisappearingConfigurationForMessage:incomingMessage];
// Update thread preview in inbox
[thread touch];
// TODO Delay notification by 100ms?
// It's pretty annoying when you're phone keeps buzzing while you're having a conversation on Desktop.
NSString *name = [thread name];
[[TextSecureKitEnv sharedEnv].notificationsManager notifyUserForIncomingMessage:incomingMessage
from:name
inThread:thread];
}
return incomingMessage;
2015-12-07 03:31:43 +01:00
}
- (void)becomeConsistentWithDisappearingConfigurationForMessage:(TSMessage *)message
{
// Become eventually consistent in the case that the remote changed their settings at the same time.
// Also in case remote doesn't support expiring messages
OWSDisappearingMessagesConfiguration *disappearingMessagesConfiguration =
[OWSDisappearingMessagesConfiguration fetchObjectWithUniqueID:message.uniqueThreadId];
BOOL changed = NO;
if (message.expiresInSeconds == 0) {
if (disappearingMessagesConfiguration.isEnabled) {
changed = YES;
DDLogWarn(@"%@ Received remote message which had no expiration set, disabling our expiration to become "
@"consistent.",
self.tag);
disappearingMessagesConfiguration.enabled = NO;
[disappearingMessagesConfiguration save];
}
} else if (message.expiresInSeconds != disappearingMessagesConfiguration.durationSeconds) {
changed = YES;
DDLogInfo(
@"%@ Received remote message with different expiration set, updating our expiration to become consistent.",
self.tag);
disappearingMessagesConfiguration.enabled = YES;
disappearingMessagesConfiguration.durationSeconds = message.expiresInSeconds;
[disappearingMessagesConfiguration save];
}
if (!changed) {
return;
}
if ([message isKindOfClass:[TSIncomingMessage class]]) {
TSIncomingMessage *incomingMessage = (TSIncomingMessage *)message;
NSString *contactName = [self.contactsManager nameStringForPhoneIdentifier:incomingMessage.authorId];
[[[OWSDisappearingConfigurationUpdateInfoMessage alloc] initWithTimestamp:message.timestamp
thread:message.thread
configuration:disappearingMessagesConfiguration
createdByRemoteName:contactName] save];
} else {
[[[OWSDisappearingConfigurationUpdateInfoMessage alloc] initWithTimestamp:message.timestamp
thread:message.thread
configuration:disappearingMessagesConfiguration]
save];
}
}
- (void)processException:(NSException *)exception envelope:(OWSSignalServiceProtosEnvelope *)envelope
{
DDLogError(@"%@ Got exception: %@ of type: %@", self.tag, exception.description, exception.name);
2015-12-07 03:31:43 +01:00
[self.dbConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) {
TSErrorMessage *errorMessage;
if ([exception.name isEqualToString:NoSessionException]) {
errorMessage = [TSErrorMessage missingSessionWithEnvelope:envelope withTransaction:transaction];
2015-12-07 03:31:43 +01:00
} else if ([exception.name isEqualToString:InvalidKeyException]) {
errorMessage = [TSErrorMessage invalidKeyExceptionWithEnvelope:envelope withTransaction:transaction];
2015-12-07 03:31:43 +01:00
} else if ([exception.name isEqualToString:InvalidKeyIdException]) {
errorMessage = [TSErrorMessage invalidKeyExceptionWithEnvelope:envelope withTransaction:transaction];
2015-12-07 03:31:43 +01:00
} else if ([exception.name isEqualToString:DuplicateMessageException]) {
// Duplicate messages are dismissed
return;
} else if ([exception.name isEqualToString:InvalidVersionException]) {
errorMessage = [TSErrorMessage invalidVersionWithEnvelope:envelope withTransaction:transaction];
2015-12-07 03:31:43 +01:00
} else if ([exception.name isEqualToString:UntrustedIdentityKeyException]) {
errorMessage =
[TSInvalidIdentityKeyReceivingErrorMessage untrustedKeyWithEnvelope:envelope withTransaction:transaction];
2015-12-07 03:31:43 +01:00
} else {
errorMessage = [TSErrorMessage corruptedMessageWithEnvelope:envelope withTransaction:transaction];
2015-12-07 03:31:43 +01:00
}
[errorMessage saveWithTransaction:transaction];
}];
}
- (void)processException:(NSException *)exception
outgoingMessage:(TSOutgoingMessage *)message
inThread:(TSThread *)thread
{
DDLogWarn(@"%@ Got exception: %@", self.tag, exception);
2015-12-07 03:31:43 +01:00
[self.dbConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) {
TSErrorMessage *errorMessage;
if ([exception.name isEqualToString:UntrustedIdentityKeyException]) {
errorMessage = [TSInvalidIdentityKeySendingErrorMessage
untrustedKeyWithOutgoingMessage:message
inThread:thread
forRecipient:exception.userInfo[TSInvalidRecipientKey]
preKeyBundle:exception.userInfo[TSInvalidPreKeyBundleKey]
withTransaction:transaction];
message.messageState = TSOutgoingMessageStateUnsent;
[message saveWithTransaction:transaction];
} else if (message.groupMetaMessage == TSGroupMessageNone) {
// Only update this with exception if it is not a group message as group
// messages may except for one group
// send but not another and the UI doesn't know how to handle that
[message setMessageState:TSOutgoingMessageStateUnsent];
[message saveWithTransaction:transaction];
}
2015-12-07 03:31:43 +01:00
[errorMessage saveWithTransaction:transaction];
2015-12-07 03:31:43 +01:00
}];
}
- (TSThread *)threadForEnvelope:(OWSSignalServiceProtosEnvelope *)envelope
dataMessage:(OWSSignalServiceProtosDataMessage *)dataMessage
{
if (dataMessage.hasGroup) {
return [TSGroupThread getOrCreateThreadWithGroupIdData:dataMessage.group.id];
} else {
return [TSContactThread getOrCreateThreadWithContactId:envelope.source];
}
}
2015-12-07 03:31:43 +01:00
- (NSUInteger)unreadMessagesCount {
__block NSUInteger numberOfItems;
[self.dbConnection readWithBlock:^(YapDatabaseReadTransaction *transaction) {
numberOfItems = [[transaction ext:TSUnreadDatabaseViewExtensionName] numberOfItemsInAllGroups];
}];
return numberOfItems;
}
- (NSUInteger)unreadMessagesCountExcept:(TSThread *)thread {
__block NSUInteger numberOfItems;
[self.dbConnection readWithBlock:^(YapDatabaseReadTransaction *transaction) {
numberOfItems = [[transaction ext:TSUnreadDatabaseViewExtensionName] numberOfItemsInAllGroups];
numberOfItems =
numberOfItems - [[transaction ext:TSUnreadDatabaseViewExtensionName] numberOfItemsInGroup:thread.uniqueId];
}];
return numberOfItems;
}
- (NSUInteger)unreadMessagesInThread:(TSThread *)thread {
__block NSUInteger numberOfItems;
[self.dbConnection readWithBlock:^(YapDatabaseReadTransaction *transaction) {
numberOfItems = [[transaction ext:TSUnreadDatabaseViewExtensionName] numberOfItemsInGroup:thread.uniqueId];
}];
return numberOfItems;
}
#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
NS_ASSUME_NONNULL_END