您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

Caps.js 8.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. import $ from 'jquery';
  2. import { Strophe, b64_sha1 } from 'strophe.js'; // eslint-disable-line camelcase
  3. import { XMPPEvents } from '../../service/xmpp/XMPPEvents';
  4. import Listenable from '../util/Listenable';
  5. /**
  6. * The property
  7. */
  8. const IDENTITY_PROPERTIES = [ 'category', 'type', 'lang', 'name' ];
  9. const IDENTITY_PROPERTIES_FOR_COMPARE = [ 'category', 'type', 'lang' ];
  10. const HASH = 'sha-1';
  11. /**
  12. *
  13. * @param a
  14. * @param b
  15. */
  16. function compareIdentities(a, b) {
  17. let res = 0;
  18. IDENTITY_PROPERTIES_FOR_COMPARE.some(key =>
  19. (res = ((a[key] > b[key]) && 1) || ((a[key] < b[key]) && -1)) !== 0
  20. );
  21. return res;
  22. }
  23. /**
  24. * Produces a sha-1 from provided identity and features values.
  25. *
  26. * @param {Array<Object>} identities - The identity objects.
  27. * @param {Array<string>} features - The features.
  28. * @returns {string}
  29. */
  30. function generateSha(identities, features) {
  31. const sortedIdentities = identities.sort(compareIdentities).reduce(
  32. (accumulatedValue, identity) => `${
  33. IDENTITY_PROPERTIES.reduce(
  34. (tmp, key, idx) =>
  35. tmp
  36. + (idx === 0 ? '' : '/')
  37. + (identity[key] ? identity[key] : ''),
  38. '')
  39. }<`, '');
  40. const sortedFeatures = features.sort().reduce(
  41. (tmp, feature) => `${tmp + feature}<`, '');
  42. return b64_sha1(sortedIdentities + sortedFeatures);
  43. }
  44. /**
  45. * Parses the disco-info node and returns the sets of features and identities.
  46. * @param {String} node The node with results to parse.
  47. * @returns {{features: Set<any>, identities: Set<any>}}
  48. */
  49. export function parseDiscoInfo(node) {
  50. const features = new Set();
  51. const identities = new Set();
  52. $(node).find('>query>feature')
  53. .each((_, el) => features.add(el.getAttribute('var')));
  54. $(node).find('>query>identity')
  55. .each((_, el) => identities.add({
  56. type: el.getAttribute('type'),
  57. name: el.getAttribute('name'),
  58. category: el.getAttribute('category')
  59. }));
  60. return {
  61. features,
  62. identities
  63. };
  64. }
  65. /**
  66. * Implements xep-0115 ( http://xmpp.org/extensions/xep-0115.html )
  67. */
  68. export default class Caps extends Listenable {
  69. /**
  70. * Constructs new Caps instance.
  71. * @param {Strophe.Connection} connection the strophe connection object
  72. * @param {String} node the value of the node attribute of the "c" xml node
  73. * that will be sent to the other participants
  74. */
  75. constructor(connection = {}, node = 'http://jitsi.org/jitsimeet') {
  76. super();
  77. this.node = node;
  78. this.disco = connection.disco;
  79. if (!this.disco) {
  80. throw new Error(
  81. 'Missing strophe-plugins '
  82. + '(disco plugin is required)!');
  83. }
  84. this.version = '';
  85. this.rooms = new Set();
  86. // We keep track of features added outside the library and we publish them
  87. // in the presence of the participant for simplicity, avoiding the disco info request-response.
  88. this.externalFeatures = new Set();
  89. const emuc = connection.emuc;
  90. emuc.addListener(XMPPEvents.EMUC_ROOM_ADDED,
  91. room => this._addChatRoom(room));
  92. emuc.addListener(XMPPEvents.EMUC_ROOM_REMOVED,
  93. room => this._removeChatRoom(room));
  94. Object.keys(emuc.rooms).forEach(jid => {
  95. this._addChatRoom(emuc.rooms[jid]);
  96. });
  97. Strophe.addNamespace('CAPS', 'http://jabber.org/protocol/caps');
  98. this.disco.addFeature(Strophe.NS.CAPS);
  99. }
  100. /**
  101. * Adds new feature to the list of supported features for the local
  102. * participant
  103. * @param {String} feature the name of the feature.
  104. * @param {boolean} submit if true - new presence with updated "c" node
  105. * will be sent.
  106. * @param {boolean} external whether this feature was added externally to the library.
  107. * We put features used directly by the clients (is jibri, remote-control enabled etc.) in the presence
  108. * to avoid additional disco-info queries by those clients.
  109. */
  110. addFeature(feature, submit = false, external = false) {
  111. this.disco.addFeature(feature);
  112. this._generateVersion();
  113. if (external && !this.externalFeatures.has(feature)) {
  114. this.externalFeatures.add(feature);
  115. this.rooms.forEach(room => this._updateRoomWithExternalFeatures(room));
  116. }
  117. if (submit) {
  118. this.submit();
  119. }
  120. }
  121. /**
  122. * Removes a feature from the list of supported features for the local
  123. * participant
  124. * @param {String} feature the name of the feature.
  125. * @param {boolean} submit if true - new presence with updated "c" node
  126. * will be sent.
  127. * @param {boolean} external whether this feature was added externally to the library.
  128. */
  129. removeFeature(feature, submit = false, external = false) {
  130. this.disco.removeFeature(feature);
  131. this._generateVersion();
  132. if (external && this.externalFeatures.has(feature)) {
  133. this.externalFeatures.delete(feature);
  134. this.rooms.forEach(room => this._updateRoomWithExternalFeatures(room));
  135. }
  136. if (submit) {
  137. this.submit();
  138. }
  139. }
  140. /**
  141. * Sends new presence stanza for every room from the list of rooms.
  142. */
  143. submit() {
  144. this.rooms.forEach(room => room.sendPresence());
  145. }
  146. /**
  147. * Updates the presences in the room based on the current values in externalFeatures.
  148. * @param {ChatRoom} room the room to update.
  149. * @private
  150. */
  151. _updateRoomWithExternalFeatures(room) {
  152. if (this.externalFeatures.size === 0) {
  153. room.removeFromPresence('features');
  154. } else {
  155. const children = [];
  156. this.externalFeatures.forEach(f => {
  157. children.push({
  158. 'tagName': 'feature',
  159. attributes: { 'var': f }
  160. });
  161. });
  162. room.addOrReplaceInPresence('features', { children });
  163. }
  164. }
  165. /**
  166. * Returns a set with the features for a host.
  167. * @param {String} jid the jid of the host
  168. * @param {int} timeout the timeout in ms for reply from the host.
  169. * @returns {Promise<Set<String>, Error>}
  170. */
  171. getFeaturesAndIdentities(jid, node, timeout = 5000) {
  172. return this._getDiscoInfo(jid, node, timeout);
  173. }
  174. /**
  175. * Returns a set with the features and identities for a host.
  176. * @param {String} jid the jid of the host
  177. * @param {String|null} node the node to query
  178. * @param {int} timeout the timeout in ms for reply from the host.
  179. * @returns {Promise<Object>}
  180. * @private
  181. */
  182. _getDiscoInfo(jid, node, timeout) {
  183. return new Promise((resolve, reject) =>
  184. this.disco.info(jid, node, response => {
  185. resolve(parseDiscoInfo(response));
  186. }, reject, timeout)
  187. );
  188. }
  189. /**
  190. * Adds ChatRoom instance to the list of rooms. Adds listeners to the room
  191. * and adds "c" element to the presences of the room.
  192. * @param {ChatRoom} room the room.
  193. */
  194. _addChatRoom(room) {
  195. this.rooms.add(room);
  196. this._fixChatRoomPresenceMap(room);
  197. this._updateRoomWithExternalFeatures(room);
  198. }
  199. /**
  200. * Removes ChatRoom instance from the list of rooms. Removes listeners
  201. * added from the Caps class.
  202. * @param {ChatRoom} room the room.
  203. */
  204. _removeChatRoom(room) {
  205. this.rooms.delete(room);
  206. }
  207. /**
  208. * Creates/updates the "c" xml node into the presence of the passed room.
  209. * @param {ChatRoom} room the room.
  210. */
  211. _fixChatRoomPresenceMap(room) {
  212. room.addOrReplaceInPresence('c', {
  213. attributes: {
  214. xmlns: Strophe.NS.CAPS,
  215. hash: HASH,
  216. node: this.node,
  217. ver: this.version
  218. }
  219. });
  220. }
  221. /**
  222. * Handles this.version changes.
  223. */
  224. _notifyVersionChanged() {
  225. // update the version for all rooms
  226. this.rooms.forEach(room => this._fixChatRoomPresenceMap(room));
  227. }
  228. /**
  229. * Generates the value for the "ver" attribute.
  230. */
  231. _generateVersion() {
  232. this.version
  233. = generateSha(this.disco._identities, this.disco._features);
  234. this._notifyVersionChanged();
  235. }
  236. }