[Chat][Feature] Edit Message Redux (#526)

This commit is contained in:
ShaunaSong 2022-10-19 16:51:39 -07:00 коммит произвёл GitHub
Родитель 8a6cbbf302
Коммит 899a0e96c9
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
4 изменённых файлов: 97 добавлений и 0 удалений

Просмотреть файл

@ -13,6 +13,7 @@ internal class ErrorCode : ExpandableStringEnum<ErrorCode?>() {
val CHAT_FETCH_MESSAGES_FAILED = fromString("chatFetchMessagesFailed")
val CHAT_REQUEST_PARTICIPANTS_FETCH_FAILED =
fromString("chatRequestParticipantsFetchFailed")
val CHAT_SEND_EDIT_MESSAGE_FAILED = fromString("chatSendEditMessageFailed")
val CHAT_SEND_READ_RECEIPT_FAILED = fromString("chatSendReadReceiptFailed")
val CHAT_SEND_TYPING_INDICATOR_FAILED = fromString("chatSendTypingIndicatorFailed")

Просмотреть файл

@ -20,6 +20,7 @@ internal sealed class ChatAction : Action {
class MessagesPageReceived(val messages: List<MessageInfoModel>) : ChatAction()
class EndChat : ChatAction()
class MessageReceived(val message: MessageInfoModel) : ChatAction()
class EditMessage(val message: MessageInfoModel) : ChatAction()
class MessageEdited(val message: MessageInfoModel) : ChatAction()
class MessageRead(val messageId: String) : ChatAction()
class TypingIndicator : ChatAction()

Просмотреть файл

@ -27,6 +27,7 @@ internal class ChatActionHandler(private val chatService: ChatService) {
)
is ChatAction.SendMessage -> sendMessage(action = action, dispatch = dispatch)
is ChatAction.FetchMessages -> fetchMessages()
is ChatAction.EditMessage -> editMessage(action = action, dispatch = dispatch)
is ChatAction.DeleteMessage -> deleteMessage(action = action, dispatch = dispatch)
is ChatAction.MessageRead -> sendReadReceipt(action = action, dispatch = dispatch)
is ChatAction.TypingIndicator -> sendTypingIndicator(dispatch = dispatch)
@ -97,6 +98,23 @@ internal class ChatActionHandler(private val chatService: ChatService) {
}
}
private fun editMessage(action: ChatAction.EditMessage, dispatch: Dispatch) {
chatService.editMessage(action.message.id ?: "", action.message.content ?: "")
.whenComplete { _, error ->
if (error != null) {
dispatch(
ErrorAction.ChatStateErrorOccurred(
chatStateError = ChatStateError(
errorCode = ErrorCode.CHAT_SEND_EDIT_MESSAGE_FAILED
)
)
)
} else {
dispatch(ChatAction.MessageEdited(action.message))
}
}
}
private fun sendReadReceipt(action: ChatAction.MessageRead, dispatch: Dispatch) {
chatService.sendReadReceipt(action.messageId).whenComplete { _, error ->
if (error != null) {

Просмотреть файл

@ -197,6 +197,83 @@ internal class ChatActionHandlerUnitTest : ACSBaseTestCoroutine() {
)
}
@ExperimentalCoroutinesApi
@Test
fun chatMiddlewareActionHandler_editMessage_then_dispatch_ChatActionEditMessage() =
runScopedTest {
// arrange
val messageInfoModel = MessageInfoModel(
id = "Message",
internalId = "54321",
messageType = ChatMessageType.TEXT,
content = "hello, world!"
)
val editChatMessageCompletableFuture = CompletableFuture<Void>()
val mockChatService: ChatService = mock {
on { editMessage(messageInfoModel.id.toString(), messageInfoModel.content.toString()) } doReturn editChatMessageCompletableFuture
}
val chatHandler = ChatActionHandler(mockChatService)
val action = ChatAction.EditMessage(messageInfoModel)
val mockAppStore = mock<AppStore<ReduxState>> {
on { dispatch(any()) } doAnswer { }
}
val mockAppState = mock<ReduxState> {}
// act
chatHandler.onAction(action, mockAppStore::dispatch, mockAppState)
editChatMessageCompletableFuture.complete(any())
// assert
verify(mockAppStore, times(1)).dispatch(
argThat { action ->
action is ChatAction.MessageEdited
}
)
}
@ExperimentalCoroutinesApi
@Test
fun chatMiddlewareActionHandler_editMessage_then_dispatch_ChatStateErrorOccurred() =
runScopedTest {
// arrange
val messageInfoModel = MessageInfoModel(
id = "Message",
internalId = "54321",
messageType = ChatMessageType.TEXT,
content = "hello, world!"
)
val error = Exception("test")
val editChatMessageCompletableFuture = CompletableFuture<Void>()
val mockChatService: ChatService = mock {
on { editMessage(messageInfoModel.id.toString(), messageInfoModel.content.toString()) } doReturn editChatMessageCompletableFuture
}
val chatHandler = ChatActionHandler(mockChatService)
val action = ChatAction.EditMessage(messageInfoModel)
val mockAppStore = mock<AppStore<ReduxState>> {
on { dispatch(any()) } doAnswer { }
}
val mockAppState = mock<ReduxState> {}
// act
chatHandler.onAction(action, mockAppStore::dispatch, mockAppState)
editChatMessageCompletableFuture.completeExceptionally(error)
// assert
verify(mockAppStore, times(1)).dispatch(
argThat { action ->
action is ErrorAction.ChatStateErrorOccurred
}
)
}
@ExperimentalCoroutinesApi
@Test
fun chatMiddlewareActionHandler_fetchMessage_then_call_chatServiceGetPreviousPage() =