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