|
|
|
@ -391,18 +391,63 @@ public final class LokiPublicChatAPI : LokiDotNetAPI {
|
|
|
|
|
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 LokiDotNetAPIError.parsingFailed
|
|
|
|
|
}
|
|
|
|
|
let profilePictureURL = info["avatar"] as? String
|
|
|
|
|
let storage = OWSPrimaryStorage.shared()
|
|
|
|
|
storage.dbReadWriteConnection.readWrite { transaction in
|
|
|
|
|
storage.setUserCount(memberCount, forPublicChatWithID: "\(server).\(channel)", in: transaction)
|
|
|
|
|
let publicChatInfo = LokiPublicChatInfo(displayName: displayName, profilePictureURL: profilePictureURL, memberCount: memberCount)
|
|
|
|
|
updateOpenGroupProfileIfNeeded(for: channel, on: server, with: publicChatInfo)
|
|
|
|
|
return publicChatInfo
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static func updateOpenGroupProfileIfNeeded(for channel: UInt64, on server: String, with info: LokiPublicChatInfo) {
|
|
|
|
|
let storage = OWSPrimaryStorage.shared()
|
|
|
|
|
let publicChatID = "\(server).\(channel)"
|
|
|
|
|
storage.dbReadWriteConnection.readWrite { transaction in
|
|
|
|
|
//Save user count
|
|
|
|
|
storage.setUserCount(info.memberCount, forPublicChatWithID: publicChatID, in: transaction)
|
|
|
|
|
|
|
|
|
|
let groupThread = TSGroupThread.getOrCreateThread(withGroupId: publicChatID.data(using: .utf8)!, groupType: .openGroup, transaction: transaction)
|
|
|
|
|
|
|
|
|
|
//Update display name if needed
|
|
|
|
|
let groupModel = groupThread.groupModel
|
|
|
|
|
if groupModel.groupName != info.displayName {
|
|
|
|
|
let newGroupModel = TSGroupModel.init(title: info.displayName, memberIds: groupModel.groupMemberIds, image: groupModel.groupImage, groupId: groupModel.groupId, groupType: groupModel.groupType, adminIds: groupModel.groupAdminIds)
|
|
|
|
|
groupThread.groupModel = newGroupModel
|
|
|
|
|
groupThread.save(with: transaction)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//Download and update profile picture if needed
|
|
|
|
|
let oldAvatarURL = storage.getAvatarURL(forPublicChatWithID: publicChatID, in: transaction)
|
|
|
|
|
if oldAvatarURL != info.profilePictureURL || groupModel.groupImage == nil {
|
|
|
|
|
storage.setAvatarURL(info.profilePictureURL, forPublicChatWithID: publicChatID, in: transaction)
|
|
|
|
|
if let avatarURL = info.profilePictureURL {
|
|
|
|
|
let configuration = URLSessionConfiguration.default
|
|
|
|
|
let manager = AFURLSessionManager.init(sessionConfiguration: configuration)
|
|
|
|
|
let url = URL(string: "\(server)\(avatarURL)")!
|
|
|
|
|
let request = URLRequest(url: url)
|
|
|
|
|
let task = manager.downloadTask(with: request, progress: nil,
|
|
|
|
|
destination: {(targetPath: URL, response: URLResponse) -> URL in
|
|
|
|
|
let tempFilePath = URL(fileURLWithPath: OWSTemporaryDirectoryAccessibleAfterFirstAuth()).appendingPathComponent(UUID().uuidString)
|
|
|
|
|
return tempFilePath
|
|
|
|
|
},
|
|
|
|
|
completionHandler: { (response: URLResponse, filePath: URL?, error: Error?) in
|
|
|
|
|
if let error = error {
|
|
|
|
|
print("[Loki] Couldn't download profile picture for public chat channel with ID: \(channel) on server: \(server)")
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
if let path = filePath, let avatarData = try? Data.init(contentsOf: path) {
|
|
|
|
|
let attachmentStream = TSAttachmentStream.init(contentType: OWSMimeTypeImageJpeg, byteCount: UInt32(avatarData.count), sourceFilename: nil, caption: nil, albumMessageId: nil)
|
|
|
|
|
try! attachmentStream.write(avatarData)
|
|
|
|
|
groupThread.updateAvatar(with: attachmentStream)
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
task.resume()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// TODO: Use this to update open group names as needed
|
|
|
|
|
return LokiPublicChatInfo(displayName: displayName, profilePictureURL: profilePictureURL, memberCount: memberCount)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|