Skip to content

Commit

Permalink
Fix sending message with attachments
Browse files Browse the repository at this point in the history
This commit fixes the case when OutgoingMessage stored in messagesSection did not include attachment object. It led to the situation when the attachment was missed if initial send message request failed.

MOB-3724
  • Loading branch information
Egor Egorov authored and github-review-helper committed Oct 24, 2024
1 parent 1027089 commit be239ec
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 10 deletions.
12 changes: 2 additions & 10 deletions GliaWidgets/Sources/ViewModel/Chat/ChatViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -432,7 +432,7 @@ extension ChatViewModel {
let uploads = fileUploadListModel.succeededUploads
let files = uploads.map { $0.localFile }

let payload = environment.createSendMessagePayload(messageText, nil)
let payload = environment.createSendMessagePayload(messageText, attachment)

let outgoingMessage = OutgoingMessage(
payload: payload,
Expand All @@ -446,17 +446,9 @@ extension ChatViewModel {
fileUploadListModel.succeededUploads.forEach { action?(.removeUpload($0)) }
fileUploadListModel.removeSucceededUploads()
action?(.scrollToBottom(animated: true))
let messageTextTemp = messageText
messageText = ""

// In order to keep using same payload, with its
// initial ID, we make mutable copy of it
// and assign attachment and content accordingly
// before passing payload to send-message request.
var messagePayload = outgoingMessage.payload
messagePayload.content = messageTextTemp
messagePayload.attachment = attachment
interactor.send(messagePayload: messagePayload) { [weak self] result in
interactor.send(messagePayload: outgoingMessage.payload) { [weak self] result in
guard let self else { return }

switch result {
Expand Down
33 changes: 33 additions & 0 deletions GliaWidgetsTests/Sources/ChatViewModel/ChatViewModelTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -941,6 +941,39 @@ class ChatViewModelTests: XCTestCase {
viewModel.update(for: .engaged(.mock()))
XCTAssertTrue(viewModel.getPendingMessageForTesting().isEmpty)
}

func test_messageAttachmentIsKeptAfterFailureSending() throws {
var interactorEnv = Interactor.Environment.mock
interactorEnv.coreSdk.sendMessageWithMessagePayload = { _, callback in
callback(.failure(.mock()))
}
interactorEnv.coreSdk.sendMessagePreview = { _, _ in }
let interactor = Interactor.mock(environment: interactorEnv)
interactor.state = .engaged(nil)

var viewModelEnv = ChatViewModel.Environment.failing()
viewModelEnv.fileManager.urlsForDirectoryInDomainMask = { _, _ in [.mock] }
viewModelEnv.fileManager.createDirectoryAtUrlWithIntermediateDirectories = { _, _, _ in }

let upload = FileUpload.mock()
upload.state.value = .uploaded(file: try .mock())
var fileUploadListViewModelEnv = SecureConversations.FileUploadListViewModel.Environment.mock
fileUploadListViewModelEnv.uploader.uploads = [upload]
viewModelEnv.createFileUploadListModel = { _ in .mock(environment: fileUploadListViewModelEnv) }
viewModelEnv.createSendMessagePayload = { .mock(content: $0, attachment: $1) }
let viewModel = ChatViewModel.mock(interactor: interactor, environment: viewModelEnv)

let messageContent = "Mock"
viewModel.invokeSetTextAndSendMessage(text: messageContent)

switch viewModel.messagesSection.items.last?.kind {
case let .outgoingMessage(message, _):
XCTAssertNotNil(message.payload.attachment)
XCTAssertEqual(message.payload.content, messageContent)
default:
XCTFail("message kind should be `visitorMessage`")
}
}
}

extension ChatChoiceCardOption {
Expand Down

0 comments on commit be239ec

Please sign in to comment.