2019-07-22 00:31:26 +02:00
|
|
|
import { mark, stop } from '../../_utils/marks'
|
|
|
|
import { deleteStatus } from '../deleteStatuses'
|
|
|
|
import { addStatusOrNotification } from '../addStatusOrNotification'
|
2021-03-14 02:31:17 +01:00
|
|
|
import { emit } from '../../_utils/eventBus'
|
2018-02-15 18:02:46 +01:00
|
|
|
|
2021-03-14 02:31:17 +01:00
|
|
|
const KNOWN_EVENTS = ['update', 'delete', 'notification', 'conversation', 'filters_changed']
|
2019-07-22 00:31:26 +02:00
|
|
|
|
|
|
|
export function processMessage (instanceName, timelineName, message) {
|
2019-12-15 18:58:00 +01:00
|
|
|
let { event, payload } = (message || {})
|
2019-07-22 00:31:26 +02:00
|
|
|
if (!KNOWN_EVENTS.includes(event)) {
|
2019-12-15 18:58:00 +01:00
|
|
|
console.warn('ignoring message from server', message)
|
2019-07-22 00:31:26 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
mark('processMessage')
|
2019-05-05 02:58:44 +02:00
|
|
|
if (['update', 'notification', 'conversation'].includes(event)) {
|
|
|
|
payload = JSON.parse(payload) // only these payloads are JSON-encoded for some reason
|
|
|
|
}
|
|
|
|
|
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':
|
2019-05-05 02:58:44 +02:00
|
|
|
addStatusOrNotification(instanceName, timelineName, payload)
|
2018-02-15 18:02:46 +01:00
|
|
|
break
|
|
|
|
case 'notification':
|
2019-05-05 02:58:44 +02:00
|
|
|
addStatusOrNotification(instanceName, 'notifications', payload)
|
|
|
|
if (payload.type === 'mention') {
|
|
|
|
addStatusOrNotification(instanceName, 'notifications/mentions', 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.
|
|
|
|
//
|
2019-04-13 20:04:39 +02:00
|
|
|
// It will add new DMs as new conversations instead of updating existing threads
|
2019-05-05 02:58:44 +02:00
|
|
|
addStatusOrNotification(instanceName, timelineName, payload.last_status)
|
2019-04-02 12:05:27 +02:00
|
|
|
break
|
2021-03-14 02:31:17 +01:00
|
|
|
case 'filters_changed':
|
|
|
|
emit('wordFiltersChanged', instanceName)
|
|
|
|
break
|
2018-02-15 18:02:46 +01:00
|
|
|
}
|
|
|
|
stop('processMessage')
|
2018-02-11 22:46:57 +01:00
|
|
|
}
|