|
|
|
@ -8,7 +8,7 @@ public final class LokiPublicChatAPI : DotNetAPI {
|
|
|
|
|
|
|
|
|
|
public static var displayNameUpdatees: [String:Set<String>] = [:]
|
|
|
|
|
|
|
|
|
|
internal static var useOnionRequests = true
|
|
|
|
|
public static var useOnionRequests = true
|
|
|
|
|
|
|
|
|
|
// MARK: Settings
|
|
|
|
|
private static let attachmentType = "net.app.core.oembed"
|
|
|
|
@ -31,6 +31,28 @@ public final class LokiPublicChatAPI : DotNetAPI {
|
|
|
|
|
@objc public static let lastMessageServerIDCollection = "LokiGroupChatLastMessageServerIDCollection"
|
|
|
|
|
@objc public static let lastDeletionServerIDCollection = "LokiGroupChatLastDeletionServerIDCollection"
|
|
|
|
|
|
|
|
|
|
@objc public static let openGroupPublicKeyCollection = "LokiGroupChatPublicKeyCollection"
|
|
|
|
|
|
|
|
|
|
private static func getOpenGroupPublicKey(on server: String) -> String? {
|
|
|
|
|
var result: String? = nil
|
|
|
|
|
storage.dbReadConnection.read { transaction in
|
|
|
|
|
result = transaction.object(forKey: "\(server)", inCollection: openGroupPublicKeyCollection) as! String?
|
|
|
|
|
}
|
|
|
|
|
return result
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static func setOpneGroupPublicKey(on server: String, value publicKey: String) {
|
|
|
|
|
try! Storage.writeSync { transaction in
|
|
|
|
|
transaction.setObject(publicKey, forKey: "\(server)", inCollection: openGroupPublicKeyCollection)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static func removeOpenGroupPublicKey(on server: String) {
|
|
|
|
|
try! Storage.writeSync { transaction in
|
|
|
|
|
transaction.removeObject(forKey: "\(server)", inCollection: openGroupPublicKeyCollection)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static func getLastMessageServerID(for group: UInt64, on server: String) -> UInt? {
|
|
|
|
|
var result: UInt? = nil
|
|
|
|
|
storage.dbReadConnection.read { transaction in
|
|
|
|
@ -74,6 +96,23 @@ public final class LokiPublicChatAPI : DotNetAPI {
|
|
|
|
|
public static func clearCaches(for channel: UInt64, on server: String) {
|
|
|
|
|
removeLastMessageServerID(for: channel, on: server)
|
|
|
|
|
removeLastDeletionServerID(for: channel, on: server)
|
|
|
|
|
removeOpenGroupPublicKey(on: server)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// MARK: Open Group Public Key Validation
|
|
|
|
|
public static func getOpenGroupServerPublicKey(on server: String) -> Promise<String> {
|
|
|
|
|
if let publicKey = getOpenGroupPublicKey(on: server) {
|
|
|
|
|
return Promise.value(publicKey)
|
|
|
|
|
} else {
|
|
|
|
|
return FileServerAPI.getOpenGroupKey(for: server).then2 { hexEncodedPublicKey -> Promise<String> in
|
|
|
|
|
let url = URL(string: server)!
|
|
|
|
|
let request = TSRequest(url: url)
|
|
|
|
|
return OnionRequestAPI.sendOnionRequestLsrpcDest(request, server: server, using: hexEncodedPublicKey, noJSON: true).map2 { _ -> String in
|
|
|
|
|
setOpneGroupPublicKey(on: server, value: hexEncodedPublicKey)
|
|
|
|
|
return hexEncodedPublicKey
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// MARK: Receiving
|
|
|
|
@ -83,6 +122,78 @@ public final class LokiPublicChatAPI : DotNetAPI {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static func getMessages(for channel: UInt64, on server: String) -> Promise<[LokiPublicChatMessage]> {
|
|
|
|
|
|
|
|
|
|
func handleMessages(rawResponse: Any) throws -> [LokiPublicChatMessage] {
|
|
|
|
|
guard let json = rawResponse as? JSON, let rawMessages = json["data"] as? [JSON] else {
|
|
|
|
|
print("[Loki] Couldn't parse messages for public chat channel with ID: \(channel) on server: \(server) from: \(rawResponse).")
|
|
|
|
|
throw DotNetAPIError.parsingFailed
|
|
|
|
|
}
|
|
|
|
|
return rawMessages.flatMap { message in
|
|
|
|
|
let isDeleted = (message["is_deleted"] as? Int == 1)
|
|
|
|
|
guard !isDeleted else { return nil }
|
|
|
|
|
guard let annotations = message["annotations"] as? [JSON], let annotation = annotations.first(where: { $0["type"] as? String == publicChatMessageType }), let value = annotation["value"] as? JSON,
|
|
|
|
|
let serverID = message["id"] as? UInt64, let hexEncodedSignatureData = value["sig"] as? String, let signatureVersion = value["sigver"] as? UInt64,
|
|
|
|
|
let body = message["text"] as? String, let user = message["user"] as? JSON, let hexEncodedPublicKey = user["username"] as? String,
|
|
|
|
|
let timestamp = value["timestamp"] as? UInt64 else {
|
|
|
|
|
print("[Loki] Couldn't parse message for public chat channel with ID: \(channel) on server: \(server) from: \(message).")
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
var profilePicture: LokiPublicChatMessage.ProfilePicture? = nil
|
|
|
|
|
let displayName = user["name"] as? String ?? NSLocalizedString("Anonymous", comment: "")
|
|
|
|
|
if let userAnnotations = user["annotations"] as? [JSON], let profilePictureAnnotation = userAnnotations.first(where: { $0["type"] as? String == profilePictureType }),
|
|
|
|
|
let profilePictureValue = profilePictureAnnotation["value"] as? JSON, let profileKeyString = profilePictureValue["profileKey"] as? String, let profileKey = Data(base64Encoded: profileKeyString), let url = profilePictureValue["url"] as? String {
|
|
|
|
|
profilePicture = LokiPublicChatMessage.ProfilePicture(profileKey: profileKey, url: url)
|
|
|
|
|
}
|
|
|
|
|
let lastMessageServerID = getLastMessageServerID(for: channel, on: server)
|
|
|
|
|
if serverID > (lastMessageServerID ?? 0) { setLastMessageServerID(for: channel, on: server, to: serverID) }
|
|
|
|
|
let quote: LokiPublicChatMessage.Quote?
|
|
|
|
|
if let quoteAsJSON = value["quote"] as? JSON, let quotedMessageTimestamp = quoteAsJSON["id"] as? UInt64, let quoteeHexEncodedPublicKey = quoteAsJSON["author"] as? String,
|
|
|
|
|
let quotedMessageBody = quoteAsJSON["text"] as? String {
|
|
|
|
|
let quotedMessageServerID = message["reply_to"] as? UInt64
|
|
|
|
|
quote = LokiPublicChatMessage.Quote(quotedMessageTimestamp: quotedMessageTimestamp, quoteeHexEncodedPublicKey: quoteeHexEncodedPublicKey, quotedMessageBody: quotedMessageBody,
|
|
|
|
|
quotedMessageServerID: quotedMessageServerID)
|
|
|
|
|
} else {
|
|
|
|
|
quote = nil
|
|
|
|
|
}
|
|
|
|
|
let signature = LokiPublicChatMessage.Signature(data: Data(hex: hexEncodedSignatureData), version: signatureVersion)
|
|
|
|
|
let attachmentsAsJSON = annotations.filter { $0["type"] as? String == attachmentType }
|
|
|
|
|
let attachments: [LokiPublicChatMessage.Attachment] = attachmentsAsJSON.compactMap { attachmentAsJSON in
|
|
|
|
|
guard let value = attachmentAsJSON["value"] as? JSON, let kindAsString = value["lokiType"] as? String, let kind = LokiPublicChatMessage.Attachment.Kind(rawValue: kindAsString),
|
|
|
|
|
let serverID = value["id"] as? UInt64, let contentType = value["contentType"] as? String, let size = value["size"] as? UInt, let url = value["url"] as? String else { return nil }
|
|
|
|
|
let fileName = value["fileName"] as? String ?? UUID().description
|
|
|
|
|
let width = value["width"] as? UInt ?? 0
|
|
|
|
|
let height = value["height"] as? UInt ?? 0
|
|
|
|
|
let flags = (value["flags"] as? UInt) ?? 0
|
|
|
|
|
let caption = value["caption"] as? String
|
|
|
|
|
let linkPreviewURL = value["linkPreviewUrl"] as? String
|
|
|
|
|
let linkPreviewTitle = value["linkPreviewTitle"] as? String
|
|
|
|
|
if kind == .linkPreview {
|
|
|
|
|
guard linkPreviewURL != nil && linkPreviewTitle != nil else {
|
|
|
|
|
print("[Loki] Ignoring public chat message with invalid link preview.")
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return LokiPublicChatMessage.Attachment(kind: kind, server: server, serverID: serverID, contentType: contentType, size: size, fileName: fileName, flags: flags,
|
|
|
|
|
width: width, height: height, caption: caption, url: url, linkPreviewURL: linkPreviewURL, linkPreviewTitle: linkPreviewTitle)
|
|
|
|
|
}
|
|
|
|
|
let result = LokiPublicChatMessage(serverID: serverID, hexEncodedPublicKey: hexEncodedPublicKey, displayName: displayName, profilePicture: profilePicture,
|
|
|
|
|
body: body, type: publicChatMessageType, timestamp: timestamp, quote: quote, attachments: attachments, signature: signature)
|
|
|
|
|
guard result.hasValidSignature() else {
|
|
|
|
|
print("[Loki] Ignoring public chat message with invalid signature.")
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
var existingMessageID: String? = nil
|
|
|
|
|
storage.dbReadConnection.read { transaction in
|
|
|
|
|
existingMessageID = storage.getIDForMessage(withServerID: UInt(result.serverID!), in: transaction)
|
|
|
|
|
}
|
|
|
|
|
guard existingMessageID == nil else {
|
|
|
|
|
print("[Loki] Ignoring duplicate public chat message.")
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
return result
|
|
|
|
|
}.sorted { $0.timestamp < $1.timestamp }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var queryParameters = "include_annotations=1"
|
|
|
|
|
if let lastMessageServerID = getLastMessageServerID(for: channel, on: server) {
|
|
|
|
|
queryParameters += "&since_id=\(lastMessageServerID)"
|
|
|
|
@ -93,75 +204,15 @@ public final class LokiPublicChatAPI : DotNetAPI {
|
|
|
|
|
let url = URL(string: "\(server)/channels/\(channel)/messages?\(queryParameters)")!
|
|
|
|
|
let request = TSRequest(url: url)
|
|
|
|
|
request.allHTTPHeaderFields = [ "Content-Type" : "application/json", "Authorization" : "Bearer \(token)" ]
|
|
|
|
|
return LokiFileServerProxy(for: server).perform(request).map(on: DispatchQueue.global(qos: .default)) { rawResponse in
|
|
|
|
|
guard let json = rawResponse as? JSON, let rawMessages = json["data"] as? [JSON] else {
|
|
|
|
|
print("[Loki] Couldn't parse messages for public chat channel with ID: \(channel) on server: \(server) from: \(rawResponse).")
|
|
|
|
|
throw DotNetAPIError.parsingFailed
|
|
|
|
|
}
|
|
|
|
|
return rawMessages.flatMap { message in
|
|
|
|
|
let isDeleted = (message["is_deleted"] as? Int == 1)
|
|
|
|
|
guard !isDeleted else { return nil }
|
|
|
|
|
guard let annotations = message["annotations"] as? [JSON], let annotation = annotations.first(where: { $0["type"] as? String == publicChatMessageType }), let value = annotation["value"] as? JSON,
|
|
|
|
|
let serverID = message["id"] as? UInt64, let hexEncodedSignatureData = value["sig"] as? String, let signatureVersion = value["sigver"] as? UInt64,
|
|
|
|
|
let body = message["text"] as? String, let user = message["user"] as? JSON, let hexEncodedPublicKey = user["username"] as? String,
|
|
|
|
|
let timestamp = value["timestamp"] as? UInt64 else {
|
|
|
|
|
print("[Loki] Couldn't parse message for public chat channel with ID: \(channel) on server: \(server) from: \(message).")
|
|
|
|
|
return nil
|
|
|
|
|
if (useOnionRequests) {
|
|
|
|
|
return getOpenGroupServerPublicKey(on: server).then2 { hexEncodedPublickey in
|
|
|
|
|
OnionRequestAPI.sendOnionRequestLsrpcDest(request, server: server, using: hexEncodedPublickey).map2 { rawResponse in
|
|
|
|
|
return try handleMessages(rawResponse: rawResponse)
|
|
|
|
|
}
|
|
|
|
|
var profilePicture: LokiPublicChatMessage.ProfilePicture? = nil
|
|
|
|
|
let displayName = user["name"] as? String ?? NSLocalizedString("Anonymous", comment: "")
|
|
|
|
|
if let userAnnotations = user["annotations"] as? [JSON], let profilePictureAnnotation = userAnnotations.first(where: { $0["type"] as? String == profilePictureType }),
|
|
|
|
|
let profilePictureValue = profilePictureAnnotation["value"] as? JSON, let profileKeyString = profilePictureValue["profileKey"] as? String, let profileKey = Data(base64Encoded: profileKeyString), let url = profilePictureValue["url"] as? String {
|
|
|
|
|
profilePicture = LokiPublicChatMessage.ProfilePicture(profileKey: profileKey, url: url)
|
|
|
|
|
}
|
|
|
|
|
let lastMessageServerID = getLastMessageServerID(for: channel, on: server)
|
|
|
|
|
if serverID > (lastMessageServerID ?? 0) { setLastMessageServerID(for: channel, on: server, to: serverID) }
|
|
|
|
|
let quote: LokiPublicChatMessage.Quote?
|
|
|
|
|
if let quoteAsJSON = value["quote"] as? JSON, let quotedMessageTimestamp = quoteAsJSON["id"] as? UInt64, let quoteeHexEncodedPublicKey = quoteAsJSON["author"] as? String,
|
|
|
|
|
let quotedMessageBody = quoteAsJSON["text"] as? String {
|
|
|
|
|
let quotedMessageServerID = message["reply_to"] as? UInt64
|
|
|
|
|
quote = LokiPublicChatMessage.Quote(quotedMessageTimestamp: quotedMessageTimestamp, quoteeHexEncodedPublicKey: quoteeHexEncodedPublicKey, quotedMessageBody: quotedMessageBody,
|
|
|
|
|
quotedMessageServerID: quotedMessageServerID)
|
|
|
|
|
} else {
|
|
|
|
|
quote = nil
|
|
|
|
|
}
|
|
|
|
|
let signature = LokiPublicChatMessage.Signature(data: Data(hex: hexEncodedSignatureData), version: signatureVersion)
|
|
|
|
|
let attachmentsAsJSON = annotations.filter { $0["type"] as? String == attachmentType }
|
|
|
|
|
let attachments: [LokiPublicChatMessage.Attachment] = attachmentsAsJSON.compactMap { attachmentAsJSON in
|
|
|
|
|
guard let value = attachmentAsJSON["value"] as? JSON, let kindAsString = value["lokiType"] as? String, let kind = LokiPublicChatMessage.Attachment.Kind(rawValue: kindAsString),
|
|
|
|
|
let serverID = value["id"] as? UInt64, let contentType = value["contentType"] as? String, let size = value["size"] as? UInt, let url = value["url"] as? String else { return nil }
|
|
|
|
|
let fileName = value["fileName"] as? String ?? UUID().description
|
|
|
|
|
let width = value["width"] as? UInt ?? 0
|
|
|
|
|
let height = value["height"] as? UInt ?? 0
|
|
|
|
|
let flags = (value["flags"] as? UInt) ?? 0
|
|
|
|
|
let caption = value["caption"] as? String
|
|
|
|
|
let linkPreviewURL = value["linkPreviewUrl"] as? String
|
|
|
|
|
let linkPreviewTitle = value["linkPreviewTitle"] as? String
|
|
|
|
|
if kind == .linkPreview {
|
|
|
|
|
guard linkPreviewURL != nil && linkPreviewTitle != nil else {
|
|
|
|
|
print("[Loki] Ignoring public chat message with invalid link preview.")
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return LokiPublicChatMessage.Attachment(kind: kind, server: server, serverID: serverID, contentType: contentType, size: size, fileName: fileName, flags: flags,
|
|
|
|
|
width: width, height: height, caption: caption, url: url, linkPreviewURL: linkPreviewURL, linkPreviewTitle: linkPreviewTitle)
|
|
|
|
|
}
|
|
|
|
|
let result = LokiPublicChatMessage(serverID: serverID, hexEncodedPublicKey: hexEncodedPublicKey, displayName: displayName, profilePicture: profilePicture,
|
|
|
|
|
body: body, type: publicChatMessageType, timestamp: timestamp, quote: quote, attachments: attachments, signature: signature)
|
|
|
|
|
guard result.hasValidSignature() else {
|
|
|
|
|
print("[Loki] Ignoring public chat message with invalid signature.")
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
var existingMessageID: String? = nil
|
|
|
|
|
storage.dbReadConnection.read { transaction in
|
|
|
|
|
existingMessageID = storage.getIDForMessage(withServerID: UInt(result.serverID!), in: transaction)
|
|
|
|
|
}
|
|
|
|
|
guard existingMessageID == nil else {
|
|
|
|
|
print("[Loki] Ignoring duplicate public chat message.")
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
return result
|
|
|
|
|
}.sorted { $0.timestamp < $1.timestamp }
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return LokiFileServerProxy(for: server).perform(request).map(on: DispatchQueue.global(qos: .default)) { rawResponse in
|
|
|
|
|
return try handleMessages(rawResponse: rawResponse)
|
|
|
|
|
}
|
|
|
|
|
}.handlingInvalidAuthTokenIfNeeded(for: server)
|
|
|
|
|
}
|
|
|
|
@ -173,6 +224,20 @@ public final class LokiPublicChatAPI : DotNetAPI {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static func sendMessage(_ message: LokiPublicChatMessage, to channel: UInt64, on server: String) -> Promise<LokiPublicChatMessage> {
|
|
|
|
|
|
|
|
|
|
func handleSendMessageResult(rawResponse: Any, with displayName: String, for signedMessage: LokiPublicChatMessage) throws -> LokiPublicChatMessage {
|
|
|
|
|
// ISO8601DateFormatter doesn't support milliseconds before iOS 11
|
|
|
|
|
let dateFormatter = DateFormatter()
|
|
|
|
|
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZ"
|
|
|
|
|
guard let json = rawResponse as? JSON, let messageAsJSON = json["data"] as? JSON, let serverID = messageAsJSON["id"] as? UInt64, let body = messageAsJSON["text"] as? String,
|
|
|
|
|
let dateAsString = messageAsJSON["created_at"] as? String, let date = dateFormatter.date(from: dateAsString) else {
|
|
|
|
|
print("[Loki] Couldn't parse message for public chat channel with ID: \(channel) on server: \(server) from: \(rawResponse).")
|
|
|
|
|
throw DotNetAPIError.parsingFailed
|
|
|
|
|
}
|
|
|
|
|
let timestamp = UInt64(date.timeIntervalSince1970) * 1000
|
|
|
|
|
return LokiPublicChatMessage(serverID: serverID, hexEncodedPublicKey: getUserHexEncodedPublicKey(), displayName: displayName, profilePicture: signedMessage.profilePicture, body: body, type: publicChatMessageType, timestamp: timestamp, quote: signedMessage.quote, attachments: signedMessage.attachments, signature: signedMessage.signature)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
print("[Loki] Sending message to public chat channel with ID: \(channel) on server: \(server).")
|
|
|
|
|
let (promise, seal) = Promise<LokiPublicChatMessage>.pending()
|
|
|
|
|
DispatchQueue.global(qos: .userInitiated).async { [privateKey = userKeyPair.privateKey] in
|
|
|
|
@ -184,17 +249,15 @@ public final class LokiPublicChatAPI : DotNetAPI {
|
|
|
|
|
let request = TSRequest(url: url, method: "POST", parameters: parameters)
|
|
|
|
|
request.allHTTPHeaderFields = [ "Content-Type" : "application/json", "Authorization" : "Bearer \(token)" ]
|
|
|
|
|
let displayName = userDisplayName
|
|
|
|
|
return LokiFileServerProxy(for: server).perform(request).map(on: DispatchQueue.global(qos: .default)) { rawResponse in
|
|
|
|
|
// ISO8601DateFormatter doesn't support milliseconds before iOS 11
|
|
|
|
|
let dateFormatter = DateFormatter()
|
|
|
|
|
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZ"
|
|
|
|
|
guard let json = rawResponse as? JSON, let messageAsJSON = json["data"] as? JSON, let serverID = messageAsJSON["id"] as? UInt64, let body = messageAsJSON["text"] as? String,
|
|
|
|
|
let dateAsString = messageAsJSON["created_at"] as? String, let date = dateFormatter.date(from: dateAsString) else {
|
|
|
|
|
print("[Loki] Couldn't parse message for public chat channel with ID: \(channel) on server: \(server) from: \(rawResponse).")
|
|
|
|
|
throw DotNetAPIError.parsingFailed
|
|
|
|
|
if (useOnionRequests) {
|
|
|
|
|
return getOpenGroupServerPublicKey(on: server).then2 { hexEncodedPublicKey in
|
|
|
|
|
OnionRequestAPI.sendOnionRequestLsrpcDest(request, server: server, using: hexEncodedPublicKey).map2 { rawResponse in
|
|
|
|
|
return try handleSendMessageResult(rawResponse: rawResponse, with: displayName, for: signedMessage)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
let timestamp = UInt64(date.timeIntervalSince1970) * 1000
|
|
|
|
|
return LokiPublicChatMessage(serverID: serverID, hexEncodedPublicKey: getUserHexEncodedPublicKey(), displayName: displayName, profilePicture: signedMessage.profilePicture, body: body, type: publicChatMessageType, timestamp: timestamp, quote: signedMessage.quote, attachments: signedMessage.attachments, signature: signedMessage.signature)
|
|
|
|
|
}
|
|
|
|
|
return LokiFileServerProxy(for: server).perform(request).map(on: DispatchQueue.global(qos: .default)) { rawResponse in
|
|
|
|
|
return try handleSendMessageResult(rawResponse: rawResponse, with: displayName, for: signedMessage)
|
|
|
|
|
}
|
|
|
|
|
}.handlingInvalidAuthTokenIfNeeded(for: server)
|
|
|
|
|
}.done(on: DispatchQueue.global(qos: .default)) { message in
|
|
|
|
@ -208,6 +271,23 @@ public final class LokiPublicChatAPI : DotNetAPI {
|
|
|
|
|
|
|
|
|
|
// MARK: Deletion
|
|
|
|
|
public static func getDeletedMessageServerIDs(for channel: UInt64, on server: String) -> Promise<[UInt64]> {
|
|
|
|
|
|
|
|
|
|
func handleDeletedMessageServerIDs(rawResponse: Any) throws -> [UInt64] {
|
|
|
|
|
guard let json = rawResponse as? JSON, let deletions = json["data"] as? [JSON] else {
|
|
|
|
|
print("[Loki] Couldn't parse deleted messages for public chat channel with ID: \(channel) on server: \(server) from: \(rawResponse).")
|
|
|
|
|
throw DotNetAPIError.parsingFailed
|
|
|
|
|
}
|
|
|
|
|
return deletions.flatMap { deletion in
|
|
|
|
|
guard let serverID = deletion["id"] as? UInt64, let messageServerID = deletion["message_id"] as? UInt64 else {
|
|
|
|
|
print("[Loki] Couldn't parse deleted message for public chat channel with ID: \(channel) on server: \(server) from: \(deletion).")
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
let lastDeletionServerID = getLastDeletionServerID(for: channel, on: server)
|
|
|
|
|
if serverID > (lastDeletionServerID ?? 0) { setLastDeletionServerID(for: channel, on: server, to: serverID) }
|
|
|
|
|
return messageServerID
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
print("[Loki] Getting deleted messages for public chat channel with ID: \(channel) on server: \(server).")
|
|
|
|
|
let queryParameters: String
|
|
|
|
|
if let lastDeletionServerID = getLastDeletionServerID(for: channel, on: server) {
|
|
|
|
@ -219,21 +299,16 @@ public final class LokiPublicChatAPI : DotNetAPI {
|
|
|
|
|
let url = URL(string: "\(server)/loki/v1/channel/\(channel)/deletes?\(queryParameters)")!
|
|
|
|
|
let request = TSRequest(url: url)
|
|
|
|
|
request.allHTTPHeaderFields = [ "Content-Type" : "application/json", "Authorization" : "Bearer \(token)" ]
|
|
|
|
|
return LokiFileServerProxy(for: server).perform(request).map(on: DispatchQueue.global(qos: .default)) { rawResponse in
|
|
|
|
|
guard let json = rawResponse as? JSON, let deletions = json["data"] as? [JSON] else {
|
|
|
|
|
print("[Loki] Couldn't parse deleted messages for public chat channel with ID: \(channel) on server: \(server) from: \(rawResponse).")
|
|
|
|
|
throw DotNetAPIError.parsingFailed
|
|
|
|
|
}
|
|
|
|
|
return deletions.flatMap { deletion in
|
|
|
|
|
guard let serverID = deletion["id"] as? UInt64, let messageServerID = deletion["message_id"] as? UInt64 else {
|
|
|
|
|
print("[Loki] Couldn't parse deleted message for public chat channel with ID: \(channel) on server: \(server) from: \(deletion).")
|
|
|
|
|
return nil
|
|
|
|
|
if (useOnionRequests) {
|
|
|
|
|
return getOpenGroupServerPublicKey(on: server).then2 { hexEncodedPublicKey in
|
|
|
|
|
OnionRequestAPI.sendOnionRequestLsrpcDest(request, server: server, using: hexEncodedPublicKey).map2 { rawResponse in
|
|
|
|
|
return try handleDeletedMessageServerIDs(rawResponse: rawResponse)
|
|
|
|
|
}
|
|
|
|
|
let lastDeletionServerID = getLastDeletionServerID(for: channel, on: server)
|
|
|
|
|
if serverID > (lastDeletionServerID ?? 0) { setLastDeletionServerID(for: channel, on: server, to: serverID) }
|
|
|
|
|
return messageServerID
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return LokiFileServerProxy(for: server).perform(request).map(on: DispatchQueue.global(qos: .default)) { rawResponse in
|
|
|
|
|
return try handleDeletedMessageServerIDs(rawResponse: rawResponse)
|
|
|
|
|
}
|
|
|
|
|
}.handlingInvalidAuthTokenIfNeeded(for: server)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -251,6 +326,13 @@ public final class LokiPublicChatAPI : DotNetAPI {
|
|
|
|
|
let url = URL(string: urlAsString)!
|
|
|
|
|
let request = TSRequest(url: url, method: "DELETE", parameters: [:])
|
|
|
|
|
request.allHTTPHeaderFields = [ "Content-Type" : "application/json", "Authorization" : "Bearer \(token)" ]
|
|
|
|
|
if (useOnionRequests) {
|
|
|
|
|
return getOpenGroupServerPublicKey(on: server).then2 { hexEncodedPublicKey in
|
|
|
|
|
OnionRequestAPI.sendOnionRequestLsrpcDest(request, server: server, using: hexEncodedPublicKey).done2 { result -> Void in
|
|
|
|
|
print("[Loki] Deleted message with ID: \(messageID) on server: \(server).")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return LokiFileServerProxy(for: server).perform(request).done(on: DispatchQueue.global(qos: .default)) { result -> Void in
|
|
|
|
|
print("[Loki] Deleted message with ID: \(messageID) on server: \(server).")
|
|
|
|
|
}
|
|
|
|
@ -260,6 +342,23 @@ public final class LokiPublicChatAPI : DotNetAPI {
|
|
|
|
|
|
|
|
|
|
// MARK: Display Name & Profile Picture
|
|
|
|
|
public static func getDisplayNames(for channel: UInt64, on server: String) -> Promise<Void> {
|
|
|
|
|
|
|
|
|
|
func handleDisplayNames(rawResponse: Any, for hexEncodedPublicKeys: Set<String>) throws {
|
|
|
|
|
guard let json = rawResponse as? JSON, let data = json["data"] as? [JSON] else {
|
|
|
|
|
print("[Loki] Couldn't parse display names for users: \(hexEncodedPublicKeys) from: \(rawResponse).")
|
|
|
|
|
throw DotNetAPIError.parsingFailed
|
|
|
|
|
}
|
|
|
|
|
try! Storage.writeSync { transaction in
|
|
|
|
|
data.forEach { data in
|
|
|
|
|
guard let user = data["user"] as? JSON, let hexEncodedPublicKey = user["username"] as? String, let rawDisplayName = user["name"] as? String else { return }
|
|
|
|
|
let endIndex = hexEncodedPublicKey.endIndex
|
|
|
|
|
let cutoffIndex = hexEncodedPublicKey.index(endIndex, offsetBy: -8)
|
|
|
|
|
let displayName = "\(rawDisplayName) (...\(hexEncodedPublicKey[cutoffIndex..<endIndex]))"
|
|
|
|
|
transaction.setObject(displayName, forKey: hexEncodedPublicKey, inCollection: "\(server).\(channel)")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let publicChatID = "\(server).\(channel)"
|
|
|
|
|
guard let hexEncodedPublicKeys = displayNameUpdatees[publicChatID] else { return Promise.value(()) }
|
|
|
|
|
displayNameUpdatees[publicChatID] = []
|
|
|
|
@ -268,21 +367,16 @@ public final class LokiPublicChatAPI : DotNetAPI {
|
|
|
|
|
let queryParameters = "ids=\(hexEncodedPublicKeys.map { "@\($0)" }.joined(separator: ","))&include_user_annotations=1"
|
|
|
|
|
let url = URL(string: "\(server)/users?\(queryParameters)")!
|
|
|
|
|
let request = TSRequest(url: url)
|
|
|
|
|
return LokiFileServerProxy(for: server).perform(request).map(on: DispatchQueue.global(qos: .default)) { rawResponse in
|
|
|
|
|
guard let json = rawResponse as? JSON, let data = json["data"] as? [JSON] else {
|
|
|
|
|
print("[Loki] Couldn't parse display names for users: \(hexEncodedPublicKeys) from: \(rawResponse).")
|
|
|
|
|
throw DotNetAPIError.parsingFailed
|
|
|
|
|
}
|
|
|
|
|
try! Storage.writeSync { transaction in
|
|
|
|
|
data.forEach { data in
|
|
|
|
|
guard let user = data["user"] as? JSON, let hexEncodedPublicKey = user["username"] as? String, let rawDisplayName = user["name"] as? String else { return }
|
|
|
|
|
let endIndex = hexEncodedPublicKey.endIndex
|
|
|
|
|
let cutoffIndex = hexEncodedPublicKey.index(endIndex, offsetBy: -8)
|
|
|
|
|
let displayName = "\(rawDisplayName) (...\(hexEncodedPublicKey[cutoffIndex..<endIndex]))"
|
|
|
|
|
transaction.setObject(displayName, forKey: hexEncodedPublicKey, inCollection: "\(server).\(channel)")
|
|
|
|
|
if (useOnionRequests) {
|
|
|
|
|
return getOpenGroupServerPublicKey(on: server).then2 { hexEncodedPublicKey in
|
|
|
|
|
OnionRequestAPI.sendOnionRequestLsrpcDest(request, server: server, using: hexEncodedPublicKey).map2 { rawResponse in
|
|
|
|
|
try handleDisplayNames(rawResponse: rawResponse, for: hexEncodedPublicKeys)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return LokiFileServerProxy(for: server).perform(request).map(on: DispatchQueue.global(qos: .default)) { rawResponse in
|
|
|
|
|
try handleDisplayNames(rawResponse: rawResponse, for: hexEncodedPublicKeys)
|
|
|
|
|
}
|
|
|
|
|
}.handlingInvalidAuthTokenIfNeeded(for: server)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -299,6 +393,14 @@ public final class LokiPublicChatAPI : DotNetAPI {
|
|
|
|
|
let url = URL(string: "\(server)/users/me")!
|
|
|
|
|
let request = TSRequest(url: url, method: "PATCH", parameters: parameters)
|
|
|
|
|
request.allHTTPHeaderFields = [ "Content-Type" : "application/json", "Authorization" : "Bearer \(token)" ]
|
|
|
|
|
if (useOnionRequests) {
|
|
|
|
|
return getOpenGroupServerPublicKey(on: server).then2 { hexEncodedPublicKey in
|
|
|
|
|
OnionRequestAPI.sendOnionRequestLsrpcDest(request, server: server, using: hexEncodedPublicKey).map2 { _ in }.recover(on: DispatchQueue.global(qos: .default)) { error in
|
|
|
|
|
print("Couldn't update display name due to error: \(error).")
|
|
|
|
|
throw error
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return LokiFileServerProxy(for: server).perform(request).map(on: DispatchQueue.global(qos: .default)) { _ in }.recover(on: DispatchQueue.global(qos: .default)) { error in
|
|
|
|
|
print("Couldn't update display name due to error: \(error).")
|
|
|
|
|
throw error
|
|
|
|
@ -324,6 +426,14 @@ public final class LokiPublicChatAPI : DotNetAPI {
|
|
|
|
|
let url = URL(string: "\(server)/users/me")!
|
|
|
|
|
let request = TSRequest(url: url, method: "PATCH", parameters: parameters)
|
|
|
|
|
request.allHTTPHeaderFields = [ "Content-Type" : "application/json", "Authorization" : "Bearer \(token)" ]
|
|
|
|
|
if (useOnionRequests) {
|
|
|
|
|
return getOpenGroupServerPublicKey(on: server).then2 { hexEncodedPublicKey in
|
|
|
|
|
OnionRequestAPI.sendOnionRequestLsrpcDest(request, server: server, using: hexEncodedPublicKey).map2 { _ in }.recover(on: DispatchQueue.global(qos: .default)) { error in
|
|
|
|
|
print("[Loki] Couldn't update profile picture due to error: \(error).")
|
|
|
|
|
throw error
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return LokiFileServerProxy(for: server).perform(request).map(on: DispatchQueue.global(qos: .default)) { _ in }.recover(on: DispatchQueue.global(qos: .default)) { error in
|
|
|
|
|
print("[Loki] Couldn't update profile picture due to error: \(error).")
|
|
|
|
|
throw error
|
|
|
|
@ -384,31 +494,43 @@ public final class LokiPublicChatAPI : DotNetAPI {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static func getInfo(for channel: UInt64, on server: String) -> Promise<LokiPublicChatInfo> {
|
|
|
|
|
|
|
|
|
|
func handleInfo(rawResponse: Any) throws -> LokiPublicChatInfo {
|
|
|
|
|
guard let json = rawResponse as? JSON,
|
|
|
|
|
let data = json["data"] as? JSON,
|
|
|
|
|
let annotations = data["annotations"] as? [JSON],
|
|
|
|
|
let annotation = annotations.first,
|
|
|
|
|
let info = annotation["value"] as? JSON,
|
|
|
|
|
let displayName = info["name"] as? String,
|
|
|
|
|
let profilePictureURL = info["avatar"] as? String,
|
|
|
|
|
let countInfo = data["counts"] as? JSON,
|
|
|
|
|
let memberCount = countInfo["subscribers"] as? Int else {
|
|
|
|
|
print("[Loki] Couldn't parse info for public chat channel with ID: \(channel) on server: \(server) from: \(rawResponse).")
|
|
|
|
|
throw DotNetAPIError.parsingFailed
|
|
|
|
|
}
|
|
|
|
|
let storage = OWSPrimaryStorage.shared()
|
|
|
|
|
try! Storage.writeSync { transaction in
|
|
|
|
|
storage.setUserCount(memberCount, forPublicChatWithID: "\(server).\(channel)", in: transaction)
|
|
|
|
|
}
|
|
|
|
|
let publicChatInfo = LokiPublicChatInfo(displayName: displayName, profilePictureURL: profilePictureURL, memberCount: memberCount)
|
|
|
|
|
updateProfileIfNeeded(for: channel, on: server, from: publicChatInfo)
|
|
|
|
|
return publicChatInfo
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return attempt(maxRetryCount: maxRetryCount, recoveringOn: DispatchQueue.global(qos: .default)) {
|
|
|
|
|
getAuthToken(for: server).then(on: DispatchQueue.global(qos: .default)) { token -> Promise<LokiPublicChatInfo> in
|
|
|
|
|
let url = URL(string: "\(server)/channels/\(channel)?include_annotations=1")!
|
|
|
|
|
let request = TSRequest(url: url)
|
|
|
|
|
request.allHTTPHeaderFields = [ "Content-Type" : "application/json", "Authorization" : "Bearer \(token)" ]
|
|
|
|
|
return LokiFileServerProxy(for: server).perform(request).map(on: DispatchQueue.global(qos: .default)) { rawResponse in
|
|
|
|
|
guard let json = rawResponse as? JSON,
|
|
|
|
|
let data = json["data"] as? JSON,
|
|
|
|
|
let annotations = data["annotations"] as? [JSON],
|
|
|
|
|
let annotation = annotations.first,
|
|
|
|
|
let info = annotation["value"] as? JSON,
|
|
|
|
|
let displayName = info["name"] as? String,
|
|
|
|
|
let profilePictureURL = info["avatar"] as? String,
|
|
|
|
|
let countInfo = data["counts"] as? JSON,
|
|
|
|
|
let memberCount = countInfo["subscribers"] as? Int else {
|
|
|
|
|
print("[Loki] Couldn't parse info for public chat channel with ID: \(channel) on server: \(server) from: \(rawResponse).")
|
|
|
|
|
throw DotNetAPIError.parsingFailed
|
|
|
|
|
}
|
|
|
|
|
let storage = OWSPrimaryStorage.shared()
|
|
|
|
|
try! Storage.writeSync { transaction in
|
|
|
|
|
storage.setUserCount(memberCount, forPublicChatWithID: "\(server).\(channel)", in: transaction)
|
|
|
|
|
if (useOnionRequests) {
|
|
|
|
|
return getOpenGroupServerPublicKey(on: server).then2 { hexEncodedPublicKey in
|
|
|
|
|
return OnionRequestAPI.sendOnionRequestLsrpcDest(request, server: server, using: hexEncodedPublicKey).map2 { rawResponse in
|
|
|
|
|
return try handleInfo(rawResponse: rawResponse)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
let publicChatInfo = LokiPublicChatInfo(displayName: displayName, profilePictureURL: profilePictureURL, memberCount: memberCount)
|
|
|
|
|
updateProfileIfNeeded(for: channel, on: server, from: publicChatInfo)
|
|
|
|
|
return publicChatInfo
|
|
|
|
|
}
|
|
|
|
|
return LokiFileServerProxy(for: server).perform(request).map(on: DispatchQueue.global(qos: .default)) { rawResponse in
|
|
|
|
|
return try handleInfo(rawResponse: rawResponse)
|
|
|
|
|
}
|
|
|
|
|
}.handlingInvalidAuthTokenIfNeeded(for: server)
|
|
|
|
|
}
|
|
|
|
@ -420,6 +542,13 @@ public final class LokiPublicChatAPI : DotNetAPI {
|
|
|
|
|
let url = URL(string: "\(server)/channels/\(channel)/subscribe")!
|
|
|
|
|
let request = TSRequest(url: url, method: "POST", parameters: [:])
|
|
|
|
|
request.allHTTPHeaderFields = [ "Content-Type" : "application/json", "Authorization" : "Bearer \(token)" ]
|
|
|
|
|
if (useOnionRequests) {
|
|
|
|
|
return getOpenGroupServerPublicKey(on: server).then2 { hexEncodedPublicKey in
|
|
|
|
|
OnionRequestAPI.sendOnionRequestLsrpcDest(request, server: server, using: hexEncodedPublicKey).done2 { result -> Void in
|
|
|
|
|
print("[Loki] Joined channel with ID: \(channel) on server: \(server).")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return LokiFileServerProxy(for: server).perform(request).done(on: DispatchQueue.global(qos: .default)) { result -> Void in
|
|
|
|
|
print("[Loki] Joined channel with ID: \(channel) on server: \(server).")
|
|
|
|
|
}
|
|
|
|
@ -433,6 +562,13 @@ public final class LokiPublicChatAPI : DotNetAPI {
|
|
|
|
|
let url = URL(string: "\(server)/channels/\(channel)/subscribe")!
|
|
|
|
|
let request = TSRequest(url: url, method: "DELETE", parameters: [:])
|
|
|
|
|
request.allHTTPHeaderFields = [ "Content-Type" : "application/json", "Authorization" : "Bearer \(token)" ]
|
|
|
|
|
if (useOnionRequests) {
|
|
|
|
|
return getOpenGroupServerPublicKey(on: server).then2 { hexEncodedPublicKey in
|
|
|
|
|
OnionRequestAPI.sendOnionRequestLsrpcDest(request, server: server, using: hexEncodedPublicKey).done2 { result -> Void in
|
|
|
|
|
print("[Loki] Left channel with ID: \(channel) on server: \(server).")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return LokiFileServerProxy(for: server).perform(request).done(on: DispatchQueue.global(qos: .default)) { result -> Void in
|
|
|
|
|
print("[Loki] Left channel with ID: \(channel) on server: \(server).")
|
|
|
|
|
}
|
|
|
|
@ -450,27 +586,44 @@ public final class LokiPublicChatAPI : DotNetAPI {
|
|
|
|
|
let url = URL(string: "\(server)/loki/v1/channels/\(channel)/messages/\(messageID)/report")!
|
|
|
|
|
let request = TSRequest(url: url, method: "POST", parameters: [:])
|
|
|
|
|
// Only used for the Loki Public Chat which doesn't require authentication
|
|
|
|
|
if (useOnionRequests) {
|
|
|
|
|
return getOpenGroupServerPublicKey(on: server).then2 { hexEncodedPublicKey in
|
|
|
|
|
OnionRequestAPI.sendOnionRequestLsrpcDest(request, server: server, using: hexEncodedPublicKey).map2 { _ in}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return LokiFileServerProxy(for: server).perform(request).map(on: DispatchQueue.global(qos: .default)) { _ in }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// MARK: Moderators
|
|
|
|
|
public static func getModerators(for channel: UInt64, on server: String) -> Promise<Set<String>> {
|
|
|
|
|
|
|
|
|
|
func handleModerators(rawResponse: Any) throws -> Set<String> {
|
|
|
|
|
guard let json = rawResponse as? JSON, let moderators = json["moderators"] as? [String] else {
|
|
|
|
|
print("[Loki] Couldn't parse moderators for public chat channel with ID: \(channel) on server: \(server) from: \(rawResponse).")
|
|
|
|
|
throw DotNetAPIError.parsingFailed
|
|
|
|
|
}
|
|
|
|
|
let moderatorsAsSet = Set(moderators);
|
|
|
|
|
if self.moderators.keys.contains(server) {
|
|
|
|
|
self.moderators[server]![channel] = moderatorsAsSet
|
|
|
|
|
} else {
|
|
|
|
|
self.moderators[server] = [ channel : moderatorsAsSet ]
|
|
|
|
|
}
|
|
|
|
|
return moderatorsAsSet
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return getAuthToken(for: server).then(on: DispatchQueue.global(qos: .default)) { token -> Promise<Set<String>> in
|
|
|
|
|
let url = URL(string: "\(server)/loki/v1/channel/\(channel)/get_moderators")!
|
|
|
|
|
let request = TSRequest(url: url)
|
|
|
|
|
request.allHTTPHeaderFields = [ "Content-Type" : "application/json", "Authorization" : "Bearer \(token)" ]
|
|
|
|
|
return LokiFileServerProxy(for: server).perform(request).map(on: DispatchQueue.global(qos: .default)) { rawResponse in
|
|
|
|
|
guard let json = rawResponse as? JSON, let moderators = json["moderators"] as? [String] else {
|
|
|
|
|
print("[Loki] Couldn't parse moderators for public chat channel with ID: \(channel) on server: \(server) from: \(rawResponse).")
|
|
|
|
|
throw DotNetAPIError.parsingFailed
|
|
|
|
|
}
|
|
|
|
|
let moderatorsAsSet = Set(moderators);
|
|
|
|
|
if self.moderators.keys.contains(server) {
|
|
|
|
|
self.moderators[server]![channel] = moderatorsAsSet
|
|
|
|
|
} else {
|
|
|
|
|
self.moderators[server] = [ channel : moderatorsAsSet ]
|
|
|
|
|
if (useOnionRequests) {
|
|
|
|
|
return getOpenGroupServerPublicKey(on: server).then2 { hexEncodedPublicKey in
|
|
|
|
|
OnionRequestAPI.sendOnionRequestLsrpcDest(request, server: server, using: hexEncodedPublicKey).map2 { rawResponse in
|
|
|
|
|
return try handleModerators(rawResponse: rawResponse)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return moderatorsAsSet
|
|
|
|
|
}
|
|
|
|
|
return LokiFileServerProxy(for: server).perform(request).map(on: DispatchQueue.global(qos: .default)) { rawResponse in
|
|
|
|
|
return try handleModerators(rawResponse: rawResponse)
|
|
|
|
|
}
|
|
|
|
|
}.handlingInvalidAuthTokenIfNeeded(for: server)
|
|
|
|
|
}
|
|
|
|
|