presence.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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 received presence stanza", function () {
  17. it("has its priority taken into account", mock.initConverse(function (_converse) {
  18. test_utils.openControlBox();
  19. test_utils.createContacts(_converse, 'current'); // Create some contacts so that we can test positioning
  20. var contact_jid = mock.cur_names[8].replace(/ /g,'.').toLowerCase() + '@localhost';
  21. var contact = _converse.roster.get(contact_jid);
  22. var stanza = $(
  23. '<presence xmlns="jabber:client"'+
  24. ' to="dummy@localhost/converse.js-21770972"'+
  25. ' from="'+contact_jid+'/priority-1-resource">'+
  26. ' <priority>1</priority>'+
  27. ' <c xmlns="http://jabber.org/protocol/caps" hash="sha-1" ext="voice-v1 camera-v1 video-v1"'+
  28. ' ver="AcN1/PEN8nq7AHD+9jpxMV4U6YM=" node="http://pidgin.im/"/>'+
  29. ' <x xmlns="vcard-temp:x:update">'+
  30. ' <photo>ce51d94f7f22b87a21274abb93710b9eb7cc1c65</photo>'+
  31. ' </x>'+
  32. ' <delay xmlns="urn:xmpp:delay" stamp="2017-02-15T20:26:05Z" from="'+contact_jid+'/priority-1-resource"/>'+
  33. '</presence>');
  34. _converse.connection._dataRecv(test_utils.createRequest(stanza[0]));
  35. expect(contact.get('chat_status')).toBe('online');
  36. expect(_.keys(contact.get('resources')).length).toBe(1);
  37. expect(contact.get('resources')['priority-1-resource']['priority']).toBe(1);
  38. expect(contact.get('resources')['priority-1-resource']['status']).toBe('online');
  39. stanza = $(
  40. '<presence xmlns="jabber:client"'+
  41. ' to="dummy@localhost/converse.js-21770972"'+
  42. ' from="'+contact_jid+'/priority-0-resource">'+
  43. ' <status/>'+
  44. ' <priority>0</priority>'+
  45. ' <show>xa</show>'+
  46. ' <c xmlns="http://jabber.org/protocol/caps" ver="GyIX/Kpa4ScVmsZCxRBboJlLAYU=" hash="sha-1"'+
  47. ' node="http://www.igniterealtime.org/projects/smack/"/>'+
  48. ' <delay xmlns="urn:xmpp:delay" stamp="2017-02-15T17:02:24Z" from="'+contact_jid+'/priority-0-resource"/>'+
  49. '</presence>');
  50. _converse.connection._dataRecv(test_utils.createRequest(stanza[0]));
  51. expect(_converse.roster.get(contact_jid).get('chat_status')).toBe('online');
  52. expect(_.keys(contact.get('resources')).length).toBe(2);
  53. expect(contact.get('resources')['priority-0-resource']['priority']).toBe(0);
  54. expect(contact.get('resources')['priority-0-resource']['status']).toBe('xa');
  55. expect(contact.get('resources')['priority-1-resource']['priority']).toBe(1);
  56. expect(contact.get('resources')['priority-1-resource']['status']).toBe('online');
  57. stanza = $(
  58. '<presence xmlns="jabber:client"'+
  59. ' to="dummy@localhost/converse.js-21770972"'+
  60. ' from="'+contact_jid+'/priority-2-resource">'+
  61. ' <priority>2</priority>'+
  62. ' <show>dnd</show>'+
  63. '</presence>');
  64. _converse.connection._dataRecv(test_utils.createRequest(stanza[0]));
  65. expect(_converse.roster.get(contact_jid).get('chat_status')).toBe('dnd');
  66. expect(_.keys(contact.get('resources')).length).toBe(3);
  67. expect(contact.get('resources')['priority-0-resource']['priority']).toBe(0);
  68. expect(contact.get('resources')['priority-0-resource']['status']).toBe('xa');
  69. expect(contact.get('resources')['priority-1-resource']['priority']).toBe(1);
  70. expect(contact.get('resources')['priority-1-resource']['status']).toBe('online');
  71. expect(contact.get('resources')['priority-2-resource']['priority']).toBe(2);
  72. expect(contact.get('resources')['priority-2-resource']['status']).toBe('dnd');
  73. stanza = $(
  74. '<presence xmlns="jabber:client"'+
  75. ' to="dummy@localhost/converse.js-21770972"'+
  76. ' from="'+contact_jid+'/priority-3-resource">'+
  77. ' <priority>3</priority>'+
  78. ' <show>away</show>'+
  79. '</presence>');
  80. _converse.connection._dataRecv(test_utils.createRequest(stanza[0]));
  81. expect(_converse.roster.get(contact_jid).get('chat_status')).toBe('away');
  82. expect(_.keys(contact.get('resources')).length).toBe(4);
  83. expect(contact.get('resources')['priority-0-resource']['priority']).toBe(0);
  84. expect(contact.get('resources')['priority-0-resource']['status']).toBe('xa');
  85. expect(contact.get('resources')['priority-1-resource']['priority']).toBe(1);
  86. expect(contact.get('resources')['priority-1-resource']['status']).toBe('online');
  87. expect(contact.get('resources')['priority-2-resource']['priority']).toBe(2);
  88. expect(contact.get('resources')['priority-2-resource']['status']).toBe('dnd');
  89. expect(contact.get('resources')['priority-3-resource']['priority']).toBe(3);
  90. expect(contact.get('resources')['priority-3-resource']['status']).toBe('away');
  91. stanza = $(
  92. '<presence xmlns="jabber:client"'+
  93. ' to="dummy@localhost/converse.js-21770972"'+
  94. ' from="'+contact_jid+'/older-priority-1-resource">'+
  95. ' <priority>1</priority>'+
  96. ' <show>dnd</show>'+
  97. ' <delay xmlns="urn:xmpp:delay" stamp="2017-02-15T15:02:24Z" from="'+contact_jid+'/older-priority-1-resource"/>'+
  98. '</presence>');
  99. _converse.connection._dataRecv(test_utils.createRequest(stanza[0]));
  100. expect(_converse.roster.get(contact_jid).get('chat_status')).toBe('away');
  101. expect(_.keys(contact.get('resources')).length).toBe(5);
  102. expect(contact.get('resources')['older-priority-1-resource']['priority']).toBe(1);
  103. expect(contact.get('resources')['older-priority-1-resource']['status']).toBe('dnd');
  104. expect(contact.get('resources')['priority-0-resource']['priority']).toBe(0);
  105. expect(contact.get('resources')['priority-0-resource']['status']).toBe('xa');
  106. expect(contact.get('resources')['priority-1-resource']['priority']).toBe(1);
  107. expect(contact.get('resources')['priority-1-resource']['status']).toBe('online');
  108. expect(contact.get('resources')['priority-2-resource']['priority']).toBe(2);
  109. expect(contact.get('resources')['priority-2-resource']['status']).toBe('dnd');
  110. expect(contact.get('resources')['priority-3-resource']['priority']).toBe(3);
  111. expect(contact.get('resources')['priority-3-resource']['status']).toBe('away');
  112. stanza = $(
  113. '<presence xmlns="jabber:client"'+
  114. ' to="dummy@localhost/converse.js-21770972"'+
  115. ' type="unavailable"'+
  116. ' from="'+contact_jid+'/priority-3-resource">'+
  117. '</presence>');
  118. _converse.connection._dataRecv(test_utils.createRequest(stanza[0]));
  119. expect(_converse.roster.get(contact_jid).get('chat_status')).toBe('dnd');
  120. expect(_.keys(contact.get('resources')).length).toBe(4);
  121. expect(contact.get('resources')['priority-0-resource']['priority']).toBe(0);
  122. expect(contact.get('resources')['priority-0-resource']['status']).toBe('xa');
  123. expect(contact.get('resources')['priority-1-resource']['priority']).toBe(1);
  124. expect(contact.get('resources')['priority-1-resource']['status']).toBe('online');
  125. expect(contact.get('resources')['priority-2-resource']['priority']).toBe(2);
  126. expect(contact.get('resources')['priority-2-resource']['status']).toBe('dnd');
  127. expect(contact.get('resources')['older-priority-1-resource']['priority']).toBe(1);
  128. expect(contact.get('resources')['older-priority-1-resource']['status']).toBe('dnd');
  129. stanza = $(
  130. '<presence xmlns="jabber:client"'+
  131. ' to="dummy@localhost/converse.js-21770972"'+
  132. ' type="unavailable"'+
  133. ' from="'+contact_jid+'/priority-2-resource">'+
  134. '</presence>');
  135. _converse.connection._dataRecv(test_utils.createRequest(stanza[0]));
  136. expect(_converse.roster.get(contact_jid).get('chat_status')).toBe('online');
  137. expect(_.keys(contact.get('resources')).length).toBe(3);
  138. expect(contact.get('resources')['priority-0-resource']['priority']).toBe(0);
  139. expect(contact.get('resources')['priority-0-resource']['status']).toBe('xa');
  140. expect(contact.get('resources')['priority-1-resource']['priority']).toBe(1);
  141. expect(contact.get('resources')['priority-1-resource']['status']).toBe('online');
  142. expect(contact.get('resources')['older-priority-1-resource']['priority']).toBe(1);
  143. expect(contact.get('resources')['older-priority-1-resource']['status']).toBe('dnd');
  144. stanza = $(
  145. '<presence xmlns="jabber:client"'+
  146. ' to="dummy@localhost/converse.js-21770972"'+
  147. ' type="unavailable"'+
  148. ' from="'+contact_jid+'/priority-1-resource">'+
  149. '</presence>');
  150. _converse.connection._dataRecv(test_utils.createRequest(stanza[0]));
  151. expect(_converse.roster.get(contact_jid).get('chat_status')).toBe('dnd');
  152. expect(_.keys(contact.get('resources')).length).toBe(2);
  153. expect(contact.get('resources')['priority-0-resource']['priority']).toBe(0);
  154. expect(contact.get('resources')['priority-0-resource']['status']).toBe('xa');
  155. expect(contact.get('resources')['older-priority-1-resource']['priority']).toBe(1);
  156. expect(contact.get('resources')['older-priority-1-resource']['status']).toBe('dnd');
  157. stanza = $(
  158. '<presence xmlns="jabber:client"'+
  159. ' to="dummy@localhost/converse.js-21770972"'+
  160. ' type="unavailable"'+
  161. ' from="'+contact_jid+'/older-priority-1-resource">'+
  162. '</presence>');
  163. _converse.connection._dataRecv(test_utils.createRequest(stanza[0]));
  164. expect(_converse.roster.get(contact_jid).get('chat_status')).toBe('xa');
  165. expect(_.keys(contact.get('resources')).length).toBe(1);
  166. expect(contact.get('resources')['priority-0-resource']['priority']).toBe(0);
  167. expect(contact.get('resources')['priority-0-resource']['status']).toBe('xa');
  168. stanza = $(
  169. '<presence xmlns="jabber:client"'+
  170. ' to="dummy@localhost/converse.js-21770972"'+
  171. ' type="unavailable"'+
  172. ' from="'+contact_jid+'/priority-0-resource">'+
  173. '</presence>');
  174. _converse.connection._dataRecv(test_utils.createRequest(stanza[0]));
  175. expect(_converse.roster.get(contact_jid).get('chat_status')).toBe('offline');
  176. expect(_.keys(contact.get('resources')).length).toBe(0);
  177. }));
  178. });
  179. }));