presence.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /*jshint sub:true*/
  2. /*eslint dot-notation: "off"*/
  3. (function (root, factory) {
  4. define([
  5. "jquery",
  6. "converse-core",
  7. "mock",
  8. "test-utils",
  9. "lodash"], factory);
  10. } (this, function ($, converse, mock, test_utils, _) {
  11. "use strict";
  12. var Strophe = converse.env.Strophe;
  13. var $iq = converse.env.$iq;
  14. var $pres = converse.env.$pres;
  15. // See: https://xmpp.org/rfcs/rfc3921.html
  16. describe("A sent presence stanza", function () {
  17. it("has a given priority", mock.initConverse(function (_converse) {
  18. var pres = _converse.xmppstatus.constructPresence('online', 'Hello world');
  19. expect(pres.toLocaleString()).toBe(
  20. "<presence xmlns='jabber:client'>"+
  21. "<status>Hello world</status>"+
  22. "<priority>0</priority>"+
  23. "</presence>"
  24. );
  25. _converse.priority = 2;
  26. pres = _converse.xmppstatus.constructPresence('away', 'Going jogging');
  27. expect(pres.toLocaleString()).toBe(
  28. "<presence xmlns='jabber:client'>"+
  29. "<show>away</show>"+
  30. "<status>Going jogging</status>"+
  31. "<priority>2</priority>"+
  32. "</presence>"
  33. );
  34. delete _converse.priority;
  35. pres = _converse.xmppstatus.constructPresence('dnd', 'Doing taxes');
  36. expect(pres.toLocaleString()).toBe(
  37. "<presence xmlns='jabber:client'>"+
  38. "<show>dnd</show>"+
  39. "<status>Doing taxes</status>"+
  40. "<priority>0</priority>"+
  41. "</presence>"
  42. );
  43. }));
  44. });
  45. describe("A received presence stanza", function () {
  46. it("has its priority taken into account", mock.initConverse(function (_converse) {
  47. test_utils.openControlBox();
  48. test_utils.createContacts(_converse, 'current'); // Create some contacts so that we can test positioning
  49. var contact_jid = mock.cur_names[8].replace(/ /g,'.').toLowerCase() + '@localhost';
  50. var contact = _converse.roster.get(contact_jid);
  51. var stanza = $(
  52. '<presence xmlns="jabber:client"'+
  53. ' to="dummy@localhost/converse.js-21770972"'+
  54. ' from="'+contact_jid+'/priority-1-resource">'+
  55. ' <priority>1</priority>'+
  56. ' <c xmlns="http://jabber.org/protocol/caps" hash="sha-1" ext="voice-v1 camera-v1 video-v1"'+
  57. ' ver="AcN1/PEN8nq7AHD+9jpxMV4U6YM=" node="http://pidgin.im/"/>'+
  58. ' <x xmlns="vcard-temp:x:update">'+
  59. ' <photo>ce51d94f7f22b87a21274abb93710b9eb7cc1c65</photo>'+
  60. ' </x>'+
  61. ' <delay xmlns="urn:xmpp:delay" stamp="2017-02-15T20:26:05Z" from="'+contact_jid+'/priority-1-resource"/>'+
  62. '</presence>');
  63. _converse.connection._dataRecv(test_utils.createRequest(stanza[0]));
  64. expect(contact.get('chat_status')).toBe('online');
  65. expect(_.keys(contact.get('resources')).length).toBe(1);
  66. expect(contact.get('resources')['priority-1-resource']['priority']).toBe(1);
  67. expect(contact.get('resources')['priority-1-resource']['status']).toBe('online');
  68. stanza = $(
  69. '<presence xmlns="jabber:client"'+
  70. ' to="dummy@localhost/converse.js-21770972"'+
  71. ' from="'+contact_jid+'/priority-0-resource">'+
  72. ' <status/>'+
  73. ' <priority>0</priority>'+
  74. ' <show>xa</show>'+
  75. ' <c xmlns="http://jabber.org/protocol/caps" ver="GyIX/Kpa4ScVmsZCxRBboJlLAYU=" hash="sha-1"'+
  76. ' node="http://www.igniterealtime.org/projects/smack/"/>'+
  77. ' <delay xmlns="urn:xmpp:delay" stamp="2017-02-15T17:02:24Z" from="'+contact_jid+'/priority-0-resource"/>'+
  78. '</presence>');
  79. _converse.connection._dataRecv(test_utils.createRequest(stanza[0]));
  80. expect(_converse.roster.get(contact_jid).get('chat_status')).toBe('online');
  81. expect(_.keys(contact.get('resources')).length).toBe(2);
  82. expect(contact.get('resources')['priority-0-resource']['priority']).toBe(0);
  83. expect(contact.get('resources')['priority-0-resource']['status']).toBe('xa');
  84. expect(contact.get('resources')['priority-1-resource']['priority']).toBe(1);
  85. expect(contact.get('resources')['priority-1-resource']['status']).toBe('online');
  86. stanza = $(
  87. '<presence xmlns="jabber:client"'+
  88. ' to="dummy@localhost/converse.js-21770972"'+
  89. ' from="'+contact_jid+'/priority-2-resource">'+
  90. ' <priority>2</priority>'+
  91. ' <show>dnd</show>'+
  92. '</presence>');
  93. _converse.connection._dataRecv(test_utils.createRequest(stanza[0]));
  94. expect(_converse.roster.get(contact_jid).get('chat_status')).toBe('dnd');
  95. expect(_.keys(contact.get('resources')).length).toBe(3);
  96. expect(contact.get('resources')['priority-0-resource']['priority']).toBe(0);
  97. expect(contact.get('resources')['priority-0-resource']['status']).toBe('xa');
  98. expect(contact.get('resources')['priority-1-resource']['priority']).toBe(1);
  99. expect(contact.get('resources')['priority-1-resource']['status']).toBe('online');
  100. expect(contact.get('resources')['priority-2-resource']['priority']).toBe(2);
  101. expect(contact.get('resources')['priority-2-resource']['status']).toBe('dnd');
  102. stanza = $(
  103. '<presence xmlns="jabber:client"'+
  104. ' to="dummy@localhost/converse.js-21770972"'+
  105. ' from="'+contact_jid+'/priority-3-resource">'+
  106. ' <priority>3</priority>'+
  107. ' <show>away</show>'+
  108. '</presence>');
  109. _converse.connection._dataRecv(test_utils.createRequest(stanza[0]));
  110. expect(_converse.roster.get(contact_jid).get('chat_status')).toBe('away');
  111. expect(_.keys(contact.get('resources')).length).toBe(4);
  112. expect(contact.get('resources')['priority-0-resource']['priority']).toBe(0);
  113. expect(contact.get('resources')['priority-0-resource']['status']).toBe('xa');
  114. expect(contact.get('resources')['priority-1-resource']['priority']).toBe(1);
  115. expect(contact.get('resources')['priority-1-resource']['status']).toBe('online');
  116. expect(contact.get('resources')['priority-2-resource']['priority']).toBe(2);
  117. expect(contact.get('resources')['priority-2-resource']['status']).toBe('dnd');
  118. expect(contact.get('resources')['priority-3-resource']['priority']).toBe(3);
  119. expect(contact.get('resources')['priority-3-resource']['status']).toBe('away');
  120. stanza = $(
  121. '<presence xmlns="jabber:client"'+
  122. ' to="dummy@localhost/converse.js-21770972"'+
  123. ' from="'+contact_jid+'/older-priority-1-resource">'+
  124. ' <priority>1</priority>'+
  125. ' <show>dnd</show>'+
  126. ' <delay xmlns="urn:xmpp:delay" stamp="2017-02-15T15:02:24Z" from="'+contact_jid+'/older-priority-1-resource"/>'+
  127. '</presence>');
  128. _converse.connection._dataRecv(test_utils.createRequest(stanza[0]));
  129. expect(_converse.roster.get(contact_jid).get('chat_status')).toBe('away');
  130. expect(_.keys(contact.get('resources')).length).toBe(5);
  131. expect(contact.get('resources')['older-priority-1-resource']['priority']).toBe(1);
  132. expect(contact.get('resources')['older-priority-1-resource']['status']).toBe('dnd');
  133. expect(contact.get('resources')['priority-0-resource']['priority']).toBe(0);
  134. expect(contact.get('resources')['priority-0-resource']['status']).toBe('xa');
  135. expect(contact.get('resources')['priority-1-resource']['priority']).toBe(1);
  136. expect(contact.get('resources')['priority-1-resource']['status']).toBe('online');
  137. expect(contact.get('resources')['priority-2-resource']['priority']).toBe(2);
  138. expect(contact.get('resources')['priority-2-resource']['status']).toBe('dnd');
  139. expect(contact.get('resources')['priority-3-resource']['priority']).toBe(3);
  140. expect(contact.get('resources')['priority-3-resource']['status']).toBe('away');
  141. stanza = $(
  142. '<presence xmlns="jabber:client"'+
  143. ' to="dummy@localhost/converse.js-21770972"'+
  144. ' type="unavailable"'+
  145. ' from="'+contact_jid+'/priority-3-resource">'+
  146. '</presence>');
  147. _converse.connection._dataRecv(test_utils.createRequest(stanza[0]));
  148. expect(_converse.roster.get(contact_jid).get('chat_status')).toBe('dnd');
  149. expect(_.keys(contact.get('resources')).length).toBe(4);
  150. expect(contact.get('resources')['priority-0-resource']['priority']).toBe(0);
  151. expect(contact.get('resources')['priority-0-resource']['status']).toBe('xa');
  152. expect(contact.get('resources')['priority-1-resource']['priority']).toBe(1);
  153. expect(contact.get('resources')['priority-1-resource']['status']).toBe('online');
  154. expect(contact.get('resources')['priority-2-resource']['priority']).toBe(2);
  155. expect(contact.get('resources')['priority-2-resource']['status']).toBe('dnd');
  156. expect(contact.get('resources')['older-priority-1-resource']['priority']).toBe(1);
  157. expect(contact.get('resources')['older-priority-1-resource']['status']).toBe('dnd');
  158. stanza = $(
  159. '<presence xmlns="jabber:client"'+
  160. ' to="dummy@localhost/converse.js-21770972"'+
  161. ' type="unavailable"'+
  162. ' from="'+contact_jid+'/priority-2-resource">'+
  163. '</presence>');
  164. _converse.connection._dataRecv(test_utils.createRequest(stanza[0]));
  165. expect(_converse.roster.get(contact_jid).get('chat_status')).toBe('online');
  166. expect(_.keys(contact.get('resources')).length).toBe(3);
  167. expect(contact.get('resources')['priority-0-resource']['priority']).toBe(0);
  168. expect(contact.get('resources')['priority-0-resource']['status']).toBe('xa');
  169. expect(contact.get('resources')['priority-1-resource']['priority']).toBe(1);
  170. expect(contact.get('resources')['priority-1-resource']['status']).toBe('online');
  171. expect(contact.get('resources')['older-priority-1-resource']['priority']).toBe(1);
  172. expect(contact.get('resources')['older-priority-1-resource']['status']).toBe('dnd');
  173. stanza = $(
  174. '<presence xmlns="jabber:client"'+
  175. ' to="dummy@localhost/converse.js-21770972"'+
  176. ' type="unavailable"'+
  177. ' from="'+contact_jid+'/priority-1-resource">'+
  178. '</presence>');
  179. _converse.connection._dataRecv(test_utils.createRequest(stanza[0]));
  180. expect(_converse.roster.get(contact_jid).get('chat_status')).toBe('dnd');
  181. expect(_.keys(contact.get('resources')).length).toBe(2);
  182. expect(contact.get('resources')['priority-0-resource']['priority']).toBe(0);
  183. expect(contact.get('resources')['priority-0-resource']['status']).toBe('xa');
  184. expect(contact.get('resources')['older-priority-1-resource']['priority']).toBe(1);
  185. expect(contact.get('resources')['older-priority-1-resource']['status']).toBe('dnd');
  186. stanza = $(
  187. '<presence xmlns="jabber:client"'+
  188. ' to="dummy@localhost/converse.js-21770972"'+
  189. ' type="unavailable"'+
  190. ' from="'+contact_jid+'/older-priority-1-resource">'+
  191. '</presence>');
  192. _converse.connection._dataRecv(test_utils.createRequest(stanza[0]));
  193. expect(_converse.roster.get(contact_jid).get('chat_status')).toBe('xa');
  194. expect(_.keys(contact.get('resources')).length).toBe(1);
  195. expect(contact.get('resources')['priority-0-resource']['priority']).toBe(0);
  196. expect(contact.get('resources')['priority-0-resource']['status']).toBe('xa');
  197. stanza = $(
  198. '<presence xmlns="jabber:client"'+
  199. ' to="dummy@localhost/converse.js-21770972"'+
  200. ' type="unavailable"'+
  201. ' from="'+contact_jid+'/priority-0-resource">'+
  202. '</presence>');
  203. _converse.connection._dataRecv(test_utils.createRequest(stanza[0]));
  204. expect(_converse.roster.get(contact_jid).get('chat_status')).toBe('offline');
  205. expect(_.keys(contact.get('resources')).length).toBe(0);
  206. }));
  207. });
  208. }));