api.js 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. import { isValidJID } from '../../utils/jid.js';
  2. import _converse from '../../shared/_converse.js';
  3. import api from '../../shared/api/index.js';
  4. import converse from '../../shared/api/public.js';
  5. const { Strophe } = converse.env;
  6. export default {
  7. /**
  8. * @namespace _converse.api.contacts
  9. * @memberOf _converse.api
  10. *
  11. * @typedef {import('./contact').default} RosterContact
  12. */
  13. contacts: {
  14. /**
  15. * This method is used to retrieve roster contacts.
  16. *
  17. * @method _converse.api.contacts.get
  18. * @param {(string[]|string)} jids The JID or JIDs of the contacts to be returned.
  19. * @returns {promise} Promise which resolves with the
  20. * {@link RosterContact} (or an array of them) representing the contact.
  21. *
  22. * @example
  23. * // Fetch a single contact
  24. * _converse.api.listen.on('rosterContactsFetched', function () {
  25. * const contact = await _converse.api.contacts.get('buddy@example.com')
  26. * // ...
  27. * });
  28. *
  29. * @example
  30. * // To get multiple contacts, pass in an array of JIDs:
  31. * _converse.api.listen.on('rosterContactsFetched', function () {
  32. * const contacts = await _converse.api.contacts.get(
  33. * ['buddy1@example.com', 'buddy2@example.com']
  34. * )
  35. * // ...
  36. * });
  37. *
  38. * @example
  39. * // To return all contacts, simply call ``get`` without any parameters:
  40. * _converse.api.listen.on('rosterContactsFetched', function () {
  41. * const contacts = await _converse.api.contacts.get();
  42. * // ...
  43. * });
  44. */
  45. async get (jids) {
  46. await api.waitUntil('rosterContactsFetched');
  47. const { roster } = _converse.state;
  48. const _getter = /** @param {string} jid */(jid) => roster.get(Strophe.getBareJidFromJid(jid));
  49. if (jids === undefined) {
  50. jids = roster.pluck('jid');
  51. } else if (typeof jids === 'string') {
  52. return _getter(jids);
  53. }
  54. return /** @type {string[]} */(jids).map(_getter);
  55. },
  56. /**
  57. * Remove a contact from the roster
  58. * @param {string} jid
  59. * @param {boolean} [unsubscribe] - Whether we should unsubscribe
  60. * from the contact's presence updates.
  61. */
  62. async remove (jid, unsubscribe) {
  63. await api.waitUntil('rosterContactsFetched');
  64. const contact = await api.contacts.get(jid);
  65. contact.remove(unsubscribe);
  66. },
  67. /**
  68. * Add a contact.
  69. * @param {import('./types').RosterContactAttributes} attributes
  70. * @param {boolean} [persist=true] - Whether the contact should be persisted to the user's roster.
  71. * @param {boolean} [subscribe=true] - Whether we should subscribe to the contacts presence updates.
  72. * @param {string} [message=''] - An optional message to include with the presence subscription
  73. * @param {boolean} subscribe - Whether a presense subscription should
  74. * be sent out to the contact being added.
  75. * @returns {Promise<RosterContact>}
  76. * @example
  77. * api.contacts.add({ jid: 'buddy@example.com', groups: ['Buddies'] })
  78. */
  79. async add (attributes, persist=true, subscribe=true, message='') {
  80. if (!isValidJID(attributes?.jid)) throw new Error('api.contacts.add: Valid JID required');
  81. await api.waitUntil('rosterContactsFetched');
  82. const { roster } = _converse.state;
  83. return roster.addContact(attributes, persist, subscribe, message);
  84. }
  85. }
  86. }