2021-04-25 10:24:08 +00:00
|
|
|
import { reactive } from 'vue'
|
2020-05-07 13:10:53 +00:00
|
|
|
import { find, omitBy, orderBy, sumBy } from 'lodash'
|
|
|
|
import chatService from '../services/chat_service/chat_service.js'
|
|
|
|
import { parseChat, parseChatMessage } from '../services/entity_normalizer/entity_normalizer.service.js'
|
2020-07-12 21:06:45 +00:00
|
|
|
import { maybeShowChatNotification } from '../services/chat_utils/chat_utils.js'
|
2020-09-04 08:19:53 +00:00
|
|
|
import { promiseInterval } from '../services/promise_interval/promise_interval.js'
|
2020-05-07 13:10:53 +00:00
|
|
|
|
|
|
|
const emptyChatList = () => ({
|
|
|
|
data: [],
|
|
|
|
idStore: {}
|
|
|
|
})
|
|
|
|
|
|
|
|
const defaultState = {
|
|
|
|
chatList: emptyChatList(),
|
|
|
|
chatListFetcher: null,
|
2021-04-25 10:24:08 +00:00
|
|
|
openedChats: reactive({}),
|
|
|
|
openedChatMessageServices: reactive({}),
|
2020-05-07 13:10:53 +00:00
|
|
|
fetcher: undefined,
|
2020-10-29 10:33:06 +00:00
|
|
|
currentChatId: null,
|
|
|
|
lastReadMessageId: null
|
2020-05-07 13:10:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const getChatById = (state, id) => {
|
|
|
|
return find(state.chatList.data, { id })
|
|
|
|
}
|
|
|
|
|
|
|
|
const sortedChatList = (state) => {
|
|
|
|
return orderBy(state.chatList.data, ['updated_at'], ['desc'])
|
|
|
|
}
|
|
|
|
|
|
|
|
const unreadChatCount = (state) => {
|
|
|
|
return sumBy(state.chatList.data, 'unread')
|
|
|
|
}
|
|
|
|
|
|
|
|
const chats = {
|
|
|
|
state: { ...defaultState },
|
|
|
|
getters: {
|
|
|
|
currentChat: state => state.openedChats[state.currentChatId],
|
|
|
|
currentChatMessageService: state => state.openedChatMessageServices[state.currentChatId],
|
|
|
|
findOpenedChatByRecipientId: state => recipientId => find(state.openedChats, c => c.account.id === recipientId),
|
|
|
|
sortedChatList,
|
|
|
|
unreadChatCount
|
|
|
|
},
|
|
|
|
actions: {
|
|
|
|
// Chat list
|
|
|
|
startFetchingChats ({ dispatch, commit }) {
|
2020-09-02 18:01:31 +00:00
|
|
|
const fetcher = () => dispatch('fetchChats', { latest: true })
|
2020-05-07 13:10:53 +00:00
|
|
|
fetcher()
|
|
|
|
commit('setChatListFetcher', {
|
2020-09-04 08:19:53 +00:00
|
|
|
fetcher: () => promiseInterval(fetcher, 5000)
|
2020-05-07 13:10:53 +00:00
|
|
|
})
|
|
|
|
},
|
|
|
|
stopFetchingChats ({ commit }) {
|
|
|
|
commit('setChatListFetcher', { fetcher: undefined })
|
|
|
|
},
|
|
|
|
fetchChats ({ dispatch, rootState, commit }, params = {}) {
|
|
|
|
return rootState.api.backendInteractor.chats()
|
|
|
|
.then(({ chats }) => {
|
|
|
|
dispatch('addNewChats', { chats })
|
|
|
|
return chats
|
|
|
|
})
|
|
|
|
},
|
2020-07-12 21:06:45 +00:00
|
|
|
addNewChats (store, { chats }) {
|
|
|
|
const { commit, dispatch, rootGetters } = store
|
|
|
|
const newChatMessageSideEffects = (chat) => {
|
|
|
|
maybeShowChatNotification(store, chat)
|
|
|
|
}
|
|
|
|
commit('addNewChats', { dispatch, chats, rootGetters, newChatMessageSideEffects })
|
2020-05-07 13:10:53 +00:00
|
|
|
},
|
|
|
|
updateChat ({ commit }, { chat }) {
|
|
|
|
commit('updateChat', { chat })
|
|
|
|
},
|
|
|
|
|
|
|
|
// Opened Chats
|
|
|
|
startFetchingCurrentChat ({ commit, dispatch }, { fetcher }) {
|
|
|
|
dispatch('setCurrentChatFetcher', { fetcher })
|
|
|
|
},
|
|
|
|
setCurrentChatFetcher ({ rootState, commit }, { fetcher }) {
|
|
|
|
commit('setCurrentChatFetcher', { fetcher })
|
|
|
|
},
|
|
|
|
addOpenedChat ({ rootState, commit, dispatch }, { chat }) {
|
|
|
|
commit('addOpenedChat', { dispatch, chat: parseChat(chat) })
|
|
|
|
dispatch('addNewUsers', [chat.account])
|
|
|
|
},
|
|
|
|
addChatMessages ({ commit }, value) {
|
|
|
|
commit('addChatMessages', { commit, ...value })
|
|
|
|
},
|
|
|
|
resetChatNewMessageCount ({ commit }, value) {
|
|
|
|
commit('resetChatNewMessageCount', value)
|
|
|
|
},
|
|
|
|
clearCurrentChat ({ rootState, commit, dispatch }, value) {
|
|
|
|
commit('setCurrentChatId', { chatId: undefined })
|
|
|
|
commit('setCurrentChatFetcher', { fetcher: undefined })
|
|
|
|
},
|
|
|
|
readChat ({ rootState, commit, dispatch }, { id, lastReadId }) {
|
2020-10-29 10:33:06 +00:00
|
|
|
const isNewMessage = rootState.chats.lastReadMessageId !== lastReadId
|
|
|
|
|
2020-05-07 13:10:53 +00:00
|
|
|
dispatch('resetChatNewMessageCount')
|
2020-10-29 10:33:06 +00:00
|
|
|
commit('readChat', { id, lastReadId })
|
|
|
|
|
|
|
|
if (isNewMessage) {
|
|
|
|
rootState.api.backendInteractor.readChat({ id, lastReadId })
|
|
|
|
}
|
2020-05-07 13:10:53 +00:00
|
|
|
},
|
|
|
|
deleteChatMessage ({ rootState, commit }, value) {
|
|
|
|
rootState.api.backendInteractor.deleteChatMessage(value)
|
|
|
|
commit('deleteChatMessage', { commit, ...value })
|
|
|
|
},
|
|
|
|
resetChats ({ commit, dispatch }) {
|
|
|
|
dispatch('clearCurrentChat')
|
|
|
|
commit('resetChats', { commit })
|
|
|
|
},
|
|
|
|
clearOpenedChats ({ rootState, commit, dispatch, rootGetters }) {
|
|
|
|
commit('clearOpenedChats', { commit })
|
2020-10-29 10:33:06 +00:00
|
|
|
},
|
|
|
|
handleMessageError ({ commit }, value) {
|
|
|
|
commit('handleMessageError', { commit, ...value })
|
2021-02-17 08:16:58 +00:00
|
|
|
},
|
|
|
|
cullOlderMessages ({ commit }, chatId) {
|
|
|
|
commit('cullOlderMessages', chatId)
|
2020-05-07 13:10:53 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
mutations: {
|
|
|
|
setChatListFetcher (state, { commit, fetcher }) {
|
|
|
|
const prevFetcher = state.chatListFetcher
|
|
|
|
if (prevFetcher) {
|
2020-09-04 08:22:14 +00:00
|
|
|
prevFetcher.stop()
|
2020-05-07 13:10:53 +00:00
|
|
|
}
|
|
|
|
state.chatListFetcher = fetcher && fetcher()
|
|
|
|
},
|
|
|
|
setCurrentChatFetcher (state, { fetcher }) {
|
|
|
|
const prevFetcher = state.fetcher
|
|
|
|
if (prevFetcher) {
|
2020-09-04 08:22:14 +00:00
|
|
|
prevFetcher.stop()
|
2020-05-07 13:10:53 +00:00
|
|
|
}
|
|
|
|
state.fetcher = fetcher && fetcher()
|
|
|
|
},
|
|
|
|
addOpenedChat (state, { _dispatch, chat }) {
|
|
|
|
state.currentChatId = chat.id
|
2021-04-25 10:24:08 +00:00
|
|
|
state.openedChats[chat.id] = chat
|
2020-05-07 13:10:53 +00:00
|
|
|
|
|
|
|
if (!state.openedChatMessageServices[chat.id]) {
|
2021-04-25 10:24:08 +00:00
|
|
|
state.openedChatMessageServices[chat.id] = chatService.empty(chat.id)
|
2020-05-07 13:10:53 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
setCurrentChatId (state, { chatId }) {
|
|
|
|
state.currentChatId = chatId
|
|
|
|
},
|
2020-07-12 21:06:45 +00:00
|
|
|
addNewChats (state, { chats, newChatMessageSideEffects }) {
|
2020-05-07 13:10:53 +00:00
|
|
|
chats.forEach((updatedChat) => {
|
|
|
|
const chat = getChatById(state, updatedChat.id)
|
|
|
|
|
|
|
|
if (chat) {
|
2020-07-12 21:06:45 +00:00
|
|
|
const isNewMessage = (chat.lastMessage && chat.lastMessage.id) !== (updatedChat.lastMessage && updatedChat.lastMessage.id)
|
2020-05-07 13:10:53 +00:00
|
|
|
chat.lastMessage = updatedChat.lastMessage
|
|
|
|
chat.unread = updatedChat.unread
|
2020-09-16 02:08:12 +00:00
|
|
|
chat.updated_at = updatedChat.updated_at
|
2020-07-12 21:06:45 +00:00
|
|
|
if (isNewMessage && chat.unread) {
|
|
|
|
newChatMessageSideEffects(updatedChat)
|
|
|
|
}
|
2020-05-07 13:10:53 +00:00
|
|
|
} else {
|
|
|
|
state.chatList.data.push(updatedChat)
|
2021-04-25 10:24:08 +00:00
|
|
|
state.chatList.idStore[updatedChat.id] = updatedChat
|
2020-05-07 13:10:53 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
},
|
|
|
|
updateChat (state, { _dispatch, chat: updatedChat, _rootGetters }) {
|
|
|
|
const chat = getChatById(state, updatedChat.id)
|
|
|
|
if (chat) {
|
|
|
|
chat.lastMessage = updatedChat.lastMessage
|
|
|
|
chat.unread = updatedChat.unread
|
|
|
|
chat.updated_at = updatedChat.updated_at
|
|
|
|
}
|
|
|
|
if (!chat) { state.chatList.data.unshift(updatedChat) }
|
2021-04-25 10:24:08 +00:00
|
|
|
state.chatList.idStore[updatedChat.id] = updatedChat
|
2020-05-07 13:10:53 +00:00
|
|
|
},
|
|
|
|
deleteChat (state, { _dispatch, id, _rootGetters }) {
|
|
|
|
state.chats.data = state.chats.data.filter(conversation =>
|
|
|
|
conversation.last_status.id !== id
|
|
|
|
)
|
|
|
|
state.chats.idStore = omitBy(state.chats.idStore, conversation => conversation.last_status.id === id)
|
|
|
|
},
|
|
|
|
resetChats (state, { commit }) {
|
|
|
|
state.chatList = emptyChatList()
|
|
|
|
state.currentChatId = null
|
|
|
|
commit('setChatListFetcher', { fetcher: undefined })
|
|
|
|
for (const chatId in state.openedChats) {
|
|
|
|
chatService.clear(state.openedChatMessageServices[chatId])
|
2021-04-25 10:24:08 +00:00
|
|
|
delete state.openedChats[chatId]
|
|
|
|
delete state.openedChatMessageServices[chatId]
|
2020-05-07 13:10:53 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
setChatsLoading (state, { value }) {
|
|
|
|
state.chats.loading = value
|
|
|
|
},
|
2020-09-15 23:34:19 +00:00
|
|
|
addChatMessages (state, { chatId, messages, updateMaxId }) {
|
2020-05-07 13:10:53 +00:00
|
|
|
const chatMessageService = state.openedChatMessageServices[chatId]
|
|
|
|
if (chatMessageService) {
|
2020-09-15 23:34:19 +00:00
|
|
|
chatService.add(chatMessageService, { messages: messages.map(parseChatMessage), updateMaxId })
|
2020-05-07 13:10:53 +00:00
|
|
|
}
|
|
|
|
},
|
2020-09-15 23:34:19 +00:00
|
|
|
deleteChatMessage (state, { chatId, messageId }) {
|
2020-05-07 13:10:53 +00:00
|
|
|
const chatMessageService = state.openedChatMessageServices[chatId]
|
|
|
|
if (chatMessageService) {
|
|
|
|
chatService.deleteMessage(chatMessageService, messageId)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
resetChatNewMessageCount (state, _value) {
|
|
|
|
const chatMessageService = state.openedChatMessageServices[state.currentChatId]
|
|
|
|
chatService.resetNewMessageCount(chatMessageService)
|
|
|
|
},
|
|
|
|
// Used when a connection loss occurs
|
|
|
|
clearOpenedChats (state) {
|
|
|
|
const currentChatId = state.currentChatId
|
|
|
|
for (const chatId in state.openedChats) {
|
|
|
|
if (currentChatId !== chatId) {
|
|
|
|
chatService.clear(state.openedChatMessageServices[chatId])
|
2021-04-25 10:24:08 +00:00
|
|
|
delete state.openedChats[chatId]
|
|
|
|
delete state.openedChatMessageServices[chatId]
|
2020-05-07 13:10:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
2020-10-29 10:33:06 +00:00
|
|
|
readChat (state, { id, lastReadId }) {
|
|
|
|
state.lastReadMessageId = lastReadId
|
2020-05-07 13:10:53 +00:00
|
|
|
const chat = getChatById(state, id)
|
|
|
|
if (chat) {
|
|
|
|
chat.unread = 0
|
|
|
|
}
|
2020-10-29 10:33:06 +00:00
|
|
|
},
|
|
|
|
handleMessageError (state, { chatId, fakeId, isRetry }) {
|
|
|
|
const chatMessageService = state.openedChatMessageServices[chatId]
|
|
|
|
chatService.handleMessageError(chatMessageService, fakeId, isRetry)
|
2021-02-17 08:16:58 +00:00
|
|
|
},
|
|
|
|
cullOlderMessages (state, chatId) {
|
|
|
|
chatService.cullOlderMessages(state.openedChatMessageServices[chatId])
|
2020-05-07 13:10:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default chats
|