Files
Morgan Pretty ceb6405bb6 Further progress wiring up Session Pro to Pro Settings
• Added some convenience code to clean up the DSL of consuming events
• Cleaned up some profile name logic
• Updated the code to use the `displayNameRetriever` approach where possible instead of resorting to a database query on the main thread
• Updated the ObservationBuilder to accept a stream (so we can observe events directly from a stream)
• Wired a bunch of the Session Pro Settings UI to the proper pro state
• Fixed a bug where the toggle wouldn't be tinted the right colour in SwiftUI screens
• Fixed a bug where mentions wouldn't work correctly
• Fixed a bug where mentions could crash when backspacing
• Fixed a bug where determining whether a string was RTL could cause lag
• Fixed a bug where the attachment and voice message buttons weren't visible in message requests
• Fixed a bug where the scroll to bottom button wouldn't work correctly
2025-11-25 14:33:59 +11:00

86 lines
2.6 KiB
Swift

// Copyright © 2022 Rangeproof Pty Ltd. All rights reserved.
import Foundation
import SessionUtilitiesKit
public extension VisibleMessage {
struct VMLinkPreview: Codable {
public let url: String
public let title: String?
public let attachmentId: String?
public let nonInsertedAttachment: Attachment?
public func validateMessage(isSending: Bool) throws {
if !url.isEmpty { throw MessageError.invalidMessage("url") }
}
// MARK: - Initialization
internal init(
url: String,
title: String?,
attachmentId: String?,
nonInsertedAttachment: Attachment?
) {
self.url = url
self.title = title
self.attachmentId = attachmentId
self.nonInsertedAttachment = nonInsertedAttachment
}
// MARK: - Proto Conversion
public static func fromProto(_ proto: SNProtoDataMessagePreview) -> VMLinkPreview? {
guard
!proto.url.isEmpty,
LinkPreviewManager.isValidLinkUrl(proto.url)
else { return nil }
return VMLinkPreview(
url: proto.url,
title: proto.title,
attachmentId: nil,
nonInsertedAttachment: proto.image.map { Attachment(proto: $0) }
)
}
public func toProto() -> SNProtoDataMessagePreview? {
let linkPreviewProto = SNProtoDataMessagePreview.builder(url: url)
if let title: String = title, !title.isEmpty { 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(
url: \(url),
title: \(title ?? "null"),
attachmentId: \(attachmentId ?? "null"),
nonInsertedAttachment: \(nonInsertedAttachment.map { "\($0)" } ?? "null")
)
"""
}
}
}
// MARK: - Database Type Conversion
public extension VisibleMessage.VMLinkPreview {
static func from(linkPreview: LinkPreview) -> VisibleMessage.VMLinkPreview {
return VisibleMessage.VMLinkPreview(
url: linkPreview.url,
title: linkPreview.title,
attachmentId: linkPreview.attachmentId,
nonInsertedAttachment: nil
)
}
}