Push.js 4.0 KB

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