Show real profile picture of open groups

pull/205/head
ryanzhao 5 years ago
parent 70ccea6a28
commit c8af41cd5c

@ -141,15 +141,20 @@ final class ConversationCell : UITableViewCell {
profilePictureView.hexEncodedPublicKey = ""
profilePictureView.isRSSFeed = true
} else {
var users = MentionsManager.userPublicKeyCache[threadViewModel.threadRecord.uniqueId!] ?? []
users.remove(getUserHexEncodedPublicKey())
let randomUsers = users.sorted().prefix(2) // Sort to provide a level of stability
if !randomUsers.isEmpty {
profilePictureView.hexEncodedPublicKey = randomUsers[0]
profilePictureView.additionalHexEncodedPublicKey = randomUsers.count >= 2 ? randomUsers[1] : ""
if let groupAvatarImage = (threadViewModel.threadRecord as? TSGroupThread)?.groupModel.groupImage {
profilePictureView.groupAvatarImage = groupAvatarImage
} else {
profilePictureView.hexEncodedPublicKey = ""
profilePictureView.additionalHexEncodedPublicKey = ""
profilePictureView.groupAvatarImage = nil
var users = MentionsManager.userPublicKeyCache[threadViewModel.threadRecord.uniqueId!] ?? []
users.remove(getUserHexEncodedPublicKey())
let randomUsers = users.sorted().prefix(2) // Sort to provide a level of stability
if !randomUsers.isEmpty {
profilePictureView.hexEncodedPublicKey = randomUsers[0]
profilePictureView.additionalHexEncodedPublicKey = randomUsers.count >= 2 ? randomUsers[1] : ""
} else {
profilePictureView.hexEncodedPublicKey = ""
profilePictureView.additionalHexEncodedPublicKey = ""
}
}
profilePictureView.isRSSFeed = (threadViewModel.threadRecord as? TSGroupThread)?.isRSSFeed ?? false
}

@ -957,8 +957,14 @@ const CGFloat kIconViewLength = 24;
[stackView setLayoutMarginsRelativeArrangement:YES];
if (self.isGroupThread) {
profilePictureView.hexEncodedPublicKey = @"";
profilePictureView.isRSSFeed = true; // For now just always show the Session logo
TSGroupThread* groupThread = (TSGroupThread*)self.thread;
if (groupThread.isPublicChat && groupThread.groupModel.groupImage != nil) {
profilePictureView.groupAvatarImage = groupThread.groupModel.groupImage;
profilePictureView.isRSSFeed = false;
} else {
profilePictureView.hexEncodedPublicKey = @"";
profilePictureView.isRSSFeed = true; // For now just always show the Session logo
}
} else {
profilePictureView.hexEncodedPublicKey = self.thread.contactIdentifier;

@ -7,6 +7,7 @@ public final class ProfilePictureView : UIView {
@objc public var isRSSFeed = false
@objc public var hexEncodedPublicKey: String!
@objc public var additionalHexEncodedPublicKey: String?
@objc public var groupAvatarImage: UIImage?
// MARK: Components
private lazy var imageView = getImageView()
@ -61,8 +62,9 @@ public final class ProfilePictureView : UIView {
additionalImageView.isHidden = true
additionalImageView.image = nil
}
guard hexEncodedPublicKey != nil else { return } // Can happen in rare cases
imageView.image = isRSSFeed ? nil : getProfilePicture(of: size, for: hexEncodedPublicKey)
guard hexEncodedPublicKey != nil || groupAvatarImage != nil else { return } // Can happen in rare cases
imageView.image = isRSSFeed ? nil : groupAvatarImage != nil ? groupAvatarImage! : getProfilePicture(of: size, for: hexEncodedPublicKey)
imageView.backgroundColor = isRSSFeed ? UIColor(rgbHex: 0x353535) : UIColor(rgbHex: 0xD8D8D8) // UIColor(rgbHex: 0xD8D8D8) = Colors.unimportant
imageView.layer.cornerRadius = size / 2
imageView.contentMode = isRSSFeed ? .center : .scaleAspectFit

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

@ -151,6 +151,7 @@ public extension OWSPrimaryStorage {
// MARK: - Open Groups
private static let openGroupUserCountCollection = "LokiPublicChatUserCountCollection"
private static let openGroupAvatarURLCollection = "LokiPublicChatAvatarURLCollection"
public func getUserCount(for publicChat: LokiPublicChat, in transaction: YapDatabaseReadTransaction) -> Int? {
return transaction.object(forKey: publicChat.id, inCollection: OWSPrimaryStorage.openGroupUserCountCollection) as? Int
@ -159,4 +160,12 @@ public extension OWSPrimaryStorage {
public func setUserCount(_ userCount: Int, forPublicChatWithID publicChatID: String, in transaction: YapDatabaseReadWriteTransaction) {
transaction.setObject(userCount, forKey: publicChatID, inCollection: OWSPrimaryStorage.openGroupUserCountCollection)
}
public func getAvatarURL(forPublicChatWithID publicChatID: String, in transaction: YapDatabaseReadTransaction) -> String? {
return transaction.object(forKey: publicChatID, inCollection: OWSPrimaryStorage.openGroupAvatarURLCollection) as? String
}
public func setAvatarURL(_ avatarURL: String?, forPublicChatWithID publicChatID: String, in transaction: YapDatabaseReadWriteTransaction) {
transaction.setObject(avatarURL, forKey: publicChatID, inCollection: OWSPrimaryStorage.openGroupAvatarURLCollection)
}
}

Loading…
Cancel
Save