123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272 |
- // wrapped by build app
- define("dojox/xmpp/RosterService", ["dijit","dojo","dojox"], function(dijit,dojo,dojox){
- dojo.provide("dojox.xmpp.RosterService");
- dojox.xmpp.roster = {
- ADDED: 101,
- CHANGED: 102,
- REMOVED: 103
- }
- dojo.declare("dojox.xmpp.RosterService", null, {
- constructor: function(xmppSession){
- this.session = xmppSession;
- },
- addRosterItem: function(jid, name, groups){
- if(!jid){
- throw new Error ("Roster::addRosterItem() - User ID is null");
- }
- var iqId = this.session.getNextIqId();
- var req = {
- id: iqId,
- from: this.session.jid + "/" + this.session.resource,
- type: "set"
- }
- var request = new dojox.string.Builder(dojox.xmpp.util.createElement("iq", req, false));
- request.append(dojox.xmpp.util.createElement("query",{xmlns: 'jabber:iq:roster'},false));
- jid = dojox.xmpp.util.encodeJid(jid);
- if (jid.indexOf('@')== -1){
- jid = jid + '@' + this.session.domain;
- }
- request.append(dojox.xmpp.util.createElement("item",{jid:jid,name:dojox.xmpp.util.xmlEncode(name)},false));
- if (groups){
- for (var i=0; i<groups.length; i++){
- request.append("<group>");
- request.append(groups[i]);
- request.append("</group>");
- }
- }
-
- request.append("</item></query></iq>");
- //console.log(request.toString());
- var def = this.session.dispatchPacket(request.toString(),"iq",req.id);
- def.addCallback(this, "verifyRoster");
- return def;
- },
- updateRosterItem: function(jid, name, groups){
- if (jid.indexOf('@') == -1){
- jid += jid + '@' + this.session.domain;
- }
- var req = {
- id: this.session.getNextIqId(),
- from: this.session.jid + "/" + this.session.resource,
- type: "set"
- }
- var request = new dojox.string.Builder(dojox.xmpp.util.createElement("iq", req, false));
- request.append(dojox.xmpp.util.createElement("query",{xmlns: 'jabber:iq:roster'},false));
- var i = this.session.getRosterIndex(jid);
- //item not found
- if (i==-1){return;}
- var item = {
- jid:jid
- };
- if(name){
- item.name = name;
- } else if(this.session.roster[i].name){
- item.name = this.session.roster[i].name;
- }
- if(item.name) {
- item.name = dojox.xmpp.util.xmlEncode(item.name);
- }
- request.append(dojox.xmpp.util.createElement("item",item,false));
-
- var newGroups = groups ? groups : this.session.roster[i].groups;
-
- if (newGroups){
- for (var x=0;x<newGroups.length;x++){
- request.append("<group>");
- request.append(newGroups[x]);
- request.append("</group>");
- }
- }
-
- request.append("</item></query></iq>");
-
- var def = this.session.dispatchPacket(request.toString(),"iq",req.id);
- def.addCallback(this, "verifyRoster");
- return def;
- },
- verifyRoster: function(res){
- if (res.getAttribute('type')=='result'){
- //this.onAddRosterItem(res.getAttribute('id'));
- }else{
- var err=this.session.processXmppError(res);
- this.onAddRosterItemFailed(err);
- }
- return res;
- },
- addRosterItemToGroup: function(jid, group){
- if (!jid) throw new Error("Roster::addRosterItemToGroup() JID is null or undefined");
- if (!group) throw new Error("Roster::addRosterItemToGroup() group is null or undefined");
- var index = this.session.getRosterIndex(jid);
- if (index==-1){return;}
- var item = this.session.roster[index];
- var tgroups = [];
- var found = false;
- for (var i=0; ((item<item.groups.length) && (!found)); i++){
- if (item.groups[i]!=group){continue;}
- found=true;
- }
-
- if(!found){
- return this.updateRosterItem(jid, item.name, item.groups.concat(group),index);
- }
-
- return dojox.xmpp.xmpp.INVALID_ID;
- },
-
- removeRosterGroup: function(group) {
- var roster = this.session.roster;
- for(var i=0;i<roster.length;i++){
- var item = roster[i];
- if(item.groups.length > 0) {
- //var found = false;
- for(var j = 0;j < item.groups.length; j++) {
- if (item.groups[j]==group){
- item.groups.splice(j,1);
- this.updateRosterItem(item.jid, item.name, item.groups);
- //found=true;
- }
- }
- }
- }
- },
-
- renameRosterGroup: function(group, newGroup) {
- var roster = this.session.roster;
- for(var i=0;i<roster.length;i++){
- var item = roster[i];
- if(item.groups.length > 0) {
- //var found = false;
- for(var j = 0;j < item.groups.length; j++) {
- if (item.groups[j]==group){
- item.groups[j] = newGroup;
- this.updateRosterItem(item.jid, item.name, item.groups);
- // found=true;
- }
- }
- }
- }
- },
- removeRosterItemFromGroup: function(jid, group){
- if (!jid) throw new Error("Roster::addRosterItemToGroup() JID is null or undefined");
- if (!group) throw new Error("Roster::addRosterItemToGroup() group is null or undefined");
- var index = this.session.getRosterIndex(jid);
- if (index==-1){return;}
- var item = this.session.roster[index];
- var found = false;
- for (var i=0; ((i<item.groups.length) && (!found)); i++){
- if (item.groups[i]!=group){continue;}
- found=true;
- index = i;
- }
- if(found==true){
- item.groups.splice(index,1);
- return this.updateRosterItem(jid, item.name, item.groups);
- }
-
- return dojox.xmpp.xmpp.INVALID_ID;
- },
-
- rosterItemRenameGroup: function(jid, oldGroup, newGroup){
- if (!jid) throw new Error("Roster::rosterItemRenameGroup() JID is null or undefined");
- if (!newGroup) throw new Error("Roster::rosterItemRenameGroup() group is null or undefined");
-
- var index = this.session.getRosterIndex(jid);
- if (index==-1){return;}
- var item = this.session.roster[index];
- var found = false;
- for (var i=0; ((i<item.groups.length) && (!found)); i++){
- if (item.groups[i]==oldGroup){
- item.groups[i] = newGroup;
- found=true;
- }
- }
- if(found==true){
- return this.updateRosterItem(jid, item.name, item.groups);
- }
-
- return dojox.xmpp.xmpp.INVALID_ID;
- },
- renameRosterItem: function(jid,newName){
- if (!jid) throw new Error("Roster::addRosterItemToGroup() JID is null or undefined");
- if (!newName) throw new Error("Roster::addRosterItemToGroup() New Name is null or undefined");
- var index = this.session.getRosterIndex(jid);
- if (index==-1){return;}
- return this.updateRosterItem(jid, newName, this.session.roster.groups,index);
- },
- removeRosterItem: function(jid){
- if (!jid) throw new Error("Roster::addRosterItemToGroup() JID is null or undefined");
-
- var req={
- id: this.session.getNextIqId(),
- from: this.session.jid + "/" + this.session.resource,
- type: 'set'
- };
- var request = new dojox.string.Builder(dojox.xmpp.util.createElement("iq", req, false));
-
- request.append(dojox.xmpp.util.createElement("query",{xmlns: "jabber:iq:roster"},false));
- if (jid.indexOf('@')== -1){
- jid += jid + '@' + this.session.domain;
- }
- request.append(dojox.xmpp.util.createElement('item',{jid:jid,subscription:"remove"},true));
- request.append("</query></iq>");
- var def = this.session.dispatchPacket(request.toString(),"iq",req.id);
- def.addCallback(this, "verifyRoster");
- return def;
- },
- //Avatar functions...I removed this stuff for now..can we even do anything useful
- //with this data even if we have it?
- getAvatar: function(jid){
- },
- publishAvatar: function(type,binval){
- },
- //EVENTS
- onVerifyRoster: function(id){
- //console.log("Roster::onVerifyRoster() - ", id);
- },
- onVerifyRosterFailed: function(err){
- //console.log("onVerifyRosterFailed: ", err);
- }
- });
- });
|