RosterService.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. /*
  2. Copyright (c) 2004-2012, The Dojo Foundation All Rights Reserved.
  3. Available via Academic Free License >= 2.1 OR the modified BSD license.
  4. see: http://dojotoolkit.org/license for details
  5. */
  6. if(!dojo._hasResource["dojox.xmpp.RosterService"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
  7. dojo._hasResource["dojox.xmpp.RosterService"] = true;
  8. dojo.provide("dojox.xmpp.RosterService");
  9. dojox.xmpp.roster = {
  10. ADDED: 101,
  11. CHANGED: 102,
  12. REMOVED: 103
  13. }
  14. dojo.declare("dojox.xmpp.RosterService", null, {
  15. constructor: function(xmppSession){
  16. this.session = xmppSession;
  17. },
  18. addRosterItem: function(jid, name, groups){
  19. if(!jid){
  20. throw new Error ("Roster::addRosterItem() - User ID is null");
  21. }
  22. var iqId = this.session.getNextIqId();
  23. var req = {
  24. id: iqId,
  25. from: this.session.jid + "/" + this.session.resource,
  26. type: "set"
  27. }
  28. var request = new dojox.string.Builder(dojox.xmpp.util.createElement("iq", req, false));
  29. request.append(dojox.xmpp.util.createElement("query",{xmlns: 'jabber:iq:roster'},false));
  30. jid = dojox.xmpp.util.encodeJid(jid);
  31. if (jid.indexOf('@')== -1){
  32. jid = jid + '@' + this.session.domain;
  33. }
  34. request.append(dojox.xmpp.util.createElement("item",{jid:jid,name:dojox.xmpp.util.xmlEncode(name)},false));
  35. if (groups){
  36. for (var i=0; i<groups.length; i++){
  37. request.append("<group>");
  38. request.append(groups[i]);
  39. request.append("</group>");
  40. }
  41. }
  42. request.append("</item></query></iq>");
  43. //console.log(request.toString());
  44. var def = this.session.dispatchPacket(request.toString(),"iq",req.id);
  45. def.addCallback(this, "verifyRoster");
  46. return def;
  47. },
  48. updateRosterItem: function(jid, name, groups){
  49. if (jid.indexOf('@') == -1){
  50. jid += jid + '@' + this.session.domain;
  51. }
  52. var req = {
  53. id: this.session.getNextIqId(),
  54. from: this.session.jid + "/" + this.session.resource,
  55. type: "set"
  56. }
  57. var request = new dojox.string.Builder(dojox.xmpp.util.createElement("iq", req, false));
  58. request.append(dojox.xmpp.util.createElement("query",{xmlns: 'jabber:iq:roster'},false));
  59. var i = this.session.getRosterIndex(jid);
  60. //item not found
  61. if (i==-1){return;}
  62. var item = {
  63. jid:jid
  64. };
  65. if(name){
  66. item.name = name;
  67. } else if(this.session.roster[i].name){
  68. item.name = this.session.roster[i].name;
  69. }
  70. if(item.name) {
  71. item.name = dojox.xmpp.util.xmlEncode(item.name);
  72. }
  73. request.append(dojox.xmpp.util.createElement("item",item,false));
  74. var newGroups = groups ? groups : this.session.roster[i].groups;
  75. if (newGroups){
  76. for (var x=0;x<newGroups.length;x++){
  77. request.append("<group>");
  78. request.append(newGroups[x]);
  79. request.append("</group>");
  80. }
  81. }
  82. request.append("</item></query></iq>");
  83. var def = this.session.dispatchPacket(request.toString(),"iq",req.id);
  84. def.addCallback(this, "verifyRoster");
  85. return def;
  86. },
  87. verifyRoster: function(res){
  88. if (res.getAttribute('type')=='result'){
  89. //this.onAddRosterItem(res.getAttribute('id'));
  90. }else{
  91. var err=this.session.processXmppError(res);
  92. this.onAddRosterItemFailed(err);
  93. }
  94. return res;
  95. },
  96. addRosterItemToGroup: function(jid, group){
  97. if (!jid) throw new Error("Roster::addRosterItemToGroup() JID is null or undefined");
  98. if (!group) throw new Error("Roster::addRosterItemToGroup() group is null or undefined");
  99. var index = this.session.getRosterIndex(jid);
  100. if (index==-1){return;}
  101. var item = this.session.roster[index];
  102. var tgroups = [];
  103. var found = false;
  104. for (var i=0; ((item<item.groups.length) && (!found)); i++){
  105. if (item.groups[i]!=group){continue;}
  106. found=true;
  107. }
  108. if(!found){
  109. return this.updateRosterItem(jid, item.name, item.groups.concat(group),index);
  110. }
  111. return dojox.xmpp.xmpp.INVALID_ID;
  112. },
  113. removeRosterGroup: function(group) {
  114. var roster = this.session.roster;
  115. for(var i=0;i<roster.length;i++){
  116. var item = roster[i];
  117. if(item.groups.length > 0) {
  118. //var found = false;
  119. for(var j = 0;j < item.groups.length; j++) {
  120. if (item.groups[j]==group){
  121. item.groups.splice(j,1);
  122. this.updateRosterItem(item.jid, item.name, item.groups);
  123. //found=true;
  124. }
  125. }
  126. }
  127. }
  128. },
  129. renameRosterGroup: function(group, newGroup) {
  130. var roster = this.session.roster;
  131. for(var i=0;i<roster.length;i++){
  132. var item = roster[i];
  133. if(item.groups.length > 0) {
  134. //var found = false;
  135. for(var j = 0;j < item.groups.length; j++) {
  136. if (item.groups[j]==group){
  137. item.groups[j] = newGroup;
  138. this.updateRosterItem(item.jid, item.name, item.groups);
  139. // found=true;
  140. }
  141. }
  142. }
  143. }
  144. },
  145. removeRosterItemFromGroup: function(jid, group){
  146. if (!jid) throw new Error("Roster::addRosterItemToGroup() JID is null or undefined");
  147. if (!group) throw new Error("Roster::addRosterItemToGroup() group is null or undefined");
  148. var index = this.session.getRosterIndex(jid);
  149. if (index==-1){return;}
  150. var item = this.session.roster[index];
  151. var found = false;
  152. for (var i=0; ((i<item.groups.length) && (!found)); i++){
  153. if (item.groups[i]!=group){continue;}
  154. found=true;
  155. index = i;
  156. }
  157. if(found==true){
  158. item.groups.splice(index,1);
  159. return this.updateRosterItem(jid, item.name, item.groups);
  160. }
  161. return dojox.xmpp.xmpp.INVALID_ID;
  162. },
  163. rosterItemRenameGroup: function(jid, oldGroup, newGroup){
  164. if (!jid) throw new Error("Roster::rosterItemRenameGroup() JID is null or undefined");
  165. if (!newGroup) throw new Error("Roster::rosterItemRenameGroup() group is null or undefined");
  166. var index = this.session.getRosterIndex(jid);
  167. if (index==-1){return;}
  168. var item = this.session.roster[index];
  169. var found = false;
  170. for (var i=0; ((i<item.groups.length) && (!found)); i++){
  171. if (item.groups[i]==oldGroup){
  172. item.groups[i] = newGroup;
  173. found=true;
  174. }
  175. }
  176. if(found==true){
  177. return this.updateRosterItem(jid, item.name, item.groups);
  178. }
  179. return dojox.xmpp.xmpp.INVALID_ID;
  180. },
  181. renameRosterItem: function(jid,newName){
  182. if (!jid) throw new Error("Roster::addRosterItemToGroup() JID is null or undefined");
  183. if (!newName) throw new Error("Roster::addRosterItemToGroup() New Name is null or undefined");
  184. var index = this.session.getRosterIndex(jid);
  185. if (index==-1){return;}
  186. return this.updateRosterItem(jid, newName, this.session.roster.groups,index);
  187. },
  188. removeRosterItem: function(jid){
  189. if (!jid) throw new Error("Roster::addRosterItemToGroup() JID is null or undefined");
  190. var req={
  191. id: this.session.getNextIqId(),
  192. from: this.session.jid + "/" + this.session.resource,
  193. type: 'set'
  194. };
  195. var request = new dojox.string.Builder(dojox.xmpp.util.createElement("iq", req, false));
  196. request.append(dojox.xmpp.util.createElement("query",{xmlns: "jabber:iq:roster"},false));
  197. if (jid.indexOf('@')== -1){
  198. jid += jid + '@' + this.session.domain;
  199. }
  200. request.append(dojox.xmpp.util.createElement('item',{jid:jid,subscription:"remove"},true));
  201. request.append("</query></iq>");
  202. var def = this.session.dispatchPacket(request.toString(),"iq",req.id);
  203. def.addCallback(this, "verifyRoster");
  204. return def;
  205. },
  206. //Avatar functions...I removed this stuff for now..can we even do anything useful
  207. //with this data even if we have it?
  208. getAvatar: function(jid){
  209. },
  210. publishAvatar: function(type,binval){
  211. },
  212. //EVENTS
  213. onVerifyRoster: function(id){
  214. //console.log("Roster::onVerifyRoster() - ", id);
  215. },
  216. onVerifyRosterFailed: function(err){
  217. //console.log("onVerifyRosterFailed: ", err);
  218. }
  219. });
  220. }