|
|
|
@ -1409,14 +1409,7 @@ NS_ASSUME_NONNULL_BEGIN
|
|
|
|
|
dispatch_async(dispatch_get_main_queue(), ^{
|
|
|
|
|
NSString *url = [OWSLinkPreview previewURLForRawBodyText:incomingMessage.body];
|
|
|
|
|
if (url != nil) {
|
|
|
|
|
[OWSLinkPreview tryToBuildPreviewInfoObjcWithPreviewUrl:url]
|
|
|
|
|
.thenOn(dispatch_get_main_queue(), ^(OWSLinkPreviewDraft *linkPreviewDraft) {
|
|
|
|
|
[OWSPrimaryStorage.sharedManager.dbReadWriteConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) {
|
|
|
|
|
OWSLinkPreview *linkPreview = [OWSLinkPreview buildValidatedLinkPreviewFromInfo:linkPreviewDraft transaction:transaction error:nil];
|
|
|
|
|
incomingMessage.linkPreview = linkPreview;
|
|
|
|
|
[incomingMessage saveWithTransaction:transaction];
|
|
|
|
|
}];
|
|
|
|
|
});
|
|
|
|
|
[self handleLinkPreviewsIfNeededWithUrl:url message:incomingMessage];
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
@ -1518,46 +1511,7 @@ NS_ASSUME_NONNULL_BEGIN
|
|
|
|
|
linkPreviewURL = [OWSLinkPreview previewURLForRawBodyText:incomingMessage.body];
|
|
|
|
|
}
|
|
|
|
|
if (linkPreviewURL != nil) {
|
|
|
|
|
[OWSLinkPreview tryToBuildPreviewInfoObjcWithPreviewUrl:linkPreviewURL]
|
|
|
|
|
.thenOn(dispatch_get_main_queue(), ^(OWSLinkPreviewDraft *linkPreviewDraft) {
|
|
|
|
|
[OWSPrimaryStorage.sharedManager.dbReadWriteConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) {
|
|
|
|
|
OWSLinkPreview *linkPreview = [OWSLinkPreview buildValidatedLinkPreviewFromInfo:linkPreviewDraft transaction:transaction error:nil];
|
|
|
|
|
incomingMessage.linkPreview = linkPreview;
|
|
|
|
|
[incomingMessage saveWithTransaction:transaction];
|
|
|
|
|
}];
|
|
|
|
|
})
|
|
|
|
|
.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 ([OWSLinkPreview isInvalidContentError:error]) {
|
|
|
|
|
__block AnyPromise *promise;
|
|
|
|
|
[OWSPrimaryStorage.sharedManager.dbReadWriteConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) {
|
|
|
|
|
promise = [OWSLinkPreview getImagePreviewFromUrl:linkPreviewURL transaction:transaction];
|
|
|
|
|
}];
|
|
|
|
|
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
|
|
|
|
|
[OWSPrimaryStorage.sharedManager.dbReadWriteConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) {
|
|
|
|
|
if (linkPreview.isDirectAttachment) {
|
|
|
|
|
if (!incomingMessage.hasAttachments) {
|
|
|
|
|
[incomingMessage addAttachmentId:linkPreview.imageAttachmentId transaction:transaction];
|
|
|
|
|
|
|
|
|
|
// Set the message id in attachment
|
|
|
|
|
TSAttachment *linkPreviewAttachment = [TSAttachment fetchObjectWithUniqueID:linkPreview.imageAttachmentId transaction:transaction];
|
|
|
|
|
linkPreviewAttachment.albumMessageId = incomingMessage.uniqueId;
|
|
|
|
|
[linkPreviewAttachment saveWithTransaction:transaction];
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
incomingMessage.linkPreview = linkPreview;
|
|
|
|
|
[incomingMessage saveWithTransaction:transaction];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}];
|
|
|
|
|
});
|
|
|
|
|
[self handleLinkPreviewsIfNeededWithUrl:linkPreviewURL message:incomingMessage];
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
@ -1565,6 +1519,49 @@ NS_ASSUME_NONNULL_BEGIN
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void)handleLinkPreviewsIfNeededWithUrl:(NSString *)linkPreviewURL message:(TSMessage *)message {
|
|
|
|
|
[OWSLinkPreview tryToBuildPreviewInfoObjcWithPreviewUrl:linkPreviewURL]
|
|
|
|
|
.thenOn(dispatch_get_main_queue(), ^(OWSLinkPreviewDraft *linkPreviewDraft) {
|
|
|
|
|
[OWSPrimaryStorage.sharedManager.dbReadWriteConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) {
|
|
|
|
|
OWSLinkPreview *linkPreview = [OWSLinkPreview buildValidatedLinkPreviewFromInfo:linkPreviewDraft transaction:transaction error:nil];
|
|
|
|
|
message.linkPreview = linkPreview;
|
|
|
|
|
[message saveWithTransaction:transaction];
|
|
|
|
|
}];
|
|
|
|
|
})
|
|
|
|
|
.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 ([OWSLinkPreview isInvalidContentError:error]) {
|
|
|
|
|
__block AnyPromise *promise;
|
|
|
|
|
[OWSPrimaryStorage.sharedManager.dbReadWriteConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) {
|
|
|
|
|
promise = [OWSLinkPreview getImagePreviewFromUrl:linkPreviewURL transaction:transaction];
|
|
|
|
|
}];
|
|
|
|
|
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
|
|
|
|
|
[OWSPrimaryStorage.sharedManager.dbReadWriteConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) {
|
|
|
|
|
if (linkPreview.isDirectAttachment) {
|
|
|
|
|
if (!message.hasAttachments) {
|
|
|
|
|
[message addAttachmentId:linkPreview.imageAttachmentId transaction:transaction];
|
|
|
|
|
|
|
|
|
|
// Set the message id in attachment
|
|
|
|
|
TSAttachment *linkPreviewAttachment = [TSAttachment fetchObjectWithUniqueID:linkPreview.imageAttachmentId transaction:transaction];
|
|
|
|
|
linkPreviewAttachment.albumMessageId = message.uniqueId;
|
|
|
|
|
[linkPreviewAttachment saveWithTransaction:transaction];
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
message.linkPreview = linkPreview;
|
|
|
|
|
[message saveWithTransaction:transaction];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}];
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// The difference between this function and `handleFriendRequestAcceptIfNeededWithEnvelope:` is that this will setup the incoming message for display to the user
|
|
|
|
|
// While `handleFriendRequestAcceptIfNeededWithEnvelope:` handles friend request accepting logic and doesn't need a message
|
|
|
|
|
- (void)handleFriendRequestMessageIfNeededWithEnvelope:(SSKProtoEnvelope *)envelope message:(TSIncomingMessage *)message thread:(TSThread *)thread transaction:(YapDatabaseReadWriteTransaction *)transaction {
|
|
|
|
|