Use streaming for real time updates instead of notifications, disable
polling when streaming is enabled.
This commit is contained in:
parent
6c2c0883ce
commit
f2b3d1e6b0
4 changed files with 30 additions and 11 deletions
|
@ -85,7 +85,13 @@ const Chat = {
|
|||
newMessageCount () {
|
||||
return this.currentChatMessageService && this.currentChatMessageService.newMessageCount
|
||||
},
|
||||
...mapGetters(['currentChat', 'currentChatMessageService', 'findUser', 'findOpenedChatByRecipientId']),
|
||||
...mapGetters([
|
||||
'currentChat',
|
||||
'currentChatMessageService',
|
||||
'findUser',
|
||||
'findOpenedChatByRecipientId',
|
||||
'mergedConfig'
|
||||
]),
|
||||
...mapState({
|
||||
backendInteractor: state => state.api.backendInteractor,
|
||||
currentUser: state => state.users.currentUser,
|
||||
|
@ -253,7 +259,15 @@ const Chat = {
|
|||
this.$router.push({ name: 'chats', params: { username: this.currentUser.screen_name } })
|
||||
},
|
||||
fetchChat (isFirstFetch, chatId, opts = {}) {
|
||||
const chatMessageService = this.openedChatMessageServices[chatId]
|
||||
let maxId = opts.maxId
|
||||
let sinceId
|
||||
if (opts.sinceId && this.mergedConfig.useStreamingApi) {
|
||||
return
|
||||
}
|
||||
if (opts.sinceId) {
|
||||
sinceId = chatMessageService.lastMessage && chatMessageService.lastMessage.id
|
||||
}
|
||||
if (isFirstFetch) {
|
||||
this.scrollDown({ forceRead: true })
|
||||
}
|
||||
|
@ -264,8 +278,6 @@ const Chat = {
|
|||
positionBeforeLoading = this.getPosition()
|
||||
previousScrollTop = this.$refs.scrollable.scrollTop
|
||||
}
|
||||
const chatMessageService = this.openedChatMessageServices[chatId]
|
||||
const sinceId = chatMessageService.lastMessage && chatMessageService.lastMessage.id
|
||||
|
||||
this.backendInteractor.chatMessages({ id: chatId, maxId, sinceId })
|
||||
.then((messages) => {
|
||||
|
@ -315,7 +327,7 @@ const Chat = {
|
|||
doStartFetching () {
|
||||
let chatId = this.currentChat.id
|
||||
this.$store.dispatch('startFetchingCurrentChat', {
|
||||
fetcher: () => setInterval(() => this.fetchChat(false, chatId), 5000)
|
||||
fetcher: () => setInterval(() => this.fetchChat(false, chatId, { sinceId: true }), 5000)
|
||||
})
|
||||
this.fetchChat(true, chatId)
|
||||
},
|
||||
|
|
|
@ -66,6 +66,13 @@ const api = {
|
|||
showImmediately: timelineData.visibleStatuses.length === 0,
|
||||
timeline: 'friends'
|
||||
})
|
||||
} else if (message.event === 'pleroma:chat_update') {
|
||||
dispatch('addChatMessages', {
|
||||
chatId: message.chatUpdate.id,
|
||||
messages: [message.chatUpdate.lastMessage]
|
||||
})
|
||||
dispatch('updateChatByAccountId', { accountId: message.chatUpdate.account.id })
|
||||
// dispatch('updateUnreadChatCount', { userId, unreadChatCount })
|
||||
}
|
||||
}
|
||||
)
|
||||
|
|
|
@ -332,12 +332,6 @@ const addNewNotifications = (state, { dispatch, notifications, older, visibleNot
|
|||
dispatch('fetchEmojiReactionsBy', notification.status.id)
|
||||
}
|
||||
|
||||
// if (notification.type === 'pleroma:chat_mention') {
|
||||
// dispatch('addChatMessages', { chatId: notification.chatMessage.chat_id, messages: [notification.chatMessage] })
|
||||
// dispatch('updateChatByAccountId', { accountId: notification.from_profile.id })
|
||||
// // dispatch('updateUnreadChatCount', { userId, unreadChatCount })
|
||||
// }
|
||||
|
||||
// Only add a new notification if we don't have one for the same action
|
||||
if (!state.notifications.idStore.hasOwnProperty(notification.id)) {
|
||||
state.notifications.maxId = notification.id > state.notifications.maxId
|
||||
|
|
|
@ -1051,6 +1051,10 @@ const MASTODON_STREAMING_EVENTS = new Set([
|
|||
'filters_changed'
|
||||
])
|
||||
|
||||
const PLEROMA_STREAMING_EVENTS = new Set([
|
||||
'pleroma:chat_update'
|
||||
])
|
||||
|
||||
// A thin wrapper around WebSocket API that allows adding a pre-processor to it
|
||||
// Uses EventTarget and a CustomEvent to proxy events
|
||||
export const ProcessedWS = ({
|
||||
|
@ -1107,7 +1111,7 @@ export const handleMastoWS = (wsEvent) => {
|
|||
if (!data) return
|
||||
const parsedEvent = JSON.parse(data)
|
||||
const { event, payload } = parsedEvent
|
||||
if (MASTODON_STREAMING_EVENTS.has(event)) {
|
||||
if (MASTODON_STREAMING_EVENTS.has(event) || PLEROMA_STREAMING_EVENTS.has(event)) {
|
||||
// MastoBE and PleromaBE both send payload for delete as a PLAIN string
|
||||
if (event === 'delete') {
|
||||
return { event, id: payload }
|
||||
|
@ -1117,6 +1121,8 @@ export const handleMastoWS = (wsEvent) => {
|
|||
return { event, status: parseStatus(data) }
|
||||
} else if (event === 'notification') {
|
||||
return { event, notification: parseNotification(data) }
|
||||
} else if (event === 'pleroma:chat_update') {
|
||||
return { event, chatUpdate: parseChat(data) }
|
||||
}
|
||||
} else {
|
||||
console.warn('Unknown event', wsEvent)
|
||||
|
|
Loading…
Reference in a new issue