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

85 lines
2.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
/**
*
*/
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,
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,
2020-11-25 21:22:37 +00:00
})
2020-12-03 22:13:11 +00:00
const deleteChatMessageFail = (error) => ({
type: CHAT_MESSAGES_DELETE_FAIL,
2020-11-25 21:22:37 +00:00
error,
})