diff --git a/SignalServiceKit/src/Loki/API/Public Chat/LokiPublicChatAPI.swift b/SignalServiceKit/src/Loki/API/Public Chat/LokiPublicChatAPI.swift index 7701c9722..b95bbeb2a 100644 --- a/SignalServiceKit/src/Loki/API/Public Chat/LokiPublicChatAPI.swift +++ b/SignalServiceKit/src/Loki/API/Public Chat/LokiPublicChatAPI.swift @@ -108,7 +108,7 @@ public final class LokiPublicChatAPI : LokiDotNetAPI { quote = nil } let signature = LokiPublicChatMessage.Signature(data: Data(hex: hexEncodedSignatureData), version: signatureVersion) - let result = LokiPublicChatMessage(serverID: serverID, hexEncodedPublicKey: hexEncodedPublicKey, displayName: displayName, body: body, type: publicChatMessageType, timestamp: timestamp, quote: quote, signature: signature) + let result = LokiPublicChatMessage(serverID: serverID, hexEncodedPublicKey: hexEncodedPublicKey, displayName: displayName, body: body, type: publicChatMessageType, timestamp: timestamp, quote: quote, attachments: [], signature: signature) guard result.hasValidSignature() else { print("[Loki] Ignoring public chat message with invalid signature.") return nil @@ -137,7 +137,7 @@ public final class LokiPublicChatAPI : LokiDotNetAPI { throw Error.parsingFailed } let timestamp = UInt64(date.timeIntervalSince1970) * 1000 - return LokiPublicChatMessage(serverID: serverID, hexEncodedPublicKey: userHexEncodedPublicKey, displayName: displayName, body: body, type: publicChatMessageType, timestamp: timestamp, quote: signedMessage.quote, signature: signedMessage.signature) + return LokiPublicChatMessage(serverID: serverID, hexEncodedPublicKey: userHexEncodedPublicKey, displayName: displayName, body: body, type: publicChatMessageType, timestamp: timestamp, quote: signedMessage.quote, attachments: signedMessage.attachments, signature: signedMessage.signature) } }.recover { error -> Promise in if let error = error as? NetworkManagerError, error.statusCode == 401 { diff --git a/SignalServiceKit/src/Loki/API/Public Chat/LokiPublicChatMessage.swift b/SignalServiceKit/src/Loki/API/Public Chat/LokiPublicChatMessage.swift index 4d94ff6bf..9a75cc5fe 100644 --- a/SignalServiceKit/src/Loki/API/Public Chat/LokiPublicChatMessage.swift +++ b/SignalServiceKit/src/Loki/API/Public Chat/LokiPublicChatMessage.swift @@ -33,7 +33,7 @@ public final class LokiPublicChatMessage : NSObject { public let kind: Kind public let width: UInt public let height: UInt - public let caption: String + public let caption: String? public let url: String public let server: String public let serverDisplayName: String @@ -47,7 +47,7 @@ public final class LokiPublicChatMessage : NSObject { } // MARK: Initialization - public init(serverID: UInt64?, hexEncodedPublicKey: String, displayName: String, body: String, type: String, timestamp: UInt64, quote: Quote?, signature: Signature?) { + public init(serverID: UInt64?, hexEncodedPublicKey: String, displayName: String, body: String, type: String, timestamp: UInt64, quote: Quote?, attachments: [Attachment], signature: Signature?) { self.serverID = serverID self.hexEncodedPublicKey = hexEncodedPublicKey self.displayName = displayName @@ -55,6 +55,7 @@ public final class LokiPublicChatMessage : NSObject { self.type = type self.timestamp = timestamp self.quote = quote + self.attachments = attachments self.signature = signature super.init() } @@ -73,7 +74,7 @@ public final class LokiPublicChatMessage : NSObject { } else { signature = nil } - self.init(serverID: nil, hexEncodedPublicKey: hexEncodedPublicKey, displayName: displayName, body: body, type: type, timestamp: timestamp, quote: quote, signature: signature) + self.init(serverID: nil, hexEncodedPublicKey: hexEncodedPublicKey, displayName: displayName, body: body, type: type, timestamp: timestamp, quote: quote, attachments: [], signature: signature) } // MARK: Crypto @@ -88,7 +89,7 @@ public final class LokiPublicChatMessage : NSObject { return nil } let signature = Signature(data: signatureData, version: signatureVersion) - return LokiPublicChatMessage(serverID: serverID, hexEncodedPublicKey: hexEncodedPublicKey, displayName: displayName, body: body, type: type, timestamp: timestamp, quote: quote, signature: signature) + return LokiPublicChatMessage(serverID: serverID, hexEncodedPublicKey: hexEncodedPublicKey, displayName: displayName, body: body, type: type, timestamp: timestamp, quote: quote, attachments: attachments, signature: signature) } internal func hasValidSignature() -> Bool { @@ -109,9 +110,12 @@ public final class LokiPublicChatMessage : NSObject { value["sigver"] = signature.version } let annotation: JSON = [ "type" : type, "value" : value ] - 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 ] + let attachmentAnnotations: [JSON] = attachments.map { attachment in + var attachmentValue: JSON = [ "version" : 1, "type" : attachment.kind.rawValue, "width" : attachment.width, "height" : attachment.height, + "url" : attachment.url, "provider_name" : attachment.serverDisplayName, "provider_url" : attachment.server ] + if let caption = attachment.caption { + attachmentValue["title"] = attachment.caption + } return [ "type" : attachmentType, "value" : attachmentValue ] } var result: JSON = [ "text" : body, "annotations": [ annotation ] + attachmentAnnotations ] @@ -122,7 +126,7 @@ public final class LokiPublicChatMessage : NSObject { } // MARK: Convenience - @objc public func addAttachment(serverID: UInt64, kind: String, width: UInt, height: UInt, caption: String, url: String, server: String, serverDisplayName: String) { + @objc public func addAttachment(serverID: UInt64, 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(serverID: serverID, kind: kind, width: width, height: height, caption: caption, url: url, server: server, serverDisplayName: serverDisplayName) attachments.append(attachment) diff --git a/SignalServiceKit/src/Messages/OWSMessageSender.m b/SignalServiceKit/src/Messages/OWSMessageSender.m index a2fe7b73f..6bd4b006f 100644 --- a/SignalServiceKit/src/Messages/OWSMessageSender.m +++ b/SignalServiceKit/src/Messages/OWSMessageSender.m @@ -1214,7 +1214,7 @@ NSString *const OWSMessageSenderRateLimitedException = @"RateLimitedException"; TSAttachmentStream *attachment = [TSAttachmentStream fetchObjectWithUniqueID:attachmentID]; if (attachment == nil) { continue; } // TODO: Videos - [groupMessage addAttachmentWithServerID:attachment.serverId kind:@"photo" width:@(attachment.imageSize.width).unsignedIntValue height:@(attachment.imageSize.height).unsignedIntValue caption:attachment.caption url:attachment.downloadURL server:publicChat.server serverDisplayName:publicChat.displayName]; + [groupMessage addAttachmentWithServerID:attachment.serverId kind:@"photo" width:@(attachment.imageSize.width).unsignedIntegerValue height:@(attachment.imageSize.height).unsignedIntegerValue 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) {