using onion routing for open group avatar downloads

pull/295/head
Ryan ZHAO 5 years ago
parent e262013147
commit b30cfd960c

@ -369,7 +369,7 @@ public final class PublicChatAPI : DotNetAPI {
} }
} }
static func updateProfileIfNeeded(for channel: UInt64, on server: String, from info: PublicChatInfo) { static func updateProfileIfNeeded(for channel: UInt64, on server: String, from info: PublicChatInfo, token: String, serverPublicKey: String) {
let storage = OWSPrimaryStorage.shared() let storage = OWSPrimaryStorage.shared()
let publicChatID = "\(server).\(channel)" let publicChatID = "\(server).\(channel)"
try! Storage.writeSync { transaction in try! Storage.writeSync { transaction in
@ -388,27 +388,26 @@ public final class PublicChatAPI : DotNetAPI {
if oldProfilePictureURL != info.profilePictureURL || groupModel.groupImage == nil { if oldProfilePictureURL != info.profilePictureURL || groupModel.groupImage == nil {
storage.setProfilePictureURL(info.profilePictureURL, forPublicChatWithID: publicChatID, in: transaction) storage.setProfilePictureURL(info.profilePictureURL, forPublicChatWithID: publicChatID, in: transaction)
if let profilePictureURL = info.profilePictureURL { if let profilePictureURL = info.profilePictureURL {
let configuration = URLSessionConfiguration.default var error: NSError?
let manager = AFURLSessionManager.init(sessionConfiguration: configuration) let url = "\(server)/loki/v1\(profilePictureURL)"
let url = URL(string: "\(server)\(profilePictureURL)")! let request = AFHTTPRequestSerializer().request(withMethod: "GET", urlString: url, parameters: nil, error: &error)
let request = URLRequest(url: url) request.allHTTPHeaderFields = [ "Content-Type" : "application/json",
let task = manager.downloadTask(with: request, progress: nil, "Authorization" : "Bearer \(token)",
destination: { (targetPath: URL, response: URLResponse) -> URL in "Accept-Ranges" : "bytes"]
let tempFilePath = URL(fileURLWithPath: OWSTemporaryDirectoryAccessibleAfterFirstAuth()).appendingPathComponent(UUID().uuidString) if let error = error {
return tempFilePath print("[Loki] Couldn't download open group avatar due to error: \(error).")
}, return
completionHandler: { (response: URLResponse, filePath: URL?, error: Error?) in }
if let error = error { OnionRequestAPI.sendOnionRequest(request, to: server, using: serverPublicKey, isJSONRequired: false).map{ json in
print("[Loki] Couldn't download profile picture for public chat channel with ID: \(channel) on server: \(server).") guard let body = json["body"] as? JSON, let dataArray = body["data"] as? [UInt8] else {
return print("[Loki] Couldn't download open group avatar.")
} return
if let filePath = filePath, let avatarData = try? Data.init(contentsOf: filePath) { }
let attachmentStream = TSAttachmentStream(contentType: OWSMimeTypeImageJpeg, byteCount: UInt32(avatarData.count), sourceFilename: nil, caption: nil, albumMessageId: nil) let avatarData = Data(dataArray)
try! attachmentStream.write(avatarData) let attachmentStream = TSAttachmentStream(contentType: OWSMimeTypeImageJpeg, byteCount: UInt32(avatarData.count), sourceFilename: nil, caption: nil, albumMessageId: nil)
groupThread.updateAvatar(with: attachmentStream) try! attachmentStream.write(avatarData)
} groupThread.updateAvatar(with: attachmentStream)
}) }
task.resume()
} }
} }
} }
@ -444,7 +443,7 @@ public final class PublicChatAPI : DotNetAPI {
storage.setUserCount(memberCount, forPublicChatWithID: "\(server).\(channel)", in: transaction) storage.setUserCount(memberCount, forPublicChatWithID: "\(server).\(channel)", in: transaction)
} }
let publicChatInfo = PublicChatInfo(displayName: displayName, profilePictureURL: profilePictureURL, memberCount: memberCount) let publicChatInfo = PublicChatInfo(displayName: displayName, profilePictureURL: profilePictureURL, memberCount: memberCount)
updateProfileIfNeeded(for: channel, on: server, from: publicChatInfo) updateProfileIfNeeded(for: channel, on: server, from: publicChatInfo, token: token, serverPublicKey: serverPublicKey)
return publicChatInfo return publicChatInfo
} }
} }

Loading…
Cancel
Save