-
-
Notifications
You must be signed in to change notification settings - Fork 595
/
Copy pathParseInstallation.ts
289 lines (266 loc) · 7.45 KB
/
ParseInstallation.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
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
import CoreManager from './CoreManager';
import ParseError from './ParseError';
import ParseObject, { Attributes } from './ParseObject';
import type { AttributeKey } from './ParseObject';
interface DeviceInterface {
IOS: string;
MACOS: string;
TVOS: string;
FCM: string;
ANDROID: string;
WEB: string;
}
const DEVICE_TYPES: DeviceInterface = {
IOS: 'ios',
MACOS: 'macos',
TVOS: 'tvos',
FCM: 'fcm',
ANDROID: 'android',
WEB: 'web',
};
/**
* Parse.Installation is a local representation of installation data that can be saved and retrieved from the Parse cloud.
* This class is a subclass of a Parse.Object, and retains the same functionality of a Parse.Object, but also extends it with installation-specific features.
*
* <p>A valid Parse.Installation can only be instantiated via <code>Parse.Installation.currentInstallation()</code>
*
* Parse.Installation objects which have a valid <code>deviceToken</code> and are saved to the Parse cloud can be used to target push notifications.
* </p>
*
* @alias Parse.Installation
*/
class ParseInstallation<T extends Attributes = Attributes> extends ParseObject<T> {
/**
* @param {object} attributes The initial set of data to store in the object.
*/
constructor(attributes?: T) {
super('_Installation');
if (attributes && typeof attributes === 'object') {
try {
this.set((attributes || {}) as any);
} catch (_) {
throw new Error("Can't create an invalid Installation");
}
}
}
/**
* A unique identifier for this installation’s client application. In iOS, this is the Bundle Identifier.
*
* @property {string} appIdentifier
* @static
* @returns {string}
*/
get appIdentifier() {
return this.get('appIdentifier' as AttributeKey<T>);
}
/**
* The version string of the client application to which this installation belongs.
*
* @property {string} appVersion
* @static
* @returns {string}
*/
get appVersion() {
return this.get('appVersion' as AttributeKey<T>);
}
/**
* The display name of the client application to which this installation belongs.
*
* @property {string} appName
* @static
* @returns {string}
*/
get appName() {
return this.get('appName' as AttributeKey<T>);
}
/**
* The current value of the icon badge for iOS apps.
* Changes to this value on the server will be used
* for future badge-increment push notifications.
*
* @property {number} badge
* @static
* @returns {number}
*/
get badge() {
return this.get('badge' as AttributeKey<T>);
}
/**
* An array of the channels to which a device is currently subscribed.
*
* @property {string[]} channels
* @static
* @returns {string[]}
*/
get channels() {
return this.get('channels' as AttributeKey<T>);
}
/**
* Token used to deliver push notifications to the device.
*
* @property {string} deviceToken
* @static
* @returns {string}
*/
get deviceToken() {
return this.get('deviceToken' as AttributeKey<T>);
}
/**
* The type of device, “ios”, “android”, “web”, etc.
*
* @property {string} deviceType
* @static
* @returns {string}
*/
get deviceType() {
return this.get('deviceType' as AttributeKey<T>);
}
/**
* Gets the GCM sender identifier for this installation
*
* @property {string} GCMSenderId
* @static
* @returns {string}
*/
get GCMSenderId() {
return this.get('GCMSenderId' as AttributeKey<T>);
}
/**
* Universally Unique Identifier (UUID) for the device used by Parse. It must be unique across all of an app’s installations.
*
* @property {string} installationId
* @static
* @returns {string}
*/
get installationId() {
return this.get('installationId' as AttributeKey<T>);
}
/**
* Gets the local identifier for this installation
*
* @property {string} localeIdentifier
* @static
* @returns {string}
*/
get localeIdentifier() {
return this.get('localeIdentifier' as AttributeKey<T>);
}
/**
* Gets the parse server version for this installation
*
* @property {string} parseVersion
* @static
* @returns {string}
*/
get parseVersion() {
return this.get('parseVersion' as AttributeKey<T>);
}
/**
* This field is reserved for directing Parse to the push delivery network to be used.
*
* @property {string} pushType
* @static
* @returns {string}
*/
get pushType() {
return this.get('pushType' as AttributeKey<T>);
}
/**
* Gets the time zone for this installation
*
* @property {string} timeZone
* @static
* @returns {string}
*/
get timeZone() {
return this.get('timeZone' as AttributeKey<T>);
}
/**
* Returns the device types for used for Push Notifications.
*
* <pre>
* Parse.Installation.DEVICE_TYPES.IOS
* Parse.Installation.DEVICE_TYPES.MACOS
* Parse.Installation.DEVICE_TYPES.TVOS
* Parse.Installation.DEVICE_TYPES.FCM
* Parse.Installation.DEVICE_TYPES.ANDROID
* Parse.Installation.DEVICE_TYPES.WEB
* </pre
*
* @property {object} DEVICE_TYPES
* @static
* @returns {object}
*/
static get DEVICE_TYPES(): DeviceInterface {
return DEVICE_TYPES;
}
/**
* Wrap the default fetch behavior with functionality to update local storage.
* If the installation is deleted on the server, retry the fetch as a save operation.
*
* @param {...any} args
* @returns {Promise}
*/
async fetch(...args: any[]): Promise<this> {
try {
await super.fetch.apply(this, args);
} catch (e) {
if (e.code !== ParseError.OBJECT_NOT_FOUND) {
throw e;
}
// The installation was deleted from the server.
// We always want fetch to succeed.
delete this.id;
this._getId(); // Generate localId
this._markAllFieldsDirty();
await super.save.apply(this, args);
}
await CoreManager.getInstallationController().updateInstallationOnDisk(this as any);
return this;
}
/**
* Wrap the default save behavior with functionality to update the local storage.
* If the installation is deleted on the server, retry saving a new installation.
*
* @param {...any} args
* @returns {Promise}
*/
async save(...args: any[]): Promise<this> {
try {
await super.save.apply(this, args);
} catch (e) {
if (e.code !== ParseError.OBJECT_NOT_FOUND) {
throw e;
}
// The installation was deleted from the server.
// We always want save to succeed.
delete this.id;
this._getId(); // Generate localId
this._markAllFieldsDirty();
await super.save.apply(this, args);
}
await CoreManager.getInstallationController().updateInstallationOnDisk(this as any);
return this;
}
_markAllFieldsDirty() {
for (const [key, value] of Object.entries(this.attributes)) {
this.set(key as AttributeKey<T>, value);
}
}
/**
* Get the current Parse.Installation from disk. If doesn't exists, create an new installation.
*
* <pre>
* const installation = await Parse.Installation.currentInstallation();
* installation.set('deviceToken', '123');
* await installation.save();
* </pre>
*
* @returns {Promise} A promise that resolves to the local installation object.
*/
static currentInstallation(): Promise<ParseInstallation> {
return CoreManager.getInstallationController().currentInstallation();
}
}
ParseObject.registerSubclass('_Installation', ParseInstallation);
export default ParseInstallation;