From ee1d4b1bd5c3aabc1b836efc9fc61c0c0a55ae03 Mon Sep 17 00:00:00 2001 From: Niels Andriesse Date: Wed, 6 Jan 2021 10:37:26 +1100 Subject: [PATCH] Debug & make UI clearer --- Session/Components/UserCell.swift | 14 ++++++---- .../View Controllers/EditClosedGroupVC.swift | 27 ++++++++++++++----- .../ClosedGroupUpdateV2.swift | 7 +++++ .../Control Messages/TypingIndicator.swift | 2 +- SessionMessagingKit/Messages/Message.swift | 2 +- .../MessageSender+ClosedGroups.swift | 9 ++++--- .../Sending & Receiving/MessageSender.swift | 8 ++---- SessionSnodeKit/SnodeAPI.swift | 4 ++- 8 files changed, 49 insertions(+), 24 deletions(-) diff --git a/Session/Components/UserCell.swift b/Session/Components/UserCell.swift index 81e4516b1..b9ebce538 100644 --- a/Session/Components/UserCell.swift +++ b/Session/Components/UserCell.swift @@ -7,6 +7,7 @@ final class UserCell : UITableViewCell { // MARK: Accessory enum Accessory { case none + case lock case tick(isSelected: Bool) } @@ -21,7 +22,7 @@ final class UserCell : UITableViewCell { return result }() - private lazy var tickImageView: UIImageView = { + private lazy var accessoryImageView: UIImageView = { let result = UIImageView() result.contentMode = .scaleAspectFit let size: CGFloat = 24 @@ -61,7 +62,7 @@ final class UserCell : UITableViewCell { profilePictureView.set(.height, to: profilePictureViewSize) profilePictureView.size = profilePictureViewSize // Set up the main stack view - let stackView = UIStackView(arrangedSubviews: [ profilePictureView, displayNameLabel, tickImageView ]) + let stackView = UIStackView(arrangedSubviews: [ profilePictureView, displayNameLabel, accessoryImageView ]) stackView.axis = .horizontal stackView.alignment = .center stackView.spacing = Values.mediumSpacing @@ -85,11 +86,14 @@ final class UserCell : UITableViewCell { profilePictureView.update() displayNameLabel.text = UserDisplayNameUtilities.getPrivateChatDisplayName(for: publicKey) ?? publicKey switch accessory { - case .none: tickImageView.isHidden = true + case .none: accessoryImageView.isHidden = true + case .lock: + accessoryImageView.isHidden = false + accessoryImageView.image = #imageLiteral(resourceName: "ic_lock_outline").asTintedImage(color: Colors.text.withAlphaComponent(Values.unimportantElementOpacity))! case .tick(let isSelected): - tickImageView.isHidden = false + accessoryImageView.isHidden = false let icon = isSelected ? #imageLiteral(resourceName: "CircleCheck") : #imageLiteral(resourceName: "Circle") - tickImageView.image = isDarkMode ? icon : icon.asTintedImage(color: Colors.text)! + accessoryImageView.image = isDarkMode ? icon : icon.asTintedImage(color: Colors.text)! } } } diff --git a/Session/View Controllers/EditClosedGroupVC.swift b/Session/View Controllers/EditClosedGroupVC.swift index 597a26521..3917f40e2 100644 --- a/Session/View Controllers/EditClosedGroupVC.swift +++ b/Session/View Controllers/EditClosedGroupVC.swift @@ -23,12 +23,12 @@ final class EditClosedGroupVC : BaseVC, UITableViewDataSource, UITableViewDelega }() private lazy var addMembersButton: Button = { - let result = Button(style: .prominentOutline, size: .large) - result.setTitle("Add Members", for: UIControl.State.normal) - result.addTarget(self, action: #selector(addMembers), for: UIControl.Event.touchUpInside) - result.contentEdgeInsets = UIEdgeInsets(top: 0, leading: Values.mediumSpacing, bottom: 0, trailing: Values.mediumSpacing) - return result - }() + let result = Button(style: .prominentOutline, size: .large) + result.setTitle("Add Members", for: UIControl.State.normal) + result.addTarget(self, action: #selector(addMembers), for: UIControl.Event.touchUpInside) + result.contentEdgeInsets = UIEdgeInsets(top: 0, leading: Values.mediumSpacing, bottom: 0, trailing: Values.mediumSpacing) + return result + }() @objc private lazy var tableView: UITableView = { let result = UITableView() @@ -141,13 +141,14 @@ final class EditClosedGroupVC : BaseVC, UITableViewDataSource, UITableViewDelega let cell = tableView.dequeueReusableCell(withIdentifier: "UserCell") as! UserCell let publicKey = members[indexPath.row] cell.publicKey = publicKey + cell.accessory = !canBeRemoved(publicKey) ? .lock : .none cell.update() return cell } func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool { let publicKey = members[indexPath.row] - return publicKey != getUserHexEncodedPublicKey() + return canBeRemoved(publicKey) } func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? { @@ -286,4 +287,16 @@ final class EditClosedGroupVC : BaseVC, UITableViewDataSource, UITableViewDelega alert.addAction(UIAlertAction(title: NSLocalizedString("OK", comment: ""), style: .default, handler: nil)) presentAlert(alert) } + + private func canBeRemoved(_ publicKey: String) -> Bool { + return !isAdmin(publicKey) && !isCurrentUser(publicKey) + } + + private func isAdmin(_ publicKey: String) -> Bool { + return thread.groupModel.groupAdminIds.contains(publicKey) + } + + private func isCurrentUser(_ publicKey: String) -> Bool { + return publicKey == getUserHexEncodedPublicKey() + } } diff --git a/SessionMessagingKit/Messages/Control Messages/ClosedGroupUpdateV2.swift b/SessionMessagingKit/Messages/Control Messages/ClosedGroupUpdateV2.swift index da0a845b2..30166fda9 100644 --- a/SessionMessagingKit/Messages/Control Messages/ClosedGroupUpdateV2.swift +++ b/SessionMessagingKit/Messages/Control Messages/ClosedGroupUpdateV2.swift @@ -4,6 +4,13 @@ import SessionUtilitiesKit public final class ClosedGroupUpdateV2 : ControlMessage { public var kind: Kind? + public override var ttl: UInt64 { + switch kind { + case .encryptionKeyPair: return 4 * 24 * 60 * 60 * 1000 + default: return 2 * 24 * 60 * 60 * 1000 + } + } + // MARK: Kind public enum Kind : CustomStringConvertible { case new(publicKey: Data, name: String, encryptionKeyPair: ECKeyPair, members: [Data], admins: [Data]) diff --git a/SessionMessagingKit/Messages/Control Messages/TypingIndicator.swift b/SessionMessagingKit/Messages/Control Messages/TypingIndicator.swift index e09de0d5d..965fdfa38 100644 --- a/SessionMessagingKit/Messages/Control Messages/TypingIndicator.swift +++ b/SessionMessagingKit/Messages/Control Messages/TypingIndicator.swift @@ -4,7 +4,7 @@ import SessionUtilitiesKit public final class TypingIndicator : ControlMessage { public var kind: Kind? - public override class var ttl: UInt64 { 30 * 1000 } + public override var ttl: UInt64 { 20 * 1000 } // MARK: Kind public enum Kind : Int, CustomStringConvertible { diff --git a/SessionMessagingKit/Messages/Message.swift b/SessionMessagingKit/Messages/Message.swift index 985f9ba41..6d75b1e16 100644 --- a/SessionMessagingKit/Messages/Message.swift +++ b/SessionMessagingKit/Messages/Message.swift @@ -11,7 +11,7 @@ public class Message : NSObject, NSCoding { // NSObject/NSCoding conformance is public var groupPublicKey: String? public var openGroupServerMessageID: UInt64? - public class var ttl: UInt64 { 2 * 24 * 60 * 60 * 1000 } + public var ttl: UInt64 { 2 * 24 * 60 * 60 * 1000 } public override init() { } diff --git a/SessionMessagingKit/Sending & Receiving/MessageSender+ClosedGroups.swift b/SessionMessagingKit/Sending & Receiving/MessageSender+ClosedGroups.swift index 5ac992960..cdd367d0a 100644 --- a/SessionMessagingKit/Sending & Receiving/MessageSender+ClosedGroups.swift +++ b/SessionMessagingKit/Sending & Receiving/MessageSender+ClosedGroups.swift @@ -144,8 +144,6 @@ extension MessageSender : SharedSenderKeysDelegate { } // Generate the new encryption key pair let newKeyPair = Curve25519.generateKeyPair() - // Store it - Storage.shared.addClosedGroupEncryptionKeyPair(newKeyPair, for: groupPublicKey, using: transaction) // Distribute it let proto = try SNProtoDataMessageClosedGroupUpdateV2KeyPair.builder(publicKey: newKeyPair.publicKey, privateKey: newKeyPair.privateKey).build() @@ -155,7 +153,12 @@ extension MessageSender : SharedSenderKeysDelegate { return ClosedGroupUpdateV2.KeyPairWrapper(publicKey: publicKey, encryptedKeyPair: ciphertext) } let closedGroupUpdate = ClosedGroupUpdateV2(kind: .encryptionKeyPair(wrappers)) - MessageSender.send(closedGroupUpdate, in: thread, using: transaction) + let _ = MessageSender.sendNonDurably(closedGroupUpdate, in: thread, using: transaction).done { // FIXME: It'd be great if we could make this a durable operation + // Store it * after * having sent out the message to the group + SNMessagingKitConfiguration.shared.storage.write { transaction in + Storage.shared.addClosedGroupEncryptionKeyPair(newKeyPair, for: groupPublicKey, using: transaction) + } + } } diff --git a/SessionMessagingKit/Sending & Receiving/MessageSender.swift b/SessionMessagingKit/Sending & Receiving/MessageSender.swift index b9a8330d1..012178298 100644 --- a/SessionMessagingKit/Sending & Receiving/MessageSender.swift +++ b/SessionMessagingKit/Sending & Receiving/MessageSender.swift @@ -219,11 +219,7 @@ public final class MessageSender : NSObject { } let recipient = message.recipient! let base64EncodedData = wrappedMessage.base64EncodedString() - var ttl = type(of: message).ttl - if let closedGroupUpdate = message as? ClosedGroupUpdateV2, case .encryptionKeyPair = closedGroupUpdate.kind! { - ttl = 30 * 24 * 60 * 60 * 1000 - } - guard let (timestamp, nonce) = ProofOfWork.calculate(ttl: ttl, publicKey: recipient, data: base64EncodedData) else { + guard let (timestamp, nonce) = ProofOfWork.calculate(ttl: message.ttl, publicKey: recipient, data: base64EncodedData) else { SNLog("Proof of work calculation failed.") handleFailure(with: Error.proofOfWorkCalculationFailed, using: transaction) return promise @@ -234,7 +230,7 @@ public final class MessageSender : NSObject { NotificationCenter.default.post(name: .messageSending, object: NSNumber(value: message.sentTimestamp!)) } } - let snodeMessage = SnodeMessage(recipient: recipient, data: base64EncodedData, ttl: type(of: message).ttl, timestamp: timestamp, nonce: nonce) + let snodeMessage = SnodeMessage(recipient: recipient, data: base64EncodedData, ttl: message.ttl, timestamp: timestamp, nonce: nonce) SnodeAPI.sendMessage(snodeMessage).done(on: DispatchQueue.global(qos: .userInitiated)) { promises in var isSuccess = false let promiseCount = promises.count diff --git a/SessionSnodeKit/SnodeAPI.swift b/SessionSnodeKit/SnodeAPI.swift index eb0d8c312..894a5a5fc 100644 --- a/SessionSnodeKit/SnodeAPI.swift +++ b/SessionSnodeKit/SnodeAPI.swift @@ -75,7 +75,9 @@ public final class SnodeAPI : NSObject { @objc public static func clearSnodePool() { snodePool.removeAll() - setSnodePool(to: []) + Threading.workQueue.async { + setSnodePool(to: []) + } } // MARK: Swarm Interaction