gab-social/app/javascript/gabsocial/actions/streaming.js

92 lines
2.3 KiB
JavaScript
Raw Normal View History

2020-11-15 18:48:32 +00:00
import { connectStream } from '../stream'
2019-07-02 08:10:25 +01:00
import {
deleteFromTimelines,
connectTimeline,
disconnectTimeline,
updateTimelineQueue,
2020-11-15 18:48:32 +00:00
} from './timelines'
import { updateNotificationsQueue } from './notifications'
2020-12-16 00:31:30 +00:00
import { manageIncomingChatMessage } from './chat_messages'
2020-11-15 18:48:32 +00:00
import { fetchFilters } from './filters'
import { getLocale } from '../locales'
import { handleComposeSubmit } from './compose'
2019-07-02 08:10:25 +01:00
2020-11-15 18:48:32 +00:00
const { messages } = getLocale()
2019-07-02 08:10:25 +01:00
2020-11-15 18:48:32 +00:00
/**
*
*/
export const connectTimelineStream = (timelineId, path, pollingRefresh = null, accept = null) => {
2019-07-02 08:10:25 +01:00
return connectStream (path, pollingRefresh, (dispatch, getState) => {
2020-11-15 18:48:32 +00:00
const locale = getState().getIn(['meta', 'locale'])
2019-07-02 08:10:25 +01:00
return {
onConnect() {
2020-11-15 18:48:32 +00:00
dispatch(connectTimeline(timelineId))
2019-07-02 08:10:25 +01:00
},
onDisconnect() {
2020-11-15 18:48:32 +00:00
dispatch(disconnectTimeline(timelineId))
2019-07-02 08:10:25 +01:00
},
onReceive (data) {
switch(data.event) {
case 'update':
2020-11-15 18:48:32 +00:00
dispatch(updateTimelineQueue(timelineId, JSON.parse(data.payload), accept))
break
2019-07-02 08:10:25 +01:00
case 'delete':
2020-11-15 18:48:32 +00:00
dispatch(deleteFromTimelines(data.payload))
break
2019-07-02 08:10:25 +01:00
case 'notification':
2020-11-15 18:48:32 +00:00
dispatch(updateNotificationsQueue(JSON.parse(data.payload), messages, locale, window.location.pathname))
break
2019-07-02 08:10:25 +01:00
case 'filters_changed':
2020-11-15 18:48:32 +00:00
dispatch(fetchFilters())
break
2019-07-02 08:10:25 +01:00
}
},
2020-11-15 18:48:32 +00:00
}
})
2019-07-02 08:10:25 +01:00
}
2020-11-15 18:48:32 +00:00
/**
*
*/
export const connectStatusUpdateStream = () => {
2020-11-15 18:48:32 +00:00
return connectStream('statuscard', null, (dispatch, getState) => {
2020-11-15 18:48:32 +00:00
return {
onConnect() {},
onDisconnect() {},
onReceive (data) {
2020-11-15 18:48:32 +00:00
if (!data['event'] || !data['payload']) return
if (data.event === 'update') {
handleComposeSubmit(dispatch, getState, {data: JSON.parse(data.payload)}, null)
}
},
2020-11-15 18:48:32 +00:00
}
})
}
/**
*
*/
export const connectUserStream = () => connectTimelineStream('home', 'user')
2020-11-25 21:22:37 +00:00
/**
*
*/
export const connectChatMessagesStream = (accountId) => {
2020-12-03 22:13:11 +00:00
return connectStream(`chat_messages`, null, (dispatch, getState) => {
2020-11-25 21:22:37 +00:00
return {
2020-12-03 22:13:11 +00:00
onConnect() {},
onDisconnect() {},
2020-11-25 21:22:37 +00:00
onReceive (data) {
2020-12-03 22:13:11 +00:00
if (!data['event'] || !data['payload']) return
if (data.event === 'notification') {
2020-12-16 00:31:30 +00:00
dispatch(manageIncomingChatMessage(JSON.parse(data.payload)))
2020-12-03 22:13:11 +00:00
}
2020-11-25 21:22:37 +00:00
},
}
})
}