ParseInstallation.js 7.2 KB

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