chat updates

This commit is contained in:
Henry Jameson 2026-08-26 15:02:11 +03:00
commit bb09d6a9d1
2 changed files with 26 additions and 52 deletions

View file

@ -4,6 +4,7 @@ import { useUsersStore } from 'src/stores/users.js'
export const maybeShowChatNotification = (chat) => { export const maybeShowChatNotification = (chat) => {
if (!chat.lastMessage) return if (!chat.lastMessage) return
if (chat.unread === 0) return
if (useUsersStore().currentUser.id === chat.lastMessage.account_id) return if (useUsersStore().currentUser.id === chat.lastMessage.account_id) return
const opts = { const opts = {

View file

@ -1,4 +1,4 @@
import { find, omitBy, orderBy, sumBy } from 'lodash' import { orderBy, sumBy } from 'lodash'
import { defineStore } from 'pinia' import { defineStore } from 'pinia'
import { maybeShowChatNotification } from '../services/chat_utils/chat_utils.js' import { maybeShowChatNotification } from '../services/chat_utils/chat_utils.js'
@ -10,28 +10,19 @@ import { useUsersStore } from 'src/stores/users.js'
import { chats } from 'src/api/chats.js' import { chats } from 'src/api/chats.js'
const emptyChatList = () => ({
data: [],
idStore: {},
})
const defaultState = { const defaultState = {
chatList: emptyChatList(), data: new Map(),
chatListFetcher: null, fetcher: null,
}
const getChatById = (state, id) => {
return find(state.chatList.data, { id })
} }
export const useChatsStore = defineStore('chats', { export const useChatsStore = defineStore('chats', {
state: () => ({ ...defaultState }), state: () => ({ ...defaultState }),
getters: { getters: {
sortedChatList(state) { sortedChatList(state) {
return orderBy(state.chatList.data, ['updated_at'], ['desc']) return orderBy([...state.data.values()], ['updated_at'], ['desc'])
}, },
unreadChatsCount(state) { unreadChatsCount(state) {
return sumBy(state.chatList.data, 'unread') return sumBy([...state.data.values()], 'unread')
}, },
}, },
actions: { actions: {
@ -42,16 +33,19 @@ export const useChatsStore = defineStore('chats', {
et, et,
} }
et.addEventListener('pleroma:chat_update', this.updateChat) et.addEventListener('pleroma:chat_update', ({ data: { chatUpdate } }) => {
this.updateChat(chatUpdate)
})
useStreamingStore().addSubscriber(socket) useStreamingStore().addSubscriber(socket)
}, },
startFetching() { startFetching() {
const fetcher = () => this.fetchChats() this.fetcher = () => promiseInterval(() => this.fetchChats(), 5000)
this.setChatListFetcher(() => promiseInterval(fetcher, 5000)) this.fetcher()
}, },
stopFetching() { stopFetching() {
this.setChatListFetcher(null) this.fetcher?.stop()
this.fetcher = null
}, },
async fetchChats() { async fetchChats() {
this.addNewChats( this.addNewChats(
@ -60,16 +54,10 @@ export const useChatsStore = defineStore('chats', {
}), }),
) )
}, },
setChatListFetcher(fetcher) {
const prevFetcher = this.chatListFetcher
if (prevFetcher) {
prevFetcher.stop()
}
this.chatListFetcher = fetcher?.()
},
resetChats() { resetChats() {
this.chatList = emptyChatList() this.data = new Map()
this.setChatListFetcher(null) this.stopFetching()
this.startFetching()
}, },
addNewChats(result) { addNewChats(result) {
useUsersStore().addNewUsers({ useUsersStore().addNewUsers({
@ -77,45 +65,30 @@ export const useChatsStore = defineStore('chats', {
data: result.data.map((k) => k.account).filter(Boolean), data: result.data.map((k) => k.account).filter(Boolean),
}) })
result.data.forEach((updatedChat) => { // We do unshift in update so we reverse the chat list here
const chat = getChatById(this, updatedChat.id) result.data.forEach(chat => this.updateChat(chat))
if (chat) {
chat.lastMessage = updatedChat.lastMessage
chat.unread = updatedChat.unread
chat.updated_at = updatedChat.updated_at
} else {
this.chatList.data.push(updatedChat)
this.chatList.idStore[updatedChat.id] = updatedChat
}
})
}, },
readChat(id) { readChat(id) {
const chat = getChatById(this, id) const chat = this.data.get(id)
if (chat) { if (chat) {
chat.unread = 0 chat.unread = 0
} else {
console.error(`Chat ${id} not found!`)
} }
}, },
updateChat({ data: { chatUpdate: updatedChat } }) { updateChat(updatedChat) {
const chat = getChatById(this, updatedChat.id) const chat = this.data.get(updatedChat.id)
if (chat) { if (chat) {
chat.lastMessage = updatedChat.lastMessage chat.lastMessage = updatedChat.lastMessage
chat.unread = updatedChat.unread chat.unread = updatedChat.unread
chat.updated_at = updatedChat.updated_at chat.updated_at = updatedChat.updated_at
} else { } else {
this.chatList.data.unshift(updatedChat) this.data.set(updatedChat.id, updatedChat)
} }
maybeShowChatNotification(chat) maybeShowChatNotification(chat ?? updatedChat)
this.chatList.idStore[updatedChat.id] = updatedChat
}, },
deleteChat(id) { deleteChat(id) {
this.chats.data = this.chats.data.filter( this.data.delete(id)
(conversation) => conversation.last_status.id !== id,
)
this.chats.idStore = omitBy(
this.chats.idStore,
(conversation) => conversation.last_status.id === id,
)
}, },
}, },
}) })