diff --git a/Signal/src/ViewControllers/ConversationView/ConversationViewController.m b/Signal/src/ViewControllers/ConversationView/ConversationViewController.m index 3f8b7200e..242113d1f 100644 --- a/Signal/src/ViewControllers/ConversationView/ConversationViewController.m +++ b/Signal/src/ViewControllers/ConversationView/ConversationViewController.m @@ -1654,38 +1654,12 @@ typedef enum : NSUInteger { #pragma mark - Updating - (void)updateInputToolbar { - BOOL isEnabled; - BOOL isAttachmentButtonHidden; - if ([self.thread isKindOfClass:TSContactThread.class]) { - NSString *senderID = ((TSContactThread *)self.thread).contactIdentifier; - __block NSSet *linkedDeviceThreads; - __block BOOL isNoteToSelf; - [OWSPrimaryStorage.sharedManager.dbReadWriteConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) { - linkedDeviceThreads = [LKDatabaseUtilities getLinkedDeviceThreadsFor:senderID in:transaction]; - isNoteToSelf = [LKDatabaseUtilities isUserLinkedDevice:senderID in:transaction]; - }]; - if ([linkedDeviceThreads contains:^BOOL(TSContactThread *thread) { - return thread.isContactFriend; - }] || isNoteToSelf) { - isEnabled = true; - isAttachmentButtonHidden = false; - } else if (![linkedDeviceThreads contains:^BOOL(TSContactThread *thread) { - return thread.hasPendingFriendRequest; - }]) { - isEnabled = true; - isAttachmentButtonHidden = true; - } else { - isEnabled = false; - isAttachmentButtonHidden = true; - } - } else { - isEnabled = true; - isAttachmentButtonHidden = false; - } - [self.inputToolbar setUserInteractionEnabled:isEnabled]; - NSString *placeholderText = isEnabled ? NSLocalizedString(@"Message", "") : NSLocalizedString(@"Pending session request", ""); + BOOL shouldInputBarBeEnabled = [LKFriendRequestProtocol shouldInputBarBeEnabledForThread:self.thread]; + [self.inputToolbar setUserInteractionEnabled:shouldInputBarBeEnabled]; + NSString *placeholderText = shouldInputBarBeEnabled ? NSLocalizedString(@"Message", "") : NSLocalizedString(@"Pending session request", ""); [self.inputToolbar setPlaceholderText:placeholderText]; - [self.inputToolbar setAttachmentButtonHidden:isAttachmentButtonHidden]; + BOOL shouldAttachmentButtonBeEnabled = [LKFriendRequestProtocol shouldAttachmentButtonBeEnabledForThread:self.thread]; + [self.inputToolbar setAttachmentButtonHidden:!shouldAttachmentButtonBeEnabled]; } #pragma mark - Identity diff --git a/SignalServiceKit/src/Loki/Protocol/Friend Requests/FriendRequestProtocol.swift b/SignalServiceKit/src/Loki/Protocol/Friend Requests/FriendRequestProtocol.swift index 86d46c2af..a91506d38 100644 --- a/SignalServiceKit/src/Loki/Protocol/Friend Requests/FriendRequestProtocol.swift +++ b/SignalServiceKit/src/Loki/Protocol/Friend Requests/FriendRequestProtocol.swift @@ -15,6 +15,45 @@ public final class FriendRequestProtocol : NSObject { internal static var storage: OWSPrimaryStorage { OWSPrimaryStorage.shared() } + // MARK: - General + @objc(shouldInputBarBeEnabledForThread:) + public static func shouldInputBarBeEnabled(for thread: TSThread) -> Bool { + // Friend requests have nothing to do with groups, so if this isn't a contact thread the input bar should be enabled + guard let thread = thread as? TSContactThread else { return true } + // If this is a note to self, the input bar should be enabled + if SessionProtocol.isMessageNoteToSelf(thread) { return true } + let contactID = thread.contactIdentifier() + var linkedDeviceThreads: Set = [] + storage.dbReadConnection.read { transaction in + linkedDeviceThreads = LokiDatabaseUtilities.getLinkedDeviceThreads(for: contactID, in: transaction) + } + // If the current user is friends with any of the other user's devices, the input bar should be enabled + if linkedDeviceThreads.contains(where: { $0.isContactFriend }) { return true } + // If no friend request has been sent, the input bar should be enabled + if !linkedDeviceThreads.contains(where: { $0.hasPendingFriendRequest }) { return true } + // There must be a pending friend request + return false + } + + @objc(shouldAttachmentButtonBeEnabledForThread:) + public static func shouldAttachmentButtonBeEnabled(for thread: TSThread) -> Bool { + // Friend requests have nothing to do with groups, so if this isn't a contact thread the attachment button should be enabled + guard let thread = thread as? TSContactThread else { return true } + // If this is a note to self, the attachment button should be enabled + if SessionProtocol.isMessageNoteToSelf(thread) { return true } + let contactID = thread.contactIdentifier() + var linkedDeviceThreads: Set = [] + storage.dbReadConnection.read { transaction in + linkedDeviceThreads = LokiDatabaseUtilities.getLinkedDeviceThreads(for: contactID, in: transaction) + } + // If the current user is friends with any of the other user's devices, the attachment button should be enabled + if linkedDeviceThreads.contains(where: { $0.isContactFriend }) { return true } + // If no friend request has been sent, the attachment button should be disabled + if !linkedDeviceThreads.contains(where: { $0.hasPendingFriendRequest }) { return false } + // There must be a pending friend request + return false + } + // MARK: - Sending @objc(acceptFriendRequest:in:using:) public static func acceptFriendRequest(_ friendRequest: TSIncomingMessage, in thread: TSThread, using transaction: YapDatabaseReadWriteTransaction) {