ParseInstallation.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. "use strict";
  2. var _Object$defineProperty = require("@babel/runtime-corejs3/core-js-stable/object/define-property");
  3. var _interopRequireDefault = require("@babel/runtime-corejs3/helpers/interopRequireDefault");
  4. _Object$defineProperty(exports, "__esModule", {
  5. value: true
  6. });
  7. exports.default = void 0;
  8. var _entries = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/object/entries"));
  9. var _CoreManager = _interopRequireDefault(require("./CoreManager"));
  10. var _ParseError = _interopRequireDefault(require("./ParseError"));
  11. var _ParseObject = _interopRequireDefault(require("./ParseObject"));
  12. const DEVICE_TYPES = {
  13. IOS: 'ios',
  14. MACOS: 'macos',
  15. TVOS: 'tvos',
  16. FCM: 'fcm',
  17. ANDROID: 'android',
  18. WEB: 'web'
  19. };
  20. /**
  21. * Parse.Installation is a local representation of installation data that can be saved and retrieved from the Parse cloud.
  22. * 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.
  23. *
  24. * <p>A valid Parse.Installation can only be instantiated via <code>Parse.Installation.currentInstallation()</code>
  25. *
  26. * Parse.Installation objects which have a valid <code>deviceToken</code> and are saved to the Parse cloud can be used to target push notifications.
  27. * </p>
  28. *
  29. * @alias Parse.Installation
  30. */
  31. class ParseInstallation extends _ParseObject.default {
  32. /**
  33. * @param {object} attributes The initial set of data to store in the object.
  34. */
  35. constructor(attributes) {
  36. super('_Installation');
  37. if (attributes && typeof attributes === 'object') {
  38. if (!this.set(attributes)) {
  39. throw new Error("Can't create an invalid Installation");
  40. }
  41. }
  42. }
  43. /**
  44. * A unique identifier for this installation’s client application. In iOS, this is the Bundle Identifier.
  45. *
  46. * @property {string} appIdentifier
  47. * @static
  48. * @returns {string}
  49. */
  50. get appIdentifier() {
  51. return this.get('appIdentifier');
  52. }
  53. /**
  54. * The version string of the client application to which this installation belongs.
  55. *
  56. * @property {string} appVersion
  57. * @static
  58. * @returns {string}
  59. */
  60. get appVersion() {
  61. return this.get('appVersion');
  62. }
  63. /**
  64. * The display name of the client application to which this installation belongs.
  65. *
  66. * @property {string} appName
  67. * @static
  68. * @returns {string}
  69. */
  70. get appName() {
  71. return this.get('appName');
  72. }
  73. /**
  74. * The current value of the icon badge for iOS apps.
  75. * Changes to this value on the server will be used
  76. * for future badge-increment push notifications.
  77. *
  78. * @property {number} badge
  79. * @static
  80. * @returns {number}
  81. */
  82. get badge() {
  83. return this.get('badge');
  84. }
  85. /**
  86. * An array of the channels to which a device is currently subscribed.
  87. *
  88. * @property {string[]} channels
  89. * @static
  90. * @returns {string[]}
  91. */
  92. get channels() {
  93. return this.get('channels');
  94. }
  95. /**
  96. * Token used to deliver push notifications to the device.
  97. *
  98. * @property {string} deviceToken
  99. * @static
  100. * @returns {string}
  101. */
  102. get deviceToken() {
  103. return this.get('deviceToken');
  104. }
  105. /**
  106. * The type of device, “ios”, “android”, “web”, etc.
  107. *
  108. * @property {string} deviceType
  109. * @static
  110. * @returns {string}
  111. */
  112. get deviceType() {
  113. return this.get('deviceType');
  114. }
  115. /**
  116. * Gets the GCM sender identifier for this installation
  117. *
  118. * @property {string} GCMSenderId
  119. * @static
  120. * @returns {string}
  121. */
  122. get GCMSenderId() {
  123. return this.get('GCMSenderId');
  124. }
  125. /**
  126. * Universally Unique Identifier (UUID) for the device used by Parse. It must be unique across all of an app’s installations.
  127. *
  128. * @property {string} installationId
  129. * @static
  130. * @returns {string}
  131. */
  132. get installationId() {
  133. return this.get('installationId');
  134. }
  135. /**
  136. * Gets the local identifier for this installation
  137. *
  138. * @property {string} localeIdentifier
  139. * @static
  140. * @returns {string}
  141. */
  142. get localeIdentifier() {
  143. return this.get('localeIdentifier');
  144. }
  145. /**
  146. * Gets the parse server version for this installation
  147. *
  148. * @property {string} parseVersion
  149. * @static
  150. * @returns {string}
  151. */
  152. get parseVersion() {
  153. return this.get('parseVersion');
  154. }
  155. /**
  156. * This field is reserved for directing Parse to the push delivery network to be used.
  157. *
  158. * @property {string} pushType
  159. * @static
  160. * @returns {string}
  161. */
  162. get pushType() {
  163. return this.get('pushType');
  164. }
  165. /**
  166. * Gets the time zone for this installation
  167. *
  168. * @property {string} timeZone
  169. * @static
  170. * @returns {string}
  171. */
  172. get timeZone() {
  173. return this.get('timeZone');
  174. }
  175. /**
  176. * Returns the device types for used for Push Notifications.
  177. *
  178. * <pre>
  179. * Parse.Installation.DEVICE_TYPES.IOS
  180. * Parse.Installation.DEVICE_TYPES.MACOS
  181. * Parse.Installation.DEVICE_TYPES.TVOS
  182. * Parse.Installation.DEVICE_TYPES.FCM
  183. * Parse.Installation.DEVICE_TYPES.ANDROID
  184. * Parse.Installation.DEVICE_TYPES.WEB
  185. * </pre
  186. *
  187. * @property {object} DEVICE_TYPES
  188. * @static
  189. * @returns {object}
  190. */
  191. static get DEVICE_TYPES() {
  192. return DEVICE_TYPES;
  193. }
  194. /**
  195. * Wrap the default fetch behavior with functionality to update local storage.
  196. * If the installation is deleted on the server, retry the fetch as a save operation.
  197. *
  198. * @param {...any} args
  199. * @returns {Promise}
  200. */
  201. async fetch() {
  202. for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
  203. args[_key] = arguments[_key];
  204. }
  205. try {
  206. await super.fetch.apply(this, args);
  207. } catch (e) {
  208. if (e.code !== _ParseError.default.OBJECT_NOT_FOUND) {
  209. throw e;
  210. }
  211. // The installation was deleted from the server.
  212. // We always want fetch to succeed.
  213. delete this.id;
  214. this._getId(); // Generate localId
  215. this._markAllFieldsDirty();
  216. await super.save.apply(this, args);
  217. }
  218. await _CoreManager.default.getInstallationController().updateInstallationOnDisk(this);
  219. return this;
  220. }
  221. /**
  222. * Wrap the default save behavior with functionality to update the local storage.
  223. * If the installation is deleted on the server, retry saving a new installation.
  224. *
  225. * @param {...any} args
  226. * @returns {Promise}
  227. */
  228. async save() {
  229. for (var _len2 = arguments.length, args = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
  230. args[_key2] = arguments[_key2];
  231. }
  232. try {
  233. await super.save.apply(this, args);
  234. } catch (e) {
  235. if (e.code !== _ParseError.default.OBJECT_NOT_FOUND) {
  236. throw e;
  237. }
  238. // The installation was deleted from the server.
  239. // We always want save to succeed.
  240. delete this.id;
  241. this._getId(); // Generate localId
  242. this._markAllFieldsDirty();
  243. await super.save.apply(this, args);
  244. }
  245. await _CoreManager.default.getInstallationController().updateInstallationOnDisk(this);
  246. return this;
  247. }
  248. _markAllFieldsDirty() {
  249. for (const [key, value] of (0, _entries.default)(this.attributes)) {
  250. this.set(key, value);
  251. }
  252. }
  253. /**
  254. * Get the current Parse.Installation from disk. If doesn't exists, create an new installation.
  255. *
  256. * <pre>
  257. * const installation = await Parse.Installation.currentInstallation();
  258. * installation.set('deviceToken', '123');
  259. * await installation.save();
  260. * </pre>
  261. *
  262. * @returns {Promise} A promise that resolves to the local installation object.
  263. */
  264. static currentInstallation() {
  265. return _CoreManager.default.getInstallationController().currentInstallation();
  266. }
  267. }
  268. _ParseObject.default.registerSubclass('_Installation', ParseInstallation);
  269. module.exports = ParseInstallation;
  270. var _default = exports.default = ParseInstallation;