developer_api.rst 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361
  1. .. raw:: html
  2. <div id="banner"><a href="https://github.com/jcbrand/converse.js/blob/master/docs/source/theming.rst">Edit me on GitHub</a></div>
  3. =========================
  4. The old API documentation
  5. =========================
  6. .. note:: The API documented here is available in Converse 0.8.4 and higher.
  7. Earlier versions of Converse might have different API methods or none at all.
  8. .. note:: From version 3.0.0 and onwards many API methods have been made
  9. private and available to plugins only. This means that if you want to
  10. use the API, you'll first need to create a plugin from which you can
  11. access it. This change is done to avoid leakage of sensitive data to
  12. malicious or non-whitelisted scripts.
  13. The Converse API is broken up into different logical "groupings" (for
  14. example ``converse.plugins`` or ``converse.contacts``).
  15. There are some exceptions to this, like ``converse.initialize``, which aren't
  16. groupings but single methods.
  17. The groupings logically group methods, such as standardised accessors and
  18. mutators::
  19. .get
  20. .set
  21. .add
  22. .remove
  23. So for example, to get a contact, you would do the following::
  24. _converse.api.contacts.get('jid@example.com');
  25. To get multiple contacts, just pass in an array of jids::
  26. _converse.api.contacts.get(['jid1@example.com', 'jid2@example.com']);
  27. To get all contacts, simply call ``get`` without any jids::
  28. _converse.api.contacts.get();
  29. Public API methods
  30. ==================
  31. Publich API methods are those methods that are accessible on the global
  32. ``window.converse`` object. They are public, because any JavaScript in the page
  33. can call them. Public methods therefore don't expose any sensitive or closured
  34. data. To do that, you'll need to create a plugin, which has access to the
  35. private API method.
  36. .. _`initialize`:
  37. initialize
  38. ----------
  39. .. note:: This method is the one exception of a method which is not logically grouped as explained above.
  40. Publich API method which initializes Converse.
  41. This method must always be called when using Converse.
  42. The `initialize` method takes a map of :ref:`configuration-settings`.
  43. Example:
  44. .. code-block:: javascript
  45. converse.initialize({
  46. allow_otr: true,
  47. auto_list_rooms: false,
  48. auto_subscribe: false,
  49. bosh_service_url: 'https://bind.example.com',
  50. hide_muc_server: false,
  51. i18n: locales['en'],
  52. keepalive: true,
  53. play_sounds: true,
  54. prebind: false,
  55. show_controlbox_by_default: true,
  56. debug: false,
  57. roster_groups: true
  58. });
  59. The **plugin** grouping
  60. ------------------------
  61. Exposes methods for adding and removing plugins. You'll need to write a plugin
  62. if you want to have access to the private API methods defined further down below.
  63. For more information on plugins, read the section :ref:`writing-a-plugin`.
  64. add
  65. ~~~
  66. Registers a new plugin.
  67. .. code-block:: javascript
  68. var plugin = {
  69. initialize: function () {
  70. // method on any plugin (if it exists) as soon as the plugin has
  71. // been loaded.
  72. // Inside this method, you have access to the closured
  73. // _converse object, which contains the core logic and data
  74. // structures of Converse
  75. }
  76. }
  77. converse.plugins.add('myplugin', plugin);
  78. Private API methods
  79. ===================
  80. The private API methods are only accessible via the closured ``_converse``
  81. object, which is only available to plugins.
  82. These methods are kept private (i.e. not global) because they may return
  83. sensitive data which should be kept off-limits to other 3rd-party scripts
  84. that might be running in the page.
  85. .. note:: The example code snippets shown below are a bit contrived. I've added
  86. the minimum plugin boilerplace around the actual example, to show that
  87. these API methods can only be called inside a plugin where the
  88. ``_converse`` object is available. However, sometimes other considerations
  89. need to be made as well. For example, for certain API methods it is
  90. necessary to first wait until the data has been received from the XMPP
  91. server (or from the browser's sessionStorage cache). Due to
  92. time-constriaints these limitations are ignored in the examples below. For
  93. a fuller picture, refer to the section :ref:`events-API` as well.
  94. emit
  95. ----
  96. This method allows you to emit events, which can be listened to via
  97. ``_converse.api.listen.on`` or ``_converse.api.listen.once``.
  98. For example:
  99. .. code-block:: javascript
  100. _converse.api.trigger('foo-completed');
  101. Additionally, if a promise has been registered under the same name
  102. (via ``_converse.api.promises.add``), then that promise will also be resolved
  103. when calling ``emit``.
  104. send
  105. ----
  106. Allows you to send XML stanzas.
  107. For example, to send a message stanza:
  108. .. code-block:: javascript
  109. converse.plugins.add('myplugin', {
  110. initialize: function () {
  111. var msg = converse.env.$msg({
  112. from: 'juliet@example.com/balcony',
  113. to:'romeo@example.net',
  114. type:'chat'
  115. });
  116. this._converse.api.send(msg);
  117. }
  118. });
  119. .. _`waituntil-grouping`:
  120. waitUntil
  121. ---------
  122. This method can be used to wait for promises. Promises are similar to events
  123. (for event handling, refer to the :ref:`listen-grouping`), but they differ in
  124. two important ways:
  125. * A promise gets resolved only once, whereas events can fire multiple times.
  126. * A handler registered for a promise, will still fire *after* the promise has
  127. been resolved, which is not the case with an event handler.
  128. Converse has the following promises:
  129. * :ref:`cachedRoster`
  130. * :ref:`chatBoxesFetched`
  131. * :ref:`pluginsInitialized`
  132. * :ref:`roster`
  133. * :ref:`rosterContactsFetched`
  134. * :ref:`rosterGroupsFetched`
  135. * :ref:`rosterInitialized`
  136. * :ref:`statusInitialized`
  137. * :ref:`roomsPanelRendered` (only via the `converse-muc` plugin)
  138. Below is an example from `converse-muc.js <https://github.com/jcbrand/converse.js/blob/master/src/converse-muc.js>`_
  139. where the `rosterContactsFetched` promise is waited on. The method
  140. `this.initInviteWidget` will initialize the chatroom invitation widget.
  141. .. code-block:: javascript
  142. _converse.api.waitUntil('rosterContactsFetched').then(this.initInviteWidget.bind(this));
  143. The line above executes only once a chatroom has been opened and entered, so
  144. using an event handler here would not work, since the event might have fired
  145. already by that time.
  146. The **archive** grouping
  147. ------------------------
  148. Converse supports the *Message Archive Management*
  149. (`XEP-0313 <https://xmpp.org/extensions/xep-0313.html>`_) protocol,
  150. through which it is able to query an XMPP server for archived messages.
  151. See also the **message_archiving** option in the :ref:`configuration-settings` section, which you'll usually
  152. want to in conjunction with this API.
  153. query
  154. ~~~~~
  155. The ``query`` method is used to query for archived messages.
  156. It accepts the following optional parameters:
  157. * **options** an object containing the query parameters. Valid query parameters
  158. are ``with``, ``start``, ``end``, ``first``, ``last``, ``after``, ``before``, ``index`` and ``count``.
  159. * **callback** is the callback method that will be called when all the messages
  160. have been received.
  161. * **errback** is the callback method to be called when an error is returned by
  162. the XMPP server, for example when it doesn't support message archiving.
  163. Examples
  164. ^^^^^^^^
  165. **Requesting all archived messages**
  166. The simplest query that can be made is to simply not pass in any parameters.
  167. Such a query will return all archived messages for the current user.
  168. Generally, you'll however always want to pass in a callback method, to receive
  169. the returned messages.
  170. .. code-block:: javascript
  171. converse.plugins.add('myplugin', {
  172. initialize: function () {
  173. var errback = function (iq) {
  174. // The query was not successful, perhaps inform the user?
  175. // The IQ stanza returned by the XMPP server is passed in, so that you
  176. // may inspect it and determine what the problem was.
  177. }
  178. var callback = function (messages) {
  179. // Do something with the messages, like showing them in your webpage.
  180. }
  181. this._converse.api.archive.query(callback, errback))
  182. }
  183. });
  184. **Waiting until server support has been determined**
  185. The query method will only work if Converse has been able to determine that
  186. the server supports MAM queries, otherwise the following error will be raised:
  187. - *This server does not support XEP-0313, Message Archive Management*
  188. The very first time Converse loads in a browser tab, if you call the query
  189. API too quickly, the above error might appear because service discovery has not
  190. yet been completed.
  191. To work solve this problem, you can first listen for the ``serviceDiscovered`` event,
  192. through which you can be informed once support for MAM has been determined.
  193. For example:
  194. .. code-block:: javascript
  195. converse.plugins.add('myplugin', {
  196. initialize: function () {
  197. var _converse = this._converse;
  198. _converse.api.listen.on('serviceDiscovered', function (feature) {
  199. if (feature.get('var') === converse.env.Strophe.NS.MAM) {
  200. _converse.api.archive.query()
  201. }
  202. });
  203. converse.listen.on('serviceDiscovered', function (feature) {
  204. if (feature.get('var') === converse.env.Strophe.NS.MAM) {
  205. converse.archive.query()
  206. }
  207. });
  208. **Requesting all archived messages for a particular contact or room**
  209. To query for messages sent between the current user and another user or room,
  210. the query options need to contain the the JID (Jabber ID) of the user or
  211. room under the ``with`` key.
  212. .. code-block:: javascript
  213. converse.plugins.add('myplugin', {
  214. initialize: function () {
  215. // For a particular user
  216. this._converse.api.archive.query({'with': 'john@doe.net'}, callback, errback);)
  217. // For a particular room
  218. this._converse.api.archive.query({'with': 'discuss@conference.doglovers.net'}, callback, errback);)
  219. }
  220. });
  221. **Requesting all archived messages before or after a certain date**
  222. The ``start`` and ``end`` parameters are used to query for messages
  223. within a certain timeframe. The passed in date values may either be ISO8601
  224. formatted date strings, or JavaScript Date objects.
  225. .. code-block:: javascript
  226. converse.plugins.add('myplugin', {
  227. initialize: function () {
  228. var options = {
  229. 'with': 'john@doe.net',
  230. 'start': '2010-06-07T00:00:00Z',
  231. 'end': '2010-07-07T13:23:54Z'
  232. };
  233. this._converse.api.archive.query(options, callback, errback);
  234. }
  235. });
  236. **Limiting the amount of messages returned**
  237. The amount of returned messages may be limited with the ``max`` parameter.
  238. By default, the messages are returned from oldest to newest.
  239. .. code-block:: javascript
  240. converse.plugins.add('myplugin', {
  241. initialize: function () {
  242. // Return maximum 10 archived messages
  243. this._converse.api.archive.query({'with': 'john@doe.net', 'max':10}, callback, errback);
  244. }
  245. });
  246. **Paging forwards through a set of archived messages**
  247. When limiting the amount of messages returned per query, you might want to
  248. repeatedly make a further query to fetch the next batch of messages.
  249. To simplify this usecase for you, the callback method receives not only an array
  250. with the returned archived messages, but also a special RSM (*Result Set
  251. Management*) object which contains the query parameters you passed in, as well
  252. as two utility methods ``next``, and ``previous``.
  253. When you call one of these utility methods on the returned RSM object, and then
  254. pass the result into a new query, you'll receive the next or previous batch of
  255. archived messages. Please note, when calling these methods, pass in an integer
  256. to limit your results.
  257. .. code-block:: javascript
  258. converse.plugins.add('myplugin', {
  259. initialize: function () {
  260. var _converse = this._converse;
  261. var callback = function (messages, rsm) {
  262. // Do something with the messages, like showing them in your webpage.
  263. // ...
  264. // You can now use the returned "rsm" object, to fetch the next batch of messages:
  265. _converse.api.archive.query(rsm.next(10), callback, errback))
  266. }
  267. _converse.api.archive.query({'with': 'john@doe.net', 'max':10}, callback, errback);
  268. }
  269. });
  270. **Paging backwards through a set of archived messages**
  271. To page backwards through the archive, you need to know the UID of the message
  272. which you'd like to page backwards from and then pass that as value for the
  273. ``before`` parameter. If you simply want to page backwards from the most recent
  274. message, pass in the ``before`` parameter with an empty string value ``''``.
  275. .. code-block:: javascript
  276. converse.plugins.add('myplugin', {
  277. initialize: function () {
  278. var _converse = this._converse;
  279. _converse.api.archive.query({'before': '', 'max':5}, function (message, rsm) {
  280. // Do something with the messages, like showing them in your webpage.
  281. // ...
  282. // You can now use the returned "rsm" object, to fetch the previous batch of messages:
  283. rsm.previous(5); // Call previous method, to update the object's parameters,
  284. // passing in a limit value of 5.
  285. // Now we query again, to get the previous batch.
  286. _converse.api.archive.query(rsm, callback, errback);
  287. }
  288. }
  289. });
  290. The **connection** grouping
  291. ---------------------------
  292. This grouping collects API functions related to the XMPP connection.
  293. connected
  294. ~~~~~~~~~
  295. A boolean attribute (i.e. not a callable) which is set to `true` or `false` depending
  296. on whether there is an established connection.
  297. disconnect
  298. ~~~~~~~~~~
  299. Terminates the connection.
  300. The **disco** grouping
  301. ----------------------
  302. This grouping collects API functions related to `service discovery
  303. <https://xmpp.org/extensions/xep-0030.html>`_.
  304. The **disco.own** grouping
  305. ~~~~~~~~~~~~~~~~~~~~~~~~~~
  306. The **disco.own.features** grouping
  307. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  308. add
  309. ***
  310. Paramters:
  311. * (String) name
  312. get
  313. ***
  314. Returns all of the identities registered for this client (i.e. instance of Converse).
  315. .. code-block:: javascript
  316. const identities = _converse.api.disco.own.identities.get();
  317. The **disco.own.identities** grouping
  318. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  319. add
  320. ***
  321. Paramters:
  322. * (String) category
  323. * (String) type
  324. * (String) name
  325. * (String) lang
  326. Lets you add new identities for this client (i.e. instance of Converse).
  327. .. code-block:: javascript
  328. _converse.api.disco.own.identities.add('client', 'web', 'Converse');
  329. get
  330. ***
  331. Returns all of the identities registered for this client (i.e. instance of Converse).
  332. .. code-block:: javascript
  333. const identities = _converse.api.disco.own.identities.get();
  334. clear
  335. *****
  336. Clears all previously set identities.
  337. getIdentity
  338. ~~~~~~~~~~~
  339. Paramters:
  340. * (String) category
  341. * (String) type
  342. * (String) entity JID
  343. Get the identity (with the given category and type) for a given disco entity.
  344. For example, when determining support for PEP (personal eventing protocol), you
  345. want to know whether the user's own JID has an identity with
  346. ``category='pubsub'`` and ``type='pep'`` as explained in this section of
  347. XEP-0163: https://xmpp.org/extensions/xep-0163.html#support
  348. .. code-block:: javascript
  349. converse.plugins.add('myplugin', {
  350. initialize: function () {
  351. _converse.api.disco.getIdentity('pubsub', 'pep', _converse.bare_jid).then(
  352. function (identity) {
  353. if (_.isNil(identity)) {
  354. // The entity DOES NOT have this identity
  355. } else {
  356. // The entity DOES have this identity
  357. }
  358. }
  359. ).catch(_.partial(_converse.log, _, Strophe.LogLevel.FATAL));
  360. }
  361. });
  362. supports
  363. ~~~~~~~~
  364. Used to determine whether an entity supports a given feature.
  365. Returns a `Promise` which, when resolved, returns a map/object with keys
  366. `supported` (a boolean) and `feature` which is a `Backbone.Model <http://backbonejs.org/#Model>`_.
  367. .. code-block:: javascript
  368. converse.plugins.add('myplugin', {
  369. initialize: function () {
  370. _converse.api.disco.supports(Strophe.NS.MAM, _converse.bare_jid)
  371. .then(value => {
  372. // `value` is a map with two keys, `supported` and `feature`.
  373. if (value.supported) {
  374. // The feature is supported
  375. } else {
  376. // The feature is not supported
  377. }
  378. }).catch(() => {
  379. _converse.log(
  380. "Error or timeout while checking for feature support",
  381. Strophe.LogLevel.ERROR
  382. );
  383. });
  384. }
  385. });
  386. The **user** grouping
  387. ---------------------
  388. This grouping collects API functions related to the current logged in user.
  389. jid
  390. ~~~
  391. Returns the current user's full JID (Jabber ID).
  392. .. code-block:: javascript
  393. converse.plugins.add('myplugin', {
  394. initialize: function () {
  395. alert(this._converse.api.user.jid());
  396. }
  397. });
  398. login
  399. ~~~~~
  400. Logs the user in. This method can accept a map with the credentials, like this:
  401. .. code-block:: javascript
  402. converse.plugins.add('myplugin', {
  403. initialize: function () {
  404. this._converse.api.user.login({
  405. 'jid': 'dummy@example.com',
  406. 'password': 'secret'
  407. });
  408. }
  409. });
  410. or it can be called without any parameters, in which case Converse will try
  411. to log the user in by calling the `prebind_url` or `credentials_url` depending
  412. on whether prebinding is used or not.
  413. logout
  414. ~~~~~~
  415. Log the user out of the current XMPP session.
  416. .. code-block:: javascript
  417. converse.plugins.add('myplugin', {
  418. initialize: function () {
  419. this._converse.api.user.logout();
  420. }
  421. });
  422. The **status** sub-grouping
  423. ~~~~~~~~~~~~~~~~~~~~~~~~~~~
  424. Set and get the user's chat status, also called their *availability*.
  425. get
  426. ^^^
  427. Return the current user's availability status:
  428. .. code-block:: javascript
  429. converse.plugins.add('myplugin', {
  430. initialize: function () {
  431. alert(this._converse.api.user.status.get()); // For example "dnd"
  432. }
  433. });
  434. set
  435. ^^^
  436. The user's status can be set to one of the following values:
  437. * **away**
  438. * **dnd**
  439. * **offline**
  440. * **online**
  441. * **unavailable**
  442. * **xa**
  443. For example:
  444. .. code-block:: javascript
  445. converse.plugins.add('myplugin', {
  446. initialize: function () {
  447. this._converse.api.user.status.set('dnd');
  448. }
  449. });
  450. Because the user's availability is often set together with a custom status
  451. message, this method also allows you to pass in a status message as a
  452. second parameter:
  453. .. code-block:: javascript
  454. converse.plugins.add('myplugin', {
  455. initialize: function () {
  456. this._converse.api.user.status.set('dnd', 'In a meeting');
  457. }
  458. });
  459. The **message** sub-grouping
  460. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  461. The ``user.status.message`` sub-grouping exposes methods for setting and
  462. retrieving the user's custom status message.
  463. .. code-block:: javascript
  464. converse.plugins.add('myplugin', {
  465. initialize: function () {
  466. this._converse.api.user.status.message.set('In a meeting');
  467. // Returns "In a meeting"
  468. return this._converse.api.user.status.message.get();
  469. }
  470. });
  471. The **contacts** grouping
  472. -------------------------
  473. get
  474. ~~~
  475. This method is used to retrieve roster contacts.
  476. To get a single roster contact, call the method with the contact's JID (Jabber ID):
  477. .. code-block:: javascript
  478. converse.plugins.add('myplugin', {
  479. initialize: function () {
  480. var _converse = this._converse;
  481. _converse.api.listen.on('rosterContactsFetched', function () {
  482. var contact = _converse.api.contacts.get('buddy@example.com')
  483. });
  484. }
  485. });
  486. To get multiple contacts, pass in an array of JIDs:
  487. .. code-block:: javascript
  488. converse.plugins.add('myplugin', {
  489. initialize: function () {
  490. var _converse = this._converse;
  491. _converse.api.listen.on('rosterContactsFetched', function () {
  492. var contacts = _converse.api.contacts.get(
  493. ['buddy1@example.com', 'buddy2@example.com']
  494. )
  495. });
  496. }
  497. });
  498. To return all contacts, simply call ``get`` without any parameters:
  499. .. code-block:: javascript
  500. converse.plugins.add('myplugin', {
  501. initialize: function () {
  502. var _converse = this._converse;
  503. _converse.api.listen.on('rosterContactsFetched', function () {
  504. var contacts = _converse.api.contacts.get();
  505. });
  506. }
  507. });
  508. The returned roster contact is a `Backbone.Model <http://backbonejs.org/#Model>`_ of type _converse.RosterContacts.
  509. It has the following attributes (which should be accessed via `get <http://backbonejs.org/#Model-get>`_).
  510. +----------------+-----------------------------------------------------------------------------------------------------------------+
  511. | Attribute | |
  512. +================+=================================================================================================================+
  513. | ask | If ask === 'subscribe', then we have asked this person to be our chat buddy. |
  514. +----------------+-----------------------------------------------------------------------------------------------------------------+
  515. | fullname | The person's full name. |
  516. +----------------+-----------------------------------------------------------------------------------------------------------------+
  517. | jid | The person's Jabber/XMPP username. |
  518. +----------------+-----------------------------------------------------------------------------------------------------------------+
  519. | requesting | If true, then this person is asking to be our chat buddy. |
  520. +----------------+-----------------------------------------------------------------------------------------------------------------+
  521. | subscription | The subscription state between the current user and this chat buddy. Can be `none`, `to`, `from` or `both`. |
  522. +----------------+-----------------------------------------------------------------------------------------------------------------+
  523. | id | A unique id, same as the jid. |
  524. +----------------+-----------------------------------------------------------------------------------------------------------------+
  525. | chat_status | The person's chat status. Can be `online`, `offline`, `busy`, `xa` (extended away) or `away`. |
  526. +----------------+-----------------------------------------------------------------------------------------------------------------+
  527. | user_id | The user id part of the JID (the part before the `@`). |
  528. +----------------+-----------------------------------------------------------------------------------------------------------------+
  529. | resources | The known resources for this chat buddy. Each resource denotes a separate and connected chat client. |
  530. +----------------+-----------------------------------------------------------------------------------------------------------------+
  531. | groups | The roster groups in which this chat buddy was placed. |
  532. +----------------+-----------------------------------------------------------------------------------------------------------------+
  533. | status | Their human readable custom status message. |
  534. +----------------+-----------------------------------------------------------------------------------------------------------------+
  535. | image_type | The image's file type. |
  536. +----------------+-----------------------------------------------------------------------------------------------------------------+
  537. | image | The Base64 encoded image data. |
  538. +----------------+-----------------------------------------------------------------------------------------------------------------+
  539. | url | The buddy's website URL, as specified in their VCard data. |
  540. +----------------+-----------------------------------------------------------------------------------------------------------------+
  541. | vcard_updated | When last the buddy's VCard was updated. |
  542. +----------------+-----------------------------------------------------------------------------------------------------------------+
  543. add
  544. ~~~
  545. Add a contact.
  546. Provide the JID of the contact you want to add:
  547. .. code-block:: javascript
  548. _converse.api.contacts.add('buddy@example.com')
  549. You may also provide the fullname. If not present, we use the jid as fullname:
  550. .. code-block:: javascript
  551. _converse.api.contacts.add('buddy@example.com', 'Buddy')
  552. The **chats** grouping
  553. ----------------------
  554. Note, for MUC chatrooms, you need to use the "rooms" grouping instead.
  555. get
  556. ~~~
  557. Returns an object representing a chat. The chat should already be open.
  558. To return a single chat, provide the JID of the contact you're chatting
  559. with in that chat:
  560. .. code-block:: javascript
  561. _converse.api.chats.get('buddy@example.com')
  562. To return an array of chats, provide an array of JIDs:
  563. .. code-block:: javascript
  564. _converse.api.chats.get(['buddy1@example.com', 'buddy2@example.com'])
  565. To return all open chats, call the method without any JIDs::
  566. _converse.api.chats.get()
  567. open
  568. ~~~~
  569. Opens a new chat.
  570. It returns an promise which will resolve with a `Backbone.Model <https://backbonejs.org/#Model>`_ representing the chat.
  571. Note that converse doesn't allow opening chats with users who aren't in your roster
  572. (unless you have set :ref:`allow_non_roster_messaging` to ``true``).
  573. These two events fire only once per session, so they're also available as promises.
  574. So, to open a single chat:
  575. .. code-block:: javascript
  576. converse.plugins.add('myplugin', {
  577. initialize: function() {
  578. var _converse = this._converse;
  579. // Note, buddy@example.org must be in your contacts roster!
  580. _converse.api.chats.open('buddy@example.com').then((chat) => {
  581. // Now you can do something with the chat model
  582. });
  583. }
  584. });
  585. To return an array of chats, provide an array of JIDs:
  586. .. code-block:: javascript
  587. converse.plugins.add('myplugin', {
  588. initialize: function () {
  589. var _converse = this._converse;
  590. // Note, these users must first be in your contacts roster!
  591. _converse.api.chats.open(['buddy1@example.com', 'buddy2@example.com']).then((chats) => {
  592. // Now you can do something with the chat models
  593. });
  594. }
  595. });
  596. The **chatviews** grouping
  597. --------------------------
  598. .. note:: This is only for private chats.
  599. get
  600. ~~~
  601. Returns a `Backbone.View <http://backbonejs.org/#View>`_ of type _converse.ChatBoxView.
  602. The chat should already be open, otherwise `undefined` will be returned.
  603. To return a single view, provide the JID of the contact:
  604. .. code-block:: javascript
  605. _converse.api.chatviews.get('buddy@example.com')
  606. To return an array of views, provide an array of JIDs:
  607. .. code-block:: javascript
  608. _converse.api.chatviews.get(['buddy1@example.com', 'buddy2@example.com'])
  609. .. _`listen-grouping`:
  610. The **listen** grouping
  611. -----------------------
  612. Converse emits events to which you can subscribe to.
  613. Concerning events, the following methods are available under the "listen"
  614. grouping:
  615. * **on(eventName, callback, [context])**:
  616. Calling the ``on`` method allows you to subscribe to an event.
  617. Every time the event fires, the callback method specified by ``callback`` will be
  618. called.
  619. Parameters:
  620. * ``eventName`` is the event name as a string.
  621. * ``callback`` is the callback method to be called when the event is emitted.
  622. * ``context`` (optional), the value of the `this` parameter for the callback.
  623. For example:
  624. .. code-block:: javascript
  625. _converse.api.listen.on('message', function (messageXML) { ... });
  626. * **once(eventName, callback, [context])**:
  627. Calling the ``once`` method allows you to listen to an event
  628. exactly once.
  629. Parameters:
  630. * ``eventName`` is the event name as a string.
  631. * ``callback`` is the callback method to be called when the event is emitted.
  632. * ``context`` (optional), the value of the `this` parameter for the callback.
  633. For example:
  634. .. code-block:: javascript
  635. _converse.api.listen.once('message', function (messageXML) { ... });
  636. * **not(eventName, callback)**
  637. To stop listening to an event, you can use the ``not`` method.
  638. Parameters:
  639. * ``eventName`` is the event name as a string.
  640. * ``callback`` refers to the function that is to be no longer executed.
  641. For example:
  642. .. code-block:: javascript
  643. _converse.api.listen.not('message', function (messageXML) { ... });
  644. The **rooms** grouping
  645. ----------------------
  646. get
  647. ~~~
  648. Returns an object representing a multi user chat (room).
  649. It takes 3 parameters:
  650. * the room JID (if not specified, all rooms will be returned).
  651. * a map (object) containing any extra room attributes For example, if you want
  652. to specify the nickname, use ``{'nick': 'bloodninja'}``. Previously (before
  653. version 1.0.7, the second parameter only accepted the nickname (as a string
  654. value). This is currently still accepted, but then you can't pass in any
  655. other room attributes. If the nickname is not specified then the node part of
  656. the user's JID will be used.
  657. * a boolean, indicating whether the room should be created if not found (default: `false`)
  658. .. code-block:: javascript
  659. converse.plugins.add('myplugin', {
  660. initialize: function () {
  661. var _converse = this._converse;
  662. _converse.api.waitUntil('roomsAutoJoined').then(function () {
  663. var create_if_not_found = true;
  664. this._converse.api.rooms.get(
  665. 'group@muc.example.com',
  666. {'nick': 'dread-pirate-roberts'},
  667. create_if_not_found
  668. )
  669. });
  670. }
  671. });
  672. open
  673. ~~~~
  674. Opens a multi user chat and returns an object representing it.
  675. Similar to the ``chats.get`` API.
  676. It takes 2 parameters:
  677. * The room JID or JIDs (if not specified, all currently open rooms will be returned).
  678. * A map (object) containing any extra room attributes. For example, if you want
  679. to specify the nickname, use ``{'nick': 'bloodninja'}``.
  680. To open a single multi user chat, provide the JID of the room:
  681. .. code-block:: javascript
  682. converse.plugins.add('myplugin', {
  683. initialize: function () {
  684. this._converse.api.rooms.open('group@muc.example.com')
  685. }
  686. });
  687. To return an array of rooms, provide an array of room JIDs:
  688. .. code-block:: javascript
  689. converse.plugins.add('myplugin', {
  690. initialize: function () {
  691. this._converse.api.rooms.open(['group1@muc.example.com', 'group2@muc.example.com'])
  692. }
  693. });
  694. To setup a custom nickname when joining the room, provide the optional nick argument:
  695. .. code-block:: javascript
  696. converse.plugins.add('myplugin', {
  697. initialize: function () {
  698. this._converse.api.rooms.open('group@muc.example.com', {'nick': 'mycustomnick'})
  699. }
  700. });
  701. Room attributes that may be passed in:
  702. * *nick*: The nickname to be used
  703. * *auto_configure*: A boolean, indicating whether the room should be configured
  704. automatically or not. If set to ``true``, then it makes sense to pass in
  705. configuration settings.
  706. * *roomconfig*: A map of configuration settings to be used when the room gets
  707. configured automatically. Currently it doesn't make sense to specify
  708. ``roomconfig`` values if ``auto_configure`` is set to ``false``.
  709. For a list of configuration values that can be passed in, refer to these values
  710. in the `XEP-0045 MUC specification <https://xmpp.org/extensions/xep-0045.html#registrar-formtype-owner>`_.
  711. The values should be named without the ``muc#roomconfig_`` prefix.
  712. * *maximize*: A boolean, indicating whether minimized rooms should also be
  713. maximized, when opened. Set to ``false`` by default.
  714. * *bring_to_foreground*: A boolean indicating whether the room should be
  715. brought to the foreground and therefore replace the currently shown chat.
  716. If there is no chat currently open, then this option is ineffective.
  717. For example, opening a room with a specific default configuration:
  718. .. code-block:: javascript
  719. converse.plugins.add('myplugin', {
  720. initialize: function () {
  721. this._converse.api.rooms.open(
  722. 'myroom@conference.example.org',
  723. { 'nick': 'coolguy69',
  724. 'auto_configure': true,
  725. 'roomconfig': {
  726. 'changesubject': false,
  727. 'membersonly': true,
  728. 'persistentroom': true,
  729. 'publicroom': true,
  730. 'roomdesc': 'Comfy room for hanging out',
  731. 'whois': 'anyone'
  732. }
  733. },
  734. true
  735. );
  736. }
  737. });
  738. .. note:: `multi-list` configuration values are not yet supported.
  739. close
  740. ~~~~~
  741. Lets you close open chatrooms. You can call this method without any arguments
  742. to close all open chatrooms, or you can specify a single JID or an array of
  743. JIDs.
  744. .. _`promises-grouping`:
  745. The **promises** grouping
  746. -------------------------
  747. Converse and its plugins emit various events which you can listen to via the
  748. :ref:`listen-grouping`.
  749. Some of these events are also available as `ES2015 Promises <http://es6-features.org/#PromiseUsage>`_,
  750. although not all of them could logically act as promises, since some events
  751. might be fired multpile times whereas promises are to be resolved (or
  752. rejected) only once.
  753. The core events, which are also promises are:
  754. * :ref:`cachedRoster`
  755. * :ref:`chatBoxesFetched`
  756. * :ref:`pluginsInitialized`
  757. * :ref:`roster`
  758. * :ref:`rosterContactsFetched`
  759. * :ref:`rosterGroupsFetched`
  760. * :ref:`rosterInitialized`
  761. * :ref:`statusInitialized`
  762. * :ref:`roomsPanelRendered` (only via the `converse-muc` plugin)
  763. The various plugins might also provide promises, and they do this by using the
  764. ``promises.add`` api method.
  765. add(promises)
  766. ~~~~~~~~~~~~~
  767. By calling ``promises.add``, a new promise is made available for other code or
  768. plugins to depend on via the ``_converse.api.waitUntil`` method.
  769. This method accepts either a string or list of strings which specify the
  770. promise(s) to be added.
  771. For example:
  772. .. code-block:: javascript
  773. converse.plugins.add('myplugin', {
  774. initialize: function () {
  775. this._converse.api.promises.add('foo-completed');
  776. }
  777. });
  778. Generally, it's the responsibility of the plugin which adds the promise to
  779. also resolve it.
  780. This is done by calling ``_converse.api.emit``, which not only resolve the
  781. promise, but also emit an event with the same name (which can be listened to
  782. via ``_converse.api.listen``).
  783. For example:
  784. .. code-block:: javascript
  785. _converse.api.trigger('foo-completed');
  786. The **settings** grouping
  787. -------------------------
  788. This grouping allows access to the configuration settings of Converse.
  789. .. _`settings-update`:
  790. update(settings)
  791. ~~~~~~~~~~~~~~~~
  792. Allows new configuration settings to be specified, or new default values for
  793. existing configuration settings to be specified.
  794. For example:
  795. .. code-block:: javascript
  796. converse.plugins.add('myplugin', {
  797. initialize: function () {
  798. this._converse.api.settings.update({
  799. 'enable_foo': true
  800. });
  801. }
  802. });
  803. The user can then override the default value of the configuration setting when
  804. calling `converse.initialize`.
  805. For example:
  806. .. code-block:: javascript
  807. converse.initialize({
  808. 'enable_foo': false
  809. });
  810. get(key)
  811. ~~~~~~~~
  812. Returns the value of the particular configuration setting. For example:
  813. .. code-block:: javascript
  814. converse.plugins.add('myplugin', {
  815. initialize: function () {
  816. // default value would be false;
  817. alert(this._converse.api.settings.get("play_sounds"));
  818. }
  819. });
  820. set(key, value) or set(object)
  821. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  822. Set one or many configuration settings. For example:
  823. .. code-block:: javascript
  824. converse.plugins.add('myplugin', {
  825. initialize: function () {
  826. this._converse.api.settings.set("play_sounds", true);
  827. }
  828. });
  829. or :
  830. .. code-block:: javascript
  831. converse.plugins.add('myplugin', {
  832. initialize: function () {
  833. this._converse.api.settings.set({
  834. "play_sounds", true,
  835. "hide_offline_users" true
  836. });
  837. }
  838. });
  839. Note, this is not an alternative to calling ``converse.initialize``, which still needs
  840. to be called. Generally, you'd use this method after Converse is already
  841. running and you want to change the configuration on-the-fly.
  842. The **tokens** grouping
  843. -----------------------
  844. get
  845. ~~~
  846. Returns a token, either the RID or SID token depending on what's asked for.
  847. Example:
  848. .. code-block:: javascript
  849. converse.plugins.add('myplugin', {
  850. initialize: function () {
  851. alert(this._converse.api.tokens.get('rid'));
  852. }
  853. });
  854. The **vcard** grouping
  855. -----------------------
  856. get
  857. ~~~
  858. Parameters:
  859. * ``model`` either a `Backbone.Model` instance, or a string JID.
  860. * ``force`` (optional), a boolean indicating whether the vcard should be
  861. fetched even if it's been fetched before.
  862. Returns a Promise which results with the VCard data for a particular JID or for
  863. a `Backbone.Model` instance which represents an entity with a JID (such as a roster contact,
  864. chat or chatroom occupant).
  865. If a `Backbone.Model` instance is passed in, then it must have either a `jid`
  866. attribute or a `muc_jid` attribute.
  867. Example:
  868. .. code-block:: javascript
  869. converse.plugins.add('myplugin', {
  870. initialize: function () {
  871. _converse.api.waitUntil('rosterContactsFetched').then(() => {
  872. this._converse.api.vcard.get('someone@example.org').then(
  873. (vcard) => {
  874. // Do something with the vcard...
  875. }
  876. );
  877. });
  878. }
  879. });
  880. set
  881. ~~~
  882. Parameters:
  883. * ``data`` a map of VCard keys and values
  884. Enables setting new values for a VCard.
  885. Example:
  886. .. code-block:: javascript
  887. converse.plugins.add('myplugin', {
  888. initialize: function () {
  889. _converse.api.waitUntil('rosterContactsFetched').then(() => {
  890. this._converse.api.vcard.set({
  891. 'jid': 'someone@example.org',
  892. 'fn': 'Someone Somewhere',
  893. 'nickname': 'someone'
  894. }).then(() => {
  895. // Succes
  896. }).catch(() => {
  897. // Failure
  898. }).
  899. });
  900. }
  901. });
  902. update
  903. ~~~~~~
  904. Parameters:
  905. * ``model`` a `Backbone.Model` instance
  906. * ``force`` (optional), a boolean indicating whether the vcard should be
  907. fetched again even if it's been fetched before.
  908. Fetches the VCard associated with a particular `Backbone.Model` instance
  909. (by using its `jid` or `muc_jid` attribute) and then updates the model with the
  910. returned VCard data.
  911. Returns a promise;
  912. Example:
  913. .. code-block:: javascript
  914. converse.plugins.add('myplugin', {
  915. initialize: function () {
  916. _converse.api.waitUntil('rosterContactsFetched').then(() => {
  917. const chatbox = _converse.chatboxes.getChatBox('someone@example.org');
  918. _converse.api.vcard.update(chatbox);
  919. });
  920. }
  921. });