From f2b3d1e6b0e094a458845e9b46a9365dc14429f8 Mon Sep 17 00:00:00 2001 From: eugenijm Date: Sat, 30 May 2020 14:54:16 +0300 Subject: [PATCH] Use streaming for real time updates instead of notifications, disable polling when streaming is enabled. --- src/components/chat/chat.js | 20 ++++++++++++++++---- src/modules/api.js | 7 +++++++ src/modules/statuses.js | 6 ------ src/services/api/api.service.js | 8 +++++++- 4 files changed, 30 insertions(+), 11 deletions(-) diff --git a/src/components/chat/chat.js b/src/components/chat/chat.js index 2ca7eaee..9093c79e 100644 --- a/src/components/chat/chat.js +++ b/src/components/chat/chat.js @@ -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) }, diff --git a/src/modules/api.js b/src/modules/api.js index 748570e5..e0afc497 100644 --- a/src/modules/api.js +++ b/src/modules/api.js @@ -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 }) } } ) diff --git a/src/modules/statuses.js b/src/modules/statuses.js index 6ed71c24..8c110dce 100644 --- a/src/modules/statuses.js +++ b/src/modules/statuses.js @@ -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 diff --git a/src/services/api/api.service.js b/src/services/api/api.service.js index 74645afb..67d46151 100644 --- a/src/services/api/api.service.js +++ b/src/services/api/api.service.js @@ -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)