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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix sending message with attachments #1088

Merged
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading