Fix link previews

pull/52/head
Niels Andriesse 6 years ago
parent d6e499bd9a
commit 30061b28b2

@ -105,14 +105,8 @@ public final class LokiGroupChatPoller : NSObject {
guard let messageID = message.uniqueId else { return print("[Loki] Failed to save group message.") } guard let messageID = message.uniqueId else { return print("[Loki] Failed to save group message.") }
storage.setIDForMessageWithServerID(UInt(messageServerID), to: messageID, in: transaction) storage.setIDForMessageWithServerID(UInt(messageServerID), to: messageID, in: transaction)
} }
if let url = OWSLinkPreview.previewUrl(forMessageBodyText: message.body, selectedRange: nil) { if let linkPreviewURL = OWSLinkPreview.previewUrl(forMessageBodyText: message.body, selectedRange: nil) {
let _ = OWSLinkPreview.tryToBuildPreviewInfo(previewUrl: url).done { linkPreviewDraft in message.generateLinkPreviewIfNeeded(fromURL: linkPreviewURL)
OWSPrimaryStorage.shared().dbReadWriteConnection.readWrite { transaction in
guard let linkPreview = try? OWSLinkPreview.buildValidatedLinkPreview(fromInfo: linkPreviewDraft, transaction: transaction) else { return }
message.linkPreview = linkPreview
message.save(with: transaction)
}
}
} }
} }
// Poll // Poll

@ -338,6 +338,9 @@ public class OWSLinkPreview: MTLModel {
// Giphy // Giphy
"giphy.com", "giphy.com",
"media.giphy.com", "media.giphy.com",
"media1.giphy.com",
"media2.giphy.com",
"media3.giphy.com",
"gph.is" "gph.is"
] ]
@ -724,7 +727,7 @@ public class OWSLinkPreview: MTLModel {
} }
} }
@objc(getImagePreviewFromUrl:transaction:) @objc(getImagePreviewWithURL:transaction:)
public class func objc_getImagePreview(url: String, in transaction: YapDatabaseReadWriteTransaction) -> AnyPromise { public class func objc_getImagePreview(url: String, in transaction: YapDatabaseReadWriteTransaction) -> AnyPromise {
return AnyPromise.from(getImagePreview(from: url, in: transaction)) return AnyPromise.from(getImagePreview(from: url, in: transaction))
} }

@ -516,8 +516,6 @@ static const NSUInteger OWSMessageSchemaVersion = 4;
- (void)generateLinkPreviewIfNeededFromURL:(NSString *)url { - (void)generateLinkPreviewIfNeededFromURL:(NSString *)url {
// If we already have a link preview or attachment then don't bother
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) {
@ -531,29 +529,26 @@ static const NSUInteger OWSMessageSchemaVersion = 4;
if ([OWSLinkPreview isInvalidContentError:error]) { if ([OWSLinkPreview isInvalidContentError:error]) {
__block AnyPromise *promise; __block AnyPromise *promise;
[self.dbReadWriteConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) { [self.dbReadWriteConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) {
promise = [OWSLinkPreview getImagePreviewFromUrl:url transaction:transaction]; promise = [OWSLinkPreview getImagePreviewWithURL:url transaction:transaction]
}];
return promise;
}
return [AnyPromise promiseWithValue:error];
})
.thenOn(dispatch_get_main_queue(), ^(OWSLinkPreview *linkPreview) { .thenOn(dispatch_get_main_queue(), ^(OWSLinkPreview *linkPreview) {
// If we managed to get a direct image preview then render it // 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) {
[self addAttachmentWithID:linkPreview.imageAttachmentId in:transaction]; [self addAttachmentWithID:linkPreview.imageAttachmentId in:transaction];
TSAttachmentStream *linkPreviewAttachment = [TSAttachmentStream fetchObjectWithUniqueID:linkPreview.imageAttachmentId transaction:transaction]; TSAttachmentStream *attachment = [TSAttachmentStream fetchObjectWithUniqueID:linkPreview.imageAttachmentId transaction:transaction];
linkPreviewAttachment.albumMessageId = self.uniqueId; attachment.albumMessageId = self.uniqueId;
linkPreviewAttachment.isUploaded = true; attachment.isUploaded = true;
[linkPreviewAttachment saveWithTransaction:transaction]; [attachment saveWithTransaction:transaction];
}
} else { } else {
self.linkPreview = linkPreview; // Do nothing
[self saveWithTransaction:transaction];
} }
}]; }];
}); });
}];
return promise;
}
@throw error;
});
} }
@end @end

@ -1407,9 +1407,9 @@ NS_ASSUME_NONNULL_BEGIN
} }
dispatch_async(dispatch_get_main_queue(), ^{ dispatch_async(dispatch_get_main_queue(), ^{
NSString *url = [OWSLinkPreview previewURLForRawBodyText:incomingMessage.body]; NSString *linkPreviewURL = [OWSLinkPreview previewURLForRawBodyText:incomingMessage.body];
if (url != nil) { if (linkPreviewURL != nil) {
[incomingMessage generateLinkPreviewIfNeededFromURL:url]; [incomingMessage generateLinkPreviewIfNeededFromURL:linkPreviewURL];
} }
}); });

@ -1244,8 +1244,8 @@ NSString *const OWSMessageSenderRateLimitedException = @"RateLimitedException";
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(), ^{
NSString *url = [OWSLinkPreview previewURLForRawBodyText:message.body]; NSString *linkPreviewURL = [OWSLinkPreview previewURLForRawBodyText:message.body];
if (url) { [message generateLinkPreviewIfNeededFromURL:url]; } if (linkPreviewURL) { [message generateLinkPreviewIfNeededFromURL:linkPreviewURL]; }
}); });
} }

Loading…
Cancel
Save