2018-02-11 13:46:57 -08:00
|
|
|
import { TimelineStream } from '../_api/TimelineStream'
|
2018-02-11 22:40:56 -08:00
|
|
|
import { mark, stop } from '../_utils/marks'
|
2018-02-16 19:38:21 -08:00
|
|
|
import { deleteStatus } from './deleteStatuses'
|
|
|
|
import { addStatusOrNotification } from './addStatusOrNotification'
|
2018-02-15 09:02:46 -08:00
|
|
|
|
|
|
|
function processMessage (instanceName, timelineName, message) {
|
|
|
|
mark('processMessage')
|
2018-02-11 13:46:57 -08:00
|
|
|
let { event, payload } = message
|
2018-02-15 09:02:46 -08:00
|
|
|
switch (event) {
|
|
|
|
case 'delete':
|
2018-03-10 16:21:10 -08:00
|
|
|
deleteStatus(instanceName, payload)
|
2018-02-15 09:02:46 -08:00
|
|
|
break
|
|
|
|
case 'update':
|
2018-03-10 16:21:10 -08:00
|
|
|
addStatusOrNotification(instanceName, timelineName, JSON.parse(payload))
|
2018-02-15 09:02:46 -08:00
|
|
|
break
|
|
|
|
case 'notification':
|
2018-03-10 16:21:10 -08:00
|
|
|
addStatusOrNotification(instanceName, 'notifications', JSON.parse(payload))
|
2018-02-15 09:02:46 -08: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.
|
|
|
|
//
|
2019-04-13 11:04:39 -07:00
|
|
|
// It will add new DMs as new conversations instead of updating existing threads
|
2019-04-02 12:05:27 +02:00
|
|
|
addStatusOrNotification(instanceName, timelineName, JSON.parse(payload).last_status)
|
|
|
|
break
|
2018-02-15 09:02:46 -08:00
|
|
|
}
|
|
|
|
stop('processMessage')
|
2018-02-11 13:46:57 -08:00
|
|
|
}
|
|
|
|
|
2018-03-19 18:00:49 -07:00
|
|
|
export function createStream (streamingApi, instanceName, accessToken,
|
2018-04-18 20:43:13 -07:00
|
|
|
timelineName, onOpenStream) {
|
2018-02-11 14:11:03 -08: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 13:46:57 -08:00
|
|
|
console.error("don't know how to handle event", msg)
|
|
|
|
return
|
|
|
|
}
|
2018-04-02 18:00:45 -07:00
|
|
|
processMessage(instanceName, timelineName, msg)
|
2018-02-11 13:46:57 -08:00
|
|
|
},
|
2018-02-11 14:11:03 -08:00
|
|
|
onOpen () {
|
2018-03-19 18:00:49 -07:00
|
|
|
if (onOpenStream) {
|
|
|
|
onOpenStream()
|
|
|
|
}
|
2018-02-11 13:46:57 -08:00
|
|
|
console.log('opened stream for timeline', timelineName)
|
|
|
|
},
|
2018-02-11 14:11:03 -08:00
|
|
|
onClose () {
|
2018-02-11 13:46:57 -08:00
|
|
|
console.log('closed stream for timeline', timelineName)
|
|
|
|
},
|
2018-02-11 14:11:03 -08:00
|
|
|
onReconnect () {
|
2018-02-11 13:46:57 -08:00
|
|
|
console.log('reconnected stream for timeline', timelineName)
|
|
|
|
}
|
|
|
|
})
|
2018-02-13 19:35:46 -08:00
|
|
|
}
|