gab-social/app/javascript/gabsocial/reducers/chat_conversations.js

74 lines
2.7 KiB
JavaScript
Raw Normal View History

import {
Map as ImmutableMap,
List as ImmutableList,
fromJS,
} from 'immutable'
import { me } from '../initial_state'
2020-12-03 22:13:11 +00:00
import {
CHAT_MESSAGES_SEND_SUCCESS,
CHAT_MESSAGES_DELETE_REQUEST,
2020-12-16 00:31:30 +00:00
CHAT_MESSAGES_PURGE_REQUEST,
2020-12-03 22:13:11 +00:00
} from '../actions/chat_messages'
import {
CHAT_CONVERSATIONS_CREATE_SUCCESS,
CHAT_CONVERSATIONS_APPROVED_FETCH_SUCCESS,
CHAT_CONVERSATIONS_APPROVED_EXPAND_SUCCESS,
CHAT_CONVERSATIONS_REQUESTED_FETCH_SUCCESS,
CHAT_CONVERSATIONS_REQUESTED_EXPAND_SUCCESS,
2020-12-22 19:19:38 +00:00
CHAT_CONVERSATIONS_MUTED_FETCH_SUCCESS,
CHAT_CONVERSATIONS_MUTED_EXPAND_SUCCESS,
CHAT_CONVERSATION_REQUEST_APPROVE_SUCCESS,
2020-12-16 00:31:30 +00:00
CHAT_CONVERSATION_MARK_READ_SUCCESS,
2020-12-19 06:33:33 +00:00
SET_CHAT_CONVERSATION_EXPIRATION_SUCCESS,
CHAT_CONVERSATION_APPROVED_SEARCH_FETCH_SUCCESS,
2020-12-22 19:19:38 +00:00
CHAT_CONVERSATION_HIDE_FETCH,
} from '../actions/chat_conversations'
const initialState = ImmutableMap()
export const normalizeChatConversation = (chatConversation) => {
const { other_accounts, ...rest } = chatConversation
return fromJS({
...rest,
other_account_ids: other_accounts.map((a) => a.id),
})
}
2020-12-03 22:13:11 +00:00
const setLastChatMessage = (state, chatMessage) => {
return state.setIn([chatMessage.chat_conversation_id, 'last_chat_message'], fromJS(chatMessage))
}
const importChatConversation = (state, chatConversation) => state.set(chatConversation.chat_conversation_id, normalizeChatConversation(chatConversation))
const importChatConversations = (state, chatConversations) => {
return state.withMutations((mutable) => chatConversations.forEach((chatConversation) => importChatConversation(mutable, chatConversation)))
}
export default function chat_conversations(state = initialState, action) {
switch(action.type) {
case CHAT_CONVERSATION_REQUEST_APPROVE_SUCCESS:
2020-12-19 06:33:33 +00:00
case SET_CHAT_CONVERSATION_EXPIRATION_SUCCESS:
case CHAT_CONVERSATIONS_CREATE_SUCCESS:
return importChatConversation(state, action.chatConversation)
case CHAT_CONVERSATIONS_APPROVED_FETCH_SUCCESS:
case CHAT_CONVERSATIONS_APPROVED_EXPAND_SUCCESS:
case CHAT_CONVERSATIONS_REQUESTED_FETCH_SUCCESS:
case CHAT_CONVERSATIONS_REQUESTED_EXPAND_SUCCESS:
2020-12-22 19:19:38 +00:00
case CHAT_CONVERSATIONS_MUTED_FETCH_SUCCESS:
case CHAT_CONVERSATIONS_MUTED_EXPAND_SUCCESS:
case CHAT_CONVERSATION_APPROVED_SEARCH_FETCH_SUCCESS:
return importChatConversations(state, action.chatConversations)
2020-12-03 22:13:11 +00:00
case CHAT_MESSAGES_SEND_SUCCESS:
return setLastChatMessage(state, action.chatMessage)
2020-12-22 19:19:38 +00:00
case CHAT_CONVERSATION_HIDE_FETCH:
return state.delete(action.chatConversationId)
2020-12-16 00:31:30 +00:00
case CHAT_MESSAGES_PURGE_REQUEST:
// : todo :
return state
case CHAT_CONVERSATION_MARK_READ_SUCCESS:
return importChatConversation(state, action.chatConversation)
default:
return state
}
}