Files
Morgan Pretty eddffae6a1 Refactored a number of processes to no longer require database access
• Refactored the MessageSender to no longer require database access
• Refactored the attachment upload process to no longer require database access
• Updated the OpenGroupAPI to no longer require database access
• Added back logic to send the `endCall` message when receiving a call while on another call
• Added `community` to `AuthenticationMethod` to help standardise the code behaviour
• Updated the `toProto` functions to no longer required a `Database` instance
• Updated a bunch of functions to be @MainActor (so we can ditch thread swapping code)
2025-06-03 08:51:05 +10:00

73 lines
2.2 KiB
Swift

// Copyright © 2022 Rangeproof Pty Ltd. All rights reserved.
import Foundation
import SessionUtilitiesKit
public extension VisibleMessage {
struct VMLinkPreview: Codable {
public let title: String?
public let url: String?
public let attachmentId: String?
public func isValid(isSending: Bool) -> Bool { title != nil && url != nil && attachmentId != nil }
// MARK: - Initialization
internal init(title: String?, url: String, attachmentId: String?) {
self.title = title
self.url = url
self.attachmentId = attachmentId
}
// MARK: - Proto Conversion
public static func fromProto(_ proto: SNProtoDataMessagePreview) -> VMLinkPreview? {
return VMLinkPreview(
title: proto.title,
url: proto.url,
attachmentId: nil
)
}
public func toProto() -> SNProtoDataMessagePreview? {
guard let url = url else {
Log.warn(.messageSender, "Couldn't construct link preview proto from: \(self).")
return nil
}
let linkPreviewProto = SNProtoDataMessagePreview.builder(url: url)
if let title = title { linkPreviewProto.setTitle(title) }
do {
return try linkPreviewProto.build()
} catch {
Log.warn(.messageSender, "Couldn't construct link preview proto from: \(self).")
return nil
}
}
// MARK: - Description
public var description: String {
"""
LinkPreview(
title: \(title ?? "null"),
url: \(url ?? "null"),
attachmentId: \(attachmentId ?? "null")
)
"""
}
}
}
// MARK: - Database Type Conversion
public extension VisibleMessage.VMLinkPreview {
static func from(linkPreview: LinkPreview) -> VisibleMessage.VMLinkPreview {
return VisibleMessage.VMLinkPreview(
title: linkPreview.title,
url: linkPreview.url,
attachmentId: linkPreview.attachmentId
)
}
}