biome format --write
This commit is contained in:
parent
8372348148
commit
9262e803ec
415 changed files with 54076 additions and 17419 deletions
|
|
@ -1,13 +1,16 @@
|
|||
import { reactive } from 'vue'
|
||||
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'
|
||||
import {
|
||||
parseChat,
|
||||
parseChatMessage,
|
||||
} from '../services/entity_normalizer/entity_normalizer.service.js'
|
||||
import { maybeShowChatNotification } from '../services/chat_utils/chat_utils.js'
|
||||
import { promiseInterval } from '../services/promise_interval/promise_interval.js'
|
||||
|
||||
const emptyChatList = () => ({
|
||||
data: [],
|
||||
idStore: {}
|
||||
idStore: {},
|
||||
})
|
||||
|
||||
const defaultState = {
|
||||
|
|
@ -17,7 +20,7 @@ const defaultState = {
|
|||
openedChatMessageServices: reactive({}),
|
||||
fetcher: undefined,
|
||||
currentChatId: null,
|
||||
lastReadMessageId: null
|
||||
lastReadMessageId: null,
|
||||
}
|
||||
|
||||
const getChatById = (state, id) => {
|
||||
|
|
@ -35,65 +38,74 @@ const unreadChatCount = (state) => {
|
|||
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),
|
||||
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
|
||||
unreadChatCount,
|
||||
},
|
||||
actions: {
|
||||
// Chat list
|
||||
startFetchingChats ({ dispatch, commit }) {
|
||||
startFetchingChats({ dispatch, commit }) {
|
||||
const fetcher = () => dispatch('fetchChats', { latest: true })
|
||||
fetcher()
|
||||
commit('setChatListFetcher', {
|
||||
fetcher: () => promiseInterval(fetcher, 5000)
|
||||
fetcher: () => promiseInterval(fetcher, 5000),
|
||||
})
|
||||
},
|
||||
stopFetchingChats ({ commit }) {
|
||||
stopFetchingChats({ commit }) {
|
||||
commit('setChatListFetcher', { fetcher: undefined })
|
||||
},
|
||||
fetchChats ({ dispatch, rootState }) {
|
||||
return rootState.api.backendInteractor.chats()
|
||||
.then(({ chats }) => {
|
||||
dispatch('addNewChats', { chats })
|
||||
return chats
|
||||
})
|
||||
fetchChats({ dispatch, rootState }) {
|
||||
return rootState.api.backendInteractor.chats().then(({ chats }) => {
|
||||
dispatch('addNewChats', { chats })
|
||||
return chats
|
||||
})
|
||||
},
|
||||
addNewChats (store, { chats }) {
|
||||
addNewChats(store, { chats }) {
|
||||
const { commit, dispatch, rootGetters } = store
|
||||
const newChatMessageSideEffects = (chat) => {
|
||||
maybeShowChatNotification(store, chat)
|
||||
}
|
||||
commit('addNewUsers', chats.map(k => k.account).filter(k => k))
|
||||
commit('addNewChats', { dispatch, chats, rootGetters, newChatMessageSideEffects })
|
||||
commit(
|
||||
'addNewUsers',
|
||||
chats.map((k) => k.account).filter((k) => k),
|
||||
)
|
||||
commit('addNewChats', {
|
||||
dispatch,
|
||||
chats,
|
||||
rootGetters,
|
||||
newChatMessageSideEffects,
|
||||
})
|
||||
},
|
||||
updateChat ({ commit }, { chat }) {
|
||||
updateChat({ commit }, { chat }) {
|
||||
commit('updateChat', { chat })
|
||||
},
|
||||
|
||||
// Opened Chats
|
||||
startFetchingCurrentChat ({ dispatch }, { fetcher }) {
|
||||
startFetchingCurrentChat({ dispatch }, { fetcher }) {
|
||||
dispatch('setCurrentChatFetcher', { fetcher })
|
||||
},
|
||||
setCurrentChatFetcher ({ commit }, { fetcher }) {
|
||||
setCurrentChatFetcher({ commit }, { fetcher }) {
|
||||
commit('setCurrentChatFetcher', { fetcher })
|
||||
},
|
||||
addOpenedChat ({ commit, dispatch }, { chat }) {
|
||||
addOpenedChat({ commit, dispatch }, { chat }) {
|
||||
commit('addOpenedChat', { dispatch, chat: parseChat(chat) })
|
||||
dispatch('addNewUsers', [chat.account])
|
||||
},
|
||||
addChatMessages ({ commit }, value) {
|
||||
addChatMessages({ commit }, value) {
|
||||
commit('addChatMessages', { commit, ...value })
|
||||
},
|
||||
resetChatNewMessageCount ({ commit }, value) {
|
||||
resetChatNewMessageCount({ commit }, value) {
|
||||
commit('resetChatNewMessageCount', value)
|
||||
},
|
||||
clearCurrentChat ({ commit }) {
|
||||
clearCurrentChat({ commit }) {
|
||||
commit('setCurrentChatId', { chatId: undefined })
|
||||
commit('setCurrentChatFetcher', { fetcher: undefined })
|
||||
},
|
||||
readChat ({ rootState, commit, dispatch }, { id, lastReadId }) {
|
||||
readChat({ rootState, commit, dispatch }, { id, lastReadId }) {
|
||||
const isNewMessage = rootState.chats.lastReadMessageId !== lastReadId
|
||||
|
||||
dispatch('resetChatNewMessageCount')
|
||||
|
|
@ -103,40 +115,40 @@ const chats = {
|
|||
rootState.api.backendInteractor.readChat({ id, lastReadId })
|
||||
}
|
||||
},
|
||||
deleteChatMessage ({ rootState, commit }, value) {
|
||||
deleteChatMessage({ rootState, commit }, value) {
|
||||
rootState.api.backendInteractor.deleteChatMessage(value)
|
||||
commit('deleteChatMessage', { commit, ...value })
|
||||
},
|
||||
resetChats ({ commit, dispatch }) {
|
||||
resetChats({ commit, dispatch }) {
|
||||
dispatch('clearCurrentChat')
|
||||
commit('resetChats', { commit })
|
||||
},
|
||||
clearOpenedChats ({ commit }) {
|
||||
clearOpenedChats({ commit }) {
|
||||
commit('clearOpenedChats', { commit })
|
||||
},
|
||||
handleMessageError ({ commit }, value) {
|
||||
handleMessageError({ commit }, value) {
|
||||
commit('handleMessageError', { commit, ...value })
|
||||
},
|
||||
cullOlderMessages ({ commit }, chatId) {
|
||||
cullOlderMessages({ commit }, chatId) {
|
||||
commit('cullOlderMessages', chatId)
|
||||
}
|
||||
},
|
||||
},
|
||||
mutations: {
|
||||
setChatListFetcher (state, { fetcher }) {
|
||||
setChatListFetcher(state, { fetcher }) {
|
||||
const prevFetcher = state.chatListFetcher
|
||||
if (prevFetcher) {
|
||||
prevFetcher.stop()
|
||||
}
|
||||
state.chatListFetcher = fetcher && fetcher()
|
||||
},
|
||||
setCurrentChatFetcher (state, { fetcher }) {
|
||||
setCurrentChatFetcher(state, { fetcher }) {
|
||||
const prevFetcher = state.fetcher
|
||||
if (prevFetcher) {
|
||||
prevFetcher.stop()
|
||||
}
|
||||
state.fetcher = fetcher && fetcher()
|
||||
},
|
||||
addOpenedChat (state, { chat }) {
|
||||
addOpenedChat(state, { chat }) {
|
||||
state.currentChatId = chat.id
|
||||
state.openedChats[chat.id] = chat
|
||||
|
||||
|
|
@ -144,15 +156,17 @@ const chats = {
|
|||
state.openedChatMessageServices[chat.id] = chatService.empty(chat.id)
|
||||
}
|
||||
},
|
||||
setCurrentChatId (state, { chatId }) {
|
||||
setCurrentChatId(state, { chatId }) {
|
||||
state.currentChatId = chatId
|
||||
},
|
||||
addNewChats (state, { chats, newChatMessageSideEffects }) {
|
||||
addNewChats(state, { chats, newChatMessageSideEffects }) {
|
||||
chats.forEach((updatedChat) => {
|
||||
const chat = getChatById(state, updatedChat.id)
|
||||
|
||||
if (chat) {
|
||||
const isNewMessage = (chat.lastMessage && chat.lastMessage.id) !== (updatedChat.lastMessage && updatedChat.lastMessage.id)
|
||||
const isNewMessage =
|
||||
(chat.lastMessage && chat.lastMessage.id) !==
|
||||
(updatedChat.lastMessage && updatedChat.lastMessage.id)
|
||||
chat.lastMessage = updatedChat.lastMessage
|
||||
chat.unread = updatedChat.unread
|
||||
chat.updated_at = updatedChat.updated_at
|
||||
|
|
@ -165,23 +179,28 @@ const chats = {
|
|||
}
|
||||
})
|
||||
},
|
||||
updateChat (state, { chat: updatedChat }) {
|
||||
updateChat(state, { chat: updatedChat }) {
|
||||
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) }
|
||||
if (!chat) {
|
||||
state.chatList.data.unshift(updatedChat)
|
||||
}
|
||||
state.chatList.idStore[updatedChat.id] = updatedChat
|
||||
},
|
||||
deleteChat (state, { id }) {
|
||||
state.chats.data = state.chats.data.filter(conversation =>
|
||||
conversation.last_status.id !== id
|
||||
deleteChat(state, { id }) {
|
||||
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,
|
||||
)
|
||||
state.chats.idStore = omitBy(state.chats.idStore, conversation => conversation.last_status.id === id)
|
||||
},
|
||||
resetChats (state, { commit }) {
|
||||
resetChats(state, { commit }) {
|
||||
state.chatList = emptyChatList()
|
||||
state.currentChatId = null
|
||||
commit('setChatListFetcher', { fetcher: undefined })
|
||||
|
|
@ -191,27 +210,31 @@ const chats = {
|
|||
delete state.openedChatMessageServices[chatId]
|
||||
}
|
||||
},
|
||||
setChatsLoading (state, { value }) {
|
||||
setChatsLoading(state, { value }) {
|
||||
state.chats.loading = value
|
||||
},
|
||||
addChatMessages (state, { chatId, messages, updateMaxId }) {
|
||||
addChatMessages(state, { chatId, messages, updateMaxId }) {
|
||||
const chatMessageService = state.openedChatMessageServices[chatId]
|
||||
if (chatMessageService) {
|
||||
chatService.add(chatMessageService, { messages: messages.map(parseChatMessage), updateMaxId })
|
||||
chatService.add(chatMessageService, {
|
||||
messages: messages.map(parseChatMessage),
|
||||
updateMaxId,
|
||||
})
|
||||
}
|
||||
},
|
||||
deleteChatMessage (state, { chatId, messageId }) {
|
||||
deleteChatMessage(state, { chatId, messageId }) {
|
||||
const chatMessageService = state.openedChatMessageServices[chatId]
|
||||
if (chatMessageService) {
|
||||
chatService.deleteMessage(chatMessageService, messageId)
|
||||
}
|
||||
},
|
||||
resetChatNewMessageCount (state) {
|
||||
const chatMessageService = state.openedChatMessageServices[state.currentChatId]
|
||||
resetChatNewMessageCount(state) {
|
||||
const chatMessageService =
|
||||
state.openedChatMessageServices[state.currentChatId]
|
||||
chatService.resetNewMessageCount(chatMessageService)
|
||||
},
|
||||
// Used when a connection loss occurs
|
||||
clearOpenedChats (state) {
|
||||
clearOpenedChats(state) {
|
||||
const currentChatId = state.currentChatId
|
||||
for (const chatId in state.openedChats) {
|
||||
if (currentChatId !== chatId) {
|
||||
|
|
@ -221,21 +244,21 @@ const chats = {
|
|||
}
|
||||
}
|
||||
},
|
||||
readChat (state, { id, lastReadId }) {
|
||||
readChat(state, { id, lastReadId }) {
|
||||
state.lastReadMessageId = lastReadId
|
||||
const chat = getChatById(state, id)
|
||||
if (chat) {
|
||||
chat.unread = 0
|
||||
}
|
||||
},
|
||||
handleMessageError (state, { chatId, fakeId, isRetry }) {
|
||||
handleMessageError(state, { chatId, fakeId, isRetry }) {
|
||||
const chatMessageService = state.openedChatMessageServices[chatId]
|
||||
chatService.handleMessageError(chatMessageService, fakeId, isRetry)
|
||||
},
|
||||
cullOlderMessages (state, chatId) {
|
||||
cullOlderMessages(state, chatId) {
|
||||
chatService.cullOlderMessages(state.openedChatMessageServices[chatId])
|
||||
}
|
||||
}
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
export default chats
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue