From 61aeade0bf91e6ff6027182e1176e05cf88f709e Mon Sep 17 00:00:00 2001 From: Nuno Vieira Date: Mon, 12 Feb 2024 15:23:15 +0000 Subject: [PATCH 1/6] Increase enrich debouncer to 0.4s --- Sources/StreamChatUI/Composer/ComposerVC.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sources/StreamChatUI/Composer/ComposerVC.swift b/Sources/StreamChatUI/Composer/ComposerVC.swift index 3375aacc91f..e46a3a7b4da 100644 --- a/Sources/StreamChatUI/Composer/ComposerVC.swift +++ b/Sources/StreamChatUI/Composer/ComposerVC.swift @@ -289,7 +289,7 @@ open class ComposerVC: _ViewController, open var cooldownTracker: CooldownTracker = CooldownTracker(timer: ScheduledStreamTimer(interval: 1)) - public var enrichUrlDebouncer = Debouncer(0.3, queue: .main) + public var enrichUrlDebouncer = Debouncer(0.4, queue: .main) lazy var linkDetector = TextLinkDetector() From 1ae33f5a7ae7585bff5278ec44d023519f713ed2 Mon Sep 17 00:00:00 2001 From: Nuno Vieira Date: Mon, 12 Feb 2024 15:23:58 +0000 Subject: [PATCH 2/6] Fix composer link preview overriding current input link preview --- .../StreamChatUI/Composer/ComposerVC.swift | 12 ++++++- .../Composer/ComposerVC_Tests.swift | 31 +++++++++++++++++++ 2 files changed, 42 insertions(+), 1 deletion(-) diff --git a/Sources/StreamChatUI/Composer/ComposerVC.swift b/Sources/StreamChatUI/Composer/ComposerVC.swift index e46a3a7b4da..f1bdc49af35 100644 --- a/Sources/StreamChatUI/Composer/ComposerVC.swift +++ b/Sources/StreamChatUI/Composer/ComposerVC.swift @@ -1007,7 +1007,17 @@ open class ComposerVC: _ViewController, self?.channelController?.enrichUrl(link.url) { [weak self] result in switch result { case let .success(linkPayload): - self?.showLinkPreview(for: linkPayload) + let enrichedUrlText = linkPayload.originalURL.absoluteString + let currentLinks = self?.composerView.inputMessageView.textView.links ?? [] + guard let currentUrlText = currentLinks.first?.url.absoluteString else { + return + } + // Only show enrichment if the current url is still the one + // that should be shown. Since we currently do not support + // cancelling previous requests, this is the current optimal solution. + if enrichedUrlText == currentUrlText { + self?.showLinkPreview(for: linkPayload) + } case .failure: self?.dismissLinkPreview() } diff --git a/Tests/StreamChatUITests/SnapshotTests/Composer/ComposerVC_Tests.swift b/Tests/StreamChatUITests/SnapshotTests/Composer/ComposerVC_Tests.swift index 662cab62a45..13d7e9abf52 100644 --- a/Tests/StreamChatUITests/SnapshotTests/Composer/ComposerVC_Tests.swift +++ b/Tests/StreamChatUITests/SnapshotTests/Composer/ComposerVC_Tests.swift @@ -733,6 +733,37 @@ final class ComposerVC_Tests: XCTestCase { } } + func test_didChangeLinks_whenEnrichSuccess_whenUrlDoesNotEqualToCurrentInput_thenDoNotCallShowPreview() { + let composerVC = SpyComposerVC() + composerVC.components.isComposerLinkPreviewEnabled = true + let mock = ChatChannelController_Mock.mock(client: .mock()) + mock.channel_mock = .mockNonDMChannel(config: .mock(urlEnrichmentEnabled: true)) + let mockAPIClient = mock.client.mockAPIClient + composerVC.channelController = mock + composerVC.enrichUrlDebouncer = .init(0, queue: .main) + composerVC.content = .initial() + composerVC.content.text = """ + Some link: https://github.com/GetStream/stream-chat-swiftui + Another one: www.google.com + """ + composerVC.updateContent() + + let url = URL(string: "https://github.com/GetStream/stream-chat-swift")! + mockAPIClient.test_mockResponseResult(.success(LinkAttachmentPayload( + originalURL: url + ))) + + composerVC.didChangeLinks([ + .init(url: url, originalText: "https://github.com/GetStream/stream-chat-swift", range: .init(location: 0, length: 0)), + .init(url: URL(string: "http://www.google.com")!, originalText: "www.google.com", range: .init(location: 0, length: 0)) + ]) + + AssertAsync { + Assert.willBeEqual(composerVC.showLinkPreviewCallCount, 0) + Assert.willBeEqual(composerVC.dismissLinkPreviewCallCount, 0) + } + } + func test_didChangeLinks_whenEnrichFails_thenDismissLinkPreview() { let composerVC = SpyComposerVC() composerVC.components.isComposerLinkPreviewEnabled = true From 7ea6fa748d5febdf427a4d8b404e09c9be18f5ef Mon Sep 17 00:00:00 2001 From: Nuno Vieira Date: Mon, 12 Feb 2024 15:40:26 +0000 Subject: [PATCH 3/6] Also cover the case where wrong url could dismiss the current valid url --- .../StreamChatUI/Composer/ComposerVC.swift | 25 +++++++++------- .../Composer/ComposerVC_Tests.swift | 29 +++++++++++++++++++ 2 files changed, 43 insertions(+), 11 deletions(-) diff --git a/Sources/StreamChatUI/Composer/ComposerVC.swift b/Sources/StreamChatUI/Composer/ComposerVC.swift index f1bdc49af35..9aec35ec3c8 100644 --- a/Sources/StreamChatUI/Composer/ComposerVC.swift +++ b/Sources/StreamChatUI/Composer/ComposerVC.swift @@ -1005,19 +1005,22 @@ open class ComposerVC: _ViewController, enrichUrlDebouncer.execute { [weak self] in self?.channelController?.enrichUrl(link.url) { [weak self] result in + let enrichedUrlText = link.url.absoluteString + let currentLinks = self?.composerView.inputMessageView.textView.links ?? [] + guard let currentUrlText = currentLinks.first?.url.absoluteString else { + return + } + + // Only show/dismiss enrichment if the current url is still the one + // that should be shown. Since we currently do not support + // cancelling previous requests, this is the current optimal solution. + guard enrichedUrlText == currentUrlText else { + return + } + switch result { case let .success(linkPayload): - let enrichedUrlText = linkPayload.originalURL.absoluteString - let currentLinks = self?.composerView.inputMessageView.textView.links ?? [] - guard let currentUrlText = currentLinks.first?.url.absoluteString else { - return - } - // Only show enrichment if the current url is still the one - // that should be shown. Since we currently do not support - // cancelling previous requests, this is the current optimal solution. - if enrichedUrlText == currentUrlText { - self?.showLinkPreview(for: linkPayload) - } + self?.showLinkPreview(for: linkPayload) case .failure: self?.dismissLinkPreview() } diff --git a/Tests/StreamChatUITests/SnapshotTests/Composer/ComposerVC_Tests.swift b/Tests/StreamChatUITests/SnapshotTests/Composer/ComposerVC_Tests.swift index 13d7e9abf52..6fbec1daa6c 100644 --- a/Tests/StreamChatUITests/SnapshotTests/Composer/ComposerVC_Tests.swift +++ b/Tests/StreamChatUITests/SnapshotTests/Composer/ComposerVC_Tests.swift @@ -792,6 +792,35 @@ final class ComposerVC_Tests: XCTestCase { } } + func test_didChangeLinks_whenUrlDoesNotEqualToCurrentInput_thenDoNotCallDismissPreview() { + let composerVC = SpyComposerVC() + composerVC.components.isComposerLinkPreviewEnabled = true + let mock = ChatChannelController_Mock.mock(client: .mock()) + mock.channel_mock = .mockNonDMChannel(config: .mock(urlEnrichmentEnabled: true)) + let mockAPIClient = mock.client.mockAPIClient + composerVC.channelController = mock + composerVC.enrichUrlDebouncer = .init(0, queue: .main) + composerVC.content = .initial() + composerVC.content.text = """ + Some link: https://github.com/GetStream/stream-chat-swiftui + Another one: www.google.com + """ + composerVC.updateContent() + + let url = URL(string: "https://github.com/GetStream/stream-chat-swift")! + mockAPIClient.test_mockResponseResult(Result.failure(ClientError())) + + composerVC.didChangeLinks([ + .init(url: url, originalText: "https://github.com/GetStream/stream-chat-swift", range: .init(location: 0, length: 0)), + .init(url: URL(string: "http://www.google.com")!, originalText: "www.google.com", range: .init(location: 0, length: 0)) + ]) + + AssertAsync { + Assert.willBeEqual(composerVC.showLinkPreviewCallCount, 0) + Assert.willBeEqual(composerVC.dismissLinkPreviewCallCount, 0) + } + } + func test_didChangeLinks_whenEnrichNotEnabled_thenDoNotShowLinkPreview() { let composerVC = SpyComposerVC() composerVC.components.isComposerLinkPreviewEnabled = true From 9bd1cc823bdc575c70ff43fb9c59270779f47ae4 Mon Sep 17 00:00:00 2001 From: Nuno Vieira Date: Mon, 12 Feb 2024 15:50:26 +0000 Subject: [PATCH 4/6] Update CHANGELOG.md --- CHANGELOG.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e1a80d6a8ec..669bd123adc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,7 +3,9 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). # Upcoming -### 🔄 Changed +## StreamChatUI +### 🐞 Fixed +- Fix overriding of actual current composer link preview [#3025](https://github.com/GetStream/stream-chat-swift/pull/3025) # [4.48.1](https://github.com/GetStream/stream-chat-swift/releases/tag/4.48.1) _February 09, 2024_ From b5b3d944bdaa54e6cd626e9634c0e46c5253eaf3 Mon Sep 17 00:00:00 2001 From: Nuno Vieira Date: Mon, 12 Feb 2024 16:02:31 +0000 Subject: [PATCH 5/6] Fix unit test typo --- .../SnapshotTests/Composer/ComposerVC_Tests.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Tests/StreamChatUITests/SnapshotTests/Composer/ComposerVC_Tests.swift b/Tests/StreamChatUITests/SnapshotTests/Composer/ComposerVC_Tests.swift index 6fbec1daa6c..242e939cff4 100644 --- a/Tests/StreamChatUITests/SnapshotTests/Composer/ComposerVC_Tests.swift +++ b/Tests/StreamChatUITests/SnapshotTests/Composer/ComposerVC_Tests.swift @@ -792,7 +792,7 @@ final class ComposerVC_Tests: XCTestCase { } } - func test_didChangeLinks_whenUrlDoesNotEqualToCurrentInput_thenDoNotCallDismissPreview() { + func test_didChangeLinks_whenEnrichFails_whenUrlDoesNotEqualToCurrentInput_thenDoNotCallDismissPreview() { let composerVC = SpyComposerVC() composerVC.components.isComposerLinkPreviewEnabled = true let mock = ChatChannelController_Mock.mock(client: .mock()) From 94271b99e4646ffd53ff968710d98e1458e19ff9 Mon Sep 17 00:00:00 2001 From: Nuno Vieira Date: Mon, 12 Feb 2024 17:02:51 +0000 Subject: [PATCH 6/6] Update CHANGELOG.md --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 669bd123adc..ff692bf64f0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,7 +5,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). ## StreamChatUI ### 🐞 Fixed -- Fix overriding of actual current composer link preview [#3025](https://github.com/GetStream/stream-chat-swift/pull/3025) +- Fix composer link preview overridden by previous enrichment [#3025](https://github.com/GetStream/stream-chat-swift/pull/3025) # [4.48.1](https://github.com/GetStream/stream-chat-swift/releases/tag/4.48.1) _February 09, 2024_