RosterService.js 7.1 KB

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