You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
session-ios/SessionMessagingKit/Messages/Visible Messages/VisibleMessage+Quote.swift

110 lines
Swift

// Copyright © 2022 Rangeproof Pty Ltd. All rights reserved.
import Foundation
import GRDB
import SessionUtilitiesKit
public extension VisibleMessage {
struct VMQuote: Codable {
public let timestamp: UInt64?
public let publicKey: String?
public let text: String?
public let attachmentId: String?
4 years ago
public var isValid: Bool { timestamp != nil && publicKey != nil }
4 years ago
// MARK: - Initialization
internal init(timestamp: UInt64, publicKey: String, text: String?, attachmentId: String?) {
4 years ago
self.timestamp = timestamp
self.publicKey = publicKey
self.text = text
self.attachmentId = attachmentId
4 years ago
}
// MARK: - Proto Conversion
4 years ago
public static func fromProto(_ proto: SNProtoDataMessageQuote) -> VMQuote? {
return VMQuote(
timestamp: proto.id,
publicKey: proto.author,
text: proto.text,
attachmentId: nil
)
4 years ago
}
public func toProto() -> SNProtoDataMessageQuote? {
Merge branch 'feature/database-refactor' into emoji-reacts # Conflicts: # Session.xcodeproj/project.pbxproj # Session/Conversations/Context Menu/ContextMenuVC+Action.swift # Session/Conversations/Context Menu/ContextMenuVC+ActionView.swift # Session/Conversations/Context Menu/ContextMenuVC.swift # Session/Conversations/ConversationVC+Interaction.swift # Session/Conversations/ConversationVC.swift # Session/Conversations/ConversationViewItem.h # Session/Conversations/ConversationViewItem.m # Session/Conversations/Message Cells/Content Views/LinkPreviewView.swift # Session/Conversations/Message Cells/MessageCell.swift # Session/Conversations/Message Cells/VisibleMessageCell.swift # Session/Conversations/Views & Modals/BodyTextView.swift # Session/Meta/Translations/en.lproj/Localizable.strings # Session/Shared/UserCell.swift # SessionMessagingKit/Jobs/MessageSendJob.swift # SessionMessagingKit/Messages/Signal/TSMessage.h # SessionMessagingKit/Messages/Signal/TSMessage.m # SessionMessagingKit/Messages/Visible Messages/VisibleMessage.swift # SessionMessagingKit/Open Groups/OpenGroupAPIV2.swift # SessionMessagingKit/Sending & Receiving/MessageReceiver+Handling.swift # SessionMessagingKit/Sending & Receiving/Notifications/NotificationsProtocol.h # SessionMessagingKit/Sending & Receiving/Pollers/OpenGroupPollerV2.swift # SessionMessagingKit/Utilities/General.swift # SessionNotificationServiceExtension/NSENotificationPresenter.swift # SignalUtilitiesKit/Utilities/DisplayableText.swift # SignalUtilitiesKit/Utilities/NoopNotificationsManager.swift # SignalUtilitiesKit/Utilities/Notification+Loki.swift
2 years ago
preconditionFailure("Use toProto(_:) instead.")
}
public func toProto(_ db: Database) -> SNProtoDataMessageQuote? {
guard let timestamp = timestamp, let publicKey = publicKey else {
4 years ago
SNLog("Couldn't construct quote proto from: \(self).")
return nil
}
let quoteProto = SNProtoDataMessageQuote.builder(id: timestamp, author: publicKey)
if let text = text { quoteProto.setText(text) }
addAttachmentsIfNeeded(db, to: quoteProto)
4 years ago
do {
return try quoteProto.build()
} catch {
SNLog("Couldn't construct quote proto from: \(self).")
return nil
}
}
private func addAttachmentsIfNeeded(_ db: Database, to quoteProto: SNProtoDataMessageQuote.SNProtoDataMessageQuoteBuilder) {
guard let attachmentId = attachmentId else { return }
guard
let attachment: Attachment = try? Attachment.fetchOne(db, id: attachmentId),
attachment.state == .uploaded
else {
#if DEBUG
preconditionFailure("Sending a message before all associated attachments have been uploaded.")
#else
return
#endif
}
let quotedAttachmentProto = SNProtoDataMessageQuoteQuotedAttachment.builder()
quotedAttachmentProto.setContentType(attachment.contentType)
if let fileName = attachment.sourceFilename { quotedAttachmentProto.setFileName(fileName) }
guard let attachmentProto = attachment.buildProto() else {
return SNLog("Ignoring invalid attachment for quoted message.")
}
quotedAttachmentProto.setThumbnail(attachmentProto)
do {
try quoteProto.addAttachments(quotedAttachmentProto.build())
} catch {
SNLog("Couldn't construct quoted attachment proto from: \(self).")
}
}
// MARK: - Description
public var description: String {
"""
Quote(
timestamp: \(timestamp?.description ?? "null"),
publicKey: \(publicKey ?? "null"),
text: \(text ?? "null"),
attachmentId: \(attachmentId ?? "null")
)
"""
}
}
}
// MARK: - Database Type Conversion
public extension VisibleMessage.VMQuote {
static func from(_ db: Database, quote: Quote) -> VisibleMessage.VMQuote {
return VisibleMessage.VMQuote(
timestamp: UInt64(quote.timestampMs),
publicKey: quote.authorId,
text: quote.body,
attachmentId: quote.attachmentId
)
}
}