2017-02-15 23:09:55 +01:00
|
|
|
//
|
|
|
|
// Copyright (c) 2017 Open Whisper Systems. All rights reserved.
|
|
|
|
//
|
2015-12-07 03:31:43 +01:00
|
|
|
|
|
|
|
#import "TSOutgoingMessage.h"
|
2016-10-05 17:42:44 +02:00
|
|
|
#import "NSDate+millisecondTimeStamp.h"
|
2016-08-23 22:38:05 +02:00
|
|
|
#import "OWSSignalServiceProtos.pb.h"
|
|
|
|
#import "TSAttachmentStream.h"
|
|
|
|
#import "TSContactThread.h"
|
2016-07-28 01:58:49 +02:00
|
|
|
#import "TSGroupThread.h"
|
2015-12-07 03:31:43 +01:00
|
|
|
|
2016-08-23 22:38:05 +02:00
|
|
|
NS_ASSUME_NONNULL_BEGIN
|
|
|
|
|
2015-12-07 03:31:43 +01:00
|
|
|
@implementation TSOutgoingMessage
|
|
|
|
|
2016-10-07 01:42:52 +02:00
|
|
|
- (instancetype)initWithCoder:(NSCoder *)coder
|
|
|
|
{
|
2017-04-13 18:54:03 +02:00
|
|
|
self = [super initWithCoder:coder];
|
|
|
|
if (self) {
|
|
|
|
if (!_attachmentFilenameMap) {
|
|
|
|
_attachmentFilenameMap = [NSMutableDictionary new];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return self;
|
2016-10-07 01:42:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
- (instancetype)initWithTimestamp:(uint64_t)timestamp
|
|
|
|
{
|
|
|
|
return [self initWithTimestamp:timestamp inThread:nil];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (instancetype)initWithTimestamp:(uint64_t)timestamp inThread:(nullable TSThread *)thread
|
|
|
|
{
|
2016-10-09 20:12:49 +02:00
|
|
|
return [self initWithTimestamp:timestamp inThread:thread messageBody:nil];
|
2016-10-07 01:42:52 +02:00
|
|
|
}
|
|
|
|
|
2015-12-07 03:31:43 +01:00
|
|
|
- (instancetype)initWithTimestamp:(uint64_t)timestamp
|
2016-08-29 00:48:35 +02:00
|
|
|
inThread:(nullable TSThread *)thread
|
|
|
|
messageBody:(nullable NSString *)body
|
|
|
|
{
|
2016-10-07 01:42:52 +02:00
|
|
|
return [self initWithTimestamp:timestamp inThread:thread messageBody:body attachmentIds:[NSMutableArray new]];
|
2016-08-29 00:48:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
- (instancetype)initWithTimestamp:(uint64_t)timestamp
|
|
|
|
inThread:(nullable TSThread *)thread
|
|
|
|
messageBody:(nullable NSString *)body
|
2016-07-29 21:33:25 +02:00
|
|
|
attachmentIds:(NSMutableArray<NSString *> *)attachmentIds
|
|
|
|
{
|
2016-10-07 01:42:52 +02:00
|
|
|
return [self initWithTimestamp:timestamp
|
|
|
|
inThread:thread
|
|
|
|
messageBody:body
|
2016-10-07 21:36:01 +02:00
|
|
|
attachmentIds:attachmentIds
|
2016-10-07 01:42:52 +02:00
|
|
|
expiresInSeconds:0];
|
2016-10-05 17:42:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
- (instancetype)initWithTimestamp:(uint64_t)timestamp
|
|
|
|
inThread:(nullable TSThread *)thread
|
|
|
|
messageBody:(nullable NSString *)body
|
|
|
|
attachmentIds:(NSMutableArray<NSString *> *)attachmentIds
|
|
|
|
expiresInSeconds:(uint32_t)expiresInSeconds
|
|
|
|
{
|
2016-10-07 01:42:52 +02:00
|
|
|
return [self initWithTimestamp:timestamp
|
|
|
|
inThread:thread
|
|
|
|
messageBody:body
|
|
|
|
attachmentIds:attachmentIds
|
|
|
|
expiresInSeconds:expiresInSeconds
|
2016-10-08 01:17:38 +02:00
|
|
|
expireStartedAt:0];
|
2016-10-07 01:42:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
- (instancetype)initWithTimestamp:(uint64_t)timestamp
|
|
|
|
inThread:(nullable TSThread *)thread
|
|
|
|
messageBody:(nullable NSString *)body
|
|
|
|
attachmentIds:(NSMutableArray<NSString *> *)attachmentIds
|
|
|
|
expiresInSeconds:(uint32_t)expiresInSeconds
|
|
|
|
expireStartedAt:(uint64_t)expireStartedAt
|
|
|
|
{
|
2016-10-05 17:42:44 +02:00
|
|
|
self = [super initWithTimestamp:timestamp
|
|
|
|
inThread:thread
|
|
|
|
messageBody:body
|
|
|
|
attachmentIds:attachmentIds
|
|
|
|
expiresInSeconds:expiresInSeconds
|
2016-10-07 01:42:52 +02:00
|
|
|
expireStartedAt:expireStartedAt];
|
2016-07-29 21:33:25 +02:00
|
|
|
if (!self) {
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
_messageState = TSOutgoingMessageStateAttemptingOut;
|
2016-08-23 22:38:05 +02:00
|
|
|
_hasSyncedTranscript = NO;
|
|
|
|
|
2016-07-29 21:33:25 +02:00
|
|
|
if ([thread isKindOfClass:[TSGroupThread class]]) {
|
|
|
|
self.groupMetaMessage = TSGroupMessageDeliver;
|
|
|
|
} else {
|
|
|
|
self.groupMetaMessage = TSGroupMessageNone;
|
2015-12-07 03:31:43 +01:00
|
|
|
}
|
2017-04-13 18:54:03 +02:00
|
|
|
_attachmentFilenameMap = [NSMutableDictionary new];
|
2015-12-07 03:31:43 +01:00
|
|
|
|
2017-02-15 23:09:55 +01:00
|
|
|
OWSAssert(self.receivedAtDate);
|
|
|
|
|
2015-12-07 03:31:43 +01:00
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2016-10-14 23:00:29 +02:00
|
|
|
- (void)saveWithTransaction:(YapDatabaseReadWriteTransaction *)transaction
|
|
|
|
{
|
|
|
|
if (!(self.groupMetaMessage == TSGroupMessageDeliver || self.groupMetaMessage == TSGroupMessageNone)) {
|
|
|
|
DDLogDebug(@"%@ Skipping save for group meta message.", self.tag);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
[super saveWithTransaction:transaction];
|
|
|
|
}
|
|
|
|
|
2016-08-23 22:38:05 +02:00
|
|
|
- (nullable NSString *)recipientIdentifier
|
|
|
|
{
|
|
|
|
return self.thread.contactIdentifier;
|
|
|
|
}
|
|
|
|
|
2016-10-08 01:17:38 +02:00
|
|
|
- (BOOL)shouldStartExpireTimer
|
|
|
|
{
|
|
|
|
switch (self.messageState) {
|
|
|
|
case TSOutgoingMessageStateSent:
|
|
|
|
case TSOutgoingMessageStateDelivered:
|
|
|
|
return self.isExpiringMessage;
|
|
|
|
case TSOutgoingMessageStateAttemptingOut:
|
|
|
|
case TSOutgoingMessageStateUnsent:
|
|
|
|
return NO;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-14 23:00:29 +02:00
|
|
|
- (void)setSendingError:(NSError *)error
|
|
|
|
{
|
|
|
|
_mostRecentFailureText = error.localizedDescription;
|
|
|
|
}
|
|
|
|
|
2016-10-05 17:42:44 +02:00
|
|
|
- (OWSSignalServiceProtosDataMessageBuilder *)dataMessageBuilder
|
2016-08-23 22:38:05 +02:00
|
|
|
{
|
|
|
|
TSThread *thread = self.thread;
|
|
|
|
OWSSignalServiceProtosDataMessageBuilder *builder = [OWSSignalServiceProtosDataMessageBuilder new];
|
|
|
|
[builder setBody:self.body];
|
2016-08-29 00:31:27 +02:00
|
|
|
BOOL attachmentWasGroupAvatar = NO;
|
2016-08-23 22:38:05 +02:00
|
|
|
if ([thread isKindOfClass:[TSGroupThread class]]) {
|
|
|
|
TSGroupThread *gThread = (TSGroupThread *)thread;
|
|
|
|
OWSSignalServiceProtosGroupContextBuilder *groupBuilder = [OWSSignalServiceProtosGroupContextBuilder new];
|
|
|
|
|
|
|
|
switch (self.groupMetaMessage) {
|
|
|
|
case TSGroupMessageQuit:
|
|
|
|
[groupBuilder setType:OWSSignalServiceProtosGroupContextTypeQuit];
|
|
|
|
break;
|
|
|
|
case TSGroupMessageUpdate:
|
|
|
|
case TSGroupMessageNew: {
|
2016-08-29 00:31:27 +02:00
|
|
|
if (gThread.groupModel.groupImage != nil && self.attachmentIds.count == 1) {
|
|
|
|
attachmentWasGroupAvatar = YES;
|
2017-04-13 18:54:03 +02:00
|
|
|
[groupBuilder
|
|
|
|
setAvatar:[self buildAttachmentProtoForAttachmentId:self.attachmentIds[0] filename:nil]];
|
2016-08-23 22:38:05 +02:00
|
|
|
}
|
2016-08-29 00:31:27 +02:00
|
|
|
|
2016-08-23 22:38:05 +02:00
|
|
|
[groupBuilder setMembersArray:gThread.groupModel.groupMemberIds];
|
|
|
|
[groupBuilder setName:gThread.groupModel.groupName];
|
|
|
|
[groupBuilder setType:OWSSignalServiceProtosGroupContextTypeUpdate];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
[groupBuilder setType:OWSSignalServiceProtosGroupContextTypeDeliver];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
[groupBuilder setId:gThread.groupModel.groupId];
|
|
|
|
[builder setGroup:groupBuilder.build];
|
|
|
|
}
|
2016-08-29 00:31:27 +02:00
|
|
|
if (!attachmentWasGroupAvatar) {
|
2016-08-23 22:38:05 +02:00
|
|
|
NSMutableArray *attachments = [NSMutableArray new];
|
|
|
|
for (NSString *attachmentId in self.attachmentIds) {
|
2017-04-13 18:54:03 +02:00
|
|
|
NSString *filename = self.attachmentFilenameMap[attachmentId];
|
|
|
|
[attachments addObject:[self buildAttachmentProtoForAttachmentId:attachmentId filename:filename]];
|
2016-08-23 22:38:05 +02:00
|
|
|
}
|
|
|
|
[builder setAttachmentsArray:attachments];
|
|
|
|
}
|
2016-10-05 17:42:44 +02:00
|
|
|
[builder setExpireTimer:self.expiresInSeconds];
|
|
|
|
return builder;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (OWSSignalServiceProtosDataMessage *)buildDataMessage
|
|
|
|
{
|
|
|
|
return [[self dataMessageBuilder] build];
|
2016-08-23 22:38:05 +02:00
|
|
|
}
|
|
|
|
|
2016-12-18 22:08:26 +01:00
|
|
|
// For legacy message this is a serialized DataMessage.
|
|
|
|
// For modern messages, e.g. Sync and Call messages, this is a serialized Contact
|
2016-08-23 22:38:05 +02:00
|
|
|
- (NSData *)buildPlainTextData
|
|
|
|
{
|
|
|
|
return [[self buildDataMessage] data];
|
|
|
|
}
|
|
|
|
|
2016-12-18 22:08:26 +01:00
|
|
|
- (BOOL)isLegacyMessage
|
|
|
|
{
|
|
|
|
return YES;
|
|
|
|
}
|
|
|
|
|
2016-08-23 22:38:05 +02:00
|
|
|
- (BOOL)shouldSyncTranscript
|
|
|
|
{
|
|
|
|
return !self.hasSyncedTranscript;
|
|
|
|
}
|
|
|
|
|
2016-09-03 01:44:51 +02:00
|
|
|
- (OWSSignalServiceProtosAttachmentPointer *)buildAttachmentProtoForAttachmentId:(NSString *)attachmentId
|
2017-04-13 18:54:03 +02:00
|
|
|
filename:(nullable NSString *)filename
|
2016-08-29 00:31:27 +02:00
|
|
|
{
|
2017-04-13 18:54:03 +02:00
|
|
|
OWSAssert(attachmentId.length > 0);
|
|
|
|
|
2016-08-29 00:31:27 +02:00
|
|
|
TSAttachment *attachment = [TSAttachmentStream fetchObjectWithUniqueID:attachmentId];
|
|
|
|
if (![attachment isKindOfClass:[TSAttachmentStream class]]) {
|
|
|
|
DDLogError(@"Unexpected type for attachment builder: %@", attachment);
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
TSAttachmentStream *attachmentStream = (TSAttachmentStream *)attachment;
|
|
|
|
|
|
|
|
OWSSignalServiceProtosAttachmentPointerBuilder *builder = [OWSSignalServiceProtosAttachmentPointerBuilder new];
|
2016-10-14 23:00:29 +02:00
|
|
|
[builder setId:attachmentStream.serverId];
|
2016-08-29 00:31:27 +02:00
|
|
|
[builder setContentType:attachmentStream.contentType];
|
2017-04-13 18:54:03 +02:00
|
|
|
[builder setFileName:filename];
|
2016-08-29 00:31:27 +02:00
|
|
|
[builder setKey:attachmentStream.encryptionKey];
|
2017-03-10 18:10:52 +01:00
|
|
|
[builder setDigest:attachmentStream.digest];
|
2016-08-29 00:31:27 +02:00
|
|
|
|
2016-09-03 01:44:51 +02:00
|
|
|
return [builder build];
|
2016-08-29 00:31:27 +02:00
|
|
|
}
|
|
|
|
|
2016-10-14 23:00:29 +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-08-23 22:38:05 +02:00
|
|
|
|
|
|
|
NS_ASSUME_NONNULL_END
|