@ -532,16 +532,34 @@ extension ConversationVC:
let insertedInteraction : Interaction = try optimisticData . interaction . inserted ( db )
self ? . viewModel . associate ( optimisticMessageId : optimisticData . id , to : insertedInteraction . id )
// I f t h e r e i s a L i n k P r e v i e w a n d i t d o e s n ' t m a t c h a n e x i s t i n g o n e t h e n a d d i t n o w
if
let linkPreviewDraft : LinkPreviewDraft = optimisticData . linkPreviewDraft ,
( try ? insertedInteraction . linkPreview . isEmpty ( db ) ) = = true
{
try LinkPreview (
url : linkPreviewDraft . urlString ,
title : linkPreviewDraft . title ,
attachmentId : try optimisticData . linkPreviewAttachment ? . inserted ( db ) . id
) . insert ( db )
// I f t h e r e i s a L i n k P r e v i e w d r a f t t h e n c h e c k t h e s t a t e o f a n y e x i s t i n g l i n k p r e v i e w s a n d
// i n s e r t a n e w o n e i f n e e d e d
if let linkPreviewDraft : LinkPreviewDraft = optimisticData . linkPreviewDraft {
let invalidLinkPreviewAttachmentStates : [ Attachment . State ] = [
. failedDownload , . pendingDownload , . downloading , . failedUpload , . invalid
]
let linkPreviewAttachmentId : String ? = try ? insertedInteraction . linkPreview
. select ( . attachmentId )
. asRequest ( of : String . self )
. fetchOne ( db )
let linkPreviewAttachmentState : Attachment . State = linkPreviewAttachmentId
. map {
try ? Attachment
. filter ( id : $0 )
. select ( . state )
. asRequest ( of : Attachment . State . self )
. fetchOne ( db )
}
. defaulting ( to : . invalid )
// I f w e d o n ' t h a v e a " v a l i d " e x i s t i n g l i n k p r e v i e w t h e n u p s e r t a n e w o n e
if invalidLinkPreviewAttachmentStates . contains ( linkPreviewAttachmentState ) {
try LinkPreview (
url : linkPreviewDraft . urlString ,
title : linkPreviewDraft . title ,
attachmentId : try optimisticData . linkPreviewAttachment ? . inserted ( db ) . id
) . save ( db )
}
}
// I f t h e r e i s a Q u o t e t h e i n s e r t i t n o w