Skip to content

Commit

Permalink
Update SC availability taking into account 'transferring' state
Browse files Browse the repository at this point in the history
Add 'transferring' and 'engagement.capabilities.text == true' to be used for SC availability.

MOB-3654
  • Loading branch information
igorkravchenko authored and Egor Egorov committed Dec 13, 2024
1 parent 8e77a4a commit 4490165
Show file tree
Hide file tree
Showing 13 changed files with 158 additions and 60 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -146,10 +146,14 @@ extension SecureConversations {
availability.checkSecureConversationsAvailability(for: environment.queueIds) { [weak self] result in
guard let self else { return }
switch result {
case let .success(.available(queueIds)):
case let .success(.available(.queues(queueIds))):
self.environment.queueIds = queueIds
self.isSecureConversationsAvailable = true
self.fileUploadListModel.isEnabled = true
case .success(.available(.transferred)):
self.environment.queueIds = []
self.isSecureConversationsAvailable = true
self.fileUploadListModel.isEnabled = true
case .failure, .success(.unavailable(.emptyQueue)), .success(.unavailable(.unauthenticated)):
// For chat screen we no longer show unavailability dialog, but unavailability banner instead.
self.isSecureConversationsAvailable = false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,24 @@ extension SecureConversations {
let filteredQueues = fetchedQueues.filter(defaultPredicate)

// Check if matched queues match support `messaging` and
// have status other than `closed`
// have status other than `closed`.
guard !filteredQueues.isEmpty else {
// In case of "transferred SC" we should treat SC as available.
if let engagement = environment.getCurrentEngagement(),
engagement.status == .transferring,
engagement.capabilities?.text == true {
completion(.success(.available(.transferred)))
return
}

environment.log.warning("Provided queue IDs do not match with queues that have status other than closed and support messaging.")
completion(.success(.unavailable(.emptyQueue)))
return
}
let queueIds = filteredQueues.map { $0.id }

environment.log.info("Secure Messaging is available in queues with IDs: \(queueIds).")
completion(.success(.available(queueIds: queueIds)))
completion(.success(.available(.queues(queueIds: queueIds))))
}

private var defaultPredicate: (CoreSdkClient.Queue) -> Bool {
Expand All @@ -67,12 +75,17 @@ extension SecureConversations.Availability {
var isAuthenticated: () -> Bool
var log: CoreSdkClient.Logger
var queuesMonitor: QueuesMonitor
var getCurrentEngagement: CoreSdkClient.GetCurrentEngagement
}
}

extension SecureConversations.Availability {
enum Status: Equatable {
case available(queueIds: [String] = [])
enum Available: Equatable {
case queues(queueIds: [String])
case transferred
}
case available(Available)
case unavailable(UnavailabilityReason)

static func == (lhs: Status, rhs: Status) -> Bool {
Expand All @@ -85,6 +98,15 @@ extension SecureConversations.Availability {
return false
}
}

var isAvailable: Bool {
switch self {
case .available:
true
case .unavailable:
false
}
}
}
}

Expand All @@ -101,7 +123,8 @@ extension SecureConversations.Availability.Environment {
listQueues: environment.listQueues,
isAuthenticated: environment.isAuthenticated,
log: environment.log,
queuesMonitor: environment.queuesMonitor
queuesMonitor: environment.queuesMonitor,
getCurrentEngagement: environment.getCurrentEngagement
)
}

Expand All @@ -110,7 +133,8 @@ extension SecureConversations.Availability.Environment {
listQueues: environment.listQueues,
isAuthenticated: environment.isAuthenticated,
log: environment.log,
queuesMonitor: environment.queuesMonitor
queuesMonitor: environment.queuesMonitor,
getCurrentEngagement: environment.getCurrentEngagement
)
}
}
Expand All @@ -129,13 +153,15 @@ extension SecureConversations.Availability.Environment {
listQueues: @escaping CoreSdkClient.ListQueues = { _ in },
isAuthenticated: @escaping () -> Bool = { false },
log: CoreSdkClient.Logger = .mock,
queuesMonitor: QueuesMonitor = .mock()
queuesMonitor: QueuesMonitor = .mock(),
getCurrentEngagement: @escaping CoreSdkClient.GetCurrentEngagement = { .mock() }
) -> Self {
.init(
listQueues: listQueues,
isAuthenticated: isAuthenticated,
log: log,
queuesMonitor: queuesMonitor
queuesMonitor: queuesMonitor,
getCurrentEngagement: getCurrentEngagement
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ extension SecureConversations {
/// Flag indicating attachment(s) availability.
/// By default attachments are not available, until site configurations are fetched.
private(set) var isAttachmentsAvailable: Bool = false { didSet { reportChange() } }
var availabilityStatus: Availability.Status = .available(queueIds: []) { didSet { reportChange() } }
var availabilityStatus: Availability.Status = .available(.queues(queueIds: [])) { didSet { reportChange() } }
var messageInputState: MessageInputState = .normal { didSet { reportChange() } }
var sendMessageRequestState: SendMessageRequestState = .waiting { didSet { reportChange() } }

Expand Down Expand Up @@ -64,9 +64,12 @@ extension SecureConversations {
availability.checkSecureConversationsAvailability(for: environment.queueIds) { [weak self] result in
guard let self else { return }
switch result {
case let .success(.available(queueIds)):
case let .success(.available(.queues(queueIds))):
self.environment.queueIds = queueIds
self.availabilityStatus = .available(queueIds: queueIds)
self.availabilityStatus = .available(.queues(queueIds: queueIds))
case .success(.available(.transferred)):
self.environment.queueIds = []
self.availabilityStatus = .available(.transferred)
case .success(.unavailable(.emptyQueue)), .failure:
self.availabilityStatus = .unavailable(.emptyQueue)
self.delegate?(.showAlert(.unavailableMessageCenter()))
Expand Down Expand Up @@ -178,7 +181,7 @@ extension SecureConversations.WelcomeViewModel {
) -> SecureConversations.WelcomeStyle {
var style = instance.environment.welcomeStyle

if instance.availabilityStatus != .available() {
if !instance.availabilityStatus.isAvailable {
style.messageTitleStyle = nil
}

Expand All @@ -191,7 +194,7 @@ extension SecureConversations.WelcomeViewModel {
var filePickerButton: WelcomeViewProps.FilePickerButton?

let isFilePickerEnabled = !instance.fileUploadListModel.isLimitReached
if instance.availabilityStatus == .available(), instance.isAttachmentsAvailable {
if instance.availabilityStatus.isAvailable, instance.isAttachmentsAvailable {
filePickerButton = WelcomeViewProps.FilePickerButton(
isEnabled: isFilePickerEnabled,
tap: Command { originView in
Expand All @@ -203,7 +206,7 @@ extension SecureConversations.WelcomeViewModel {
return filePickerButton
}
static func textViewState(for instance: SecureConversations.WelcomeViewModel) -> TextViewProps? {
guard instance.availabilityStatus == .available() else {
guard instance.availabilityStatus.isAvailable else {
return nil
}

Expand Down Expand Up @@ -254,7 +257,7 @@ extension SecureConversations.WelcomeViewModel {
for instance: SecureConversations.WelcomeViewModel
) -> SendMessageButton? {
// Is service available?
guard instance.availabilityStatus == .available() else {
guard instance.availabilityStatus.isAvailable else {
return nil
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ extension SecureConversations.Availability.Environment {
return false
},
log: .failing,
queuesMonitor: .failing
queuesMonitor: .failing,
getCurrentEngagement: {
fail("\(Self.self).getCurrentEngagement")
return .mock()
}
)
}
38 changes: 36 additions & 2 deletions GliaWidgetsTests/SecureConversations/AvailabilityTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ final class AvailabilityTests: XCTestCase {
env.log = logger
env.isAuthenticated = { true }
env.queuesMonitor = .mock(listQueues: env.listQueues)
env.getCurrentEngagement = { .mock() }
let queueIds = [UUID.mock.uuidString]
let availability = Availability(environment: env)
var receivedResult: Result<Availability.Status, CoreSdkClient.SalemoveError>?
Expand Down Expand Up @@ -81,7 +82,7 @@ final class AvailabilityTests: XCTestCase {
let result = try XCTUnwrap(receivedResult)
switch result {
case let .success(.available(queueIds)):
XCTAssertEqual(queueIds, [queueId])
XCTAssertEqual(queueIds, .queues(queueIds: [queueId]))
default:
XCTFail("Result should be `.success(.available)`")
}
Expand All @@ -100,6 +101,7 @@ final class AvailabilityTests: XCTestCase {
env.log = logger
env.isAuthenticated = { true }
env.queuesMonitor = .mock(listQueues: env.listQueues)
env.getCurrentEngagement = { .mock() }
let availability = Availability(environment: env)
var receivedResult: Result<Availability.Status, CoreSdkClient.SalemoveError>?
availability.checkSecureConversationsAvailability(for: queueIds) { result in
Expand All @@ -121,6 +123,7 @@ final class AvailabilityTests: XCTestCase {
env.log = logger
env.isAuthenticated = { true }
env.queuesMonitor = .mock(listQueues: env.listQueues)
env.getCurrentEngagement = { .mock() }
let availability = Availability(environment: env)
var receivedResult: Result<Availability.Status, CoreSdkClient.SalemoveError>?
availability.checkSecureConversationsAvailability(for: [queueId]) { result in
Expand All @@ -136,6 +139,7 @@ final class AvailabilityTests: XCTestCase {
env.listQueues = { callback in
callback([.mock(id: queueId, status: .closed, media: [.text])], nil)
}
env.getCurrentEngagement = { .mock() }
var logger = CoreSdkClient.Logger.failing
logger.prefixedClosure = { _ in logger }
logger.warningClosure = { _, _, _, _ in }
Expand Down Expand Up @@ -170,7 +174,7 @@ final class AvailabilityTests: XCTestCase {
}
let result = try XCTUnwrap(receivedResult)
switch result {
case let .success(.available(queueIds)):
case let .success(.available(.queues(queueIds))):
XCTAssertEqual(queueIds, [queueId])
default:
XCTFail("Result should be `.success(.available)`")
Expand All @@ -189,11 +193,41 @@ final class AvailabilityTests: XCTestCase {
env.log = logger
env.isAuthenticated = { true }
env.queuesMonitor = .mock(listQueues: env.listQueues)
env.getCurrentEngagement = { .mock() }
let availability = Availability(environment: env)
var receivedResult: Result<Availability.Status, CoreSdkClient.SalemoveError>?
availability.checkSecureConversationsAvailability(for: []) { result in
receivedResult = result
}
try XCTAssertEqual(XCTUnwrap(receivedResult), .success(.unavailable(.emptyQueue)))
}

func testAvailableStatusIfTransferredAndCapabilitiesTextTrue() throws {
var env = Availability.Environment.failing
env.getCurrentEngagement = {
.mock(status: .transferring, capabilities: .init(text: true))
}
env.listQueues = { callback in
callback([], nil)
}
var logger = CoreSdkClient.Logger.failing
logger.prefixedClosure = { _ in logger }
logger.infoClosure = { _, _, _, _ in }
logger.warningClosure = { _, _, _, _ in }
env.log = logger
env.isAuthenticated = { true }
env.queuesMonitor = .mock(listQueues: env.listQueues)
let availability = Availability(environment: env)
var receivedResult: Result<Availability.Status, CoreSdkClient.SalemoveError>?
availability.checkSecureConversationsAvailability(for: []) { result in
receivedResult = result
}
let result = try XCTUnwrap(receivedResult)
switch result {
case let .success(status):
XCTAssertEqual(status, .available(.transferred))
default:
XCTFail("Result should be `.success(.available)`. Got `\(result)` instead.")
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,8 @@ extension SecureConversationsTranscriptModelTests {
listQueues: modelEnv.listQueues,
isAuthenticated: { true },
log: logger,
queuesMonitor: .mock()
queuesMonitor: .mock(),
getCurrentEngagement: { .mock() }
)

let viewModel = TranscriptModel(
Expand Down Expand Up @@ -200,7 +201,8 @@ private extension SecureConversationsTranscriptModelTests {
listQueues: modelEnv.listQueues,
isAuthenticated: { true },
log: logger,
queuesMonitor: .mock()
queuesMonitor: .mock(),
getCurrentEngagement: { .mock() }
)

return TranscriptModel(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,8 @@ private extension SecureConversationsTranscriptModelTests {
listQueues: modelEnv.listQueues,
isAuthenticated: { true },
log: logger,
queuesMonitor: .mock()
queuesMonitor: .mock(),
getCurrentEngagement: { .mock() }
)

return TranscriptModel(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ private extension SecureConversationsTranscriptModelTests {
listQueues: modelEnv.listQueues,
isAuthenticated: { true },
log: logger,
queuesMonitor: .mock()
queuesMonitor: .mock(),
getCurrentEngagement: { .mock() }
)

return TranscriptModel(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,8 @@ private extension SecureConversationsTranscriptModelTests {
listQueues: modelEnv.listQueues,
isAuthenticated: { true },
log: logger,
queuesMonitor: .mock()
queuesMonitor: .mock(),
getCurrentEngagement: { .mock() }
)

return TranscriptModel(
Expand Down
Loading

0 comments on commit 4490165

Please sign in to comment.