Push.js 3.5 KB

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