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

138 lines
3.8 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-16 00:31:30 +00:00
/**
*
*/
export const manageIncomingChatMessage = (chatMessage) => (dispatch, getState) => {
if (!chatMessage) return
dispatch(sendChatMessageSuccess(chatMessage))
const isOnline = getState().getIn(['chat_conversation_messages', chatMessage.chat_conversation_id, 'online'])
// : todo :
// Check if is online for conversation, if not increase total/convo unread count
}
2020-11-25 21:22:37 +00:00
/**
*
*/
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
2020-12-19 06:33:33 +00:00
dispatch(purgeChatMessagesRequest(chatConversationId))
2020-12-10 16:51:45 +00:00
2020-12-16 00:31:30 +00:00
api(getState).delete(`/api/v1/chat_conversations/messages/${chatConversationId}/destroy_all`).then((response) => {
2020-12-19 06:33:33 +00:00
dispatch(purgeChatMessagesSuccess(chatConversationId))
2020-12-10 16:51:45 +00:00
}).catch((error) => {
2020-12-19 06:33:33 +00:00
dispatch(purgeChatMessagesFail(error))
2020-12-10 16:51:45 +00:00
})
}
2020-12-16 00:31:30 +00:00
const purgeChatMessagesRequest = (chatConversationId) => ({
2020-12-10 16:51:45 +00:00
type: CHAT_MESSAGES_PURGE_REQUEST,
2020-12-16 00:31:30 +00:00
chatConversationId,
2020-12-10 16:51:45 +00:00
})
2020-12-16 00:31:30 +00:00
const purgeChatMessagesSuccess = (chatConversationId) => ({
2020-12-10 16:51:45 +00:00
type: CHAT_MESSAGES_PURGE_SUCCESS,
chatConversationId,
showToast: true,
})
2020-12-16 00:31:30 +00:00
const purgeChatMessagesFail = (error) => ({
2020-12-10 16:51:45 +00:00
type: CHAT_MESSAGES_PURGE_FAIL,
showToast: true,
error,
2020-11-25 21:22:37 +00:00
})