semaphore/src/routes/_actions/stream/processMessage.js

46 lines
1.7 KiB
JavaScript
Raw Normal View History

import { mark, stop } from '../../_utils/marks.js'
import { deleteStatus } from '../deleteStatuses.js'
import { addStatusOrNotification } from '../addStatusOrNotification.js'
import { emit } from '../../_utils/eventBus.js'
2018-02-15 17:02:46 +00:00
const KNOWN_EVENTS = ['update', 'delete', 'notification', 'conversation', 'filters_changed']
2019-07-21 23:31:26 +01:00
export function processMessage (instanceName, timelineName, message) {
let { event, payload } = (message || {})
2019-07-21 23:31:26 +01:00
if (!KNOWN_EVENTS.includes(event)) {
console.warn('ignoring message from server', message)
2019-07-21 23:31:26 +01:00
return
}
mark('processMessage')
if (['update', 'notification', 'conversation'].includes(event)) {
payload = JSON.parse(payload) // only these payloads are JSON-encoded for some reason
}
2018-02-15 17:02:46 +00:00
switch (event) {
case 'delete':
2018-03-11 00:21:10 +00:00
deleteStatus(instanceName, payload)
2018-02-15 17:02:46 +00:00
break
case 'update':
addStatusOrNotification(instanceName, timelineName, payload)
2018-02-15 17:02:46 +00:00
break
case 'notification':
addStatusOrNotification(instanceName, 'notifications', payload)
if (payload.type === 'mention') {
addStatusOrNotification(instanceName, 'notifications/mentions', payload)
}
2018-02-15 17:02:46 +00:00
break
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 instead of updating existing threads
addStatusOrNotification(instanceName, timelineName, payload.last_status)
break
case 'filters_changed':
emit('wordFiltersChanged', instanceName)
break
2018-02-15 17:02:46 +00:00
}
stop('processMessage')
2018-02-11 21:46:57 +00:00
}