Add debug UI for multi-image sends.

pull/1/head
Matthew Chen 7 years ago
parent d04f1e6e36
commit f6591fac25

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

@ -634,7 +634,8 @@ class MediaGallery: NSObject, MediaGalleryDataSource, MediaTileViewControllerDel
var hasFetchedMostRecent = false var hasFetchedMostRecent = false
func buildGalleryItem(message: TSMessage, transaction: YapDatabaseReadTransaction) -> MediaGalleryItem? { 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") owsFailDebug("attachment was unexpectedly empty")
return nil return nil
} }

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

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

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

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

@ -630,27 +630,6 @@ typedef void (^OWSLoadedThumbnailSuccess)(OWSLoadedThumbnail *loadedThumbnail);
return [OWSBackupFragment fetchObjectWithUniqueID:self.lazyRestoreFragmentId]; 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 #pragma mark - Thumbnails
- (nullable UIImage *)thumbnailImageWithSizeHint:(CGSize)sizeHint - (nullable UIImage *)thumbnailImageWithSizeHint:(CGSize)sizeHint

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

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

@ -199,14 +199,19 @@ static const NSUInteger OWSMessageSchemaVersion = 4;
return self.attachmentIds ? (self.attachmentIds.count > 0) : NO; return self.attachmentIds ? (self.attachmentIds.count > 0) : NO;
} }
- (nullable TSAttachment *)attachmentWithTransaction:(YapDatabaseReadTransaction *)transaction - (NSArray<TSAttachment *> *)attachmentsWithTransaction:(YapDatabaseReadTransaction *)transaction
{ {
if (!self.hasAttachments) { NSMutableArray<TSAttachment *> *attachments = [NSMutableArray new];
return nil; 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);
}
} }
return [attachments copy];
OWSAssertDebug(self.attachmentIds.count == 1);
return [TSAttachment fetchObjectWithUniqueID:self.attachmentIds.firstObject transaction:transaction];
} }
- (NSString *)debugDescription - (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 - (nullable NSString *)bodyTextWithTransaction:(YapDatabaseReadTransaction *)transaction
{ {
if (self.hasAttachments) { NSString *_Nullable oversizeText = [self oversizeTextWithTransaction:transaction];
TSAttachment *_Nullable attachment = [self attachmentWithTransaction:transaction]; if (oversizeText) {
return oversizeText;
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;
}
}
}
} }
if (self.body.length > 0) { if (self.body.length > 0) {

@ -253,7 +253,8 @@ NS_ASSUME_NONNULL_BEGIN
return nil; 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]]) { if (![attachment isKindOfClass:[TSAttachmentStream class]]) {
return nil; return nil;
} }

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

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

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

@ -204,29 +204,7 @@ public class FullTextSearchFinder: NSObject {
} }
private static func oversizeText(forMessage message: TSMessage, transaction: YapDatabaseReadTransaction) -> String? { private static func oversizeText(forMessage message: TSMessage, transaction: YapDatabaseReadTransaction) -> String? {
guard message.hasAttachments() else { return message.oversizeText(with: transaction)
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
} }
private class func indexContent(object: Any, transaction: YapDatabaseReadTransaction) -> String? { private class func indexContent(object: Any, transaction: YapDatabaseReadTransaction) -> String? {

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

Loading…
Cancel
Save