Push.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. "use strict";
  2. var _interopRequireDefault = require("@babel/runtime-corejs3/helpers/interopRequireDefault");
  3. var _Object$defineProperty = require("@babel/runtime-corejs3/core-js-stable/object/define-property");
  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. * Copyright (c) 2015-present, Parse, LLC.
  14. * All rights reserved.
  15. *
  16. * This source code is licensed under the BSD-style license found in the
  17. * LICENSE file in the root directory of this source tree. An additional grant
  18. * of patent rights can be found in the PATENTS file in the same directory.
  19. *
  20. * @flow
  21. */
  22. /**
  23. * Contains functions to deal with Push in Parse.
  24. *
  25. * @class Parse.Push
  26. * @static
  27. * @hideconstructor
  28. */
  29. /**
  30. * Sends a push notification.
  31. * **Available in Cloud Code only.**
  32. *
  33. * See {@link https://docs.parseplatform.org/js/guide/#push-notifications Push Notification Guide}
  34. *
  35. * @function send
  36. * @name Parse.Push.send
  37. * @param {object} data - The data of the push notification. Valid fields
  38. * are:
  39. * <ol>
  40. * <li>channels - An Array of channels to push to.</li>
  41. * <li>push_time - A Date object for when to send the push.</li>
  42. * <li>expiration_time - A Date object for when to expire
  43. * the push.</li>
  44. * <li>expiration_interval - The seconds from now to expire the push.</li>
  45. * <li>where - A Parse.Query over Parse.Installation that is used to match
  46. * a set of installations to push to.</li>
  47. * <li>data - The data to send as part of the push.</li>
  48. * <ol>
  49. * @param {object} options Valid options
  50. * are:<ul>
  51. * <li>useMasterKey: In Cloud Code and Node only, causes the Master Key to
  52. * be used for this request.
  53. * </ul>
  54. * @returns {Promise} A promise that is fulfilled when the push request
  55. * completes.
  56. */
  57. function send(data
  58. /*: PushData*/
  59. )
  60. /*: Promise*/
  61. {
  62. var options
  63. /*:: ?: FullOptions*/
  64. = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
  65. if (data.where && data.where instanceof _ParseQuery.default) {
  66. data.where = data.where.toJSON().where;
  67. }
  68. if (data.push_time && (0, _typeof2.default)(data.push_time) === 'object') {
  69. data.push_time = data.push_time.toJSON();
  70. }
  71. if (data.expiration_time && (0, _typeof2.default)(data.expiration_time) === 'object') {
  72. data.expiration_time = data.expiration_time.toJSON();
  73. }
  74. if (data.expiration_time && data.expiration_interval) {
  75. throw new Error('expiration_time and expiration_interval cannot both be set.');
  76. }
  77. var pushOptions = {
  78. useMasterKey: true
  79. };
  80. if (options.hasOwnProperty('useMasterKey')) {
  81. pushOptions.useMasterKey = options.useMasterKey;
  82. }
  83. return _CoreManager.default.getPushController().send(data, pushOptions);
  84. }
  85. /**
  86. * Gets push status by Id
  87. *
  88. * @function getPushStatus
  89. * @name Parse.Push.getPushStatus
  90. * @param {string} pushStatusId The Id of Push Status.
  91. * @param {object} options Valid options
  92. * are:<ul>
  93. * <li>useMasterKey: In Cloud Code and Node only, causes the Master Key to
  94. * be used for this request.
  95. * </ul>
  96. * @returns {Parse.Object} Status of Push.
  97. */
  98. function getPushStatus(pushStatusId
  99. /*: string*/
  100. )
  101. /*: Promise<string>*/
  102. {
  103. var options
  104. /*:: ?: FullOptions*/
  105. = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
  106. var pushOptions = {
  107. useMasterKey: true
  108. };
  109. if (options.hasOwnProperty('useMasterKey')) {
  110. pushOptions.useMasterKey = options.useMasterKey;
  111. }
  112. var query = new _ParseQuery.default('_PushStatus');
  113. return query.get(pushStatusId, pushOptions);
  114. }
  115. var DefaultController = {
  116. send: function (data
  117. /*: PushData*/
  118. , options
  119. /*:: ?: FullOptions*/
  120. ) {
  121. return _CoreManager.default.getRESTController().request('POST', 'push', data, options);
  122. }
  123. };
  124. _CoreManager.default.setPushController(DefaultController);