-
-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Expand file tree
/
Copy pathClient.js
More file actions
154 lines (139 loc) · 4.28 KB
/
Client.js
File metadata and controls
154 lines (139 loc) · 4.28 KB
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
import logger from '../logger';
import type { FlattenedObjectData } from './Subscription';
export type Message = { [attr: string]: any };
const dafaultFields = ['className', 'objectId', 'updatedAt', 'createdAt', 'ACL'];
class Client {
id: number;
parseWebSocket: any;
hasMasterKey: boolean;
sessionToken: string;
installationId: string;
userId: string;
roles: Array<string>;
subscriptionInfos: Object;
pushConnect: Function;
pushSubscribe: Function;
pushUnsubscribe: Function;
pushCreate: Function;
pushEnter: Function;
pushUpdate: Function;
pushDelete: Function;
pushLeave: Function;
pushResult: Function;
constructor(
id: number,
parseWebSocket: any,
hasMasterKey: boolean = false,
sessionToken: string,
installationId: string
) {
this.id = id;
this.parseWebSocket = parseWebSocket;
this.hasMasterKey = hasMasterKey;
this.sessionToken = sessionToken;
this.installationId = installationId;
this.roles = [];
this.subscriptionInfos = new Map();
this.pushConnect = this._pushEvent('connected');
this.pushSubscribe = this._pushEvent('subscribed');
this.pushUnsubscribe = this._pushEvent('unsubscribed');
this.pushCreate = this._pushEvent('create');
this.pushEnter = this._pushEvent('enter');
this.pushUpdate = this._pushEvent('update');
this.pushDelete = this._pushEvent('delete');
this.pushLeave = this._pushEvent('leave');
this.pushResult = this._pushQueryResult.bind(this);
}
static pushResponse(parseWebSocket: any, message: Message): void {
logger.verbose('Push Response : %j', message);
parseWebSocket.send(message);
}
static pushError(
parseWebSocket: any,
code: number,
error: string,
reconnect: boolean = true,
requestId: number | void = null
): void {
Client.pushResponse(
parseWebSocket,
JSON.stringify({
op: 'error',
error,
code,
reconnect,
requestId,
})
);
}
addSubscriptionInfo(requestId: number, subscriptionInfo: any): void {
this.subscriptionInfos.set(requestId, subscriptionInfo);
}
getSubscriptionInfo(requestId: number): any {
return this.subscriptionInfos.get(requestId);
}
deleteSubscriptionInfo(requestId: number): void {
return this.subscriptionInfos.delete(requestId);
}
_pushEvent(type: string): Function {
return function (
subscriptionId: number,
parseObjectJSON: any,
parseOriginalObjectJSON: any
): void {
const response: Message = {
op: type,
clientId: this.id,
installationId: this.installationId,
};
if (typeof subscriptionId !== 'undefined') {
response['requestId'] = subscriptionId;
}
if (typeof parseObjectJSON !== 'undefined') {
let keys;
if (this.subscriptionInfos.has(subscriptionId)) {
keys = this.subscriptionInfos.get(subscriptionId).keys;
}
response['object'] = this._toJSONWithFields(parseObjectJSON, keys);
if (parseOriginalObjectJSON) {
response['original'] = this._toJSONWithFields(parseOriginalObjectJSON, keys);
}
}
Client.pushResponse(this.parseWebSocket, JSON.stringify(response));
};
}
_toJSONWithFields(parseObjectJSON: any, fields: any): FlattenedObjectData {
if (!fields) {
return parseObjectJSON;
}
const limitedParseObject = {};
for (const field of dafaultFields) {
limitedParseObject[field] = parseObjectJSON[field];
}
for (const field of fields) {
if (field in parseObjectJSON) {
limitedParseObject[field] = parseObjectJSON[field];
}
}
return limitedParseObject;
}
_pushQueryResult(subscriptionId: number, results: any[]): void {
const response: Message = {
op: 'result',
clientId: this.id,
installationId: this.installationId,
requestId: subscriptionId,
};
if (results && Array.isArray(results)) {
let keys;
if (this.subscriptionInfos.has(subscriptionId)) {
keys = this.subscriptionInfos.get(subscriptionId).keys;
}
response['results'] = results.map(obj => this._toJSONWithFields(obj, keys));
} else {
response['results'] = [];
}
Client.pushResponse(this.parseWebSocket, JSON.stringify(response));
}
}
export { Client };