Use streaming for real time updates instead of notifications, disable

polling when streaming is enabled.
This commit is contained in:
eugenijm 2020-05-30 14:54:16 +03:00
parent 6c2c0883ce
commit f2b3d1e6b0
4 changed files with 30 additions and 11 deletions

View file

@ -85,7 +85,13 @@ const Chat = {
newMessageCount () { newMessageCount () {
return this.currentChatMessageService && this.currentChatMessageService.newMessageCount return this.currentChatMessageService && this.currentChatMessageService.newMessageCount
}, },
...mapGetters(['currentChat', 'currentChatMessageService', 'findUser', 'findOpenedChatByRecipientId']), ...mapGetters([
'currentChat',
'currentChatMessageService',
'findUser',
'findOpenedChatByRecipientId',
'mergedConfig'
]),
...mapState({ ...mapState({
backendInteractor: state => state.api.backendInteractor, backendInteractor: state => state.api.backendInteractor,
currentUser: state => state.users.currentUser, currentUser: state => state.users.currentUser,
@ -253,7 +259,15 @@ const Chat = {
this.$router.push({ name: 'chats', params: { username: this.currentUser.screen_name } }) this.$router.push({ name: 'chats', params: { username: this.currentUser.screen_name } })
}, },
fetchChat (isFirstFetch, chatId, opts = {}) { fetchChat (isFirstFetch, chatId, opts = {}) {
const chatMessageService = this.openedChatMessageServices[chatId]
let maxId = opts.maxId let maxId = opts.maxId
let sinceId
if (opts.sinceId && this.mergedConfig.useStreamingApi) {
return
}
if (opts.sinceId) {
sinceId = chatMessageService.lastMessage && chatMessageService.lastMessage.id
}
if (isFirstFetch) { if (isFirstFetch) {
this.scrollDown({ forceRead: true }) this.scrollDown({ forceRead: true })
} }
@ -264,8 +278,6 @@ const Chat = {
positionBeforeLoading = this.getPosition() positionBeforeLoading = this.getPosition()
previousScrollTop = this.$refs.scrollable.scrollTop previousScrollTop = this.$refs.scrollable.scrollTop
} }
const chatMessageService = this.openedChatMessageServices[chatId]
const sinceId = chatMessageService.lastMessage && chatMessageService.lastMessage.id
this.backendInteractor.chatMessages({ id: chatId, maxId, sinceId }) this.backendInteractor.chatMessages({ id: chatId, maxId, sinceId })
.then((messages) => { .then((messages) => {
@ -315,7 +327,7 @@ const Chat = {
doStartFetching () { doStartFetching () {
let chatId = this.currentChat.id let chatId = this.currentChat.id
this.$store.dispatch('startFetchingCurrentChat', { this.$store.dispatch('startFetchingCurrentChat', {
fetcher: () => setInterval(() => this.fetchChat(false, chatId), 5000) fetcher: () => setInterval(() => this.fetchChat(false, chatId, { sinceId: true }), 5000)
}) })
this.fetchChat(true, chatId) this.fetchChat(true, chatId)
}, },

View file

@ -66,6 +66,13 @@ const api = {
showImmediately: timelineData.visibleStatuses.length === 0, showImmediately: timelineData.visibleStatuses.length === 0,
timeline: 'friends' 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 })
} }
} }
) )

View file

@ -332,12 +332,6 @@ const addNewNotifications = (state, { dispatch, notifications, older, visibleNot
dispatch('fetchEmojiReactionsBy', notification.status.id) 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 // Only add a new notification if we don't have one for the same action
if (!state.notifications.idStore.hasOwnProperty(notification.id)) { if (!state.notifications.idStore.hasOwnProperty(notification.id)) {
state.notifications.maxId = notification.id > state.notifications.maxId state.notifications.maxId = notification.id > state.notifications.maxId

View file

@ -1051,6 +1051,10 @@ const MASTODON_STREAMING_EVENTS = new Set([
'filters_changed' '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 // A thin wrapper around WebSocket API that allows adding a pre-processor to it
// Uses EventTarget and a CustomEvent to proxy events // Uses EventTarget and a CustomEvent to proxy events
export const ProcessedWS = ({ export const ProcessedWS = ({
@ -1107,7 +1111,7 @@ export const handleMastoWS = (wsEvent) => {
if (!data) return if (!data) return
const parsedEvent = JSON.parse(data) const parsedEvent = JSON.parse(data)
const { event, payload } = parsedEvent 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 // MastoBE and PleromaBE both send payload for delete as a PLAIN string
if (event === 'delete') { if (event === 'delete') {
return { event, id: payload } return { event, id: payload }
@ -1117,6 +1121,8 @@ export const handleMastoWS = (wsEvent) => {
return { event, status: parseStatus(data) } return { event, status: parseStatus(data) }
} else if (event === 'notification') { } else if (event === 'notification') {
return { event, notification: parseNotification(data) } return { event, notification: parseNotification(data) }
} else if (event === 'pleroma:chat_update') {
return { event, chatUpdate: parseChat(data) }
} }
} else { } else {
console.warn('Unknown event', wsEvent) console.warn('Unknown event', wsEvent)