-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmessengerActions.ts
45 lines (38 loc) · 1.22 KB
/
messengerActions.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import { Buffer } from 'buffer'
import beapi from '@berty/api'
import { messengerActions } from './messengerActions.gen'
export type MessengerAction = ReturnType<typeof messengerActions[keyof typeof messengerActions]>
export const streamEventToAction: (
evt: beapi.messenger.IStreamEvent,
) => MessengerAction | undefined = evt => {
if (!evt || evt.type === null || evt.type === undefined) {
console.warn('received empty or unknown event', evt)
return
}
const enumName = beapi.messenger.StreamEvent.Type[evt.type]
if (!enumName) {
console.warn('failed to get event type name', evt.type)
return
}
const payloadName = enumName.substring('Type'.length)
const pbobj = (beapi.messenger.StreamEvent as any)[payloadName]
if (!pbobj) {
console.warn('failed to find a protobuf object matching the event type', payloadName)
return
}
let pl = evt.payload
if (typeof pl === 'string') {
pl = Buffer.from(pl, 'base64')
}
const eventPayload = pl ? pbobj.decode(pl).toJSON() : {}
if (!eventPayload) {
console.warn('failed to decode payload', payloadName, pl)
return
}
console.log('eventPayload', eventPayload)
return {
type: `messenger/${payloadName}` as any,
payload: eventPayload,
}
}
export { messengerActions }