2018-02-11 22:46:57 +01:00
|
|
|
import { TimelineStream } from '../_api/TimelineStream'
|
2018-02-12 07:40:56 +01:00
|
|
|
import { mark, stop } from '../_utils/marks'
|
2018-02-17 04:38:21 +01:00
|
|
|
import { deleteStatus } from './deleteStatuses'
|
|
|
|
import { addStatusOrNotification } from './addStatusOrNotification'
|
2018-02-15 18:02:46 +01:00
|
|
|
|
|
|
|
function processMessage (instanceName, timelineName, message) {
|
|
|
|
mark('processMessage')
|
2018-02-11 22:46:57 +01:00
|
|
|
let { event, payload } = message
|
2018-02-15 18:02:46 +01:00
|
|
|
switch (event) {
|
|
|
|
case 'delete':
|
2018-03-11 01:21:10 +01:00
|
|
|
deleteStatus(instanceName, payload)
|
2018-02-15 18:02:46 +01:00
|
|
|
break
|
|
|
|
case 'update':
|
2018-03-11 01:21:10 +01:00
|
|
|
addStatusOrNotification(instanceName, timelineName, JSON.parse(payload))
|
2018-02-15 18:02:46 +01:00
|
|
|
break
|
|
|
|
case 'notification':
|
2018-03-11 01:21:10 +01:00
|
|
|
addStatusOrNotification(instanceName, 'notifications', JSON.parse(payload))
|
2018-02-15 18:02:46 +01:00
|
|
|
break
|
2019-04-02 12:05:27 +02:00
|
|
|
case 'conversation':
|
|
|
|
// This is a hack in order to mostly fit the conversation model into
|
|
|
|
// a timeline of statuses. To have a clean implementation we would need to
|
|
|
|
// reproduce what is done for statuses for the conversation.
|
|
|
|
//
|
|
|
|
// It will add new DMs as new conversations intead of updating existing threads
|
|
|
|
addStatusOrNotification(instanceName, timelineName, JSON.parse(payload).last_status)
|
|
|
|
break
|
2018-02-15 18:02:46 +01:00
|
|
|
}
|
|
|
|
stop('processMessage')
|
2018-02-11 22:46:57 +01:00
|
|
|
}
|
|
|
|
|
2018-03-20 02:00:49 +01:00
|
|
|
export function createStream (streamingApi, instanceName, accessToken,
|
2018-04-19 05:43:13 +02:00
|
|
|
timelineName, onOpenStream) {
|
2018-02-11 23:11:03 +01:00
|
|
|
return new TimelineStream(streamingApi, accessToken, timelineName, {
|
|
|
|
onMessage (msg) {
|
2019-04-02 12:05:27 +02:00
|
|
|
if (
|
|
|
|
msg.event !== 'update' &&
|
|
|
|
msg.event !== 'delete' &&
|
|
|
|
msg.event !== 'notification' &&
|
|
|
|
msg.event !== 'conversation'
|
|
|
|
) {
|
2018-02-11 22:46:57 +01:00
|
|
|
console.error("don't know how to handle event", msg)
|
|
|
|
return
|
|
|
|
}
|
2018-04-03 03:00:45 +02:00
|
|
|
processMessage(instanceName, timelineName, msg)
|
2018-02-11 22:46:57 +01:00
|
|
|
},
|
2018-02-11 23:11:03 +01:00
|
|
|
onOpen () {
|
2018-03-20 02:00:49 +01:00
|
|
|
if (onOpenStream) {
|
|
|
|
onOpenStream()
|
|
|
|
}
|
2018-02-11 22:46:57 +01:00
|
|
|
console.log('opened stream for timeline', timelineName)
|
|
|
|
},
|
2018-02-11 23:11:03 +01:00
|
|
|
onClose () {
|
2018-02-11 22:46:57 +01:00
|
|
|
console.log('closed stream for timeline', timelineName)
|
|
|
|
},
|
2018-02-11 23:11:03 +01:00
|
|
|
onReconnect () {
|
2018-02-11 22:46:57 +01:00
|
|
|
console.log('reconnected stream for timeline', timelineName)
|
|
|
|
}
|
|
|
|
})
|
2018-02-14 04:35:46 +01:00
|
|
|
}
|