diff --git a/SignalServiceKit/src/Loki/API/Public Chat/LokiPublicChatMessage.swift b/SignalServiceKit/src/Loki/API/Public Chat/LokiPublicChatMessage.swift index 9baf2e072..17bcc6a39 100644 --- a/SignalServiceKit/src/Loki/API/Public Chat/LokiPublicChatMessage.swift +++ b/SignalServiceKit/src/Loki/API/Public Chat/LokiPublicChatMessage.swift @@ -10,6 +10,7 @@ public final class LokiPublicChatMessage : NSObject { public let timestamp: UInt64 public let type: String public let quote: Quote? + public var attachments: [Attachment] = [] public let signature: Signature? @objc(serverID) @@ -17,6 +18,7 @@ public final class LokiPublicChatMessage : NSObject { // MARK: Settings private let signatureVersion: UInt64 = 1 + private let attachmentType = "net.app.core.oembed" // MARK: Types public struct Quote { @@ -26,6 +28,18 @@ public final class LokiPublicChatMessage : NSObject { public let quotedMessageServerID: UInt64? } + public struct Attachment { + public let kind: Kind + public let width: UInt + public let height: UInt + public let caption: String + public let url: String + public let server: String + public let serverDisplayName: String + + public enum Kind : String { case photo, video } + } + public struct Signature { public let data: Data public let version: UInt64 @@ -94,7 +108,12 @@ public final class LokiPublicChatMessage : NSObject { value["sigver"] = signature.version } let annotation: JSON = [ "type" : type, "value" : value ] - var result: JSON = [ "text" : body, "annotations": [ annotation ] ] + let attachmentAnnotations: [JSON] = self.attachments.map { attachment in + let attachmentValue: JSON = [ "version" : 1, "type" : attachment.kind.rawValue, "width" : attachment.width, "height" : attachment.height, + "title" : attachment.caption, "url" : attachment.url, "provider_name" : attachment.serverDisplayName, "provider_url" : attachment.server ] + return [ "type" : attachmentType, "value" : attachmentValue ] + } + var result: JSON = [ "text" : body, "annotations": [ annotation ] + attachmentAnnotations ] if let quotedMessageServerID = quote?.quotedMessageServerID { result["reply_to"] = quotedMessageServerID } @@ -102,6 +121,12 @@ public final class LokiPublicChatMessage : NSObject { } // MARK: Convenience + @objc public func addAttachment(kind: String, width: UInt, height: UInt, caption: String, url: String, server: String, serverDisplayName: String) { + guard let kind = Attachment.Kind(rawValue: kind) else { preconditionFailure() } + let attachment = Attachment(kind: kind, width: width, height: height, caption: caption, url: url, server: server, serverDisplayName: serverDisplayName) + attachments.append(attachment) + } + private func getValidationData(for signatureVersion: UInt64) -> Data? { var string = "\(body.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines))\(timestamp)" if let quote = quote { diff --git a/SignalServiceKit/src/Messages/OWSMessageSender.m b/SignalServiceKit/src/Messages/OWSMessageSender.m index d327ddeed..449ed90b1 100644 --- a/SignalServiceKit/src/Messages/OWSMessageSender.m +++ b/SignalServiceKit/src/Messages/OWSMessageSender.m @@ -1210,6 +1210,12 @@ NSString *const OWSMessageSenderRateLimitedException = @"RateLimitedException"; } LKGroupMessage *groupMessage = [[LKGroupMessage alloc] initWithHexEncodedPublicKey:userHexEncodedPublicKey displayName:displayName body:message.body type:LKPublicChatAPI.publicChatMessageType timestamp:message.timestamp quotedMessageTimestamp:quoteID quoteeHexEncodedPublicKey:quoteeHexEncodedPublicKey quotedMessageBody:quote.body quotedMessageServerID:quotedMessageServerID signatureData:nil signatureVersion:0]; + for (NSString *attachmentID in message.attachmentIds) { + TSAttachmentStream *attachment = [TSAttachmentStream fetchObjectWithUniqueID:attachmentID]; + if (attachment == nil) { continue; } + // TODO: Videos + [groupMessage addAttachmentWithKind:@"photo" width:@(attachment.imageSize.width).unsignedIntValue height:@(attachment.imageSize.height).unsignedIntValue caption:attachment.caption url:attachment.downloadURL server:publicChat.server serverDisplayName:publicChat.displayName]; + } [[LKPublicChatAPI sendMessage:groupMessage toGroup:publicChat.channel onServer:publicChat.server] .thenOn(OWSDispatch.sendingQueue, ^(LKGroupMessage *groupMessage) { [self.dbConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) {