Skip to content

Commit

Permalink
Fix auto-enqueuing for authenticated visitor
Browse files Browse the repository at this point in the history
Previously, if an authenticated visitor has chat history, enqueuing was postponed until a visitor sends a message. This behaviour was broken after adding LO acknowledge.
This commit fixes it.
So now:
- unauthenticated visitors, if LO acknowledge is enabled, will be enqueued once they accept LO acknowledge
- unauthenticated visitors, if LO acknowledge is disabled, will be automatically enqueued
- authenticated visitors, if they do not have chat history and LO acknowledge is disabled, will be enqueued automatically
- authenticated visitors, if they do not have chat history and if LO acknowledge is enabled, will be enqueued once they accept LO acknowledge
- authenticated visitors, if LO acknowledge is enabled and if they have chat history, will be enqueued only after sending a message and accepting LO acknowledge
- authenticated visitors, if LO acknowledge is disabled and if they have chat history, will be enqueued after sending a message

MOB- 3442
  • Loading branch information
Egor Egorov authored and EgorovEI committed Jul 22, 2024
1 parent 234fcab commit 89b2592
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ class EngagementCoordinator: SubFlowCoordinator, FlowCoordinator {
showsCallBubble: false
)
engagement = .chat(chatViewController)
interactor.state = .enqueueing(.text)
navigationPresenter.setViewControllers(
[chatViewController],
animated: false
Expand Down
18 changes: 15 additions & 3 deletions GliaWidgets/Sources/ViewModel/Chat/ChatViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,18 @@ class ChatViewModel: EngagementViewModel {

override func start() {
super.start()
loadHistory()

loadHistory { [weak self] history in
guard let self = self else { return }
// We only proceed to considering enqueue flow if `startAction` is about starting of engagement.
guard case .startEngagement = self.startAction else { return }
// We enqueue eagerly in case if this is the first engagement for visitor (by evaluating previous chat history)
// or in case if engagement has been restored.

if history.isEmpty || self.environment.getCurrentEngagement() != nil {
self.interactor.state = .enqueueing(.text)
}
}
}

override func update(for state: InteractorState) {
Expand Down Expand Up @@ -366,7 +377,7 @@ extension ChatViewModel {
// MARK: History

extension ChatViewModel {
private func loadHistory() {
private func loadHistory(_ completion: @escaping ([ChatMessage]) -> Void) {
environment.fetchChatHistory { [weak self] result in
guard let self else { return }
let messages = (try? result.get()) ?? []
Expand All @@ -389,6 +400,7 @@ extension ChatViewModel {
self.historySection.set(items)
self.action?(.refreshSection(self.historySection.index))
self.action?(.scrollToBottom(animated: false))
completion(messages)
}
}
}
Expand Down Expand Up @@ -489,7 +501,7 @@ extension ChatViewModel {

case .enqueueing, .ended, .none:
handle(pendingMessage: outgoingMessage)
enqueue(mediaType: .text)
interactor.state = .enqueueing(.text)
}

messageText = ""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ final class EngagementCoordinatorTests: XCTestCase {

coordinator.start()

XCTAssertEqual(coordinator.interactor.state, .enqueueing(.text))
XCTAssertEqual(coordinator.interactor.state, .none)
let viewController = coordinator.navigationPresenter.viewControllers.first as? ChatViewController
XCTAssertNotNil(viewController)
XCTAssertNotNil(coordinator.gliaViewController)
Expand Down Expand Up @@ -169,7 +169,6 @@ final class EngagementCoordinatorTests: XCTestCase {

func test_chatCoordinatorBackOnInteractorStateNone() {
coordinator.start()
coordinator.interactor.state = .none

XCTAssertNotEqual(coordinator.coordinators.count, 0)

Expand All @@ -190,6 +189,8 @@ final class EngagementCoordinatorTests: XCTestCase {

coordinator.start()

coordinator.interactor.state = .enqueueing(.text)

let chatCoordinator = coordinator.coordinators.last as? ChatCoordinator
chatCoordinator?.delegate?(.back)

Expand Down

0 comments on commit 89b2592

Please sign in to comment.