-
Notifications
You must be signed in to change notification settings - Fork 402
/
Copy pathsubscribe.js
115 lines (97 loc) · 2.58 KB
/
subscribe.js
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/* @flow */
import {
SubscribeArguments,
PublishMetaData,
SubscribeMetadata,
SubscribeMessage,
SubscribeEnvelope,
ModulesInject,
} from '../flow_interfaces';
import operationConstants from '../constants/operations';
import utils from '../utils';
export function getOperation(): string {
return operationConstants.PNSubscribeOperation;
}
export function validateParams(modules: ModulesInject) {
let { config } = modules;
if (!config.subscribeKey) return 'Missing Subscribe Key';
}
export function getURL(
modules: ModulesInject,
incomingParams: SubscribeArguments
): string {
let { config } = modules;
let { channels = [] } = incomingParams;
let stringifiedChannels = channels.length > 0 ? channels.join(',') : ',';
return `/v2/subscribe/${config.subscribeKey}/${utils.encodeString(
stringifiedChannels
)}/0`;
}
export function getRequestTimeout({ config }: ModulesInject) {
return config.getSubscribeTimeout();
}
export function isAuthSupported() {
return true;
}
export function prepareParams(
{ config }: ModulesInject,
incomingParams: SubscribeArguments
): Object {
let {
state,
channelGroups = [],
timetoken,
filterExpression,
region,
} = incomingParams;
const params: Object = {
heartbeat: config.getPresenceTimeout(),
};
if (channelGroups.length > 0) {
params['channel-group'] = channelGroups.join(',');
}
if (filterExpression && filterExpression.length > 0) {
params['filter-expr'] = filterExpression;
}
if (Object.keys(state).length) {
params.state = JSON.stringify(state);
}
if (timetoken) {
params.tt = timetoken;
}
if (region) {
params.tr = region;
}
return params;
}
export function handleResponse(
modules: ModulesInject,
serverResponse: Object
): SubscribeEnvelope {
const messages: Array<SubscribeMessage> = [];
serverResponse.m.forEach((rawMessage) => {
let publishMetaData: PublishMetaData = {
publishTimetoken: rawMessage.p.t,
region: rawMessage.p.r,
};
let parsedMessage: SubscribeMessage = {
shard: parseInt(rawMessage.a, 10),
subscriptionMatch: rawMessage.b,
channel: rawMessage.c,
messageType: rawMessage.e,
payload: rawMessage.d,
flags: rawMessage.f,
issuingClientId: rawMessage.i,
subscribeKey: rawMessage.k,
originationTimetoken: rawMessage.o,
userMetadata: rawMessage.u,
publishMetaData,
};
messages.push(parsedMessage);
});
const metadata: SubscribeMetadata = {
timetoken: serverResponse.t.t,
region: serverResponse.t.r,
};
return { messages, metadata };
}