gab-social/app/javascript/gabsocial/actions/chat_messages.js

123 lines
3.4 KiB
JavaScript
Raw Normal View History

import { Map as ImmutableMap, List as ImmutableList, toJS } from 'immutable'
import noop from 'lodash.noop'
2020-11-25 21:22:37 +00:00
import api from '../api'
import { me } from '../initial_state'
import { importFetchedChatMessages } from './importer'
2020-11-25 21:22:37 +00:00
export const CHAT_MESSAGES_SEND_REQUEST = 'CHAT_MESSAGES_SEND_REQUEST'
export const CHAT_MESSAGES_SEND_SUCCESS = 'CHAT_MESSAGES_SEND_SUCCESS'
export const CHAT_MESSAGES_SEND_FAIL = 'CHAT_MESSAGES_SEND_FAIL'
2020-11-25 21:22:37 +00:00
export const CHAT_MESSAGES_DELETE_REQUEST = 'CHAT_MESSAGES_DELETE_REQUEST'
export const CHAT_MESSAGES_DELETE_SUCCESS = 'CHAT_MESSAGES_DELETE_SUCCESS'
export const CHAT_MESSAGES_DELETE_FAIL = 'CHAT_MESSAGES_DELETE_FAIL'
2020-11-25 21:22:37 +00:00
2020-12-10 16:51:45 +00:00
export const CHAT_MESSAGES_PURGE_REQUEST = 'CHAT_MESSAGES_PURGE_REQUEST'
export const CHAT_MESSAGES_PURGE_SUCCESS = 'CHAT_MESSAGES_PURGE_SUCCESS'
export const CHAT_MESSAGES_PURGE_FAIL = 'CHAT_MESSAGES_PURGE_FAIL'
2020-11-25 21:22:37 +00:00
/**
*
*/
export const sendChatMessage = (text = '', chatConversationId) => (dispatch, getState) => {
if (!me || !chatConversationId) return
if (text.length === 0) return
2020-11-25 21:22:37 +00:00
2020-12-03 22:13:11 +00:00
dispatch(sendChatMessageRequest(chatConversationId))
2020-11-25 21:22:37 +00:00
api(getState).post('/api/v1/chat_messages', {
2020-11-25 21:22:37 +00:00
text,
chat_conversation_id: chatConversationId,
2020-11-25 21:22:37 +00:00
}, {
// headers: {
// 'Idempotency-Key': getState().getIn(['chat_compose`', 'idempotencyKey']),
// },
2020-11-25 21:22:37 +00:00
}).then((response) => {
dispatch(importFetchedChatMessages([response.data]))
2020-12-03 22:13:11 +00:00
dispatch(sendChatMessageSuccess(response.data))
2020-11-25 21:22:37 +00:00
}).catch((error) => {
2020-12-03 22:13:11 +00:00
dispatch(sendChatMessageFail(error))
2020-11-25 21:22:37 +00:00
})
}
2020-12-03 22:13:11 +00:00
const sendChatMessageRequest = (chatConversationId) => ({
type: CHAT_MESSAGES_SEND_REQUEST,
2020-12-03 22:13:11 +00:00
chatConversationId,
2020-11-25 21:22:37 +00:00
})
2020-12-03 22:13:11 +00:00
export const sendChatMessageSuccess = (chatMessage) => ({
type: CHAT_MESSAGES_SEND_SUCCESS,
chatMessage,
2020-11-25 21:22:37 +00:00
})
2020-12-03 22:13:11 +00:00
const sendChatMessageFail = (error) => ({
type: CHAT_MESSAGES_SEND_FAIL,
showToast: true,
2020-11-25 21:22:37 +00:00
error,
})
/**
*
*/
2020-12-03 22:13:11 +00:00
export const deleteChatMessage = (chatMessageId) => (dispatch, getState) => {
if (!me || !chatMessageId) return
2020-11-25 21:22:37 +00:00
2020-12-03 22:13:11 +00:00
dispatch(deleteChatMessageRequest(chatMessageId))
2020-11-25 21:22:37 +00:00
api(getState).delete(`/api/v1/chat_messages/${chatMessageId}`, {}, {
// headers: {
// 'Idempotency-Key': getState().getIn(['chat_compose', 'idempotencyKey']),
// },
2020-11-25 21:22:37 +00:00
}).then((response) => {
2020-12-03 22:13:11 +00:00
dispatch(deleteChatMessageSuccess(response.data))
2020-11-25 21:22:37 +00:00
}).catch((error) => {
2020-12-03 22:13:11 +00:00
dispatch(deleteChatMessageFail(error))
2020-11-25 21:22:37 +00:00
})
}
2020-12-03 22:13:11 +00:00
const deleteChatMessageRequest = (chatMessageId) => ({
type: CHAT_MESSAGES_DELETE_REQUEST,
chatMessageId,
2020-11-25 21:22:37 +00:00
})
2020-12-03 22:13:11 +00:00
const deleteChatMessageSuccess = () => ({
type: CHAT_MESSAGES_DELETE_SUCCESS,
showToast: true,
2020-11-25 21:22:37 +00:00
})
2020-12-03 22:13:11 +00:00
const deleteChatMessageFail = (error) => ({
type: CHAT_MESSAGES_DELETE_FAIL,
showToast: true,
2020-11-25 21:22:37 +00:00
error,
2020-12-10 16:51:45 +00:00
})
/**
*
*/
export const purgeChatMessages = (chatConversationId) => (dispatch, getState) => {
if (!me || !chatConversationId) return
dispatch(deleteChatMessagesRequest(chatConversationId))
api(getState).delete(`/api/v1/chat_conversations/${chatConversationId}/messages/destroy_all`).then((response) => {
dispatch(deleteChatMessagesSuccess(response.data))
}).catch((error) => {
dispatch(deleteChatMessagesFail(error))
})
}
const deleteChatMessagesRequest = () => ({
type: CHAT_MESSAGES_PURGE_REQUEST,
})
const deleteChatMessagesSuccess = (chatConversationId) => ({
type: CHAT_MESSAGES_PURGE_SUCCESS,
chatConversationId,
showToast: true,
})
const deleteChatMessagesFail = (error) => ({
type: CHAT_MESSAGES_PURGE_FAIL,
showToast: true,
error,
2020-11-25 21:22:37 +00:00
})