pull/60/head
Niels Andriesse 6 years ago
parent 3ffb55fd0e
commit b05c890ab9

@ -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<LokiPublicChatMessage> in
if let error = error as? NetworkManagerError, error.statusCode == 401 {

@ -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)

@ -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) {

Loading…
Cancel
Save