2021-07-05 03:19:04 +00:00
|
|
|
import { mark, stop } from '../../_utils/marks.js'
|
|
|
|
import { deleteStatus } from '../deleteStatuses.js'
|
|
|
|
import { addStatusOrNotification } from '../addStatusOrNotification.js'
|
|
|
|
import { emit } from '../../_utils/eventBus.js'
|
2022-12-17 21:22:34 +00:00
|
|
|
import { updateStatus } from '../updateStatus.js'
|
2018-02-15 17:02:46 +00:00
|
|
|
|
2022-12-17 21:22:34 +00:00
|
|
|
const KNOWN_EVENTS = ['update', 'delete', 'notification', 'conversation', 'filters_changed', 'status.update']
|
2019-07-21 22:31:26 +00:00
|
|
|
|
|
|
|
export function processMessage (instanceName, timelineName, message) {
|
2019-12-15 17:58:00 +00:00
|
|
|
let { event, payload } = (message || {})
|
2019-07-21 22:31:26 +00:00
|
|
|
if (!KNOWN_EVENTS.includes(event)) {
|
2019-12-15 17:58:00 +00:00
|
|
|
console.warn('ignoring message from server', message)
|
2019-07-21 22:31:26 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
mark('processMessage')
|
2022-12-17 21:22:34 +00:00
|
|
|
if (['update', 'notification', 'conversation', 'status.update'].includes(event)) {
|
2019-05-05 00:58:44 +00:00
|
|
|
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':
|
2019-05-05 00:58:44 +00:00
|
|
|
addStatusOrNotification(instanceName, timelineName, payload)
|
2018-02-15 17:02:46 +00:00
|
|
|
break
|
|
|
|
case 'notification':
|
2019-05-05 00:58:44 +00:00
|
|
|
addStatusOrNotification(instanceName, 'notifications', payload)
|
|
|
|
if (payload.type === 'mention') {
|
|
|
|
addStatusOrNotification(instanceName, 'notifications/mentions', payload)
|
|
|
|
}
|
2018-02-15 17:02:46 +00:00
|
|
|
break
|
2019-04-02 10:05:27 +00: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 18:04:39 +00:00
|
|
|
// It will add new DMs as new conversations instead of updating existing threads
|
2021-12-28 04:57:16 +00:00
|
|
|
if (payload.last_status) {
|
|
|
|
// If the last_status doesn't exist, just ignore it. There's not much we can do in that case
|
|
|
|
addStatusOrNotification(instanceName, timelineName, payload.last_status)
|
|
|
|
}
|
2019-04-02 10:05:27 +00:00
|
|
|
break
|
2021-03-14 01:31:17 +00:00
|
|
|
case 'filters_changed':
|
|
|
|
emit('wordFiltersChanged', instanceName)
|
|
|
|
break
|
2022-12-17 21:22:34 +00:00
|
|
|
case 'status.update':
|
|
|
|
updateStatus(instanceName, payload)
|
|
|
|
break
|
2018-02-15 17:02:46 +00:00
|
|
|
}
|
|
|
|
stop('processMessage')
|
2018-02-11 21:46:57 +00:00
|
|
|
}
|