From a41b670bb5c6bc5d00090c91a1a9a40775fcbd42 Mon Sep 17 00:00:00 2001 From: Niels Andriesse Date: Thu, 30 Jan 2020 20:09:02 +1100 Subject: [PATCH] Refactor --- Signal.xcodeproj/project.pbxproj | 8 +++--- Signal/Signal-Info.plist | 2 +- .../Loki/Components/ConversationCell.swift | 9 ++++--- .../Components/ConversationTitleView.swift | 9 ++----- Signal/src/Loki/Components/TextField.swift | 9 ++++--- .../src/Loki/Utilities/MentionUtilities.swift | 2 +- .../View Controllers/DeviceLinkingModal.swift | 9 ++++--- .../Loki/View Controllers/DeviceLinksVC.swift | 2 +- .../View Controllers/GroupMembersVC.swift | 7 +----- Signal/src/Loki/View Controllers/HomeVC.swift | 4 +-- .../View Controllers/NewClosedGroupVC.swift | 4 +-- .../View Controllers/NewPrivateChatVC.swift | 2 +- .../src/Loki/View Controllers/QRCodeVC.swift | 2 +- .../{SeedVCV2.swift => SeedVC.swift} | 2 +- .../Loki/View Controllers/SettingsVC.swift | 2 +- SignalMessaging/Views/AvatarImageView.swift | 2 +- SignalServiceKit/src/Loki/API/LokiAPI.swift | 2 +- .../src/Loki/API/LokiLongPoller.swift | 2 +- .../src/Loki/API/LokiP2PAPI.swift | 2 +- .../Loki/API/Multi Device/DeviceLink.swift | 2 +- .../Multi Device/DeviceLinkingSession.swift | 4 +-- .../Public Chat/LokiPublicChatPoller.swift | 2 +- .../Loki/Database/LokiDatabaseUtilities.swift | 4 +-- .../Loki/Utilities/DisplayNameUtilities.swift | 2 +- .../src/Loki/Utilities/General.swift | 4 +++ .../src/Loki/Utilities/GroupUtilities.swift | 25 +++++++++++++++++++ .../src/Messages/OWSMessageManager.m | 1 - 27 files changed, 74 insertions(+), 51 deletions(-) rename Signal/src/Loki/View Controllers/{SeedVCV2.swift => SeedVC.swift} (99%) create mode 100644 SignalServiceKit/src/Loki/Utilities/General.swift create mode 100644 SignalServiceKit/src/Loki/Utilities/GroupUtilities.swift diff --git a/Signal.xcodeproj/project.pbxproj b/Signal.xcodeproj/project.pbxproj index a628555de..24f5f6253 100644 --- a/Signal.xcodeproj/project.pbxproj +++ b/Signal.xcodeproj/project.pbxproj @@ -578,7 +578,7 @@ B847570323D5698100759540 /* LokiPushNotificationManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = B847570223D5698100759540 /* LokiPushNotificationManager.swift */; }; B85357BF23A1AE0800AAF6CD /* SeedReminderView.swift in Sources */ = {isa = PBXBuildFile; fileRef = B85357BE23A1AE0800AAF6CD /* SeedReminderView.swift */; }; B85357C123A1B81900AAF6CD /* SeedReminderViewDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = B85357C023A1B81900AAF6CD /* SeedReminderViewDelegate.swift */; }; - B85357C323A1BD1200AAF6CD /* SeedVCV2.swift in Sources */ = {isa = PBXBuildFile; fileRef = B85357C223A1BD1200AAF6CD /* SeedVCV2.swift */; }; + B85357C323A1BD1200AAF6CD /* SeedVC.swift in Sources */ = {isa = PBXBuildFile; fileRef = B85357C223A1BD1200AAF6CD /* SeedVC.swift */; }; B85357C523A1F13800AAF6CD /* LinkDeviceVC.swift in Sources */ = {isa = PBXBuildFile; fileRef = B85357C423A1F13800AAF6CD /* LinkDeviceVC.swift */; }; B85357C723A1FB5100AAF6CD /* LinkDeviceVCDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = B85357C623A1FB5100AAF6CD /* LinkDeviceVCDelegate.swift */; }; B8544E3123D16CA500299F14 /* DeviceUtilities.swift in Sources */ = {isa = PBXBuildFile; fileRef = B8544E3023D16CA500299F14 /* DeviceUtilities.swift */; }; @@ -1424,7 +1424,7 @@ B847570223D5698100759540 /* LokiPushNotificationManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LokiPushNotificationManager.swift; sourceTree = ""; }; B85357BE23A1AE0800AAF6CD /* SeedReminderView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SeedReminderView.swift; sourceTree = ""; }; B85357C023A1B81900AAF6CD /* SeedReminderViewDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SeedReminderViewDelegate.swift; sourceTree = ""; }; - B85357C223A1BD1200AAF6CD /* SeedVCV2.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SeedVCV2.swift; sourceTree = ""; }; + B85357C223A1BD1200AAF6CD /* SeedVC.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SeedVC.swift; sourceTree = ""; }; B85357C423A1F13800AAF6CD /* LinkDeviceVC.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LinkDeviceVC.swift; sourceTree = ""; }; B85357C623A1FB5100AAF6CD /* LinkDeviceVCDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LinkDeviceVCDelegate.swift; sourceTree = ""; }; B8544E3023D16CA500299F14 /* DeviceUtilities.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DeviceUtilities.swift; sourceTree = ""; }; @@ -2832,7 +2832,7 @@ B82B408F239DD75000A248E7 /* RestoreVC.swift */, B893063E2383961A005EAA8E /* ScanQRCodeWrapperVC.swift */, B86BD08523399CEF000F5AE3 /* SeedModal.swift */, - B85357C223A1BD1200AAF6CD /* SeedVCV2.swift */, + B85357C223A1BD1200AAF6CD /* SeedVC.swift */, B8CCF6422397711F0091D419 /* SettingsVC.swift */, ); path = "View Controllers"; @@ -4011,7 +4011,7 @@ 34D1F0B41F86D31D0066283D /* ConversationCollectionView.m in Sources */, 34B3F8821E8DF1700035BE1A /* NewContactThreadViewController.m in Sources */, 45D308AD2049A439000189E4 /* PinEntryView.m in Sources */, - B85357C323A1BD1200AAF6CD /* SeedVCV2.swift in Sources */, + B85357C323A1BD1200AAF6CD /* SeedVC.swift in Sources */, 340FC8B1204DAC8D007AEB0F /* BlockListViewController.m in Sources */, 45B5360E206DD8BB00D61655 /* UIResponder+OWS.swift in Sources */, 4CFE6B6C21F92BA700006701 /* LegacyNotificationsAdaptee.swift in Sources */, diff --git a/Signal/Signal-Info.plist b/Signal/Signal-Info.plist index daba681ed..4a8f35485 100644 --- a/Signal/Signal-Info.plist +++ b/Signal/Signal-Info.plist @@ -5,7 +5,7 @@ BuildDetails CarthageVersion - 0.34.0 + 0.33.0 OSXVersion 10.15.2 WebRTCCommit diff --git a/Signal/src/Loki/Components/ConversationCell.swift b/Signal/src/Loki/Components/ConversationCell.swift index 2f2d40de1..34928ef87 100644 --- a/Signal/src/Loki/Components/ConversationCell.swift +++ b/Signal/src/Loki/Components/ConversationCell.swift @@ -58,6 +58,7 @@ final class ConversationCell : UITableViewCell { } private func setUpViewHierarchy() { + let cellHeight: CGFloat = 72 // Set the cell background color backgroundColor = Colors.cellBackground // Set up the highlight color @@ -66,7 +67,7 @@ final class ConversationCell : UITableViewCell { self.selectedBackgroundView = selectedBackgroundView // Set up the unread messages indicator view unreadMessagesIndicatorView.set(.width, to: Values.accentLineThickness) - unreadMessagesIndicatorView.set(.height, to: 72) + unreadMessagesIndicatorView.set(.height, to: cellHeight) // Set up the profile picture view let profilePictureViewSize = Values.mediumProfilePictureSize profilePictureView.set(.width, to: profilePictureViewSize) @@ -121,12 +122,12 @@ final class ConversationCell : UITableViewCell { labelContainerView.pin(.bottom, to: .bottom, of: bottomLabelStackView, withInset: Values.mediumSpacing) // The two lines below are part of a workaround for a weird layout bug labelContainerView.set(.width, to: UIScreen.main.bounds.width - Values.accentLineThickness - Values.mediumSpacing - profilePictureViewSize - Values.mediumSpacing - Values.mediumSpacing) - labelContainerView.set(.height, to: 72) + labelContainerView.set(.height, to: cellHeight) stackView.pin(.leading, to: .leading, of: contentView) stackView.pin(.top, to: .top, of: contentView) // The two lines below are part of a workaround for a weird layout bug stackView.set(.width, to: UIScreen.main.bounds.width - Values.mediumSpacing) - stackView.set(.height, to: 72) + stackView.set(.height, to: cellHeight) } // MARK: Updating @@ -180,7 +181,7 @@ final class ConversationCell : UITableViewCell { private func getDisplayName() -> String { if threadViewModel.isGroupThread { - if threadViewModel.name.isEmpty || threadViewModel.name == "New Group" { + if threadViewModel.name.isEmpty { return DisplayNameUtilities.getDisplayName(for: threadViewModel.threadRecord as! TSGroupThread) } else { return threadViewModel.name diff --git a/Signal/src/Loki/Components/ConversationTitleView.swift b/Signal/src/Loki/Components/ConversationTitleView.swift index 4db62bd5b..7df0bbe84 100644 --- a/Signal/src/Loki/Components/ConversationTitleView.swift +++ b/Signal/src/Loki/Components/ConversationTitleView.swift @@ -72,7 +72,7 @@ final class ConversationTitleView : UIView { private func updateTitle() { let title: String if thread.isGroupThread() { - if thread.name().isEmpty || thread.name() == "New Group" { + if thread.name().isEmpty { title = DisplayNameUtilities.getDisplayName(for: thread as! TSGroupThread) } else { title = thread.name() @@ -164,12 +164,7 @@ final class ConversationTitleView : UIView { let storage = OWSPrimaryStorage.shared() var userCount: Int? if thread.groupModel.groupType == .closedGroup { - let userHexEncodedPublicKey = OWSIdentityManager.shared().identityKeyPair()!.hexEncodedPublicKey - var linkedDeviceHexEncodedPublicKeys: Set = [ userHexEncodedPublicKey ] - OWSPrimaryStorage.shared().dbReadConnection.read { transaction in - linkedDeviceHexEncodedPublicKeys = LokiDatabaseUtilities.getLinkedDeviceHexEncodedPublicKeys(for: userHexEncodedPublicKey, in: transaction) - } - userCount = thread.groupModel.groupMemberIds.filter { !linkedDeviceHexEncodedPublicKeys.contains($0) }.count + userCount = GroupUtilities.getClosedGroupMemberCount(thread) } else if thread.groupModel.groupType == .openGroup { storage.dbReadConnection.readWrite { transaction in if let publicChat = LokiDatabaseUtilities.getPublicChat(for: self.thread.uniqueId!, in: transaction) { diff --git a/Signal/src/Loki/Components/TextField.swift b/Signal/src/Loki/Components/TextField.swift index 9bedb2e56..91ba42e13 100644 --- a/Signal/src/Loki/Components/TextField.swift +++ b/Signal/src/Loki/Components/TextField.swift @@ -1,7 +1,10 @@ final class TextField : UITextField { private let usesDefaultHeight: Bool - + + private let horizontalInset = isSmallScreen ? Values.mediumSpacing : Values.largeSpacing + private let verticalInset = isSmallScreen ? Values.smallSpacing : Values.largeSpacing + init(placeholder: String, usesDefaultHeight: Bool = true) { self.usesDefaultHeight = usesDefaultHeight super.init(frame: CGRect.zero) @@ -36,7 +39,7 @@ final class TextField : UITextField { override func textRect(forBounds bounds: CGRect) -> CGRect { if usesDefaultHeight { - return bounds.insetBy(dx: isSmallScreen ? Values.mediumSpacing : Values.largeSpacing, dy: isSmallScreen ? Values.smallSpacing : Values.largeSpacing) + return bounds.insetBy(dx: horizontalInset, dy: verticalInset) } else { return bounds.insetBy(dx: Values.mediumSpacing, dy: Values.smallSpacing) } @@ -44,7 +47,7 @@ final class TextField : UITextField { override func editingRect(forBounds bounds: CGRect) -> CGRect { if usesDefaultHeight { - return bounds.insetBy(dx: isSmallScreen ? Values.mediumSpacing : Values.largeSpacing, dy: isSmallScreen ? Values.smallSpacing : Values.largeSpacing) + return bounds.insetBy(dx: horizontalInset, dy: verticalInset) } else { return bounds.insetBy(dx: Values.mediumSpacing, dy: Values.smallSpacing) } diff --git a/Signal/src/Loki/Utilities/MentionUtilities.swift b/Signal/src/Loki/Utilities/MentionUtilities.swift index b4c92f00b..96c8b7a5b 100644 --- a/Signal/src/Loki/Utilities/MentionUtilities.swift +++ b/Signal/src/Loki/Utilities/MentionUtilities.swift @@ -9,7 +9,7 @@ public final class MentionUtilities : NSObject { } @objc public static func highlightMentions(in string: String, isOutgoingMessage: Bool, threadID: String, attributes: [NSAttributedString.Key:Any]) -> NSAttributedString { - let userHexEncodedPublicKey = OWSIdentityManager.shared().identityKeyPair()!.hexEncodedPublicKey + let userHexEncodedPublicKey = getUserHexEncodedPublicKey() var publicChat: LokiPublicChat? var userLinkedDeviceHexEncodedPublicKeys: Set! OWSPrimaryStorage.shared().dbReadConnection.read { transaction in diff --git a/Signal/src/Loki/View Controllers/DeviceLinkingModal.swift b/Signal/src/Loki/View Controllers/DeviceLinkingModal.swift index 898f11432..d18aa4a6c 100644 --- a/Signal/src/Loki/View Controllers/DeviceLinkingModal.swift +++ b/Signal/src/Loki/View Controllers/DeviceLinkingModal.swift @@ -24,8 +24,9 @@ final class DeviceLinkingModal : Modal, DeviceLinkingSessionDelegate { private lazy var qrCodeImageView: UIImageView = { let result = UIImageView() result.contentMode = .scaleAspectFit - result.set(.width, to: 128) - result.set(.height, to: 128) + let size: CGFloat = 128 + result.set(.width, to: size) + result.set(.height, to: size) return result }() @@ -116,7 +117,7 @@ final class DeviceLinkingModal : Modal, DeviceLinkingSessionDelegate { stackView.axis = .vertical switch mode { case .master: - let hexEncodedPublicKey = OWSIdentityManager.shared().identityKeyPair()!.hexEncodedPublicKey + let hexEncodedPublicKey = getUserHexEncodedPublicKey() qrCodeImageView.image = QRCode.generate(for: hexEncodedPublicKey, hasBackground: true) case .slave: spinner.set(.height, to: 64) @@ -136,7 +137,7 @@ final class DeviceLinkingModal : Modal, DeviceLinkingSessionDelegate { }() mnemonicLabel.isHidden = (mode == .master) if mode == .slave { - let hexEncodedPublicKey = OWSIdentityManager.shared().identityKeyPair()!.hexEncodedPublicKey.removing05PrefixIfNeeded() + let hexEncodedPublicKey = getUserHexEncodedPublicKey().removing05PrefixIfNeeded() mnemonicLabel.text = Mnemonic.hash(hexEncodedString: hexEncodedPublicKey) } authorizeButton.addTarget(self, action: #selector(authorizeDeviceLink), for: UIControl.Event.touchUpInside) diff --git a/Signal/src/Loki/View Controllers/DeviceLinksVC.swift b/Signal/src/Loki/View Controllers/DeviceLinksVC.swift index 159835340..c99cd79f8 100644 --- a/Signal/src/Loki/View Controllers/DeviceLinksVC.swift +++ b/Signal/src/Loki/View Controllers/DeviceLinksVC.swift @@ -86,7 +86,7 @@ final class DeviceLinksVC : UIViewController, UITableViewDataSource, UITableView // MARK: Updating private func updateDeviceLinks() { let storage = OWSPrimaryStorage.shared() - let userHexEncodedPublicKey = OWSIdentityManager.shared().identityKeyPair()!.hexEncodedPublicKey + let userHexEncodedPublicKey = getUserHexEncodedPublicKey() var deviceLinks: [DeviceLink] = [] storage.dbReadConnection.read { transaction in deviceLinks = storage.getDeviceLinks(for: userHexEncodedPublicKey, in: transaction).sorted { lhs, rhs in diff --git a/Signal/src/Loki/View Controllers/GroupMembersVC.swift b/Signal/src/Loki/View Controllers/GroupMembersVC.swift index ea652a9d7..abd24df01 100644 --- a/Signal/src/Loki/View Controllers/GroupMembersVC.swift +++ b/Signal/src/Loki/View Controllers/GroupMembersVC.swift @@ -7,12 +7,7 @@ final class GroupMembersVC : UIViewController, UITableViewDataSource { func getDisplayName(for hexEncodedPublicKey: String) -> String { return DisplayNameUtilities.getPrivateChatDisplayName(for: hexEncodedPublicKey) ?? "Unknown Contact" } - let userHexEncodedPublicKey = OWSIdentityManager.shared().identityKeyPair()!.hexEncodedPublicKey - var linkedDeviceHexEncodedPublicKeys: Set = [ userHexEncodedPublicKey ] - OWSPrimaryStorage.shared().dbReadConnection.read { transaction in - linkedDeviceHexEncodedPublicKeys = LokiDatabaseUtilities.getLinkedDeviceHexEncodedPublicKeys(for: userHexEncodedPublicKey, in: transaction) - } - return thread.groupModel.groupMemberIds.filter { !linkedDeviceHexEncodedPublicKeys.contains($0) }.sorted { getDisplayName(for: $0) < getDisplayName(for: $1) } + return GroupUtilities.getClosedGroupMembers(thread).sorted { getDisplayName(for: $0) < getDisplayName(for: $1) } }() // MARK: Components diff --git a/Signal/src/Loki/View Controllers/HomeVC.swift b/Signal/src/Loki/View Controllers/HomeVC.swift index fb09a2182..09b037ce6 100644 --- a/Signal/src/Loki/View Controllers/HomeVC.swift +++ b/Signal/src/Loki/View Controllers/HomeVC.swift @@ -244,7 +244,7 @@ final class HomeVC : UIViewController, UITableViewDataSource, UITableViewDelegat let profilePictureSize = Values.verySmallProfilePictureSize let profilePictureView = ProfilePictureView() profilePictureView.size = profilePictureSize - let userHexEncodedPublicKey = OWSIdentityManager.shared().identityKeyPair()!.hexEncodedPublicKey + let userHexEncodedPublicKey = getUserHexEncodedPublicKey() profilePictureView.hexEncodedPublicKey = userHexEncodedPublicKey profilePictureView.update() profilePictureView.set(.width, to: profilePictureSize) @@ -267,7 +267,7 @@ final class HomeVC : UIViewController, UITableViewDataSource, UITableViewDelegat // MARK: Interaction func handleContinueButtonTapped(from seedReminderView: SeedReminderView) { - let seedVC = SeedVCV2() + let seedVC = SeedVC() let navigationController = OWSNavigationController(rootViewController: seedVC) present(navigationController, animated: true, completion: nil) } diff --git a/Signal/src/Loki/View Controllers/NewClosedGroupVC.swift b/Signal/src/Loki/View Controllers/NewClosedGroupVC.swift index 46e7fd420..64ffa261b 100644 --- a/Signal/src/Loki/View Controllers/NewClosedGroupVC.swift +++ b/Signal/src/Loki/View Controllers/NewClosedGroupVC.swift @@ -12,7 +12,7 @@ final class NewClosedGroupVC : UIViewController, UITableViewDataSource, UITableV func getDisplayName(for hexEncodedPublicKey: String) -> String { return DisplayNameUtilities.getPrivateChatDisplayName(for: hexEncodedPublicKey) ?? "Unknown Contact" } - let userHexEncodedPublicKey = OWSIdentityManager.shared().identityKeyPair()!.hexEncodedPublicKey + let userHexEncodedPublicKey = getUserHexEncodedPublicKey() var linkedDeviceHexEncodedPublicKeys: Set = [ userHexEncodedPublicKey ] OWSPrimaryStorage.shared().dbReadConnection.read { transaction in linkedDeviceHexEncodedPublicKeys = LokiDatabaseUtilities.getLinkedDeviceHexEncodedPublicKeys(for: userHexEncodedPublicKey, in: transaction) @@ -147,7 +147,7 @@ final class NewClosedGroupVC : UIViewController, UITableViewDataSource, UITableV guard selectedContacts.count >= 2 else { return showError(title: NSLocalizedString("Please pick at least 2 group members", comment: "")) } - let userHexEncodedPublicKey = OWSIdentityManager.shared().identityKeyPair()!.hexEncodedPublicKey + let userHexEncodedPublicKey = getUserHexEncodedPublicKey() let members = [String](selectedContacts) + [ userHexEncodedPublicKey ] let admins = [ userHexEncodedPublicKey ] let groupID = LKGroupUtilities.getEncodedClosedGroupIDAsData(Randomness.generateRandomBytes(kGroupIdLength)!.toHexString()) diff --git a/Signal/src/Loki/View Controllers/NewPrivateChatVC.swift b/Signal/src/Loki/View Controllers/NewPrivateChatVC.swift index b7902d865..9fccf24f8 100644 --- a/Signal/src/Loki/View Controllers/NewPrivateChatVC.swift +++ b/Signal/src/Loki/View Controllers/NewPrivateChatVC.swift @@ -158,7 +158,7 @@ private final class EnterPublicKeyVC : UIViewController { if let masterHexEncodedPublicKey = UserDefaults.standard.string(forKey: "masterDeviceHexEncodedPublicKey") { return masterHexEncodedPublicKey } else { - return OWSIdentityManager.shared().identityKeyPair()!.hexEncodedPublicKey + return getUserHexEncodedPublicKey() } }() diff --git a/Signal/src/Loki/View Controllers/QRCodeVC.swift b/Signal/src/Loki/View Controllers/QRCodeVC.swift index fcb5297fa..cf11b829c 100644 --- a/Signal/src/Loki/View Controllers/QRCodeVC.swift +++ b/Signal/src/Loki/View Controllers/QRCodeVC.swift @@ -155,7 +155,7 @@ private final class ViewMyQRCodeVC : UIViewController { if let masterHexEncodedPublicKey = UserDefaults.standard.string(forKey: "masterDeviceHexEncodedPublicKey") { return masterHexEncodedPublicKey } else { - return OWSIdentityManager.shared().identityKeyPair()!.hexEncodedPublicKey + return getUserHexEncodedPublicKey() } }() diff --git a/Signal/src/Loki/View Controllers/SeedVCV2.swift b/Signal/src/Loki/View Controllers/SeedVC.swift similarity index 99% rename from Signal/src/Loki/View Controllers/SeedVCV2.swift rename to Signal/src/Loki/View Controllers/SeedVC.swift index ba48823d7..a07518575 100644 --- a/Signal/src/Loki/View Controllers/SeedVCV2.swift +++ b/Signal/src/Loki/View Controllers/SeedVC.swift @@ -1,5 +1,5 @@ -final class SeedVCV2 : UIViewController { +final class SeedVC : UIViewController { private let mnemonic: String = { let identityManager = OWSIdentityManager.shared() diff --git a/Signal/src/Loki/View Controllers/SettingsVC.swift b/Signal/src/Loki/View Controllers/SettingsVC.swift index 1ba453853..acf7b3ab2 100644 --- a/Signal/src/Loki/View Controllers/SettingsVC.swift +++ b/Signal/src/Loki/View Controllers/SettingsVC.swift @@ -8,7 +8,7 @@ final class SettingsVC : UIViewController, AvatarViewHelperDelegate { if let masterHexEncodedPublicKey = UserDefaults.standard.string(forKey: "masterDeviceHexEncodedPublicKey") { return masterHexEncodedPublicKey } else { - return OWSIdentityManager.shared().identityKeyPair()!.hexEncodedPublicKey + return getUserHexEncodedPublicKey() } }() diff --git a/SignalMessaging/Views/AvatarImageView.swift b/SignalMessaging/Views/AvatarImageView.swift index bc9167bd9..54c80445d 100644 --- a/SignalMessaging/Views/AvatarImageView.swift +++ b/SignalMessaging/Views/AvatarImageView.swift @@ -88,7 +88,7 @@ public class AvatarImageView: UIImageView { let peerInfo = LokiP2PAPI.getInfo(for: contactID) let isOnline = peerInfo?.isOnline ?? false let color: UIColor = isOnline ? .lokiGreen() : .lokiGray() - let currentUserID = OWSIdentityManager.shared().identityKeyPair()!.hexEncodedPublicKey + let currentUserID = getUserHexEncodedPublicKey() let isCurrentUser = (contactID == currentUserID) layer.borderColor = isCurrentUser ? UIColor.clear.cgColor : color.cgColor } diff --git a/SignalServiceKit/src/Loki/API/LokiAPI.swift b/SignalServiceKit/src/Loki/API/LokiAPI.swift index 6a13778a5..24d059124 100644 --- a/SignalServiceKit/src/Loki/API/LokiAPI.swift +++ b/SignalServiceKit/src/Loki/API/LokiAPI.swift @@ -9,7 +9,7 @@ public final class LokiAPI : NSObject { // MARK: Convenience internal static let storage = OWSPrimaryStorage.shared() - internal static let userHexEncodedPublicKey = OWSIdentityManager.shared().identityKeyPair()!.hexEncodedPublicKey + internal static let userHexEncodedPublicKey = getUserHexEncodedPublicKey() // MARK: Settings private static let version = "v1" diff --git a/SignalServiceKit/src/Loki/API/LokiLongPoller.swift b/SignalServiceKit/src/Loki/API/LokiLongPoller.swift index d645ba7e6..4d89ac412 100644 --- a/SignalServiceKit/src/Loki/API/LokiLongPoller.swift +++ b/SignalServiceKit/src/Loki/API/LokiLongPoller.swift @@ -14,7 +14,7 @@ public final class LokiLongPoller : NSObject { private let retryInterval: TimeInterval = 4 // MARK: Convenience - private var userHexEncodedPublicKey: String { return OWSIdentityManager.shared().identityKeyPair()!.hexEncodedPublicKey } + private var userHexEncodedPublicKey: String { return getUserHexEncodedPublicKey() } // MARK: Initialization @objc public init(onMessagesReceived: @escaping ([SSKProtoEnvelope]) -> Void) { diff --git a/SignalServiceKit/src/Loki/API/LokiP2PAPI.swift b/SignalServiceKit/src/Loki/API/LokiP2PAPI.swift index e6ca27f2a..69887139a 100644 --- a/SignalServiceKit/src/Loki/API/LokiP2PAPI.swift +++ b/SignalServiceKit/src/Loki/API/LokiP2PAPI.swift @@ -6,7 +6,7 @@ public class LokiP2PAPI : NSObject { private static let storage = OWSPrimaryStorage.shared() private static let messageSender = SSKEnvironment.shared.messageSender private static let messageReceiver = SSKEnvironment.shared.messageReceiver - private static let ourHexEncodedPubKey = OWSIdentityManager.shared().identityKeyPair()!.hexEncodedPublicKey + private static let ourHexEncodedPubKey = getUserHexEncodedPublicKey() /// The amount of time before pinging when a user is set to offline private static let offlinePingTime = 2 * kMinuteInterval diff --git a/SignalServiceKit/src/Loki/API/Multi Device/DeviceLink.swift b/SignalServiceKit/src/Loki/API/Multi Device/DeviceLink.swift index 733bbf3b9..76822e544 100644 --- a/SignalServiceKit/src/Loki/API/Multi Device/DeviceLink.swift +++ b/SignalServiceKit/src/Loki/API/Multi Device/DeviceLink.swift @@ -7,7 +7,7 @@ public final class DeviceLink : NSObject, NSCoding { @objc public var isAuthorized: Bool { return master.signature != nil } @objc public var other: Device { - let userHexEncodedPublicKey = OWSIdentityManager.shared().identityKeyPair()!.hexEncodedPublicKey + let userHexEncodedPublicKey = getUserHexEncodedPublicKey() return (userHexEncodedPublicKey == master.hexEncodedPublicKey) ? slave : master } diff --git a/SignalServiceKit/src/Loki/API/Multi Device/DeviceLinkingSession.swift b/SignalServiceKit/src/Loki/API/Multi Device/DeviceLinkingSession.swift index 3bdf3b290..35c736c1e 100644 --- a/SignalServiceKit/src/Loki/API/Multi Device/DeviceLinkingSession.swift +++ b/SignalServiceKit/src/Loki/API/Multi Device/DeviceLinkingSession.swift @@ -31,7 +31,7 @@ public final class DeviceLinkingSession : NSObject { } @objc public func processLinkingRequest(from slaveHexEncodedPublicKey: String, to masterHexEncodedPublicKey: String, with slaveSignature: Data) { - guard isListeningForLinkingRequests, !isProcessingLinkingRequest, masterHexEncodedPublicKey == OWSIdentityManager.shared().identityKeyPair()!.hexEncodedPublicKey else { return } + guard isListeningForLinkingRequests, !isProcessingLinkingRequest, masterHexEncodedPublicKey == getUserHexEncodedPublicKey() else { return } let master = DeviceLink.Device(hexEncodedPublicKey: masterHexEncodedPublicKey) let slave = DeviceLink.Device(hexEncodedPublicKey: slaveHexEncodedPublicKey, signature: slaveSignature) let deviceLink = DeviceLink(between: master, and: slave) @@ -43,7 +43,7 @@ public final class DeviceLinkingSession : NSObject { } @objc public func processLinkingAuthorization(from masterHexEncodedPublicKey: String, for slaveHexEncodedPublicKey: String, masterSignature: Data, slaveSignature: Data) { - guard isListeningForLinkingAuthorization, slaveHexEncodedPublicKey == OWSIdentityManager.shared().identityKeyPair()!.hexEncodedPublicKey else { return } + guard isListeningForLinkingAuthorization, slaveHexEncodedPublicKey == getUserHexEncodedPublicKey() else { return } let master = DeviceLink.Device(hexEncodedPublicKey: masterHexEncodedPublicKey, signature: masterSignature) let slave = DeviceLink.Device(hexEncodedPublicKey: slaveHexEncodedPublicKey, signature: slaveSignature) let deviceLink = DeviceLink(between: master, and: slave) diff --git a/SignalServiceKit/src/Loki/API/Public Chat/LokiPublicChatPoller.swift b/SignalServiceKit/src/Loki/API/Public Chat/LokiPublicChatPoller.swift index 9bad36114..f5316774d 100644 --- a/SignalServiceKit/src/Loki/API/Public Chat/LokiPublicChatPoller.swift +++ b/SignalServiceKit/src/Loki/API/Public Chat/LokiPublicChatPoller.swift @@ -10,7 +10,7 @@ public final class LokiPublicChatPoller : NSObject { private var pollForModeratorsTimer: Timer? = nil private var pollForDisplayNamesTimer: Timer? = nil private var hasStarted = false - private let userHexEncodedPublicKey = OWSIdentityManager.shared().identityKeyPair()!.hexEncodedPublicKey + private let userHexEncodedPublicKey = getUserHexEncodedPublicKey() // MARK: Settings private let pollForNewMessagesInterval: TimeInterval = 4 diff --git a/SignalServiceKit/src/Loki/Database/LokiDatabaseUtilities.swift b/SignalServiceKit/src/Loki/Database/LokiDatabaseUtilities.swift index 1d94a962c..90326cf11 100644 --- a/SignalServiceKit/src/Loki/Database/LokiDatabaseUtilities.swift +++ b/SignalServiceKit/src/Loki/Database/LokiDatabaseUtilities.swift @@ -12,7 +12,7 @@ public final class LokiDatabaseUtilities : NSObject { if let message = interaction as? TSIncomingMessage { senderHexEncodedPublicKey = message.authorId } else if let message = interaction as? TSOutgoingMessage { - senderHexEncodedPublicKey = OWSIdentityManager.shared().identityKeyPair()!.hexEncodedPublicKey + senderHexEncodedPublicKey = getUserHexEncodedPublicKey() } else { return false } @@ -40,7 +40,7 @@ public final class LokiDatabaseUtilities : NSObject { @objc(isUserLinkedDevice:in:) public static func isUserLinkedDevice(_ hexEncodedPublicKey: String, transaction: YapDatabaseReadTransaction) -> Bool { - let userHexEncodedPublicKey = OWSIdentityManager.shared().identityKeyPair()!.hexEncodedPublicKey + let userHexEncodedPublicKey = getUserHexEncodedPublicKey() let userLinkedDeviceHexEncodedPublicKeys = getLinkedDeviceHexEncodedPublicKeys(for: userHexEncodedPublicKey, in: transaction) return userLinkedDeviceHexEncodedPublicKeys.contains(hexEncodedPublicKey) } diff --git a/SignalServiceKit/src/Loki/Utilities/DisplayNameUtilities.swift b/SignalServiceKit/src/Loki/Utilities/DisplayNameUtilities.swift index 262be8037..4ab122f3b 100644 --- a/SignalServiceKit/src/Loki/Utilities/DisplayNameUtilities.swift +++ b/SignalServiceKit/src/Loki/Utilities/DisplayNameUtilities.swift @@ -7,7 +7,7 @@ public final class DisplayNameUtilities : NSObject { override private init() { } private static var userHexEncodedPublicKey: String { - return OWSIdentityManager.shared().identityKeyPair()!.hexEncodedPublicKey + return getUserHexEncodedPublicKey() } private static var userDisplayName: String? { diff --git a/SignalServiceKit/src/Loki/Utilities/General.swift b/SignalServiceKit/src/Loki/Utilities/General.swift new file mode 100644 index 000000000..d05012c3d --- /dev/null +++ b/SignalServiceKit/src/Loki/Utilities/General.swift @@ -0,0 +1,4 @@ + +public func getUserHexEncodedPublicKey() -> String { + return OWSIdentityManager.shared().identityKeyPair()!.hexEncodedPublicKey +} diff --git a/SignalServiceKit/src/Loki/Utilities/GroupUtilities.swift b/SignalServiceKit/src/Loki/Utilities/GroupUtilities.swift new file mode 100644 index 000000000..b0a72d2c7 --- /dev/null +++ b/SignalServiceKit/src/Loki/Utilities/GroupUtilities.swift @@ -0,0 +1,25 @@ + +public enum GroupUtilities { + + public static func getClosedGroupMembers(_ closedGroup: TSGroupThread) -> [String] { + var result: [String]! + OWSPrimaryStorage.shared().dbReadConnection.read { transaction in + result = getClosedGroupMembers(closedGroup, with: transaction) + } + return result + } + + public static func getClosedGroupMembers(_ closedGroup: TSGroupThread, with transaction: YapDatabaseReadTransaction) -> [String] { + let userHexEncodedPublicKey = getUserHexEncodedPublicKey() + let linkedDeviceHexEncodedPublicKeys = LokiDatabaseUtilities.getLinkedDeviceHexEncodedPublicKeys(for: userHexEncodedPublicKey, in: transaction) + return closedGroup.groupModel.groupMemberIds.filter { !linkedDeviceHexEncodedPublicKeys.contains($0) } + } + + public static func getClosedGroupMemberCount(_ closedGroup: TSGroupThread) -> Int { + return getClosedGroupMembers(closedGroup).count + } + + public static func getClosedGroupMemberCount(_ closedGroup: TSGroupThread, with transaction: YapDatabaseReadTransaction) -> Int { + return getClosedGroupMembers(closedGroup, with: transaction).count + } +} diff --git a/SignalServiceKit/src/Messages/OWSMessageManager.m b/SignalServiceKit/src/Messages/OWSMessageManager.m index d95279595..3b45c38a5 100644 --- a/SignalServiceKit/src/Messages/OWSMessageManager.m +++ b/SignalServiceKit/src/Messages/OWSMessageManager.m @@ -577,7 +577,6 @@ NS_ASSUME_NONNULL_BEGIN [self handleProfileKeyUpdateIfNeeded:dataMessage recipientId:envelope.source]; - if (dataMessage.group) { TSGroupThread *_Nullable groupThread = [TSGroupThread threadWithGroupId:dataMessage.group.id transaction:transaction];