developer_api.rst 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367
  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.emit('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).then(
  371. function (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. },
  379. function () { // Error
  380. _converse.log(
  381. "Error or timeout while checking for feature support",
  382. Strophe.LogLevel.ERROR
  383. );
  384. }
  385. ).catch((msg) => {
  386. _converse.log(msg, Strophe.LogLevel.FATAL);
  387. });
  388. }
  389. });
  390. The **user** grouping
  391. ---------------------
  392. This grouping collects API functions related to the current logged in user.
  393. jid
  394. ~~~
  395. Return's the current user's full JID (Jabber ID).
  396. .. code-block:: javascript
  397. converse.plugins.add('myplugin', {
  398. initialize: function () {
  399. alert(this._converse.api.user.jid());
  400. }
  401. });
  402. login
  403. ~~~~~
  404. Logs the user in. This method can accept a map with the credentials, like this:
  405. .. code-block:: javascript
  406. converse.plugins.add('myplugin', {
  407. initialize: function () {
  408. this._converse.api.user.login({
  409. 'jid': 'dummy@example.com',
  410. 'password': 'secret'
  411. });
  412. }
  413. });
  414. or it can be called without any parameters, in which case Converse will try
  415. to log the user in by calling the `prebind_url` or `credentials_url` depending
  416. on whether prebinding is used or not.
  417. logout
  418. ~~~~~~
  419. Log the user out of the current XMPP session.
  420. .. code-block:: javascript
  421. converse.plugins.add('myplugin', {
  422. initialize: function () {
  423. this._converse.api.user.logout();
  424. }
  425. });
  426. The **status** sub-grouping
  427. ~~~~~~~~~~~~~~~~~~~~~~~~~~~
  428. Set and get the user's chat status, also called their *availability*.
  429. get
  430. ^^^
  431. Return the current user's availability status:
  432. .. code-block:: javascript
  433. converse.plugins.add('myplugin', {
  434. initialize: function () {
  435. alert(this._converse.api.user.status.get()); // For example "dnd"
  436. }
  437. });
  438. set
  439. ^^^
  440. The user's status can be set to one of the following values:
  441. * **away**
  442. * **dnd**
  443. * **offline**
  444. * **online**
  445. * **unavailable**
  446. * **xa**
  447. For example:
  448. .. code-block:: javascript
  449. converse.plugins.add('myplugin', {
  450. initialize: function () {
  451. this._converse.api.user.status.set('dnd');
  452. }
  453. });
  454. Because the user's availability is often set together with a custom status
  455. message, this method also allows you to pass in a status message as a
  456. second parameter:
  457. .. code-block:: javascript
  458. converse.plugins.add('myplugin', {
  459. initialize: function () {
  460. this._converse.api.user.status.set('dnd', 'In a meeting');
  461. }
  462. });
  463. The **message** sub-grouping
  464. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  465. The ``user.status.message`` sub-grouping exposes methods for setting and
  466. retrieving the user's custom status message.
  467. .. code-block:: javascript
  468. converse.plugins.add('myplugin', {
  469. initialize: function () {
  470. this._converse.api.user.status.message.set('In a meeting');
  471. // Returns "In a meeting"
  472. return this._converse.api.user.status.message.get();
  473. }
  474. });
  475. The **contacts** grouping
  476. -------------------------
  477. get
  478. ~~~
  479. This method is used to retrieve roster contacts.
  480. To get a single roster contact, call the method with the contact's JID (Jabber ID):
  481. .. code-block:: javascript
  482. converse.plugins.add('myplugin', {
  483. initialize: function () {
  484. var _converse = this._converse;
  485. _converse.api.listen.on('rosterContactsFetched', function () {
  486. var contact = _converse.api.contacts.get('buddy@example.com')
  487. });
  488. }
  489. });
  490. To get multiple contacts, pass in an array of JIDs:
  491. .. code-block:: javascript
  492. converse.plugins.add('myplugin', {
  493. initialize: function () {
  494. var _converse = this._converse;
  495. _converse.api.listen.on('rosterContactsFetched', function () {
  496. var contacts = _converse.api.contacts.get(
  497. ['buddy1@example.com', 'buddy2@example.com']
  498. )
  499. });
  500. }
  501. });
  502. To return all contacts, simply call ``get`` without any parameters:
  503. .. code-block:: javascript
  504. converse.plugins.add('myplugin', {
  505. initialize: function () {
  506. var _converse = this._converse;
  507. _converse.api.listen.on('rosterContactsFetched', function () {
  508. var contacts = _converse.api.contacts.get();
  509. });
  510. }
  511. });
  512. The returned roster contact is a `Backbone.Model <http://backbonejs.org/#Model>`_ of type _converse.RosterContacts.
  513. It has the following attributes (which should be accessed via `get <http://backbonejs.org/#Model-get>`_).
  514. +----------------+-----------------------------------------------------------------------------------------------------------------+
  515. | Attribute | |
  516. +================+=================================================================================================================+
  517. | ask | If ask === 'subscribe', then we have asked this person to be our chat buddy. |
  518. +----------------+-----------------------------------------------------------------------------------------------------------------+
  519. | fullname | The person's full name. |
  520. +----------------+-----------------------------------------------------------------------------------------------------------------+
  521. | jid | The person's Jabber/XMPP username. |
  522. +----------------+-----------------------------------------------------------------------------------------------------------------+
  523. | requesting | If true, then this person is asking to be our chat buddy. |
  524. +----------------+-----------------------------------------------------------------------------------------------------------------+
  525. | subscription | The subscription state between the current user and this chat buddy. Can be `none`, `to`, `from` or `both`. |
  526. +----------------+-----------------------------------------------------------------------------------------------------------------+
  527. | id | A unique id, same as the jid. |
  528. +----------------+-----------------------------------------------------------------------------------------------------------------+
  529. | chat_status | The person's chat status. Can be `online`, `offline`, `busy`, `xa` (extended away) or `away`. |
  530. +----------------+-----------------------------------------------------------------------------------------------------------------+
  531. | user_id | The user id part of the JID (the part before the `@`). |
  532. +----------------+-----------------------------------------------------------------------------------------------------------------+
  533. | resources | The known resources for this chat buddy. Each resource denotes a separate and connected chat client. |
  534. +----------------+-----------------------------------------------------------------------------------------------------------------+
  535. | groups | The roster groups in which this chat buddy was placed. |
  536. +----------------+-----------------------------------------------------------------------------------------------------------------+
  537. | status | Their human readable custom status message. |
  538. +----------------+-----------------------------------------------------------------------------------------------------------------+
  539. | image_type | The image's file type. |
  540. +----------------+-----------------------------------------------------------------------------------------------------------------+
  541. | image | The Base64 encoded image data. |
  542. +----------------+-----------------------------------------------------------------------------------------------------------------+
  543. | url | The buddy's website URL, as specified in their VCard data. |
  544. +----------------+-----------------------------------------------------------------------------------------------------------------+
  545. | vcard_updated | When last the buddy's VCard was updated. |
  546. +----------------+-----------------------------------------------------------------------------------------------------------------+
  547. add
  548. ~~~
  549. Add a contact.
  550. Provide the JID of the contact you want to add:
  551. .. code-block:: javascript
  552. _converse.api.contacts.add('buddy@example.com')
  553. You may also provide the fullname. If not present, we use the jid as fullname:
  554. .. code-block:: javascript
  555. _converse.api.contacts.add('buddy@example.com', 'Buddy')
  556. The **chats** grouping
  557. ----------------------
  558. Note, for MUC chatrooms, you need to use the "rooms" grouping instead.
  559. get
  560. ~~~
  561. Returns an object representing a chat. The chat should already be open.
  562. To return a single chat, provide the JID of the contact you're chatting
  563. with in that chat:
  564. .. code-block:: javascript
  565. _converse.api.chats.get('buddy@example.com')
  566. To return an array of chats, provide an array of JIDs:
  567. .. code-block:: javascript
  568. _converse.api.chats.get(['buddy1@example.com', 'buddy2@example.com'])
  569. To return all open chats, call the method without any JIDs::
  570. _converse.api.chats.get()
  571. open
  572. ~~~~
  573. Opens a new chat.
  574. It returns an promise which will resolve with a `Backbone.Model <https://backbonejs.org/#Model>`_ representing the chat.
  575. Note that converse doesn't allow opening chats with users who aren't in your roster
  576. (unless you have set :ref:`allow_non_roster_messaging` to ``true``).
  577. These two events fire only once per session, so they're also available as promises.
  578. So, to open a single chat:
  579. .. code-block:: javascript
  580. converse.plugins.add('myplugin', {
  581. initialize: function() {
  582. var _converse = this._converse;
  583. // Note, buddy@example.org must be in your contacts roster!
  584. _converse.api.chats.open('buddy@example.com').then((chat) => {
  585. // Now you can do something with the chat model
  586. });
  587. }
  588. });
  589. To return an array of chats, provide an array of JIDs:
  590. .. code-block:: javascript
  591. converse.plugins.add('myplugin', {
  592. initialize: function () {
  593. var _converse = this._converse;
  594. // Note, these users must first be in your contacts roster!
  595. _converse.api.chats.open(['buddy1@example.com', 'buddy2@example.com']).then((chats) => {
  596. // Now you can do something with the chat models
  597. });
  598. }
  599. });
  600. The **chatviews** grouping
  601. --------------------------
  602. .. note:: This is only for private chats.
  603. get
  604. ~~~
  605. Returns a `Backbone.View <http://backbonejs.org/#View>`_ of type _converse.ChatBoxView.
  606. The chat should already be open, otherwise `undefined` will be returned.
  607. To return a single view, provide the JID of the contact:
  608. .. code-block:: javascript
  609. _converse.api.chatviews.get('buddy@example.com')
  610. To return an array of views, provide an array of JIDs:
  611. .. code-block:: javascript
  612. _converse.api.chatviews.get(['buddy1@example.com', 'buddy2@example.com'])
  613. .. _`listen-grouping`:
  614. The **listen** grouping
  615. -----------------------
  616. Converse emits events to which you can subscribe from your own JavaScript.
  617. Concerning events, the following methods are available under the "listen"
  618. grouping:
  619. * **on(eventName, callback, [context])**:
  620. Calling the ``on`` method allows you to subscribe to an event.
  621. Every time the event fires, the callback method specified by ``callback`` will be
  622. called.
  623. Parameters:
  624. * ``eventName`` is the event name as a string.
  625. * ``callback`` is the callback method to be called when the event is emitted.
  626. * ``context`` (optional), the value of the `this` parameter for the callback.
  627. For example:
  628. .. code-block:: javascript
  629. _converse.api.listen.on('message', function (messageXML) { ... });
  630. * **once(eventName, callback, [context])**:
  631. Calling the ``once`` method allows you to listen to an event
  632. exactly once.
  633. Parameters:
  634. * ``eventName`` is the event name as a string.
  635. * ``callback`` is the callback method to be called when the event is emitted.
  636. * ``context`` (optional), the value of the `this` parameter for the callback.
  637. For example:
  638. .. code-block:: javascript
  639. _converse.api.listen.once('message', function (messageXML) { ... });
  640. * **not(eventName, callback)**
  641. To stop listening to an event, you can use the ``not`` method.
  642. Parameters:
  643. * ``eventName`` is the event name as a string.
  644. * ``callback`` refers to the function that is to be no longer executed.
  645. For example:
  646. .. code-block:: javascript
  647. _converse.api.listen.not('message', function (messageXML) { ... });
  648. The **rooms** grouping
  649. ----------------------
  650. get
  651. ~~~
  652. Returns an object representing a multi user chat (room).
  653. It takes 3 parameters:
  654. * the room JID (if not specified, all rooms will be returned).
  655. * a map (object) containing any extra room attributes For example, if you want
  656. to specify the nickname, use ``{'nick': 'bloodninja'}``. Previously (before
  657. version 1.0.7, the second parameter only accepted the nickname (as a string
  658. value). This is currently still accepted, but then you can't pass in any
  659. other room attributes. If the nickname is not specified then the node part of
  660. the user's JID will be used.
  661. * a boolean, indicating whether the room should be created if not found (default: `false`)
  662. .. code-block:: javascript
  663. converse.plugins.add('myplugin', {
  664. initialize: function () {
  665. var _converse = this._converse;
  666. _converse.api.waitUntil('roomsAutoJoined').then(function () {
  667. var create_if_not_found = true;
  668. this._converse.api.rooms.get(
  669. 'group@muc.example.com',
  670. {'nick': 'dread-pirate-roberts'},
  671. create_if_not_found
  672. )
  673. });
  674. }
  675. });
  676. open
  677. ~~~~
  678. Opens a multi user chat and returns an object representing it.
  679. Similar to the ``chats.get`` API.
  680. It takes 2 parameters:
  681. * The room JID or JIDs (if not specified, all currently open rooms will be returned).
  682. * A map (object) containing any extra room attributes. For example, if you want
  683. to specify the nickname, use ``{'nick': 'bloodninja'}``.
  684. To open a single multi user chat, provide the JID of the room:
  685. .. code-block:: javascript
  686. converse.plugins.add('myplugin', {
  687. initialize: function () {
  688. this._converse.api.rooms.open('group@muc.example.com')
  689. }
  690. });
  691. To return an array of rooms, provide an array of room JIDs:
  692. .. code-block:: javascript
  693. converse.plugins.add('myplugin', {
  694. initialize: function () {
  695. this._converse.api.rooms.open(['group1@muc.example.com', 'group2@muc.example.com'])
  696. }
  697. });
  698. To setup a custom nickname when joining the room, provide the optional nick argument:
  699. .. code-block:: javascript
  700. converse.plugins.add('myplugin', {
  701. initialize: function () {
  702. this._converse.api.rooms.open('group@muc.example.com', {'nick': 'mycustomnick'})
  703. }
  704. });
  705. Room attributes that may be passed in:
  706. * *nick*: The nickname to be used
  707. * *auto_configure*: A boolean, indicating whether the room should be configured
  708. automatically or not. If set to ``true``, then it makes sense to pass in
  709. configuration settings.
  710. * *roomconfig*: A map of configuration settings to be used when the room gets
  711. configured automatically. Currently it doesn't make sense to specify
  712. ``roomconfig`` values if ``auto_configure`` is set to ``false``.
  713. For a list of configuration values that can be passed in, refer to these values
  714. in the `XEP-0045 MUC specification <http://xmpp.org/extensions/xep-0045.html#registrar-formtype-owner>`_.
  715. The values should be named without the ``muc#roomconfig_`` prefix.
  716. * *maximize*: A boolean, indicating whether minimized rooms should also be
  717. maximized, when opened. Set to ``false`` by default.
  718. * *bring_to_foreground*: A boolean indicating whether the room should be
  719. brought to the foreground and therefore replace the currently shown chat.
  720. If there is no chat currently open, then this option is ineffective.
  721. For example, opening a room with a specific default configuration:
  722. .. code-block:: javascript
  723. converse.plugins.add('myplugin', {
  724. initialize: function () {
  725. this._converse.api.rooms.open(
  726. 'myroom@conference.example.org',
  727. { 'nick': 'coolguy69',
  728. 'auto_configure': true,
  729. 'roomconfig': {
  730. 'changesubject': false,
  731. 'membersonly': true,
  732. 'persistentroom': true,
  733. 'publicroom': true,
  734. 'roomdesc': 'Comfy room for hanging out',
  735. 'whois': 'anyone'
  736. }
  737. },
  738. true
  739. );
  740. }
  741. });
  742. .. note:: `multi-list` configuration values are not yet supported.
  743. close
  744. ~~~~~
  745. Lets you close open chatrooms. You can call this method without any arguments
  746. to close all open chatrooms, or you can specify a single JID or an array of
  747. JIDs.
  748. .. _`promises-grouping`:
  749. The **promises** grouping
  750. -------------------------
  751. Converse and its plugins emit various events which you can listen to via the
  752. :ref:`listen-grouping`.
  753. Some of these events are also available as `ES2015 Promises <http://es6-features.org/#PromiseUsage>`_,
  754. although not all of them could logically act as promises, since some events
  755. might be fired multpile times whereas promises are to be resolved (or
  756. rejected) only once.
  757. The core events, which are also promises are:
  758. * :ref:`cachedRoster`
  759. * :ref:`chatBoxesFetched`
  760. * :ref:`pluginsInitialized`
  761. * :ref:`roster`
  762. * :ref:`rosterContactsFetched`
  763. * :ref:`rosterGroupsFetched`
  764. * :ref:`rosterInitialized`
  765. * :ref:`statusInitialized`
  766. * :ref:`roomsPanelRendered` (only via the `converse-muc` plugin)
  767. The various plugins might also provide promises, and they do this by using the
  768. ``promises.add`` api method.
  769. add(promises)
  770. ~~~~~~~~~~~~~
  771. By calling ``promises.add``, a new promise is made available for other code or
  772. plugins to depend on via the ``_converse.api.waitUntil`` method.
  773. This method accepts either a string or list of strings which specify the
  774. promise(s) to be added.
  775. For example:
  776. .. code-block:: javascript
  777. converse.plugins.add('myplugin', {
  778. initialize: function () {
  779. this._converse.api.promises.add('foo-completed');
  780. }
  781. });
  782. Generally, it's the responsibility of the plugin which adds the promise to
  783. also resolve it.
  784. This is done by calling ``_converse.api.emit``, which not only resolve the
  785. promise, but also emit an event with the same name (which can be listened to
  786. via ``_converse.api.listen``).
  787. For example:
  788. .. code-block:: javascript
  789. _converse.api.emit('foo-completed');
  790. The **settings** grouping
  791. -------------------------
  792. This grouping allows access to the configuration settings of Converse.
  793. .. _`settings-update`:
  794. update(settings)
  795. ~~~~~~~~~~~~~~~~
  796. Allows new configuration settings to be specified, or new default values for
  797. existing configuration settings to be specified.
  798. For example:
  799. .. code-block:: javascript
  800. converse.plugins.add('myplugin', {
  801. initialize: function () {
  802. this._converse.api.settings.update({
  803. 'enable_foo': true
  804. });
  805. }
  806. });
  807. The user can then override the default value of the configuration setting when
  808. calling `converse.initialize`.
  809. For example:
  810. .. code-block:: javascript
  811. converse.initialize({
  812. 'enable_foo': false
  813. });
  814. get(key)
  815. ~~~~~~~~
  816. Returns the value of a configuration settings. For example:
  817. .. code-block:: javascript
  818. converse.plugins.add('myplugin', {
  819. initialize: function () {
  820. // default value would be false;
  821. alert(this._converse.api.settings.get("play_sounds"));
  822. }
  823. });
  824. set(key, value) or set(object)
  825. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  826. Set one or many configuration settings. For example:
  827. .. code-block:: javascript
  828. converse.plugins.add('myplugin', {
  829. initialize: function () {
  830. this._converse.api.settings.set("play_sounds", true);
  831. }
  832. });
  833. or :
  834. .. code-block:: javascript
  835. converse.plugins.add('myplugin', {
  836. initialize: function () {
  837. this._converse.api.settings.set({
  838. "play_sounds", true,
  839. "hide_offline_users" true
  840. });
  841. }
  842. });
  843. Note, this is not an alternative to calling ``converse.initialize``, which still needs
  844. to be called. Generally, you'd use this method after Converse is already
  845. running and you want to change the configuration on-the-fly.
  846. The **tokens** grouping
  847. -----------------------
  848. get
  849. ~~~
  850. Returns a token, either the RID or SID token depending on what's asked for.
  851. Example:
  852. .. code-block:: javascript
  853. converse.plugins.add('myplugin', {
  854. initialize: function () {
  855. alert(this._converse.api.tokens.get('rid'));
  856. }
  857. });
  858. The **vcard** grouping
  859. -----------------------
  860. get
  861. ~~~
  862. Parameters:
  863. * ``model`` either a `Backbone.Model` instance, or a string JID.
  864. * ``force`` (optional), a boolean indicating whether the vcard should be
  865. fetched even if it's been fetched before.
  866. Returns a Promise which results with the VCard data for a particular JID or for
  867. a `Backbone.Model` instance which represents an entity with a JID (such as a roster contact,
  868. chat or chatroom occupant).
  869. If a `Backbone.Model` instance is passed in, then it must have either a `jid`
  870. attribute or a `muc_jid` attribute.
  871. Example:
  872. .. code-block:: javascript
  873. converse.plugins.add('myplugin', {
  874. initialize: function () {
  875. _converse.api.waitUntil('rosterContactsFetched').then(() => {
  876. this._converse.api.vcard.get('someone@example.org').then(
  877. (vcard) => {
  878. // Do something with the vcard...
  879. }
  880. );
  881. });
  882. }
  883. });
  884. set
  885. ~~~
  886. Parameters:
  887. * ``data`` a map of VCard keys and values
  888. Enables setting new values for a VCard.
  889. Example:
  890. .. code-block:: javascript
  891. converse.plugins.add('myplugin', {
  892. initialize: function () {
  893. _converse.api.waitUntil('rosterContactsFetched').then(() => {
  894. this._converse.api.vcard.set({
  895. 'jid': 'someone@example.org',
  896. 'fn': 'Someone Somewhere',
  897. 'nickname': 'someone'
  898. }).then(() => {
  899. // Succes
  900. }).catch(() => {
  901. // Failure
  902. }).
  903. });
  904. }
  905. });
  906. update
  907. ~~~~~~
  908. Parameters:
  909. * ``model`` a `Backbone.Model` instance
  910. * ``force`` (optional), a boolean indicating whether the vcard should be
  911. fetched again even if it's been fetched before.
  912. Fetches the VCard associated with a particular `Backbone.Model` instance
  913. (by using its `jid` or `muc_jid` attribute) and then updates the model with the
  914. returned VCard data.
  915. Returns a promise;
  916. Example:
  917. .. code-block:: javascript
  918. converse.plugins.add('myplugin', {
  919. initialize: function () {
  920. _converse.api.waitUntil('rosterContactsFetched').then(() => {
  921. const chatbox = _converse.chatboxes.getChatBox('someone@example.org');
  922. _converse.api.vcard.update(chatbox);
  923. });
  924. }
  925. });