diff --git a/Signal.xcodeproj/project.pbxproj b/Signal.xcodeproj/project.pbxproj index 2c08f62f4..31219927d 100644 --- a/Signal.xcodeproj/project.pbxproj +++ b/Signal.xcodeproj/project.pbxproj @@ -568,6 +568,7 @@ B82584A02315024B001B41CB /* RSSFeedPoller.swift in Sources */ = {isa = PBXBuildFile; fileRef = B825849F2315024B001B41CB /* RSSFeedPoller.swift */; }; B846365B22B7418B00AF1514 /* Identicon+ObjC.swift in Sources */ = {isa = PBXBuildFile; fileRef = B846365A22B7418B00AF1514 /* Identicon+ObjC.swift */; }; B84664F5235022F30083A1CD /* MentionUtilities.swift in Sources */ = {isa = PBXBuildFile; fileRef = B84664F4235022F30083A1CD /* MentionUtilities.swift */; }; + B84664F923541F070083A1CD /* DisplayNameUtilities.swift in Sources */ = {isa = PBXBuildFile; fileRef = B84664F823541F070083A1CD /* DisplayNameUtilities.swift */; }; B86BD08123399883000F5AE3 /* QRCodeModal.swift in Sources */ = {isa = PBXBuildFile; fileRef = B86BD08023399883000F5AE3 /* QRCodeModal.swift */; }; B86BD08423399ACF000F5AE3 /* Modal.swift in Sources */ = {isa = PBXBuildFile; fileRef = B86BD08323399ACF000F5AE3 /* Modal.swift */; }; B86BD08623399CEF000F5AE3 /* SeedModal.swift in Sources */ = {isa = PBXBuildFile; fileRef = B86BD08523399CEF000F5AE3 /* SeedModal.swift */; }; @@ -1380,6 +1381,7 @@ B825849F2315024B001B41CB /* RSSFeedPoller.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RSSFeedPoller.swift; sourceTree = ""; }; B846365A22B7418B00AF1514 /* Identicon+ObjC.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Identicon+ObjC.swift"; sourceTree = ""; }; B84664F4235022F30083A1CD /* MentionUtilities.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MentionUtilities.swift; sourceTree = ""; }; + B84664F823541F070083A1CD /* DisplayNameUtilities.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DisplayNameUtilities.swift; sourceTree = ""; }; B86BD08023399883000F5AE3 /* QRCodeModal.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = QRCodeModal.swift; sourceTree = ""; }; B86BD08323399ACF000F5AE3 /* Modal.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Modal.swift; sourceTree = ""; }; B86BD08523399CEF000F5AE3 /* SeedModal.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SeedModal.swift; sourceTree = ""; }; @@ -2671,6 +2673,7 @@ B846365A22B7418B00AF1514 /* Identicon+ObjC.swift */, 241C6312231F5F1D00B4198E /* CGFloat+Rounding.swift */, 241C6310231F5C4400B4198E /* UIColor+Helper.swift */, + B84664F823541F070083A1CD /* DisplayNameUtilities.swift */, ); path = Loki; sourceTree = ""; @@ -3575,6 +3578,7 @@ 450998681FD8C0FF00D89EB3 /* AttachmentSharing.m in Sources */, 347850711FDAEB17007B8332 /* OWSUserProfile.m in Sources */, 346129F81FD5F31400532771 /* OWS100RemoveTSRecipientsMigration.m in Sources */, + B84664F923541F070083A1CD /* DisplayNameUtilities.swift in Sources */, 34AC09DF211B39B100997B47 /* OWSNavigationController.m in Sources */, 34074F61203D0CBE004596AE /* OWSSounds.m in Sources */, 34BEDB1721C80BCA007B0EAE /* OWSAnyTouchGestureRecognizer.m in Sources */, diff --git a/Signal/src/Loki/NewPublicChatVC.swift b/Signal/src/Loki/NewPublicChatVC.swift index f815a149d..2d0e95260 100644 --- a/Signal/src/Loki/NewPublicChatVC.swift +++ b/Signal/src/Loki/NewPublicChatVC.swift @@ -83,11 +83,12 @@ final class NewPublicChatVC : OWSViewController { return showError(title: NSLocalizedString("Invalid URL", comment: ""), message: NSLocalizedString("Please check the URL you entered and try again.", comment: "")) } updateAddButton(isConnecting: true) + let channelID: UInt64 = 1 let urlAsString = url.absoluteString let displayName = OWSProfileManager.shared().localProfileName() - LokiPublicChatManager.shared.addChat(server: urlAsString, channel: 1) + LokiPublicChatManager.shared.addChat(server: urlAsString, channel: channelID) .done(on: .main) { [weak self] _ in - let _ = LokiGroupChatAPI.getMessages(for: 1, on: urlAsString) + let _ = LokiGroupChatAPI.getMessages(for: channelID, on: urlAsString) let _ = LokiGroupChatAPI.setDisplayName(to: displayName, on: urlAsString) self?.presentingViewController!.dismiss(animated: true, completion: nil) } diff --git a/Signal/src/Loki/Utilities/MentionUtilities.swift b/Signal/src/Loki/Utilities/MentionUtilities.swift index 0a1247bdb..3268a045d 100644 --- a/Signal/src/Loki/Utilities/MentionUtilities.swift +++ b/Signal/src/Loki/Utilities/MentionUtilities.swift @@ -4,21 +4,21 @@ public final class MentionUtilities : NSObject { override private init() { } - @objc public static func highlightMentions(in string: String, thread: TSThread) -> String { - return highlightMentions(in: string, isOutgoingMessage: false, thread: thread, attributes: [:]).string // isOutgoingMessage and attributes are irrelevant + @objc public static func highlightMentions(in string: String, threadID: String) -> String { + return highlightMentions(in: string, isOutgoingMessage: false, threadID: threadID, attributes: [:]).string // isOutgoingMessage and attributes are irrelevant } - @objc public static func highlightMentions(in string: String, isOutgoingMessage: Bool, thread: TSThread, attributes: [NSAttributedString.Key:Any]) -> NSAttributedString { + @objc public static func highlightMentions(in string: String, isOutgoingMessage: Bool, threadID: String, attributes: [NSAttributedString.Key:Any]) -> NSAttributedString { var groupChat: LokiGroupChat? OWSPrimaryStorage.shared().dbReadConnection.read { transaction in - groupChat = LokiDatabaseUtilities.objc_getGroupChat(for: thread.uniqueId!, in: transaction) + groupChat = LokiDatabaseUtilities.getGroupChat(for: threadID, in: transaction) } var string = string let regex = try! NSRegularExpression(pattern: "@[0-9a-fA-F]*", options: []) - let knownUserHexEncodedPublicKeys = LokiAPI.userHexEncodedPublicKeyCache[thread.uniqueId!] ?? [] // Should always be populated at this point + let knownUserHexEncodedPublicKeys = LokiAPI.userHexEncodedPublicKeyCache[threadID] ?? [] // Should always be populated at this point var mentions: [NSRange] = [] var outerMatch = regex.firstMatch(in: string, options: .withoutAnchoringBounds, range: NSRange(location: 0, length: string.count)) - while let match = outerMatch, thread.isGroupThread() { + while let match = outerMatch { let hexEncodedPublicKey = String((string as NSString).substring(with: match.range).dropFirst()) // Drop the @ let matchEnd: Int if knownUserHexEncodedPublicKeys.contains(hexEncodedPublicKey) { @@ -27,12 +27,9 @@ public final class MentionUtilities : NSObject { userDisplayName = OWSProfileManager.shared().localProfileName() } else { if let groupChat = groupChat { - OWSPrimaryStorage.shared().dbReadConnection.read { transaction in - let collection = "\(groupChat.server).\(groupChat.channel)" - userDisplayName = transaction.object(forKey: hexEncodedPublicKey, inCollection: collection) as! String? - } + userDisplayName = DisplayNameUtilities.getGroupChatDisplayName(for: hexEncodedPublicKey, in: groupChat.channel, on: groupChat.server) } else { - userDisplayName = nil // TODO: Implement + userDisplayName = DisplayNameUtilities.getPrivateChatDisplayName(for: hexEncodedPublicKey) } } if let userDisplayName = userDisplayName { diff --git a/Signal/src/ViewControllers/ConversationView/Cells/OWSMessageCell.m b/Signal/src/ViewControllers/ConversationView/Cells/OWSMessageCell.m index a8f59839c..ecde97570 100644 --- a/Signal/src/ViewControllers/ConversationView/Cells/OWSMessageCell.m +++ b/Signal/src/ViewControllers/ConversationView/Cells/OWSMessageCell.m @@ -303,13 +303,10 @@ NS_ASSUME_NONNULL_BEGIN [OWSPrimaryStorage.sharedManager.dbReadConnection readWithBlock:^(YapDatabaseReadTransaction *transaction) { groupChat = [LKDatabaseUtilities getGroupChatForThreadID:self.viewItem.interaction.uniqueThreadId transaction: transaction]; }]; - - if (groupChat != nil) { - BOOL isModerator = [LKGroupChatAPI isUserModerator:incomingMessage.authorId forGroup:groupChat.channel onServer:groupChat.server]; - UIImage *moderatorIcon = [UIImage imageNamed:@"Crown"]; - self.moderatorIconImageView.image = moderatorIcon; - self.moderatorIconImageView.hidden = !isModerator; - } + BOOL isModerator = [LKGroupChatAPI isUserModerator:incomingMessage.authorId forGroup:groupChat.channel onServer:groupChat.server]; + UIImage *moderatorIcon = [UIImage imageNamed:@"Crown"]; + self.moderatorIconImageView.image = moderatorIcon; + self.moderatorIconImageView.hidden = !isModerator; } [self.contentView addSubview:self.moderatorIconImageView]; diff --git a/Signal/src/ViewControllers/ConversationView/Cells/OWSQuotedMessageView.m b/Signal/src/ViewControllers/ConversationView/Cells/OWSQuotedMessageView.m index 65409f096..680a1df7d 100644 --- a/Signal/src/ViewControllers/ConversationView/Cells/OWSQuotedMessageView.m +++ b/Signal/src/ViewControllers/ConversationView/Cells/OWSQuotedMessageView.m @@ -554,11 +554,11 @@ const CGFloat kRemotelySourcedContentRowSpacing = 3; if (quotedAuthor == self.quotedMessage.authorId) { [OWSPrimaryStorage.sharedManager.dbReadConnection readWithBlock:^(YapDatabaseReadTransaction *transaction) { - LKGroupChat *chat = [LKDatabaseUtilities getGroupChatForThreadID:self.quotedMessage.threadId transaction:transaction]; - if (chat != nil) { - NSString *collection = [NSString stringWithFormat:@"%@.%@", chat.server, @(chat.channel)]; - NSString *displayName = [transaction stringForKey:self.quotedMessage.authorId inCollection:collection]; - if (displayName != nil) { quotedAuthor = displayName; } + LKGroupChat *groupChat = [LKDatabaseUtilities getGroupChatForThreadID:self.quotedMessage.threadId transaction:transaction]; + if (groupChat != nil) { + quotedAuthor = [LKDisplayNameUtilities getGroupChatDisplayNameFor:self.quotedMessage.authorId in:groupChat.channel on:groupChat.server using:transaction];; + } else { + quotedAuthor = [LKDisplayNameUtilities getPrivateChatDisplayNameFor:self.quotedMessage.authorId]; } }]; } diff --git a/Signal/src/ViewControllers/ConversationView/ConversationViewItem.m b/Signal/src/ViewControllers/ConversationView/ConversationViewItem.m index 5421291e9..cdc1ee68a 100644 --- a/Signal/src/ViewControllers/ConversationView/ConversationViewItem.m +++ b/Signal/src/ViewControllers/ConversationView/ConversationViewItem.m @@ -1262,7 +1262,7 @@ NSString *NSStringForOWSMessageCellType(OWSMessageCellType cellType) TSMessage *message = (TSMessage *)self.interaction; if (!message.isGroupChatMessage) return false; - // Make sure we have the details to contact the server + // Ensure we have the details needed to contact the server __block LKGroupChat *groupChat; [self.primaryStorage.dbReadConnection readWithBlock:^(YapDatabaseReadTransaction *transaction) { groupChat = [LKDatabaseUtilities getGroupChatForThreadID:groupThread.uniqueId transaction: transaction]; diff --git a/Signal/src/ViewControllers/DebugUI/DebugUIMessages.m b/Signal/src/ViewControllers/DebugUI/DebugUIMessages.m index 67b29b6fe..23a2caee2 100644 --- a/Signal/src/ViewControllers/DebugUI/DebugUIMessages.m +++ b/Signal/src/ViewControllers/DebugUI/DebugUIMessages.m @@ -2016,7 +2016,7 @@ NS_ASSUME_NONNULL_BEGIN transaction:transaction conversationStyle:conversationStyle]; quotedMessage = [ - [OWSQuotedReplyModel quotedReplyForSendingWithConversationViewItem:viewItem threadId:viewItem.interaction.uniqueThreadId transaction:transaction] + [OWSQuotedReplyModel quotedReplyForSendingWithConversationViewItem:viewItem threadId:viewItem.interaction.uniqueThreadId transaction:transaction] buildQuotedMessageForSending]; } else { TSOutgoingMessage *_Nullable messageToQuote = [self createFakeOutgoingMessage:thread diff --git a/Signal/src/ViewControllers/HomeView/HomeViewController.m b/Signal/src/ViewControllers/HomeView/HomeViewController.m index bcdfec4f5..23286342f 100644 --- a/Signal/src/ViewControllers/HomeView/HomeViewController.m +++ b/Signal/src/ViewControllers/HomeView/HomeViewController.m @@ -787,21 +787,15 @@ typedef NS_ENUM(NSInteger, HomeViewControllerSection) { self.navigationItem.leftBarButtonItem = settingsButton; SET_SUBVIEW_ACCESSIBILITY_IDENTIFIER(self, settingsButton); - UIBarButtonItem *newConversation = [[UIBarButtonItem alloc] + UIBarButtonItem *newPrivateChatButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCompose target:self - action:@selector(showNewConversationView) + action:@selector(showNewConversationVC) accessibilityIdentifier:ACCESSIBILITY_IDENTIFIER_WITH_NAME(self, @"compose")]; - UIBarButtonItem *newServer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd - target:self - action:@selector(showNewPublicChatView) - accessibilityIdentifier:ACCESSIBILITY_IDENTIFIER_WITH_NAME(self, @"addServer")]; + UIBarButtonItem *newGroupChatButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"group-avatar"] style:UIBarButtonItemStylePlain target:self action:@selector(showNewPublicChatVC)]; - self.navigationItem.rightBarButtonItems = @[ - newConversation, - newServer, - ]; + self.navigationItem.rightBarButtonItems = @[ newPrivateChatButton, newGroupChatButton ]; } - (void)settingsButtonPressed:(id)sender @@ -843,7 +837,7 @@ typedef NS_ENUM(NSInteger, HomeViewControllerSection) { [self.navigationController pushViewController:vc animated:NO]; } -- (void)showNewConversationView +- (void)showNewConversationVC { LKNewConversationVC *newConversationVC = [LKNewConversationVC new]; OWSNavigationController *navigationController = [[OWSNavigationController alloc] initWithRootViewController:newConversationVC]; @@ -871,7 +865,7 @@ typedef NS_ENUM(NSInteger, HomeViewControllerSection) { */ } -- (void)showNewPublicChatView +- (void)showNewPublicChatVC { LKNewPublicChatVC *newPublicChatVC = [LKNewPublicChatVC new]; OWSNavigationController *navigationController = [[OWSNavigationController alloc] initWithRootViewController:newPublicChatVC]; diff --git a/Signal/translations/en.lproj/Localizable.strings b/Signal/translations/en.lproj/Localizable.strings index af0bc2ac6..ae25f7404 100644 --- a/Signal/translations/en.lproj/Localizable.strings +++ b/Signal/translations/en.lproj/Localizable.strings @@ -2552,7 +2552,6 @@ "Password (Optional)" = "Password (Optional)"; "Next" = "Next"; "Add" = "Add"; -"Connecting to server" = "Connecting to server..."; "Please save the seed below in a safe location. It can be used to restore your account if you lose access, or to migrate to a new device." = "Please save the seed below in a safe location. It can be used to restore your account if you lose access, or to migrate to a new device."; "Restore your account by entering your seed below." = "Restore your account by entering your seed below."; "Copy" = "Copy"; @@ -2571,7 +2570,6 @@ "Start a Conversation" = "Start a Conversation"; "Invalid public key" = "Invalid public key"; "No search results" = "No search results"; -"Failed to connect to server" = "Failed to connect to server"; "Calculating proof of work" = "Calculating proof of work"; "Failed to calculate proof of work." = "Failed to calculate proof of work."; "Share Public Key" = "Share Public Key"; @@ -2648,5 +2646,4 @@ "Enter a URL" = "Enter a URL"; "Enter the URL of the public chat you'd like to join. The Loki Public Chat URL is https://chat.lokinet.org." = "Enter the URL of the public chat you'd like to join. The Loki Public Chat URL is https://chat.lokinet.org."; "Connecting..." = "Connecting..."; -"Add" = "Add"; "Couldn't Connect" = "Couldn't Connect"; diff --git a/SignalMessaging/Loki/DisplayNameUtilities.swift b/SignalMessaging/Loki/DisplayNameUtilities.swift new file mode 100644 index 000000000..7e864c105 --- /dev/null +++ b/SignalMessaging/Loki/DisplayNameUtilities.swift @@ -0,0 +1,39 @@ + +@objc(LKDisplayNameUtilities) +public final class DisplayNameUtilities : NSObject { + + override private init() { } + + private static var userHexEncodedPublicKey: String { + return OWSIdentityManager.shared().identityKeyPair()!.hexEncodedPublicKey + } + + private static var userDisplayName: String? { + return OWSProfileManager.shared().localProfileName() + } + + @objc public static func getPrivateChatDisplayName(for hexEncodedPublicKey: String) -> String? { + if hexEncodedPublicKey == userHexEncodedPublicKey { + return userDisplayName + } else { + return OWSProfileManager.shared().profileName(forRecipientId: hexEncodedPublicKey) + } + } + + @objc public static func getGroupChatDisplayName(for hexEncodedPublicKey: String, in channel: UInt64, on server: String) -> String? { + var result: String? + OWSPrimaryStorage.shared().dbReadConnection.read { transaction in + result = getGroupChatDisplayName(for: hexEncodedPublicKey, in: channel, on: server, using: transaction) + } + return result + } + + @objc public static func getGroupChatDisplayName(for hexEncodedPublicKey: String, in channel: UInt64, on server: String, using transaction: YapDatabaseReadTransaction) -> String? { + if hexEncodedPublicKey == userHexEncodedPublicKey { + return userDisplayName + } else { + let collection = "\(server).\(channel)" + return transaction.object(forKey: hexEncodedPublicKey, inCollection: collection) as! String? + } + } +} diff --git a/SignalMessaging/ViewModels/OWSQuotedReplyModel.h b/SignalMessaging/ViewModels/OWSQuotedReplyModel.h index cb8b950b2..471613b47 100644 --- a/SignalMessaging/ViewModels/OWSQuotedReplyModel.h +++ b/SignalMessaging/ViewModels/OWSQuotedReplyModel.h @@ -51,10 +51,8 @@ NS_ASSUME_NONNULL_BEGIN - (TSQuotedMessage *)buildQuotedMessageForSending; // Loki - @property (nonatomic, readonly) NSString *threadId; - @end NS_ASSUME_NONNULL_END diff --git a/SignalMessaging/environment/VersionMigrations.m b/SignalMessaging/environment/VersionMigrations.m index 57ccac8ac..dec2b762c 100644 --- a/SignalMessaging/environment/VersionMigrations.m +++ b/SignalMessaging/environment/VersionMigrations.m @@ -167,7 +167,7 @@ NS_ASSUME_NONNULL_BEGIN } } -# pragma mark Loki - Upgrading to public chat manager +# pragma mark Loki - Upgrading to Public Chat Manager // Versions less than or equal to 1.2.0 didn't store group chat mappings + (void)updatePublicChatMapping diff --git a/SignalMessaging/profiles/OWSProfileManager.m b/SignalMessaging/profiles/OWSProfileManager.m index cc0a5be77..c9edb900f 100644 --- a/SignalMessaging/profiles/OWSProfileManager.m +++ b/SignalMessaging/profiles/OWSProfileManager.m @@ -535,13 +535,15 @@ typedef void (^ProfileManagerFailureBlock)(NSError *error); OWSAssertDebug(successBlock); OWSAssertDebug(failureBlock); - __block NSDictionary *chats; - [SSKEnvironment.shared.primaryStorage.dbReadConnection readWithBlock:^(YapDatabaseReadTransaction * _Nonnull transaction) { - chats = [LKDatabaseUtilities getAllGroupChats:transaction]; + __block NSDictionary *groupChats; + [SSKEnvironment.shared.primaryStorage.dbReadConnection readWithBlock:^(YapDatabaseReadTransaction *transaction) { + groupChats = [LKDatabaseUtilities getAllGroupChats:transaction]; }]; - for (LKGroupChat *chat in chats.allValues) { - [[LKGroupChatAPI setDisplayName:localProfileName on:chat.server] retainUntilComplete]; + NSSet *servers = [NSSet setWithArray:[groupChats.allValues map:^NSString *(LKGroupChat *groupChat) { return groupChat.server; }]]; + + for (NSString *server in servers) { + [[LKGroupChatAPI setDisplayName:localProfileName on:server] retainUntilComplete]; } successBlock(); diff --git a/SignalServiceKit/src/Loki/API/Group Chat/LokiGroupChatPoller.swift b/SignalServiceKit/src/Loki/API/Group Chat/GroupChatPoller.swift similarity index 97% rename from SignalServiceKit/src/Loki/API/Group Chat/LokiGroupChatPoller.swift rename to SignalServiceKit/src/Loki/API/Group Chat/GroupChatPoller.swift index 0196e110d..20435b8d4 100644 --- a/SignalServiceKit/src/Loki/API/Group Chat/LokiGroupChatPoller.swift +++ b/SignalServiceKit/src/Loki/API/Group Chat/GroupChatPoller.swift @@ -1,6 +1,6 @@ @objc(LKGroupChatPoller) -public final class LokiGroupChatPoller : NSObject { +public final class GroupChatPoller : NSObject { private let group: LokiGroupChat private var pollForNewMessagesTimer: Timer? = nil private var pollForDeletedMessagesTimer: Timer? = nil @@ -50,7 +50,7 @@ public final class LokiGroupChatPoller : NSObject { let endIndex = senderHexEncodedPublicKey.endIndex let cutoffIndex = senderHexEncodedPublicKey.index(endIndex, offsetBy: -8) let senderDisplayName = "\(message.displayName) (...\(senderHexEncodedPublicKey[cutoffIndex.. Promise { + public static func getChannelInfo(_ channel: UInt64, on server: String) -> Promise { let url = URL(string: "\(server)/channels/\(channel)?include_annotations=1")! let request = TSRequest(url: url) return TSNetworkManager.shared().makePromise(request: request).map { $0.responseObject }.map { rawResponse in @@ -236,7 +236,7 @@ public final class LokiGroupChatAPI : LokiDotNetAPI { print("[Loki] Couldn't parse info for group chat with ID: \(channel) on server: \(server) from: \(rawResponse).") throw Error.parsingFailed } - return LokiPublicChannel(name: name) + return name } } diff --git a/SignalServiceKit/src/Loki/API/Group Chat/LokiPublicChatManager.swift b/SignalServiceKit/src/Loki/API/Group Chat/LokiPublicChatManager.swift index 18e3b27dc..cbc984352 100644 --- a/SignalServiceKit/src/Loki/API/Group Chat/LokiPublicChatManager.swift +++ b/SignalServiceKit/src/Loki/API/Group Chat/LokiPublicChatManager.swift @@ -11,7 +11,7 @@ public final class LokiPublicChatManager: NSObject { @objc public static let shared = LokiPublicChatManager() private var chats: [String: LokiGroupChat] = [:] - private var pollers: [String: LokiGroupChatPoller] = [:] + private var pollers: [String: GroupChatPoller] = [:] private var isPolling = false private let storage = OWSPrimaryStorage.shared() @@ -32,7 +32,7 @@ public final class LokiPublicChatManager: NSObject { if let poller = pollers[threadID] { poller.startIfNeeded() } else { - let poller = LokiGroupChatPoller(for: groupChat) + let poller = GroupChatPoller(for: groupChat) poller.startIfNeeded() pollers[threadID] = poller } @@ -53,7 +53,7 @@ public final class LokiPublicChatManager: NSObject { return LokiGroupChatAPI.getAuthToken(for: server).then { token in return LokiGroupChatAPI.getChannelInfo(channel, on: server) }.map { channelInfo -> LokiGroupChat in - return self.addChat(server: server, channel: channel, name: channelInfo.name) + return self.addChat(server: server, channel: channel, name: channelInfo) } } diff --git a/SignalServiceKit/src/Loki/API/LokiAPI.swift b/SignalServiceKit/src/Loki/API/LokiAPI.swift index c19652ab1..d2b28897e 100644 --- a/SignalServiceKit/src/Loki/API/LokiAPI.swift +++ b/SignalServiceKit/src/Loki/API/LokiAPI.swift @@ -309,10 +309,19 @@ public final class LokiAPI : NSObject { guard let cache = userHexEncodedPublicKeyCache[threadID] else { return [] } var candidates: [Mention] = [] // Gather candidates + var groupChat: LokiGroupChat? + OWSPrimaryStorage.shared().dbReadConnection.read { transaction in + groupChat = LokiDatabaseUtilities.getGroupChat(for: threadID, in: transaction) + } storage.dbReadConnection.read { transaction in - let collection = "https://chat.lokinet.org.1" // FIXME: Mentions need to work for every kind of chat candidates = cache.flatMap { hexEncodedPublicKey in - guard let displayName = transaction.object(forKey: hexEncodedPublicKey, inCollection: collection) as! String? else { return nil } + let uncheckedDisplayName: String? + if let groupChat = groupChat { + uncheckedDisplayName = DisplayNameUtilities.getGroupChatDisplayName(for: hexEncodedPublicKey, in: groupChat.channel, on: groupChat.server) + } else { + uncheckedDisplayName = DisplayNameUtilities.getPrivateChatDisplayName(for: hexEncodedPublicKey) + } + guard let displayName = uncheckedDisplayName else { return nil } guard !displayName.hasPrefix("Anonymous") else { return nil } return Mention(hexEncodedPublicKey: hexEncodedPublicKey, displayName: displayName) } diff --git a/SignalServiceKit/src/Loki/Database/LokiDatabaseUtilities.swift b/SignalServiceKit/src/Loki/Database/LokiDatabaseUtilities.swift index 3a7039601..90e883e4d 100644 --- a/SignalServiceKit/src/Loki/Database/LokiDatabaseUtilities.swift +++ b/SignalServiceKit/src/Loki/Database/LokiDatabaseUtilities.swift @@ -26,12 +26,12 @@ public final class LokiDatabaseUtilities : NSObject { } @objc(getAllGroupChats:) - public static func objc_getAllGroupChats(in transaction: YapDatabaseReadTransaction) -> [String: LokiGroupChat] { + public static func objc_getAllGroupChats(in transaction: YapDatabaseReadTransaction) -> [String:LokiGroupChat] { return OWSPrimaryStorage.shared().getAllGroupChats(with: transaction) } @objc(getGroupChatForThreadID:transaction:) - public static func objc_getGroupChat(for threadID: String, in transaction: YapDatabaseReadTransaction) -> LokiGroupChat? { + public static func getGroupChat(for threadID: String, in transaction: YapDatabaseReadTransaction) -> LokiGroupChat? { return OWSPrimaryStorage.shared().getGroupChat(for: threadID, in: transaction) }