source: Dev/branches/rest-dojo-ui/client/dojox/xmpp/PresenceService.js @ 256

Last change on this file since 256 was 256, checked in by hendrikvanantwerpen, 13 years ago

Reworked project structure based on REST interaction and Dojo library. As
soon as this is stable, the old jQueryUI branch can be removed (it's
kept for reference).

File size: 8.5 KB
Line 
1dojo.provide("dojox.xmpp.PresenceService");
2
3dojox.xmpp.presence = {
4        UPDATE: 201,
5        SUBSCRIPTION_REQUEST: 202,
6//      SUBSCRIPTION_REQUEST_PENDING: 203,
7        /* used when 'ask' attribute is absent on a roster item */
8        SUBSCRIPTION_SUBSTATUS_NONE: 204,
9
10        SUBSCRIPTION_NONE: 'none',
11        SUBSCRIPTION_FROM: 'from',
12        SUBSCRIPTION_TO: 'to',
13        SUBSCRIPTION_BOTH: 'both',
14        SUBSCRIPTION_REQUEST_PENDING: 'pending',
15
16        STATUS_ONLINE: 'online',
17        STATUS_AWAY: 'away',
18        STATUS_CHAT: 'chat',
19        STATUS_DND: 'dnd',
20        STATUS_EXTENDED_AWAY: 'xa',
21        STATUS_OFFLINE: 'offline',
22       
23        STATUS_INVISIBLE: 'invisible'
24}
25
26dojo.declare("dojox.xmpp.PresenceService", null, {
27        constructor: function(xmppService){
28                this.session= xmppService;
29                this.isInvisible = false;
30                this.avatarHash = null;
31                this.presence = null;
32                this.restrictedContactjids = {};
33        },
34
35        publish: function(presence){
36                ////console.log("Presence::publish() ", presence);
37                this.presence  = presence;
38                this._setPresence();
39        },
40       
41        /**
42        <presence from='juliet@capulet.com/balcony'>
43          <x xmlns='vcard-temp:x:update'>
44            <photo>sha1-hash-of-image</photo>
45          </x>
46        </presence>
47       
48       
49        <presence>
50          <x xmlns='vcard-temp:x:update'>
51            <photo/>
52          </x>
53        </presence>
54       
55        */
56       
57        sendAvatarHash: function(avatarHash) {
58                        this.avatarHash = avatarHash;
59                        this._setPresence();
60        },
61       
62       
63        _setPresence: function() {
64                        var presence =  this.presence;
65                        var p = {xmlns: 'jabber:client'};
66
67                        if (presence && presence.to){
68                                p.to = presence.to;
69                        }
70
71                        if (presence.show && presence.show==dojox.xmpp.presence.STATUS_OFFLINE){
72                                p.type = 'unavailable';
73                        }
74
75                        if (presence.show && presence.show==dojox.xmpp.presence.STATUS_INVISIBLE) {
76                                this._setInvisible();
77                                this.isInvisible = true;
78                                return;
79                        };
80
81                        if(this.isInvisible) {
82                                //console.log("was invisible, making visible");
83                                this._setVisible();
84                        }
85
86                        var req = new dojox.string.Builder(dojox.xmpp.util.createElement("presence",p, false));
87
88                        if (presence.show && presence.show!=dojox.xmpp.presence.STATUS_OFFLINE  ) {
89                                req.append(dojox.xmpp.util.createElement("show",{},false));
90                                req.append(presence.show);
91                                req.append("</show>");
92                        }
93
94                        if(presence.status) {
95                                req.append(dojox.xmpp.util.createElement("status",{},false));
96                                req.append(presence.status);
97                                req.append("</status>");
98                        }
99
100                        if(this.avatarHash) {
101                                req.append(dojox.xmpp.util.createElement("x",{xmlns: 'vcard-temp:x:update'},false));
102                                        req.append(dojox.xmpp.util.createElement("photo",{},false));
103                                        req.append(this.avatarHash);
104                                        req.append("</photo>");
105                                req.append("</x>");
106                        }
107
108
109                        if (presence.priority && presence.show!=dojox.xmpp.presence.STATUS_OFFLINE){
110                                if(presence.priority > 127 || presence.priority < -128){
111                                        presence.priority = 5;
112                                }
113                                req.append(dojox.xmpp.util.createElement("priority",{},false));
114                                req.append(presence.priority);
115                                req.append("</priority>");
116                        }
117
118                        req.append("</presence>");
119                        this.session.dispatchPacket(req.toString());
120        },
121       
122        /*
123       
124        <iq from='bilbo@tolkien.lit/shire' type='set' id='inv1'>
125          <query xmlns='jabber:iq:privacy'>
126            <list name='invisible'>
127              <item action='deny' order='1'>
128                <presence-out/>
129              </item>
130            </list>
131          </query>
132        </iq>
133       
134        <iq from='bilbo@tolkien.lit/shire' type='set' id='active1'>
135          <query xmlns='jabber:iq:privacy'>
136            <active name='invisible'/>
137          </query>
138        </iq>
139       
140        Make visible:
141        <iq from='bilbo@tolkien.lit/shire' type='set' id='active6'>
142          <query xmlns='jabber:iq:privacy'>
143            <active/>
144          </query>
145        </iq>
146       
147        */
148       
149        toggleBlockContact: function(jid) {
150                if(!this.restrictedContactjids[jid]) {
151                        this.restrictedContactjids[jid] = this._createRestrictedJid();
152                }
153               
154                this.restrictedContactjids[jid].blocked = !this.restrictedContactjids[jid].blocked;
155                //console.log("setting outbound block for ", jid, this.restrictedContactjids[jid]);
156                this._updateRestricted();
157                return this.restrictedContactjids;
158        },
159       
160       
161        toggleContactInvisiblity: function(jid) {
162                if(!this.restrictedContactjids[jid]) {
163                        this.restrictedContactjids[jid] = this._createRestrictedJid();
164                }
165               
166                this.restrictedContactjids[jid].invisible = !this.restrictedContactjids[jid].invisible;
167                //console.log("setting outbound presence for ", jid, this.restrictedContactjids[jid]);
168                this._updateRestricted();
169                return this.restrictedContactjids;
170        },
171       
172        _createRestrictedJid: function() {
173                return {invisible: false, blocked:false};
174        },
175       
176        _updateRestricted: function() {
177               
178                var props={
179                        id: this.session.getNextIqId(),
180                        from: this.session.jid + "/" + this.session.resource,
181                        type: "set"
182                };
183               
184                var req = new dojox.string.Builder(dojox.xmpp.util.createElement("iq",props,false));
185                req.append(dojox.xmpp.util.createElement("query",{xmlns: "jabber:iq:privacy"},false));
186                req.append(dojox.xmpp.util.createElement("list",{name: "iwcRestrictedContacts"},false))
187                var count = 1;
188                for(var jid in this.restrictedContactjids) {
189                        var item = this.restrictedContactjids[jid];
190                        //console.log("restricted ", jid, item);
191                        if(item.blocked || item.invisible) {
192                                req.append(dojox.xmpp.util.createElement("item",{value:  dojox.xmpp.util.encodeJid(jid), action: "deny", order: count++},false));
193                                if(item.blocked) {
194                                        req.append(dojox.xmpp.util.createElement("message",{},true));
195                                }
196                                if(item.invisible) {
197                                        req.append(dojox.xmpp.util.createElement("presence-out",{},true));
198                                }
199                                req.append("</item>");
200                        } else {
201                                delete this.restrictedContactjids[jid];
202                        }
203                       
204               
205                       
206                }
207                                req.append("</list>");
208                        req.append("</query>");
209                req.append("</iq>");
210                //console.log("Restricted list: ", req.toString());
211               
212                        var req2 = new dojox.string.Builder(dojox.xmpp.util.createElement("iq",props,false));
213                                req2.append(dojox.xmpp.util.createElement("query",{xmlns: "jabber:iq:privacy"},false));
214                                        req2.append(dojox.xmpp.util.createElement("active",{name:"iwcRestrictedContacts"},true));
215                                req2.append("</query>");
216                        req2.append("</iq>");
217               
218                                //console.log("Activate list: ", req2.toString());
219               
220               
221                this.session.dispatchPacket(req.toString());
222                this.session.dispatchPacket(req2.toString());
223        },
224       
225        _setVisible: function() {
226                        var props={
227                                id: this.session.getNextIqId(),
228                                from: this.session.jid + "/" + this.session.resource,
229                                type: "set"
230                        };
231                        var req = new dojox.string.Builder(dojox.xmpp.util.createElement("iq",props,false));
232                                req.append(dojox.xmpp.util.createElement("query",{xmlns: "jabber:iq:privacy"},false));
233                                        req.append(dojox.xmpp.util.createElement("active",{},true));
234                                req.append("</query>");
235                        req.append("</iq>");
236                        //console.log(req.toString());
237                        this.session.dispatchPacket(req.toString());
238        },
239       
240        _setInvisible: function() {
241                //console.log("Setting user as invisible");
242                var props={
243                        id: this.session.getNextIqId(),
244                        from: this.session.jid + "/" + this.session.resource,
245                        type: "set"
246                };
247                var req = new dojox.string.Builder(dojox.xmpp.util.createElement("iq",props,false));
248                        req.append(dojox.xmpp.util.createElement("query",{xmlns: "jabber:iq:privacy"},false));
249                                req.append(dojox.xmpp.util.createElement("list",{name: "invisible"},false))
250                                        req.append(dojox.xmpp.util.createElement("item",{action: "deny", order: "1"},false))
251                                                req.append(dojox.xmpp.util.createElement("presence-out",{},true));
252                                        req.append("</item>");
253                                req.append("</list>");
254                        req.append("</query>");
255                req.append("</iq>");
256               
257               
258                props={
259                        id: this.session.getNextIqId(),
260                        from: this.session.jid + "/" + this.session.resource,
261                        type: "set"
262                };
263
264                var req2 = new dojox.string.Builder(dojox.xmpp.util.createElement("iq",props,false));
265                        req2.append(dojox.xmpp.util.createElement("query",{xmlns: "jabber:iq:privacy"},false));
266                                req2.append(dojox.xmpp.util.createElement("active",{name:"invisible"},true));
267                        req2.append("</query>");
268                req2.append("</iq>");
269                //console.log(req.toString());
270                //console.log(req2.toString());
271                this.session.dispatchPacket(req.toString());
272                this.session.dispatchPacket(req2.toString());
273        },
274
275        _manageSubscriptions: function(contact, type){
276                if (!contact){return;}
277               
278                if (contact.indexOf('@')==-1){
279                        contact += '@' + this.session.domain;
280                }
281
282                var req = dojox.xmpp.util.createElement("presence",{to:contact,type:type},true);
283                this.session.dispatchPacket(req);
284
285        },
286
287        subscribe: function(contact){
288                this._manageSubscriptions(contact, "subscribe");
289        },
290
291        approveSubscription: function(contact){
292                this._manageSubscriptions(contact, "subscribed");
293        },
294
295        unsubscribe: function(contact){
296                this._manageSubscriptions(contact, "unsubscribe");
297        },
298
299        declineSubscription: function(contact){
300                this._manageSubscriptions(contact, "unsubscribed");
301        },
302       
303        cancelSubscription: function(contact){
304                this._manageSubscriptions(contact, "unsubscribed");
305        }
306
307});
Note: See TracBrowser for help on using the repository browser.