Converse converse.js

Source: headless/plugins/status/api.js

  1. import { _converse, api } from '../../core';
  2. import { STATUS_WEIGHTS } from '../../shared/constants';
  3. export default {
  4. /**
  5. * Set and get the user's chat status, also called their *availability*.
  6. * @namespace _converse.api.user.status
  7. * @memberOf _converse.api.user
  8. */
  9. status: {
  10. /**
  11. * Return the current user's availability status.
  12. * @async
  13. * @method _converse.api.user.status.get
  14. * @example _converse.api.user.status.get();
  15. */
  16. async get () {
  17. await api.waitUntil('statusInitialized');
  18. return _converse.xmppstatus.get('status');
  19. },
  20. /**
  21. * The user's status can be set to one of the following values:
  22. *
  23. * @async
  24. * @method _converse.api.user.status.set
  25. * @param { string } value The user's chat status (e.g. 'away', 'dnd', 'offline', 'online', 'unavailable' or 'xa')
  26. * @param { string } [message] A custom status message
  27. *
  28. * @example _converse.api.user.status.set('dnd');
  29. * @example _converse.api.user.status.set('dnd', 'In a meeting');
  30. */
  31. async set (value, message) {
  32. const data = {'status': value};
  33. if (!Object.keys(STATUS_WEIGHTS).includes(value)) {
  34. throw new Error(
  35. 'Invalid availability value. See https://xmpp.org/rfcs/rfc3921.html#rfc.section.2.2.2.1'
  36. );
  37. }
  38. if (typeof message === 'string') {
  39. data.status_message = message;
  40. }
  41. await api.waitUntil('statusInitialized');
  42. _converse.xmppstatus.save(data);
  43. },
  44. /**
  45. * Set and retrieve the user's custom status message.
  46. *
  47. * @namespace _converse.api.user.status.message
  48. * @memberOf _converse.api.user.status
  49. */
  50. message: {
  51. /**
  52. * @async
  53. * @method _converse.api.user.status.message.get
  54. * @returns { Promise<string> } The status message
  55. * @example const message = _converse.api.user.status.message.get()
  56. */
  57. async get () {
  58. await api.waitUntil('statusInitialized');
  59. return _converse.xmppstatus.get('status_message');
  60. },
  61. /**
  62. * @async
  63. * @method _converse.api.user.status.message.set
  64. * @param { string } status The status message
  65. * @example _converse.api.user.status.message.set('In a meeting');
  66. */
  67. async set (status) {
  68. await api.waitUntil('statusInitialized');
  69. _converse.xmppstatus.save({ status_message: status });
  70. }
  71. }
  72. }
  73. }