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