Push.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.getPushStatus = getPushStatus;
  6. exports.send = send;
  7. var _CoreManager = _interopRequireDefault(require("./CoreManager"));
  8. var _ParseQuery = _interopRequireDefault(require("./ParseQuery"));
  9. function _interopRequireDefault(obj) {
  10. return obj && obj.__esModule ? obj : {
  11. default: obj
  12. };
  13. }
  14. /**
  15. * @flow
  16. */
  17. /*:: import type { WhereClause } from './ParseQuery';*/
  18. /*:: import type { FullOptions } from './RESTController';*/
  19. /*:: export type PushData = {
  20. where?: WhereClause | ParseQuery,
  21. push_time?: Date | string,
  22. expiration_time?: Date | string,
  23. expiration_interval?: number,
  24. };*/
  25. /**
  26. * Contains functions to deal with Push in Parse.
  27. *
  28. * @class Parse.Push
  29. * @static
  30. * @hideconstructor
  31. */
  32. /**
  33. * Sends a push notification.
  34. * **Available in Cloud Code only.**
  35. *
  36. * See {@link https://docs.parseplatform.org/js/guide/#push-notifications Push Notification Guide}
  37. *
  38. * @function send
  39. * @name Parse.Push.send
  40. * @param {object} data - The data of the push notification. Valid fields
  41. * are:
  42. * <ol>
  43. * <li>channels - An Array of channels to push to.</li>
  44. * <li>push_time - A Date object for when to send the push.</li>
  45. * <li>expiration_time - A Date object for when to expire
  46. * the push.</li>
  47. * <li>expiration_interval - The seconds from now to expire the push.</li>
  48. * <li>where - A Parse.Query over Parse.Installation that is used to match
  49. * a set of installations to push to.</li>
  50. * <li>data - The data to send as part of the push.</li>
  51. * <ol>
  52. * @param {object} options Valid options
  53. * are:<ul>
  54. * <li>useMasterKey: In Cloud Code and Node only, causes the Master Key to
  55. * be used for this request.
  56. * </ul>
  57. * @returns {Promise} A promise that is fulfilled when the push request
  58. * completes.
  59. */
  60. function send(data /*: PushData*/, options /*:: ?: FullOptions*/ = {}) /*: Promise*/{
  61. if (data.where && data.where instanceof _ParseQuery.default) {
  62. data.where = data.where.toJSON().where;
  63. }
  64. if (data.push_time && typeof data.push_time === 'object') {
  65. data.push_time = data.push_time.toJSON();
  66. }
  67. if (data.expiration_time && typeof data.expiration_time === 'object') {
  68. data.expiration_time = data.expiration_time.toJSON();
  69. }
  70. if (data.expiration_time && data.expiration_interval) {
  71. throw new Error('expiration_time and expiration_interval cannot both be set.');
  72. }
  73. const pushOptions = {
  74. useMasterKey: true
  75. };
  76. if (options.hasOwnProperty('useMasterKey')) {
  77. pushOptions.useMasterKey = options.useMasterKey;
  78. }
  79. return _CoreManager.default.getPushController().send(data, pushOptions);
  80. }
  81. /**
  82. * Gets push status by Id
  83. *
  84. * @function getPushStatus
  85. * @name Parse.Push.getPushStatus
  86. * @param {string} pushStatusId The Id of Push Status.
  87. * @param {object} options Valid options
  88. * are:<ul>
  89. * <li>useMasterKey: In Cloud Code and Node only, causes the Master Key to
  90. * be used for this request.
  91. * </ul>
  92. * @returns {Parse.Object} Status of Push.
  93. */
  94. function getPushStatus(pushStatusId /*: string*/, options /*:: ?: FullOptions*/ = {}) /*: Promise<string>*/{
  95. const pushOptions = {
  96. useMasterKey: true
  97. };
  98. if (options.hasOwnProperty('useMasterKey')) {
  99. pushOptions.useMasterKey = options.useMasterKey;
  100. }
  101. const query = new _ParseQuery.default('_PushStatus');
  102. return query.get(pushStatusId, pushOptions);
  103. }
  104. const DefaultController = {
  105. send(data /*: PushData*/, options /*:: ?: FullOptions*/) {
  106. return _CoreManager.default.getRESTController().request('POST', 'push', data, options);
  107. }
  108. };
  109. _CoreManager.default.setPushController(DefaultController);