Include attachments in JSON

pull/60/head
Niels Andriesse 6 years ago
parent 4adf209b13
commit c144de002c

@ -10,6 +10,7 @@ public final class LokiPublicChatMessage : NSObject {
public let timestamp: UInt64 public let timestamp: UInt64
public let type: String public let type: String
public let quote: Quote? public let quote: Quote?
public var attachments: [Attachment] = []
public let signature: Signature? public let signature: Signature?
@objc(serverID) @objc(serverID)
@ -17,6 +18,7 @@ public final class LokiPublicChatMessage : NSObject {
// MARK: Settings // MARK: Settings
private let signatureVersion: UInt64 = 1 private let signatureVersion: UInt64 = 1
private let attachmentType = "net.app.core.oembed"
// MARK: Types // MARK: Types
public struct Quote { public struct Quote {
@ -26,6 +28,18 @@ public final class LokiPublicChatMessage : NSObject {
public let quotedMessageServerID: UInt64? 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 struct Signature {
public let data: Data public let data: Data
public let version: UInt64 public let version: UInt64
@ -94,7 +108,12 @@ public final class LokiPublicChatMessage : NSObject {
value["sigver"] = signature.version value["sigver"] = signature.version
} }
let annotation: JSON = [ "type" : type, "value" : value ] 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 { if let quotedMessageServerID = quote?.quotedMessageServerID {
result["reply_to"] = quotedMessageServerID result["reply_to"] = quotedMessageServerID
} }
@ -102,6 +121,12 @@ public final class LokiPublicChatMessage : NSObject {
} }
// MARK: Convenience // 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? { private func getValidationData(for signatureVersion: UInt64) -> Data? {
var string = "\(body.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines))\(timestamp)" var string = "\(body.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines))\(timestamp)"
if let quote = quote { if let quote = quote {

@ -1210,6 +1210,12 @@ NSString *const OWSMessageSenderRateLimitedException = @"RateLimitedException";
} }
LKGroupMessage *groupMessage = [[LKGroupMessage alloc] initWithHexEncodedPublicKey:userHexEncodedPublicKey displayName:displayName body:message.body type:LKPublicChatAPI.publicChatMessageType 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]; 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] [[LKPublicChatAPI sendMessage:groupMessage toGroup:publicChat.channel onServer:publicChat.server]
.thenOn(OWSDispatch.sendingQueue, ^(LKGroupMessage *groupMessage) { .thenOn(OWSDispatch.sendingQueue, ^(LKGroupMessage *groupMessage) {
[self.dbConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) { [self.dbConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) {

Loading…
Cancel
Save