session-ios/SessionMessagingKit/Messages/Visible Messages/VisibleMessage.swift

78 lines
3.3 KiB
Swift
Raw Normal View History

2020-11-09 00:58:47 +01:00
import SessionUtilitiesKit
2020-11-05 04:16:45 +01:00
@objc(SNVisibleMessage)
public final class VisibleMessage : Message {
2020-11-10 05:48:47 +01:00
@objc public var text: String?
@objc public var attachmentIDs: [String] = []
@objc public var quote: Quote?
@objc public var linkPreview: LinkPreview?
@objc public var contact: Contact?
@objc public var profile: Profile?
2020-11-06 04:58:47 +01:00
2020-11-06 06:08:37 +01:00
// MARK: Initialization
public override init() { super.init() }
2020-11-06 06:28:06 +01:00
// MARK: Validation
2020-11-06 06:31:56 +01:00
public override var isValid: Bool {
2020-11-17 06:23:13 +01:00
guard super.isValid else { return false }
2020-11-06 06:28:06 +01:00
if !attachmentIDs.isEmpty { return true }
if let text = text?.trimmingCharacters(in: .whitespacesAndNewlines), !text.isEmpty { return true }
return false
}
2020-11-06 04:58:47 +01:00
// MARK: Coding
public required init?(coder: NSCoder) {
super.init(coder: coder)
2020-11-08 22:15:44 +01:00
if let text = coder.decodeObject(forKey: "body") as! String? { self.text = text }
if let attachmentIDs = coder.decodeObject(forKey: "attachments") as! [String]? { self.attachmentIDs = attachmentIDs }
2020-11-06 04:58:47 +01:00
}
public override func encode(with coder: NSCoder) {
super.encode(with: coder)
2020-11-08 22:15:44 +01:00
coder.encode(text, forKey: "body")
coder.encode(attachmentIDs, forKey: "attachments")
2020-11-06 04:58:47 +01:00
}
// MARK: Proto Conversion
public override class func fromProto(_ proto: SNProtoContent) -> VisibleMessage? {
2020-11-06 06:08:37 +01:00
guard let dataMessage = proto.dataMessage else { return nil }
let result = VisibleMessage()
result.text = dataMessage.body
2020-11-20 01:10:53 +01:00
// Attachments are handled in MessageReceiver
2020-11-06 06:08:37 +01:00
if let quoteProto = dataMessage.quote, let quote = Quote.fromProto(quoteProto) { result.quote = quote }
if let linkPreviewProto = dataMessage.preview.first, let linkPreview = LinkPreview.fromProto(linkPreviewProto) { result.linkPreview = linkPreview }
// TODO: Contact
if let profile = Profile.fromProto(dataMessage) { result.profile = profile }
return result
2020-11-06 04:58:47 +01:00
}
public override func toProto() -> SNProtoContent? {
2020-11-06 06:18:08 +01:00
let proto = SNProtoContent.builder()
let dataMessage: SNProtoDataMessage.SNProtoDataMessageBuilder
if let profile = profile, let profileProto = profile.toProto() {
dataMessage = profileProto.asBuilder()
} else {
dataMessage = SNProtoDataMessage.builder()
}
if let text = text { dataMessage.setBody(text) }
2020-11-23 23:49:31 +01:00
let attachments = attachmentIDs.compactMap { TSAttachmentStream.fetch(uniqueId: $0) }
if !attachments.allSatisfy({ $0.isUploaded }) {
#if DEBUG
preconditionFailure("Sending a message before all associated attachments have been uploaded.")
#endif
}
let attachmentProtos = attachments.compactMap { TSAttachmentStream.buildProto(forAttachmentId: $0.uniqueId!) }
dataMessage.setAttachments(attachmentProtos)
2020-11-06 06:18:08 +01:00
if let quote = quote, let quoteProto = quote.toProto() { dataMessage.setQuote(quoteProto) }
if let linkPreview = linkPreview, let linkPreviewProto = linkPreview.toProto() { dataMessage.setPreview([ linkPreviewProto ]) }
// TODO: Contact
do {
proto.setDataMessage(try dataMessage.build())
return try proto.build()
} catch {
SNLog("Couldn't construct visible message proto from: \(self).")
return nil
}
2020-11-06 04:58:47 +01:00
}
2020-11-05 04:16:45 +01:00
}