pull/51/head
Niels Andriesse 6 years ago
parent 6c742349bd
commit bd309c4f6f

@ -6,14 +6,10 @@
<dict> <dict>
<key>CarthageVersion</key> <key>CarthageVersion</key>
<string>0.33.0</string> <string>0.33.0</string>
<key>DateTime</key>
<string>Fri Sep 13 00:30:48 UTC 2019</string>
<key>OSXVersion</key> <key>OSXVersion</key>
<string>10.14.6</string> <string>10.14.6</string>
<key>WebRTCCommit</key> <key>WebRTCCommit</key>
<string>1445d719bf05280270e9f77576f80f973fd847f8 M73</string> <string>1445d719bf05280270e9f77576f80f973fd847f8 M73</string>
<key>XCodeVersion</key>
<string>1000.1030</string>
</dict> </dict>
<key>CFBundleDevelopmentRegion</key> <key>CFBundleDevelopmentRegion</key>
<string>en</string> <string>en</string>

@ -290,7 +290,6 @@ NSUInteger const TSAttachmentSchemaVersion = 4;
#pragma mark - Relationships #pragma mark - Relationships
- (nullable TSMessage *)fetchAlbumMessageWithTransaction:(YapDatabaseReadTransaction *)transaction - (nullable TSMessage *)fetchAlbumMessageWithTransaction:(YapDatabaseReadTransaction *)transaction
{ {
if (self.albumMessageId == nil) { if (self.albumMessageId == nil) {

@ -123,10 +123,7 @@ public class OWSLinkPreview: MTLModel {
@objc @objc
public class func isInvalidContentError(_ error: Error) -> Bool { public class func isInvalidContentError(_ error: Error) -> Bool {
guard let error = error as? LinkPreviewError else { guard let error = error as? LinkPreviewError else { return false }
return false
}
return error == .invalidContent return error == .invalidContent
} }
@ -253,12 +250,10 @@ public class OWSLinkPreview: MTLModel {
return nil return nil
} }
attachment.save(with: transaction) attachment.save(with: transaction)
return attachment.uniqueId return attachment.uniqueId
} }
private func isValid() -> Bool { private func isValid() -> Bool {
var hasTitle = false var hasTitle = false
if let titleValue = title { if let titleValue = title {
@ -701,37 +696,36 @@ public class OWSLinkPreview: MTLModel {
return promise return promise
} }
public class func getImagePreview(fromUrl imageUrl: String, transaction: YapDatabaseReadWriteTransaction) -> Promise<OWSLinkPreview> { public class func getImagePreview(from url: String, in transaction: YapDatabaseReadWriteTransaction) -> Promise<OWSLinkPreview> {
// Get the mime types the url // Get the MIME type
guard let imageFileExtension = fileExtension(forImageUrl: imageUrl), guard let imageFileExtension = fileExtension(forImageUrl: url), let imageMIMEType = mimetype(forImageFileExtension: imageFileExtension) else {
let imageMimeType = mimetype(forImageFileExtension: imageFileExtension) else { return Promise(error: LinkPreviewError.invalidInput)
return Promise(error: LinkPreviewError.invalidInput)
} }
return downloadImage(url: imageUrl).map { data in return downloadImage(url: url).map { data in
// Make sure the downloaded image has the correct mime type // Make sure the downloaded image has the correct MIME type
guard let newImageMimeType = NSData(data: data).ows_guessMimeType() else { guard let newImageMIMEType = NSData(data: data).ows_guessMimeType() else {
throw LinkPreviewError.invalidContent throw LinkPreviewError.invalidContent
} }
// Save the attachment // Save the attachment
guard let attachmentId = saveAttachmentIfPossible(imageData: data, mimeType: newImageMimeType, transaction: transaction) else { guard let attachmentId = saveAttachmentIfPossible(imageData: data, mimeType: newImageMIMEType, transaction: transaction) else {
Logger.verbose("Error: Failed to save attachment for \(imageUrl)") Logger.verbose("Failed to save attachment for: \(url).")
throw LinkPreviewError.attachmentFailedToSave throw LinkPreviewError.attachmentFailedToSave
} }
// If we had a GIF and the data we have is not a GIF then we need to render a link preview without attachments // If it's a GIF and the data we have is not a GIF then we need to render a link preview without attachments
if (imageMimeType == OWSMimeTypeImageGif && newImageMimeType != OWSMimeTypeImageGif) { if (imageMIMEType == OWSMimeTypeImageGif && newImageMIMEType != OWSMimeTypeImageGif) {
return OWSLinkPreview(urlString: imageUrl, title: nil, imageAttachmentId: attachmentId) return OWSLinkPreview(urlString: url, title: nil, imageAttachmentId: attachmentId)
} }
return OWSLinkPreview(urlString: imageUrl, title: nil, imageAttachmentId: attachmentId, isDirectAttachment: true) return OWSLinkPreview(urlString: url, title: nil, imageAttachmentId: attachmentId, isDirectAttachment: true)
} }
} }
@objc(getImagePreviewFromUrl:transaction:) @objc(getImagePreviewFromUrl:transaction:)
public class func objc_getImagePreview(url imageUrl: String, transaction: YapDatabaseReadWriteTransaction) -> AnyPromise { public class func objc_getImagePreview(url: String, in transaction: YapDatabaseReadWriteTransaction) -> AnyPromise {
return AnyPromise.from(getImagePreview(fromUrl: imageUrl, transaction: transaction)) return AnyPromise.from(getImagePreview(from: url, in: transaction))
} }
public class func downloadImage(url imageUrl: String) -> Promise<Data> { public class func downloadImage(url imageUrl: String) -> Promise<Data> {
@ -799,7 +793,7 @@ public class OWSLinkPreview: MTLModel {
return Promise(error: LinkPreviewError.invalidContent) return Promise(error: LinkPreviewError.invalidContent)
} }
// If we have a gif then don't download it as a jpg and also we need to ensure that it's a valid GIF // Loki: If it's a GIF then ensure it's validity and don't download it as a JPG
if (imageMimeType == OWSMimeTypeImageGif && NSData(data: data).ows_isValidImage(withMimeType: OWSMimeTypeImageGif)) { return Promise.value(data) } if (imageMimeType == OWSMimeTypeImageGif && NSData(data: data).ows_isValidImage(withMimeType: OWSMimeTypeImageGif)) { return Promise.value(data) }
let maxImageSize: CGFloat = 1024 let maxImageSize: CGFloat = 1024

@ -67,7 +67,7 @@ typedef NS_ENUM(NSInteger, LKMessageFriendRequestStatus) {
- (NSArray<TSAttachment *> *)attachmentsWithTransaction:(YapDatabaseReadTransaction *)transaction; - (NSArray<TSAttachment *> *)attachmentsWithTransaction:(YapDatabaseReadTransaction *)transaction;
- (NSArray<TSAttachment *> *)mediaAttachmentsWithTransaction:(YapDatabaseReadTransaction *)transaction; - (NSArray<TSAttachment *> *)mediaAttachmentsWithTransaction:(YapDatabaseReadTransaction *)transaction;
- (nullable TSAttachment *)oversizeTextAttachmentWithTransaction:(YapDatabaseReadTransaction *)transaction; - (nullable TSAttachment *)oversizeTextAttachmentWithTransaction:(YapDatabaseReadTransaction *)transaction;
- (void)addAttachmentId:(NSString *)attachmentId transaction:(YapDatabaseReadWriteTransaction *)transaction; - (void)addAttachmentWithID:(NSString *)attachmentID in:(YapDatabaseReadWriteTransaction *)transaction;
- (void)removeAttachment:(TSAttachment *)attachment - (void)removeAttachment:(TSAttachment *)attachment
transaction:(YapDatabaseReadWriteTransaction *)transaction NS_SWIFT_NAME(removeAttachment(_:transaction:)); transaction:(YapDatabaseReadWriteTransaction *)transaction NS_SWIFT_NAME(removeAttachment(_:transaction:));
@ -98,7 +98,7 @@ typedef NS_ENUM(NSInteger, LKMessageFriendRequestStatus) {
- (void)saveGroupChatMessageID:(uint64_t)serverMessageID in:(YapDatabaseReadWriteTransaction *_Nullable)transaction; - (void)saveGroupChatMessageID:(uint64_t)serverMessageID in:(YapDatabaseReadWriteTransaction *_Nullable)transaction;
#pragma mark - Link preview #pragma mark - Link Preview
- (void)generateLinkPreviewIfNeededFromURL:(NSString *)url; - (void)generateLinkPreviewIfNeededFromURL:(NSString *)url;

@ -250,9 +250,9 @@ static const NSUInteger OWSMessageSchemaVersion = 4;
[self saveWithTransaction:transaction]; [self saveWithTransaction:transaction];
} }
- (void)addAttachmentId:(NSString *)attachmentId transaction:(YapDatabaseReadWriteTransaction *)transaction { - (void)addAttachmentWithID:(NSString *)attachmentID in:(YapDatabaseReadWriteTransaction *)transaction {
if (!self.attachmentIds) { return; } if (!self.attachmentIds) { return; }
[self.attachmentIds addObject:attachmentId]; [self.attachmentIds addObject:attachmentID];
[self saveWithTransaction:transaction]; [self saveWithTransaction:transaction];
} }
@ -512,13 +512,12 @@ static const NSUInteger OWSMessageSchemaVersion = 4;
} }
} }
#pragma mark - Link preview #pragma mark - Link Preview
- (void)generateLinkPreviewIfNeededFromURL:(NSString *)url { - (void)generateLinkPreviewIfNeededFromURL:(NSString *)url {
// If we already have previews or attachments then don't bother making link previews // If we already havea link previews or attachment then don't bother
if (self.linkPreview != nil || self.hasAttachments) { return; } if (self.linkPreview != nil || self.hasAttachments) { return; }
[OWSLinkPreview tryToBuildPreviewInfoObjcWithPreviewUrl:url] [OWSLinkPreview tryToBuildPreviewInfoObjcWithPreviewUrl:url]
.thenOn(dispatch_get_main_queue(), ^(OWSLinkPreviewDraft *linkPreviewDraft) { .thenOn(dispatch_get_main_queue(), ^(OWSLinkPreviewDraft *linkPreviewDraft) {
[self.dbReadWriteConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) { [self.dbReadWriteConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) {
@ -528,7 +527,7 @@ static const NSUInteger OWSMessageSchemaVersion = 4;
}]; }];
}) })
.catchOn(dispatch_get_main_queue(), ^(NSError *error) { .catchOn(dispatch_get_main_queue(), ^(NSError *error) {
// If we failed to get link preview due to invalid content then maybe it's a link to a direct image? // If we failed to get a link preview due to an invalid content type error then this could be a direct image link
if ([OWSLinkPreview isInvalidContentError:error]) { if ([OWSLinkPreview isInvalidContentError:error]) {
__block AnyPromise *promise; __block AnyPromise *promise;
[self.dbReadWriteConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) { [self.dbReadWriteConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) {
@ -536,18 +535,14 @@ static const NSUInteger OWSMessageSchemaVersion = 4;
}]; }];
return promise; return promise;
} }
// Return the error
return [AnyPromise promiseWithValue:error]; return [AnyPromise promiseWithValue:error];
}) })
.thenOn(dispatch_get_main_queue(), ^(OWSLinkPreview *linkPreview) { .thenOn(dispatch_get_main_queue(), ^(OWSLinkPreview *linkPreview) {
// If we managed to get direct previews then render them // If we managed to get a direct image preview then render it
[self.dbReadWriteConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) { [self.dbReadWriteConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) {
if (linkPreview.isDirectAttachment) { if (linkPreview.isDirectAttachment) {
if (!self.hasAttachments) { if (!self.hasAttachments) {
[self addAttachmentId:linkPreview.imageAttachmentId transaction:transaction]; [self addAttachmentWithID:linkPreview.imageAttachmentId in:transaction];
// Set the message id in attachment
TSAttachmentStream *linkPreviewAttachment = [TSAttachmentStream fetchObjectWithUniqueID:linkPreview.imageAttachmentId transaction:transaction]; TSAttachmentStream *linkPreviewAttachment = [TSAttachmentStream fetchObjectWithUniqueID:linkPreview.imageAttachmentId transaction:transaction];
linkPreviewAttachment.albumMessageId = self.uniqueId; linkPreviewAttachment.albumMessageId = self.uniqueId;
linkPreviewAttachment.isUploaded = true; linkPreviewAttachment.isUploaded = true;
@ -557,7 +552,6 @@ static const NSUInteger OWSMessageSchemaVersion = 4;
self.linkPreview = linkPreview; self.linkPreview = linkPreview;
[self saveWithTransaction:transaction]; [self saveWithTransaction:transaction];
} }
}]; }];
}); });
} }

@ -1240,7 +1240,7 @@ NSString *const OWSMessageSenderRateLimitedException = @"RateLimitedException";
[SignalRecipient markRecipientAsRegisteredAndGet:recipient.recipientId transaction:transaction]; [SignalRecipient markRecipientAsRegisteredAndGet:recipient.recipientId transaction:transaction];
}]; }];
// Check if we need to generate link previews // Loki: Check if we need to generate a link preview
TSMessage *message = messageSend.message; TSMessage *message = messageSend.message;
if (message.linkPreview == nil && !message.hasAttachments) { if (message.linkPreview == nil && !message.hasAttachments) {
dispatch_async(dispatch_get_main_queue(), ^{ dispatch_async(dispatch_get_main_queue(), ^{

Loading…
Cancel
Save