Add debug UI for multi-image sends.

This commit is contained in:
Matthew Chen 2018-11-02 11:33:05 -04:00
parent d04f1e6e36
commit f6591fac25
16 changed files with 185 additions and 144 deletions

View File

@ -127,7 +127,7 @@ NS_ASSUME_NONNULL_BEGIN
actionBlock:^{
[DebugUIMessages sendNTextMessagesInThread:thread];
}],
[OWSTableItem itemWithTitle:@"Send multi-image messages"
[OWSTableItem itemWithTitle:@"Send Multi-Image Message"
actionBlock:^{
[DebugUIMessages sendMultiImageMessageInThread:thread];
}],

View File

@ -634,7 +634,8 @@ class MediaGallery: NSObject, MediaGalleryDataSource, MediaTileViewControllerDel
var hasFetchedMostRecent = false
func buildGalleryItem(message: TSMessage, transaction: YapDatabaseReadTransaction) -> MediaGalleryItem? {
guard let attachmentStream = message.attachment(with: transaction) as? TSAttachmentStream else {
// TODO: Support multi-image messages.
guard let attachmentStream = message.attachments(with: transaction).first as? TSAttachmentStream else {
owsFailDebug("attachment was unexpectedly empty")
return nil
}

View File

@ -167,7 +167,7 @@ NS_ASSUME_NONNULL_BEGIN
BOOL hasText = quotedText.length > 0;
BOOL hasAttachment = NO;
TSAttachment *_Nullable attachment = [message attachmentWithTransaction:transaction];
TSAttachment *_Nullable attachment = [message attachmentsWithTransaction:transaction].firstObject;
TSAttachmentStream *quotedAttachment;
if (attachment && [attachment isKindOfClass:[TSAttachmentStream class]]) {

View File

@ -239,6 +239,11 @@ public class SignalAttachment: NSObject {
return errorDescription
}
@objc
public var outgoingAttachmentInfo: OutgoingAttachmentInfo {
return OutgoingAttachmentInfo(dataSource: dataSource, contentType: mimeType, sourceFilename: filenameOrDefault)
}
@objc
public func image() -> UIImage? {
if let cachedImage = cachedImage {

View File

@ -127,11 +127,11 @@ NS_ASSUME_NONNULL_BEGIN
quotedMessage:[quotedReplyModel buildQuotedMessageForSending]
contactShare:nil];
[self.messageSenderJobQueue addMediaMessage:message
dataSource:attachment.dataSource
contentType:attachment.mimeType
sourceFilename:attachment.filenameOrDefault
isTemporaryAttachment:NO];
NSMutableArray<OWSOutgoingAttachmentInfo *> *attachmentInfos = [NSMutableArray new];
for (SignalAttachment *attachment in attachments) {
[attachmentInfos addObject:attachment.outgoingAttachmentInfo];
}
[self.messageSenderJobQueue addMediaMessage:message attachmentInfos:attachmentInfos isTemporaryAttachment:NO];
return message;
}

View File

@ -60,9 +60,6 @@ typedef void (^OWSThumbnailFailure)(void);
- (BOOL)writeData:(NSData *)data error:(NSError **)error;
- (BOOL)writeDataSource:(DataSource *)dataSource;
- (BOOL)isOversizeText;
- (nullable NSString *)readOversizeText;
+ (void)deleteAttachments;
+ (NSString *)attachmentsFolder;

View File

@ -630,27 +630,6 @@ typedef void (^OWSLoadedThumbnailSuccess)(OWSLoadedThumbnail *loadedThumbnail);
return [OWSBackupFragment fetchObjectWithUniqueID:self.lazyRestoreFragmentId];
}
- (BOOL)isOversizeText
{
return [self.contentType isEqualToString:OWSMimeTypeOversizeTextMessage];
}
- (nullable NSString *)readOversizeText
{
if (!self.isOversizeText) {
OWSFailDebug(@"oversize text attachment has unexpected content type.");
return nil;
}
NSError *error;
NSData *_Nullable data = [self readDataFromFileWithError:&error];
if (error || !data) {
OWSFailDebug(@"could not read oversize text attachment: %@.", error);
return nil;
}
NSString *_Nullable string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
return string;
}
#pragma mark - Thumbnails
- (nullable UIImage *)thumbnailImageWithSizeHint:(CGSize)sizeHint

View File

@ -489,7 +489,6 @@ NSString *NSStringForContactAddressType(OWSContactAddressType value)
return [TSAttachment fetchObjectWithUniqueID:self.avatarAttachmentId transaction:transaction];
}
- (void)saveAvatarImage:(UIImage *)image transaction:(YapDatabaseReadWriteTransaction *)transaction
{
NSData *imageData = UIImageJPEGRepresentation(image, (CGFloat)0.9);

View File

@ -41,9 +41,11 @@ NS_ASSUME_NONNULL_BEGIN
- (nullable instancetype)initWithCoder:(NSCoder *)coder NS_DESIGNATED_INITIALIZER;
- (BOOL)hasAttachments;
- (nullable TSAttachment *)attachmentWithTransaction:(YapDatabaseReadTransaction *)transaction;
- (NSArray<TSAttachment *> *)attachmentsWithTransaction:(YapDatabaseReadTransaction *)transaction;
- (void)setQuotedMessageThumbnailAttachmentStream:(TSAttachmentStream *)attachmentStream;
- (nullable NSString *)oversizeTextWithTransaction:(YapDatabaseReadTransaction *)transaction;
- (nullable NSString *)bodyTextWithTransaction:(YapDatabaseReadTransaction *)transaction;
- (BOOL)shouldStartExpireTimerWithTransaction:(YapDatabaseReadTransaction *)transaction;

View File

@ -199,14 +199,19 @@ static const NSUInteger OWSMessageSchemaVersion = 4;
return self.attachmentIds ? (self.attachmentIds.count > 0) : NO;
}
- (nullable TSAttachment *)attachmentWithTransaction:(YapDatabaseReadTransaction *)transaction
- (NSArray<TSAttachment *> *)attachmentsWithTransaction:(YapDatabaseReadTransaction *)transaction
{
if (!self.hasAttachments) {
return nil;
NSMutableArray<TSAttachment *> *attachments = [NSMutableArray new];
for (NSString *attachmentId in self.attachmentIds) {
TSAttachment *_Nullable attachment =
[TSAttachment fetchObjectWithUniqueID:attachmentId transaction:transaction];
if (attachment) {
[attachments addObject:attachment];
} else {
OWSFailDebug(@"Missing attachment for: %@.", attachmentId);
}
}
OWSAssertDebug(self.attachmentIds.count == 1);
return [TSAttachment fetchObjectWithUniqueID:self.attachmentIds.firstObject transaction:transaction];
return [attachments copy];
}
- (NSString *)debugDescription
@ -223,23 +228,40 @@ static const NSUInteger OWSMessageSchemaVersion = 4;
}
}
- (nullable NSString *)oversizeTextWithTransaction:(YapDatabaseReadTransaction *)transaction
{
if (self.attachmentIds.count != 1) {
return nil;
}
TSAttachment *_Nullable attachment = [self attachmentsWithTransaction:transaction].firstObject;
OWSAssertDebug(attachment);
if (![OWSMimeTypeOversizeTextMessage isEqualToString:attachment.contentType]
|| ![attachment isKindOfClass:TSAttachmentStream.class]) {
return nil;
}
TSAttachmentStream *attachmentStream = (TSAttachmentStream *)attachment;
NSData *_Nullable data = [NSData dataWithContentsOfFile:attachmentStream.originalFilePath];
if (!data) {
OWSFailDebug(@"Can't load oversize text data.");
return nil;
}
NSString *_Nullable text = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
if (!text) {
OWSFailDebug(@"Can't parse oversize text data.");
return nil;
}
return text.filterStringForDisplay;
}
- (nullable NSString *)bodyTextWithTransaction:(YapDatabaseReadTransaction *)transaction
{
if (self.hasAttachments) {
TSAttachment *_Nullable attachment = [self attachmentWithTransaction:transaction];
if ([OWSMimeTypeOversizeTextMessage isEqualToString:attachment.contentType] &&
[attachment isKindOfClass:TSAttachmentStream.class]) {
TSAttachmentStream *attachmentStream = (TSAttachmentStream *)attachment;
NSData *_Nullable data = [NSData dataWithContentsOfFile:attachmentStream.originalFilePath];
if (data) {
NSString *_Nullable text = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
if (text) {
return text.filterStringForDisplay;
}
}
}
NSString *_Nullable oversizeText = [self oversizeTextWithTransaction:transaction];
if (oversizeText) {
return oversizeText;
}
if (self.body.length > 0) {

View File

@ -253,7 +253,8 @@ NS_ASSUME_NONNULL_BEGIN
return nil;
}
TSAttachment *attachment = [quotedMessage attachmentWithTransaction:transaction];
// We quote _the first_ attachment, if any.
TSAttachment *_Nullable attachment = [quotedMessage attachmentsWithTransaction:transaction].firstObject;
if (![attachment isKindOfClass:[TSAttachmentStream class]]) {
return nil;
}

View File

@ -33,6 +33,23 @@ typedef void (^RetryableFailureHandler)(NSError *_Nonnull error);
#pragma mark -
NS_SWIFT_NAME(OutgoingAttachmentInfo)
@interface OWSOutgoingAttachmentInfo : NSObject
@property (nonatomic, readonly) DataSource *dataSource;
@property (nonatomic, readonly) NSString *contentType;
@property (nonatomic, readonly, nullable) NSString *sourceFilename;
- (instancetype)init NS_UNAVAILABLE;
- (instancetype)initWithDataSource:(DataSource *)dataSource
contentType:(NSString *)contentType
sourceFilename:(nullable NSString *)sourceFilename NS_DESIGNATED_INITIALIZER;
@end
#pragma mark -
NS_SWIFT_NAME(MessageSender)
@interface OWSMessageSender : NSObject
@ -71,6 +88,8 @@ NS_SWIFT_NAME(MessageSender)
@end
#pragma mark -
@interface OutgoingMessagePreparer : NSObject
/// Persists all necessary data to disk before sending, e.g. generate thumbnails
@ -80,11 +99,10 @@ NS_SWIFT_NAME(MessageSender)
transaction:(YapDatabaseReadWriteTransaction *)transaction;
/// Writes attachment to disk and applies original filename to message attributes
+ (void)prepareAttachmentWithDataSource:(DataSource *)dataSource
contentType:(NSString *)contentType
sourceFilename:(nullable NSString *)sourceFilename
inMessage:(TSOutgoingMessage *)outgoingMessage
completionHandler:(void (^)(NSError *_Nullable error))completionHandler;
+ (void)prepareAttachments:(NSArray<OWSOutgoingAttachmentInfo *> *)attachmentInfos
inMessage:(TSOutgoingMessage *)outgoingMessage
completionHandler:(void (^)(NSError *_Nullable error))completionHandler
NS_SWIFT_NAME(prepareAttachments(_:inMessage:completionHandler:));
@end

View File

@ -81,6 +81,28 @@ void AssertIsOnSendingQueue()
#pragma mark -
@implementation OWSOutgoingAttachmentInfo
- (instancetype)initWithDataSource:(DataSource *)dataSource
contentType:(NSString *)contentType
sourceFilename:(nullable NSString *)sourceFilename
{
self = [super init];
if (!self) {
return self;
}
_dataSource = dataSource;
_contentType = contentType;
_sourceFilename = sourceFilename;
return self;
}
@end
#pragma mark -
/**
* OWSSendMessageOperation encapsulates all the work associated with sending a message, e.g. uploading attachments,
* getting proper keys, and retrying upon failure.
@ -156,13 +178,14 @@ void AssertIsOnSendingQueue()
// Sanity check preconditions
if (self.message.hasAttachments) {
[self.dbConnection readWithBlock:^(YapDatabaseReadTransaction * transaction) {
TSAttachmentStream *attachmentStream
= (TSAttachmentStream *)[self.message attachmentWithTransaction:transaction];
OWSAssertDebug(attachmentStream);
OWSAssertDebug([attachmentStream isKindOfClass:[TSAttachmentStream class]]);
OWSAssertDebug(attachmentStream.serverId);
OWSAssertDebug(attachmentStream.isUploaded);
[self.dbConnection readWithBlock:^(YapDatabaseReadTransaction *transaction) {
for (TSAttachment *attachment in [self.message attachmentsWithTransaction:transaction]) {
OWSAssertDebug([attachment isKindOfClass:[TSAttachmentStream class]]);
TSAttachmentStream *attachmentStream = (TSAttachmentStream *)attachment;
OWSAssertDebug(attachmentStream);
OWSAssertDebug(attachmentStream.serverId);
OWSAssertDebug(attachmentStream.isUploaded);
}
}];
}
@ -431,17 +454,18 @@ NSString *const OWSMessageSenderRateLimitedException = @"RateLimitedException";
failure:(void (^)(NSError *error))failureHandler
{
OWSAssertDebug(dataSource);
[OutgoingMessagePreparer prepareAttachmentWithDataSource:dataSource
contentType:contentType
sourceFilename:sourceFilename
inMessage:message
completionHandler:^(NSError *_Nullable error) {
if (error) {
failureHandler(error);
return;
}
[self sendMessage:message success:successHandler failure:failureHandler];
}];
OWSOutgoingAttachmentInfo *attachmentInfo = [[OWSOutgoingAttachmentInfo alloc] initWithDataSource:dataSource
contentType:contentType
sourceFilename:sourceFilename];
[OutgoingMessagePreparer prepareAttachments:@[ attachmentInfo ]
inMessage:message
completionHandler:^(NSError *_Nullable error) {
if (error) {
failureHandler(error);
return;
}
[self sendMessage:message success:successHandler failure:failureHandler];
}];
}
- (void)sendMessageToService:(TSOutgoingMessage *)message
@ -1768,35 +1792,42 @@ NSString *const OWSMessageSenderRateLimitedException = @"RateLimitedException";
[message updateWithMarkingAllUnsentRecipientsAsSendingWithTransaction:transaction];
}
+ (void)prepareAttachmentWithDataSource:(DataSource *)dataSource
contentType:(NSString *)contentType
sourceFilename:(nullable NSString *)sourceFilename
inMessage:(TSOutgoingMessage *)outgoingMessage
completionHandler:(void (^)(NSError *_Nullable error))completionHandler
+ (void)prepareAttachments:(NSArray<OWSOutgoingAttachmentInfo *> *)attachmentInfos
inMessage:(TSOutgoingMessage *)outgoingMessage
completionHandler:(void (^)(NSError *_Nullable error))completionHandler
{
OWSAssertDebug(dataSource);
OWSAssertDebug(attachmentInfos.count > 0);
OWSAssertDebug(outgoingMessage);
dispatch_async([OWSDispatch attachmentsQueue], ^{
TSAttachmentStream *attachmentStream =
[[TSAttachmentStream alloc] initWithContentType:contentType
byteCount:(UInt32)dataSource.dataLength
sourceFilename:sourceFilename];
if (outgoingMessage.isVoiceMessage) {
attachmentStream.attachmentType = TSAttachmentTypeVoiceMessage;
}
NSMutableArray<TSAttachmentStream *> *attachmentStreams = [NSMutableArray new];
for (OWSOutgoingAttachmentInfo *attachmentInfo in attachmentInfos) {
TSAttachmentStream *attachmentStream =
[[TSAttachmentStream alloc] initWithContentType:attachmentInfo.contentType
byteCount:(UInt32)attachmentInfo.dataSource.dataLength
sourceFilename:attachmentInfo.sourceFilename];
if (outgoingMessage.isVoiceMessage) {
attachmentStream.attachmentType = TSAttachmentTypeVoiceMessage;
}
if (![attachmentStream writeDataSource:dataSource]) {
OWSProdError([OWSAnalyticsEvents messageSenderErrorCouldNotWriteAttachment]);
NSError *error = OWSErrorMakeWriteAttachmentDataError();
completionHandler(error);
if (![attachmentStream writeDataSource:attachmentInfo.dataSource]) {
OWSProdError([OWSAnalyticsEvents messageSenderErrorCouldNotWriteAttachment]);
NSError *error = OWSErrorMakeWriteAttachmentDataError();
completionHandler(error);
return;
}
[attachmentStreams addObject:attachmentStream];
}
[self.dbConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *_Nonnull transaction) {
[attachmentStream saveWithTransaction:transaction];
for (TSAttachmentStream *attachmentStream in attachmentStreams) {
[attachmentStream saveWithTransaction:transaction];
[outgoingMessage.attachmentIds addObject:attachmentStream.uniqueId];
if (sourceFilename) {
outgoingMessage.attachmentFilenameMap[attachmentStream.uniqueId] = sourceFilename;
[outgoingMessage.attachmentIds addObject:attachmentStream.uniqueId];
if (attachmentStream.sourceFilename) {
outgoingMessage.attachmentFilenameMap[attachmentStream.uniqueId] = attachmentStream.sourceFilename;
}
}
[outgoingMessage saveWithTransaction:transaction];
}];

View File

@ -34,20 +34,25 @@ public class MessageSenderJobQueue: NSObject, JobQueue {
@objc(addMediaMessage:dataSource:contentType:sourceFilename:isTemporaryAttachment:)
public func add(mediaMessage: TSOutgoingMessage, dataSource: DataSource, contentType: String, sourceFilename: String?, isTemporaryAttachment: Bool) {
OutgoingMessagePreparer.prepareAttachment(with: dataSource,
contentType: contentType,
sourceFilename: sourceFilename,
in: mediaMessage) { error in
if let error = error {
self.dbConnection.readWrite { transaction in
mediaMessage.update(sendingError: error, transaction: transaction)
}
} else {
self.dbConnection.readWrite { transaction in
self.add(message: mediaMessage, removeMessageAfterSending: isTemporaryAttachment, transaction: transaction)
}
let attachmentInfo = OutgoingAttachmentInfo(dataSource: dataSource, contentType: contentType, sourceFilename: sourceFilename)
add(mediaMessage: mediaMessage, attachmentInfos: [attachmentInfo], isTemporaryAttachment: isTemporaryAttachment)
}
@objc(addMediaMessage:attachmentInfos:isTemporaryAttachment:)
public func add(mediaMessage: TSOutgoingMessage, attachmentInfos: [OutgoingAttachmentInfo], isTemporaryAttachment: Bool) {
OutgoingMessagePreparer.prepareAttachments(attachmentInfos,
inMessage: mediaMessage,
completionHandler: { error in
if let error = error {
self.dbConnection.readWrite { transaction in
mediaMessage.update(sendingError: error, transaction: transaction)
}
}
} else {
self.dbConnection.readWrite { transaction in
self.add(message: mediaMessage, removeMessageAfterSending: isTemporaryAttachment, transaction: transaction)
}
}
})
}
private func add(message: TSOutgoingMessage, removeMessageAfterSending: Bool, transaction: YapDatabaseReadWriteTransaction) {

View File

@ -204,29 +204,7 @@ public class FullTextSearchFinder: NSObject {
}
private static func oversizeText(forMessage message: TSMessage, transaction: YapDatabaseReadTransaction) -> String? {
guard message.hasAttachments() else {
return nil
}
guard let attachment = message.attachment(with: transaction) else {
owsFailDebug("attachment was unexpectedly nil")
return nil
}
guard let attachmentStream = attachment as? TSAttachmentStream else {
return nil
}
guard attachmentStream.isOversizeText() else {
return nil
}
guard let text = attachmentStream.readOversizeText() else {
owsFailDebug("Could not load oversize text attachment")
return nil
}
return text
return message.oversizeText(with: transaction)
}
private class func indexContent(object: Any, transaction: YapDatabaseReadTransaction) -> String? {

View File

@ -147,17 +147,20 @@ static NSString *const OWSMediaGalleryFinderExtensionName = @"OWSMediaGalleryFin
return nil;
}
TSMessage *message = (TSMessage *)object;
OWSAssertDebug(message.attachmentIds.count <= 1);
NSString *attachmentId = message.attachmentIds.firstObject;
if (attachmentId.length == 0) {
return nil;
BOOL allAttachmentsAreMedia = message.attachmentIds.count > 0;
for (NSString *attachmentId in message.attachmentIds) {
OWSAssertDebug(attachmentId.length > 0);
if (![self attachmentIdShouldAppearInMediaGallery:attachmentId transaction:transaction]) {
allAttachmentsAreMedia = NO;
break;
}
}
if ([self attachmentIdShouldAppearInMediaGallery:attachmentId transaction:transaction]) {
if (allAttachmentsAreMedia) {
return [self mediaGroupWithThreadId:message.uniqueThreadId];
}
return nil;
}];