Page MenuHomePhorge

No OneTemporary

Size
2 KB
Referenced Files
None
Subscribers
None
diff --git a/src/stores/chats.js b/src/stores/chats.js
index 6fcfbcd1e8..f2b27306d0 100644
--- a/src/stores/chats.js
+++ b/src/stores/chats.js
@@ -1,94 +1,94 @@
import { orderBy, sumBy } from 'lodash'
import { defineStore } from 'pinia'
import { maybeShowChatNotification } from '../services/chat_utils/chat_utils.js'
import { promiseInterval } from '../services/promise_interval/promise_interval.js'
import { useOAuthStore } from 'src/stores/oauth.js'
import { useStreamingStore } from 'src/stores/streaming.js'
import { useUsersStore } from 'src/stores/users.js'
import { chats } from 'src/api/chats.js'
const defaultState = {
data: new Map(),
fetcher: null,
}
export const useChatsStore = defineStore('chats', {
state: () => ({ ...defaultState }),
getters: {
sortedChatList(state) {
return orderBy([...state.data.values()], ['updated_at'], ['desc'])
},
unreadChatsCount(state) {
return sumBy([...state.data.values()], 'unread')
},
},
actions: {
attachSocket() {
const et = new EventTarget()
const socket = {
name: 'chats',
et,
}
et.addEventListener('pleroma:chat_update', ({ data: { chatUpdate } }) => {
this.updateChat(chatUpdate)
})
useStreamingStore().addSubscriber(socket)
},
startFetching() {
this.fetcher = promiseInterval(() => this.fetchChats(), 5000)
this.fetchChats()
},
stopFetching() {
this.fetcher?.stop()
this.fetcher = null
},
async fetchChats() {
this.addNewChats(
await chats({
credentials: useOAuthStore().token,
}),
)
},
resetChats() {
this.data = new Map()
},
addNewChats(result) {
useUsersStore().addNewUsers({
...result,
data: result.data.map((k) => k.account).filter(Boolean),
})
// We do unshift in update so we reverse the chat list here
result.data.forEach((chat) => this.updateChat(chat))
},
readChat(id) {
const chat = this.data.get(id)
if (chat) {
chat.unread = 0
} else {
console.error(`Chat ${id} not found!`)
}
},
updateChat(updatedChat) {
const chat = this.data.get(updatedChat.id)
if (chat) {
- const isNewMessage = chat.lastMessage !== updatedChat.lastMessage
+ const isNewMessage = chat.lastMessage?.id !== updatedChat.lastMessage?.id
chat.lastMessage = updatedChat.lastMessage
chat.unread = updatedChat.unread
chat.updated_at = updatedChat.updated_at
if (isNewMessage) maybeShowChatNotification(chat)
} else {
this.data.set(updatedChat.id, updatedChat)
maybeShowChatNotification(updatedChat)
}
},
deleteChat(id) {
this.data.delete(id)
},
},
})

File Metadata

Mime Type
text/x-diff
Expires
Tue, Oct 13, 9:23 AM (1 d, 20 h)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
1786697
Default Alt Text
(2 KB)

Event Timeline