fixup plumbing for incoming messages/synced transcripts

This commit is contained in:
Michael Kirk 2018-11-07 16:42:40 -06:00
parent e096406e56
commit 42bf267607
10 changed files with 83 additions and 136 deletions

View File

@ -1647,16 +1647,18 @@ typedef enum : NSUInteger {
attachmentPointer:(TSAttachmentPointer *)attachmentPointer
{
OWSAttachmentsProcessor *processor =
[[OWSAttachmentsProcessor alloc] initWithAttachmentPointer:attachmentPointer
networkManager:self.networkManager];
[processor fetchAttachmentsForMessage:message
primaryStorage:self.primaryStorage
success:^(NSArray<TSAttachmentStream *> *attachmentStreams) {
OWSLogInfo(@"Successfully redownloaded attachment in thread: %@", message.thread);
}
failure:^(NSError *error) {
OWSLogWarn(@"Failed to redownload message with error: %@", error);
}];
[[OWSAttachmentsProcessor alloc] initWithAttachmentPointers:@[ attachmentPointer ]];
[self.editingDatabaseConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *_Nonnull transaction) {
[processor fetchAttachmentsForMessage:message
transaction:transaction
success:^(NSArray<TSAttachmentStream *> *attachmentStreams) {
OWSLogInfo(@"Successfully redownloaded attachment in thread: %@", message.thread);
}
failure:^(NSError *error) {
OWSLogWarn(@"Failed to redownload message with error: %@", error);
}];
}];
}
- (void)handleUnsentMessageTap:(TSOutgoingMessage *)message
@ -2191,8 +2193,7 @@ typedef enum : NSUInteger {
}
OWSAttachmentsProcessor *processor =
[[OWSAttachmentsProcessor alloc] initWithAttachmentPointer:attachmentPointer
networkManager:self.networkManager];
[[OWSAttachmentsProcessor alloc] initWithAttachmentPointers:@[ attachmentPointer ]];
[self.editingDatabaseConnection asyncReadWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) {
[processor fetchAttachmentsForMessage:nil

View File

@ -241,7 +241,7 @@ public class SignalAttachment: NSObject {
@objc
public func buildOutgoingAttachmentInfo(message: TSMessage) -> OutgoingAttachmentInfo {
OWSAssertDebug(message.uniqueId)
assert(message.uniqueId != nil)
return OutgoingAttachmentInfo(dataSource: dataSource,
contentType: mimeType,
sourceFilename: filenameOrDefault,

View File

@ -78,18 +78,12 @@ NS_ASSUME_NONNULL_BEGIN
return;
}
OWSAttachmentsProcessor *attachmentsProcessor =
[[OWSAttachmentsProcessor alloc] initWithAttachmentProtos:transcript.attachmentPointerProtos
networkManager:self.networkManager
transaction:transaction];
// TODO group updates. Currently desktop doesn't support group updates, so not a problem yet.
TSOutgoingMessage *outgoingMessage =
[[TSOutgoingMessage alloc] initOutgoingMessageWithTimestamp:transcript.timestamp
inThread:transcript.thread
messageBody:transcript.body
attachmentIds:[attachmentsProcessor.attachmentIds mutableCopy]
attachmentIds:[NSMutableArray new]
expiresInSeconds:transcript.expirationDuration
expireStartedAt:transcript.expirationStartedAt
isVoiceMessage:NO
@ -97,6 +91,14 @@ NS_ASSUME_NONNULL_BEGIN
quotedMessage:transcript.quotedMessage
contactShare:transcript.contact];
NSArray<TSAttachmentPointer *> *attachmentPointers =
[TSAttachmentPointer attachmentPointersFromProtos:transcript.attachmentPointerProtos
albumMessage:outgoingMessage];
for (TSAttachmentPointer *pointer in attachmentPointers) {
[pointer saveWithTransaction:transaction];
[outgoingMessage.attachmentIds addObject:pointer.uniqueId];
}
TSQuotedMessage *_Nullable quotedMessage = transcript.quotedMessage;
if (quotedMessage && quotedMessage.thumbnailAttachmentPointerId) {
// We weren't able to derive a local thumbnail, so we'll fetch the referenced attachment.
@ -106,8 +108,7 @@ NS_ASSUME_NONNULL_BEGIN
if ([attachmentPointer isKindOfClass:[TSAttachmentPointer class]]) {
OWSAttachmentsProcessor *attachmentProcessor =
[[OWSAttachmentsProcessor alloc] initWithAttachmentPointer:attachmentPointer
networkManager:self.networkManager];
[[OWSAttachmentsProcessor alloc] initWithAttachmentPointers:@[ attachmentPointer ]];
OWSLogDebug(@"downloading thumbnail for transcript: %lu", (unsigned long)transcript.timestamp);
[attachmentProcessor fetchAttachmentsForMessage:outgoingMessage
@ -160,6 +161,8 @@ NS_ASSUME_NONNULL_BEGIN
[self.readReceiptManager applyEarlyReadReceiptsForOutgoingMessageFromLinkedDevice:outgoingMessage
transaction:transaction];
OWSAttachmentsProcessor *attachmentsProcessor =
[[OWSAttachmentsProcessor alloc] initWithAttachmentPointers:attachmentPointers];
[attachmentsProcessor
fetchAttachmentsForMessage:outgoingMessage
transaction:transaction

View File

@ -23,26 +23,16 @@ extern NSString *const kAttachmentDownloadAttachmentIDKey;
*/
@interface OWSAttachmentsProcessor : NSObject
@property (nonatomic, readonly) NSArray<NSString *> *attachmentIds;
@property (nonatomic, readonly) NSArray<TSAttachmentPointer *> *attachmentPointers;
@property (nonatomic, readonly) BOOL hasSupportedAttachments;
- (instancetype)init NS_UNAVAILABLE;
- (instancetype)initWithAttachmentProtos:(NSArray<SSKProtoAttachmentPointer *> *)attachmentProtos
networkManager:(TSNetworkManager *)networkManager
transaction:(YapDatabaseReadWriteTransaction *)transaction NS_DESIGNATED_INITIALIZER;
/*
* Retry fetching failed attachment download
*/
- (instancetype)initWithAttachmentPointer:(TSAttachmentPointer *)attachmentPointer
networkManager:(TSNetworkManager *)networkManager NS_DESIGNATED_INITIALIZER;
- (instancetype)initWithAttachmentPointers:(NSArray<TSAttachmentPointer *> *)attachmentPointers
NS_DESIGNATED_INITIALIZER;
- (void)fetchAttachmentsForMessage:(nullable TSMessage *)message
primaryStorage:(OWSPrimaryStorage *)primaryStorage
success:(void (^)(NSArray<TSAttachmentStream *> *attachmentStreams))successHandler
failure:(void (^)(NSError *error))failureHandler;
- (void)fetchAttachmentsForMessage:(nullable TSMessage *)message
transaction:(YapDatabaseReadWriteTransaction *)transaction
success:(void (^)(NSArray<TSAttachmentStream *> *attachmentStreams))successHandler

View File

@ -12,6 +12,7 @@
#import "OWSFileSystem.h"
#import "OWSPrimaryStorage.h"
#import "OWSRequestFactory.h"
#import "SSKEnvironment.h"
#import "TSAttachmentPointer.h"
#import "TSAttachmentStream.h"
#import "TSGroupModel.h"
@ -43,67 +44,26 @@ static const CGFloat kAttachmentDownloadProgressTheta = 0.001f;
@implementation OWSAttachmentsProcessor
- (instancetype)initWithAttachmentPointer:(TSAttachmentPointer *)attachmentPointer
networkManager:(TSNetworkManager *)networkManager
- (instancetype)initWithAttachmentPointers:(NSArray<TSAttachmentPointer *> *)attachmentPointers
{
self = [super init];
if (!self) {
return self;
}
_networkManager = networkManager;
_attachmentPointers = @[ attachmentPointer ];
_attachmentIds = @[ attachmentPointer.uniqueId ];
_attachmentPointers = attachmentPointers;
return self;
}
- (instancetype)initWithAttachmentProtos:(NSArray<SSKProtoAttachmentPointer *> *)attachmentProtos
networkManager:(TSNetworkManager *)networkManager
transaction:(YapDatabaseReadWriteTransaction *)transaction
#pragma mark - Dependencies
- (TSNetworkManager *)networkManager
{
self = [super init];
if (!self) {
return self;
}
_networkManager = networkManager;
NSMutableArray<NSString *> *attachmentIds = [NSMutableArray new];
NSMutableArray<TSAttachmentPointer *> *attachmentPointers = [NSMutableArray new];
for (SSKProtoAttachmentPointer *attachmentProto in attachmentProtos) {
TSAttachmentPointer *_Nullable pointer = [TSAttachmentPointer attachmentPointerFromProto:attachmentProto];
if (!pointer) {
OWSFailDebug(@"Invalid attachment.");
continue;
}
[attachmentIds addObject:pointer.uniqueId];
[pointer saveWithTransaction:transaction];
[attachmentPointers addObject:pointer];
}
_attachmentIds = [attachmentIds copy];
_attachmentPointers = [attachmentPointers copy];
return self;
return SSKEnvironment.shared.networkManager;
}
// PERF: Remove this and use a pre-existing dbConnection
- (void)fetchAttachmentsForMessage:(nullable TSMessage *)message
primaryStorage:(OWSPrimaryStorage *)primaryStorage
success:(void (^)(NSArray<TSAttachmentStream *> *attachmentStreams))successHandler
failure:(void (^)(NSError *error))failureHandler
{
[[primaryStorage newDatabaseConnection] readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) {
[self fetchAttachmentsForMessage:message
transaction:transaction
success:successHandler
failure:failureHandler];
}];
}
#pragma mark
- (void)fetchAttachmentsForMessage:(nullable TSMessage *)message
transaction:(YapDatabaseReadWriteTransaction *)transaction
@ -516,11 +476,6 @@ static const CGFloat kAttachmentDownloadProgressTheta = 0.001f;
}
}
- (BOOL)hasSupportedAttachments
{
return self.attachmentPointers.count > 0;
}
@end
NS_ASSUME_NONNULL_END

View File

@ -7,6 +7,7 @@
NS_ASSUME_NONNULL_BEGIN
@class SSKProtoAttachmentPointer;
@class TSMessage;
typedef NS_ENUM(NSUInteger, TSAttachmentPointerState) {
TSAttachmentPointerStateEnqueued = 0,
@ -31,7 +32,12 @@ typedef NS_ENUM(NSUInteger, TSAttachmentPointerState) {
albumMessageId:(nullable NSString *)albumMessageId
attachmentType:(TSAttachmentType)attachmentType NS_DESIGNATED_INITIALIZER;
+ (nullable TSAttachmentPointer *)attachmentPointerFromProto:(SSKProtoAttachmentPointer *)attachmentProto;
+ (nullable TSAttachmentPointer *)attachmentPointerFromProto:(SSKProtoAttachmentPointer *)attachmentProto
albumMessage:(nullable TSMessage *)message;
+ (NSArray<TSAttachmentPointer *> *)attachmentPointersFromProtos:
(NSArray<SSKProtoAttachmentPointer *> *)attachmentProtos
albumMessage:(TSMessage *)message;
@property (atomic) TSAttachmentPointerState state;
@property (nullable, atomic) NSString *mostRecentFailureLocalizedText;

View File

@ -55,9 +55,8 @@ NS_ASSUME_NONNULL_BEGIN
return self;
}
+ (nullable TSAttachmentPointer *)attachmentPointerFromProto:(SSKProtoAttachmentPointer *)attachmentProto
message:(TSMessage *)message
albumMessage:(nullable TSMessage *)albumMessage
{
if (attachmentProto.id < 1) {
OWSFailDebug(@"Invalid attachment id.");
@ -86,10 +85,10 @@ NS_ASSUME_NONNULL_BEGIN
if (attachmentProto.hasCaption) {
caption = attachmentProto.caption;
}
NSString *_Nullable albumMessageId;
if ([MIMETypeUtil isVisualMedia:attachmentProto.contentType]) {
OWSAssertDebug(message.uniqueId);
albumMessageId = message.uniqueId;
if (albumMessage != nil) {
albumMessageId = albumMessage.uniqueId;
}
TSAttachmentPointer *pointer = [[TSAttachmentPointer alloc] initWithServerId:attachmentProto.id
@ -104,6 +103,21 @@ NS_ASSUME_NONNULL_BEGIN
return pointer;
}
+ (NSArray<TSAttachmentPointer *> *)attachmentPointersFromProtos:
(NSArray<SSKProtoAttachmentPointer *> *)attachmentProtos
albumMessage:(TSMessage *)albumMessage
{
NSMutableArray *attachmentPointers = [NSMutableArray new];
for (SSKProtoAttachmentPointer *attachmentProto in attachmentProtos) {
TSAttachmentPointer *_Nullable attachmentPointer =
[self attachmentPointerFromProto:attachmentProto albumMessage:albumMessage];
if (attachmentPointer) {
[attachmentPointers addObject:attachmentPointer];
}
}
return [attachmentPointers copy];
}
- (BOOL)isDecimalNumberText:(NSString *)text
{
return [text componentsSeparatedByCharactersInSet:[NSCharacterSet decimalDigitCharacterSet]].count == 1;

View File

@ -1000,7 +1000,7 @@ NSString *NSStringForContactAddressType(OWSContactAddressType value)
SSKProtoAttachmentPointer *avatarAttachment = avatarInfo.avatar;
TSAttachmentPointer *_Nullable attachmentPointer =
[TSAttachmentPointer attachmentPointerFromProto:avatarAttachment];
[TSAttachmentPointer attachmentPointerFromProto:avatarAttachment albumMessage:nil];
if (attachmentPointer) {
[attachmentPointer saveWithTransaction:transaction];
contact.avatarAttachmentId = attachmentPointer.uniqueId;

View File

@ -188,7 +188,7 @@ NS_ASSUME_NONNULL_BEGIN
SSKProtoAttachmentPointer *thumbnailAttachmentProto = quotedAttachment.thumbnail;
TSAttachmentPointer *_Nullable thumbnailPointer =
[TSAttachmentPointer attachmentPointerFromProto:thumbnailAttachmentProto];
[TSAttachmentPointer attachmentPointerFromProto:thumbnailAttachmentProto albumMessage:nil];
if (thumbnailPointer) {
[thumbnailPointer saveWithTransaction:transaction];

View File

@ -729,15 +729,11 @@ NS_ASSUME_NONNULL_BEGIN
return;
}
TSAttachmentPointer *avatarPointer =
[TSAttachmentPointer attachmentPointerFromProto:dataMessage.group.avatar albumMessage:nil];
OWSAttachmentsProcessor *attachmentsProcessor =
[[OWSAttachmentsProcessor alloc] initWithAttachmentProtos:@[ dataMessage.group.avatar ]
networkManager:self.networkManager
transaction:transaction];
[[OWSAttachmentsProcessor alloc] initWithAttachmentPointers:@[ avatarPointer ]];
if (!attachmentsProcessor.hasSupportedAttachments) {
OWSLogWarn(@"received unsupported group avatar envelope");
return;
}
[attachmentsProcessor fetchAttachmentsForMessage:nil
transaction:transaction
success:^(NSArray<TSAttachmentStream *> *attachmentStreams) {
@ -775,26 +771,26 @@ NS_ASSUME_NONNULL_BEGIN
return;
}
OWSAttachmentsProcessor *attachmentsProcessor =
[[OWSAttachmentsProcessor alloc] initWithAttachmentProtos:dataMessage.attachments
networkManager:self.networkManager
transaction:transaction];
if (!attachmentsProcessor.hasSupportedAttachments) {
OWSLogWarn(@"received unsupported media envelope");
return;
}
TSIncomingMessage *_Nullable createdMessage = [self handleReceivedEnvelope:envelope
withDataMessage:dataMessage
attachmentIds:attachmentsProcessor.attachmentIds
transaction:transaction];
if (!createdMessage) {
return;
}
NSArray<TSAttachmentPointer *> *attachmentPointers =
[TSAttachmentPointer attachmentPointersFromProtos:dataMessage.attachments albumMessage:createdMessage];
for (TSAttachmentPointer *pointer in attachmentPointers) {
[pointer saveWithTransaction:transaction];
[createdMessage.attachmentIds addObject:pointer.uniqueId];
}
[createdMessage saveWithTransaction:transaction];
OWSLogDebug(@"incoming attachment message: %@", createdMessage.debugDescription);
OWSAttachmentsProcessor *attachmentsProcessor =
[[OWSAttachmentsProcessor alloc] initWithAttachmentPointers:attachmentPointers];
[attachmentsProcessor fetchAttachmentsForMessage:createdMessage
transaction:transaction
success:^(NSArray<TSAttachmentStream *> *attachmentStreams) {
@ -1054,7 +1050,7 @@ NS_ASSUME_NONNULL_BEGIN
return;
}
[self handleReceivedEnvelope:envelope withDataMessage:dataMessage attachmentIds:@[] transaction:transaction];
[self handleReceivedEnvelope:envelope withDataMessage:dataMessage transaction:transaction];
}
- (void)handleGroupInfoRequest:(SSKProtoEnvelope *)envelope
@ -1137,7 +1133,6 @@ NS_ASSUME_NONNULL_BEGIN
- (TSIncomingMessage *_Nullable)handleReceivedEnvelope:(SSKProtoEnvelope *)envelope
withDataMessage:(SSKProtoDataMessage *)dataMessage
attachmentIds:(NSArray<NSString *> *)attachmentIds
transaction:(YapDatabaseReadWriteTransaction *)transaction
{
if (!envelope) {
@ -1247,14 +1242,6 @@ NS_ASSUME_NONNULL_BEGIN
return nil;
}
if (body.length == 0 && attachmentIds.count < 1 && !contact) {
OWSLogWarn(@"ignoring empty incoming message from: %@ for group: %@ with timestamp: %lu",
envelopeAddress(envelope),
groupId,
(unsigned long)timestamp);
return nil;
}
TSQuotedMessage *_Nullable quotedMessage = [TSQuotedMessage quotedMessageForDataMessage:dataMessage
thread:oldGroupThread
transaction:transaction];
@ -1270,7 +1257,7 @@ NS_ASSUME_NONNULL_BEGIN
authorId:envelope.source
sourceDeviceId:envelope.sourceDevice
messageBody:body
attachmentIds:attachmentIds
attachmentIds:@[]
expiresInSeconds:dataMessage.expireTimer
quotedMessage:quotedMessage
contactShare:contact
@ -1289,13 +1276,6 @@ NS_ASSUME_NONNULL_BEGIN
}
}
} else {
if (body.length == 0 && attachmentIds.count < 1 && !contact) {
OWSLogWarn(@"ignoring empty incoming message from: %@ with timestamp: %lu",
envelopeAddress(envelope),
(unsigned long)timestamp);
return nil;
}
OWSLogDebug(
@"incoming message from: %@ with timestamp: %lu", envelopeAddress(envelope), (unsigned long)timestamp);
TSContactThread *thread =
@ -1311,7 +1291,7 @@ NS_ASSUME_NONNULL_BEGIN
authorId:[thread contactIdentifier]
sourceDeviceId:envelope.sourceDevice
messageBody:body
attachmentIds:attachmentIds
attachmentIds:@[]
expiresInSeconds:dataMessage.expireTimer
quotedMessage:quotedMessage
contactShare:contact
@ -1365,8 +1345,7 @@ NS_ASSUME_NONNULL_BEGIN
if ([attachmentPointer isKindOfClass:[TSAttachmentPointer class]]) {
OWSAttachmentsProcessor *attachmentProcessor =
[[OWSAttachmentsProcessor alloc] initWithAttachmentPointer:attachmentPointer
networkManager:self.networkManager];
[[OWSAttachmentsProcessor alloc] initWithAttachmentPointers:@[ attachmentPointer ]];
OWSLogDebug(@"downloading thumbnail for message: %lu", (unsigned long)incomingMessage.timestamp);
[attachmentProcessor fetchAttachmentsForMessage:incomingMessage
@ -1396,8 +1375,7 @@ NS_ASSUME_NONNULL_BEGIN
OWSFailDebug(@"avatar attachmentPointer was unexpectedly nil");
} else {
OWSAttachmentsProcessor *attachmentProcessor =
[[OWSAttachmentsProcessor alloc] initWithAttachmentPointer:attachmentPointer
networkManager:self.networkManager];
[[OWSAttachmentsProcessor alloc] initWithAttachmentPointers:@[ attachmentPointer ]];
OWSLogDebug(@"downloading contact avatar for message: %lu", (unsigned long)incomingMessage.timestamp);
[attachmentProcessor fetchAttachmentsForMessage:incomingMessage