api.js 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. import log from '../../log';
  2. import { Strophe } from 'strophe.js';
  3. import { _converse, api, converse } from '../../core.js';
  4. const { u } = converse.env;
  5. export default {
  6. /**
  7. * The "rooms" namespace groups methods relevant to chatrooms
  8. * (aka groupchats).
  9. *
  10. * @namespace api.rooms
  11. * @memberOf api
  12. */
  13. rooms: {
  14. /**
  15. * Creates a new MUC chatroom (aka groupchat)
  16. *
  17. * Similar to {@link api.rooms.open}, but creates
  18. * the chatroom in the background (i.e. doesn't cause a view to open).
  19. *
  20. * @method api.rooms.create
  21. * @param {(string[]|string)} jid|jids The JID or array of
  22. * JIDs of the chatroom(s) to create
  23. * @param { object } [attrs] attrs The room attributes
  24. * @returns {Promise} Promise which resolves with the Model representing the chat.
  25. */
  26. create (jids, attrs = {}) {
  27. attrs = typeof attrs === 'string' ? { 'nick': attrs } : attrs || {};
  28. if (!attrs.nick && api.settings.get('muc_nickname_from_jid')) {
  29. attrs.nick = Strophe.getNodeFromJid(_converse.bare_jid);
  30. }
  31. if (jids === undefined) {
  32. throw new TypeError('rooms.create: You need to provide at least one JID');
  33. } else if (typeof jids === 'string') {
  34. return api.rooms.get(u.getJIDFromURI(jids), attrs, true);
  35. }
  36. return jids.map(jid => api.rooms.get(u.getJIDFromURI(jid), attrs, true));
  37. },
  38. /**
  39. * Opens a MUC chatroom (aka groupchat)
  40. *
  41. * Similar to {@link api.chats.open}, but for groupchats.
  42. *
  43. * @method api.rooms.open
  44. * @param { string } jid The room JID or JIDs (if not specified, all
  45. * currently open rooms will be returned).
  46. * @param { string } attrs A map containing any extra room attributes.
  47. * @param { string } [attrs.nick] The current user's nickname for the MUC
  48. * @param { boolean } [attrs.auto_configure] A boolean, indicating
  49. * whether the room should be configured automatically or not.
  50. * If set to `true`, then it makes sense to pass in configuration settings.
  51. * @param { object } [attrs.roomconfig] A map of configuration settings to be used when the room gets
  52. * configured automatically. Currently it doesn't make sense to specify
  53. * `roomconfig` values if `auto_configure` is set to `false`.
  54. * For a list of configuration values that can be passed in, refer to these values
  55. * in the [XEP-0045 MUC specification](https://xmpp.org/extensions/xep-0045.html#registrar-formtype-owner).
  56. * The values should be named without the `muc#roomconfig_` prefix.
  57. * @param { boolean } [attrs.minimized] A boolean, indicating whether the room should be opened minimized or not.
  58. * @param { boolean } [attrs.bring_to_foreground] A boolean indicating whether the room should be
  59. * brought to the foreground and therefore replace the currently shown chat.
  60. * If there is no chat currently open, then this option is ineffective.
  61. * @param { Boolean } [force=false] - By default, a minimized
  62. * room won't be maximized (in `overlayed` view mode) and in
  63. * `fullscreen` view mode a newly opened room won't replace
  64. * another chat already in the foreground.
  65. * Set `force` to `true` if you want to force the room to be
  66. * maximized or shown.
  67. * @returns {Promise} Promise which resolves with the Model representing the chat.
  68. *
  69. * @example
  70. * api.rooms.open('group@muc.example.com')
  71. *
  72. * @example
  73. * // To return an array of rooms, provide an array of room JIDs:
  74. * api.rooms.open(['group1@muc.example.com', 'group2@muc.example.com'])
  75. *
  76. * @example
  77. * // To setup a custom nickname when joining the room, provide the optional nick argument:
  78. * api.rooms.open('group@muc.example.com', {'nick': 'mycustomnick'})
  79. *
  80. * @example
  81. * // For example, opening a room with a specific default configuration:
  82. * api.rooms.open(
  83. * 'myroom@conference.example.org',
  84. * { 'nick': 'coolguy69',
  85. * 'auto_configure': true,
  86. * 'roomconfig': {
  87. * 'changesubject': false,
  88. * 'membersonly': true,
  89. * 'persistentroom': true,
  90. * 'publicroom': true,
  91. * 'roomdesc': 'Comfy room for hanging out',
  92. * 'whois': 'anyone'
  93. * }
  94. * }
  95. * );
  96. */
  97. async open (jids, attrs = {}, force = false) {
  98. await api.waitUntil('chatBoxesFetched');
  99. if (jids === undefined) {
  100. const err_msg = 'rooms.open: You need to provide at least one JID';
  101. log.error(err_msg);
  102. throw new TypeError(err_msg);
  103. } else if (typeof jids === 'string') {
  104. const room = await api.rooms.get(jids, attrs, true);
  105. !attrs.hidden && room?.maybeShow(force);
  106. return room;
  107. } else {
  108. const rooms = await Promise.all(jids.map(jid => api.rooms.get(jid, attrs, true)));
  109. rooms.forEach(r => !attrs.hidden && r.maybeShow(force));
  110. return rooms;
  111. }
  112. },
  113. /**
  114. * Fetches the object representing a MUC chatroom (aka groupchat)
  115. *
  116. * @method api.rooms.get
  117. * @param { String } [jid] The room JID (if not specified, all rooms will be returned).
  118. * @param { Object } [attrs] A map containing any extra room attributes
  119. * to be set if `create` is set to `true`
  120. * @param { String } [attrs.nick] Specify the nickname
  121. * @param { String } [attrs.password ] Specify a password if needed to enter a new room
  122. * @param { Boolean } create A boolean indicating whether the room should be created
  123. * if not found (default: `false`)
  124. * @returns { Promise<_converse.ChatRoom> }
  125. * @example
  126. * api.waitUntil('roomsAutoJoined').then(() => {
  127. * const create_if_not_found = true;
  128. * api.rooms.get(
  129. * 'group@muc.example.com',
  130. * {'nick': 'dread-pirate-roberts', 'password': 'secret'},
  131. * create_if_not_found
  132. * )
  133. * });
  134. */
  135. async get (jids, attrs = {}, create = false) {
  136. await api.waitUntil('chatBoxesFetched');
  137. async function _get (jid) {
  138. jid = u.getJIDFromURI(jid);
  139. let model = await api.chatboxes.get(jid);
  140. if (!model && create) {
  141. model = await api.chatboxes.create(jid, attrs, _converse.ChatRoom);
  142. } else {
  143. model = model && model.get('type') === _converse.CHATROOMS_TYPE ? model : null;
  144. if (model && Object.keys(attrs).length) {
  145. model.save(attrs);
  146. }
  147. }
  148. return model;
  149. }
  150. if (jids === undefined) {
  151. const chats = await api.chatboxes.get();
  152. return chats.filter(c => c.get('type') === _converse.CHATROOMS_TYPE);
  153. } else if (typeof jids === 'string') {
  154. return _get(jids);
  155. }
  156. return Promise.all(jids.map(jid => _get(jid)));
  157. }
  158. }
  159. }