2019-07-21 15:31:26 -07:00
|
|
|
import { mark, stop } from '../../_utils/marks'
|
|
|
|
import { deleteStatus } from '../deleteStatuses'
|
|
|
|
import { addStatusOrNotification } from '../addStatusOrNotification'
|
2018-02-15 09:02:46 -08:00
|
|
|
|
2019-07-21 15:31:26 -07:00
|
|
|
const KNOWN_EVENTS = ['update', 'delete', 'notification', 'conversation']
|
|
|
|
|
|
|
|
export function processMessage (instanceName, timelineName, message) {
|
2019-12-15 09:58:00 -08:00
|
|
|
let { event, payload } = (message || {})
|
2019-07-21 15:31:26 -07:00
|
|
|
if (!KNOWN_EVENTS.includes(event)) {
|
2019-12-15 09:58:00 -08:00
|
|
|
console.warn('ignoring message from server', message)
|
2019-07-21 15:31:26 -07:00
|
|
|
return
|
|
|
|
}
|
|
|
|
mark('processMessage')
|
2019-05-04 17:58:44 -07:00
|
|
|
if (['update', 'notification', 'conversation'].includes(event)) {
|
|
|
|
payload = JSON.parse(payload) // only these payloads are JSON-encoded for some reason
|
|
|
|
}
|
|
|
|
|
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':
|
2019-05-04 17:58:44 -07:00
|
|
|
addStatusOrNotification(instanceName, timelineName, payload)
|
2018-02-15 09:02:46 -08:00
|
|
|
break
|
|
|
|
case 'notification':
|
2019-05-04 17:58:44 -07:00
|
|
|
addStatusOrNotification(instanceName, 'notifications', payload)
|
|
|
|
if (payload.type === 'mention') {
|
|
|
|
addStatusOrNotification(instanceName, 'notifications/mentions', 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-05-04 17:58:44 -07:00
|
|
|
addStatusOrNotification(instanceName, timelineName, payload.last_status)
|
2019-04-02 12:05:27 +02:00
|
|
|
break
|
2018-02-15 09:02:46 -08:00
|
|
|
}
|
|
|
|
stop('processMessage')
|
2018-02-11 13:46:57 -08:00
|
|
|
}
|