Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix composer link preview overridden by previous enrichment #3025

Merged
merged 6 commits into from
Feb 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).

# Upcoming

### 馃攧 Changed
## StreamChatUI
### 馃悶 Fixed
- 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_
Expand Down
15 changes: 14 additions & 1 deletion Sources/StreamChatUI/Composer/ComposerVC.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
martinmitrevski marked this conversation as resolved.
Show resolved Hide resolved

lazy var linkDetector = TextLinkDetector()

Expand Down Expand Up @@ -1005,6 +1005,19 @@ 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):
self?.showLinkPreview(for: linkPayload)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -761,6 +792,35 @@ final class ComposerVC_Tests: XCTestCase {
}
}

func test_didChangeLinks_whenEnrichFails_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<LinkAttachmentPayload, Error>.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
Expand Down