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

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

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

@ -123,10 +123,7 @@ public class OWSLinkPreview: MTLModel {
@objc
public class func isInvalidContentError(_ error: Error) -> Bool {
guard let error = error as? LinkPreviewError else {
return false
}
guard let error = error as? LinkPreviewError else { return false }
return error == .invalidContent
}
@ -253,12 +250,10 @@ public class OWSLinkPreview: MTLModel {
return nil
}
attachment.save(with: transaction)
return attachment.uniqueId
}
private func isValid() -> Bool {
var hasTitle = false
if let titleValue = title {
@ -701,37 +696,36 @@ public class OWSLinkPreview: MTLModel {
return promise
}
public class func getImagePreview(fromUrl imageUrl: String, transaction: YapDatabaseReadWriteTransaction) -> Promise<OWSLinkPreview> {
// Get the mime types the url
guard let imageFileExtension = fileExtension(forImageUrl: imageUrl),
let imageMimeType = mimetype(forImageFileExtension: imageFileExtension) else {
return Promise(error: LinkPreviewError.invalidInput)
public class func getImagePreview(from url: String, in transaction: YapDatabaseReadWriteTransaction) -> Promise<OWSLinkPreview> {
// Get the MIME type
guard let imageFileExtension = fileExtension(forImageUrl: url), let imageMIMEType = mimetype(forImageFileExtension: imageFileExtension) else {
return Promise(error: LinkPreviewError.invalidInput)
}
return downloadImage(url: imageUrl).map { data in
// Make sure the downloaded image has the correct mime type
guard let newImageMimeType = NSData(data: data).ows_guessMimeType() else {
return downloadImage(url: url).map { data in
// Make sure the downloaded image has the correct MIME type
guard let newImageMIMEType = NSData(data: data).ows_guessMimeType() else {
throw LinkPreviewError.invalidContent
}
// Save the attachment
guard let attachmentId = saveAttachmentIfPossible(imageData: data, mimeType: newImageMimeType, transaction: transaction) else {
Logger.verbose("Error: Failed to save attachment for \(imageUrl)")
guard let attachmentId = saveAttachmentIfPossible(imageData: data, mimeType: newImageMIMEType, transaction: transaction) else {
Logger.verbose("Failed to save attachment for: \(url).")
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 (imageMimeType == OWSMimeTypeImageGif && newImageMimeType != OWSMimeTypeImageGif) {
return OWSLinkPreview(urlString: imageUrl, title: nil, imageAttachmentId: attachmentId)
// 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) {
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:)
public class func objc_getImagePreview(url imageUrl: String, transaction: YapDatabaseReadWriteTransaction) -> AnyPromise {
return AnyPromise.from(getImagePreview(fromUrl: imageUrl, transaction: transaction))
public class func objc_getImagePreview(url: String, in transaction: YapDatabaseReadWriteTransaction) -> AnyPromise {
return AnyPromise.from(getImagePreview(from: url, in: transaction))
}
public class func downloadImage(url imageUrl: String) -> Promise<Data> {
@ -799,7 +793,7 @@ public class OWSLinkPreview: MTLModel {
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) }
let maxImageSize: CGFloat = 1024

@ -67,7 +67,7 @@ typedef NS_ENUM(NSInteger, LKMessageFriendRequestStatus) {
- (NSArray<TSAttachment *> *)attachmentsWithTransaction:(YapDatabaseReadTransaction *)transaction;
- (NSArray<TSAttachment *> *)mediaAttachmentsWithTransaction:(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
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;
#pragma mark - Link preview
#pragma mark - Link Preview
- (void)generateLinkPreviewIfNeededFromURL:(NSString *)url;

@ -250,9 +250,9 @@ static const NSUInteger OWSMessageSchemaVersion = 4;
[self saveWithTransaction:transaction];
}
- (void)addAttachmentId:(NSString *)attachmentId transaction:(YapDatabaseReadWriteTransaction *)transaction {
- (void)addAttachmentWithID:(NSString *)attachmentID in:(YapDatabaseReadWriteTransaction *)transaction {
if (!self.attachmentIds) { return; }
[self.attachmentIds addObject:attachmentId];
[self.attachmentIds addObject:attachmentID];
[self saveWithTransaction:transaction];
}
@ -512,13 +512,12 @@ static const NSUInteger OWSMessageSchemaVersion = 4;
}
}
#pragma mark - Link preview
#pragma mark - Link Preview
- (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; }
[OWSLinkPreview tryToBuildPreviewInfoObjcWithPreviewUrl:url]
.thenOn(dispatch_get_main_queue(), ^(OWSLinkPreviewDraft *linkPreviewDraft) {
[self.dbReadWriteConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) {
@ -528,7 +527,7 @@ static const NSUInteger OWSMessageSchemaVersion = 4;
}];
})
.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]) {
__block AnyPromise *promise;
[self.dbReadWriteConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) {
@ -536,18 +535,14 @@ static const NSUInteger OWSMessageSchemaVersion = 4;
}];
return promise;
}
// Return the error
return [AnyPromise promiseWithValue:error];
})
.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) {
if (linkPreview.isDirectAttachment) {
if (!self.hasAttachments) {
[self addAttachmentId:linkPreview.imageAttachmentId transaction:transaction];
// Set the message id in attachment
[self addAttachmentWithID:linkPreview.imageAttachmentId in:transaction];
TSAttachmentStream *linkPreviewAttachment = [TSAttachmentStream fetchObjectWithUniqueID:linkPreview.imageAttachmentId transaction:transaction];
linkPreviewAttachment.albumMessageId = self.uniqueId;
linkPreviewAttachment.isUploaded = true;
@ -557,7 +552,6 @@ static const NSUInteger OWSMessageSchemaVersion = 4;
self.linkPreview = linkPreview;
[self saveWithTransaction:transaction];
}
}];
});
}

@ -1240,7 +1240,7 @@ NSString *const OWSMessageSenderRateLimitedException = @"RateLimitedException";
[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;
if (message.linkPreview == nil && !message.hasAttachments) {
dispatch_async(dispatch_get_main_queue(), ^{

Loading…
Cancel
Save