Browse code

Sledovanie kontaktov - reagovanie v pripade vymazania alebo pridania do kontaktov niekym inym

Cinan Rakosnik authored on 06/04/2013 at 20:51:36
Showing 5 changed files
... ...
@@ -28,9 +28,9 @@ this.App =
28 28
             newContact = new Xmpp.Models.Contact(
29 29
               id: contact.jid
30 30
               jid: contact.jid
31
-              belongsTo: contact.belongsTo
31
+              belongsTo: [contact.belongsTo]
32 32
             )
33
-            App.Collections.contacts.add newContact
33
+            App.Collections.contacts.add(newContact, merge: true)
34 34
           )
35 35
       )
36 36
 
... ...
@@ -53,6 +53,9 @@ this.App =
53 53
         )
54 54
       )
55 55
 
56
+    removeContactRemote: (contact, client) ->
57
+      App.Com.trigger(event: 'app.roster.removeContact', data: {jid: contact, client: client})
58
+
56 59
     _setupBackboneComponents: ->
57 60
       App.Collections.contacts = new Xmpp.Collections.ContactsCollection()
58 61
       App.Collections.chats = new Xmpp.Collections.ChatsCollection()
... ...
@@ -70,6 +73,11 @@ this.App =
70 70
         App.Collections.contacts.udpateVcard(result)
71 71
       )
72 72
 
73
+      App._dispatcher.bind('app.roster.subscriptionChanged', (subscription) ->
74
+        App.debug 'subscription changed'
75
+        App.Collections.contacts.subscriptionChanged(subscription)
76
+      )
77
+
73 78
   Models:
74 79
     me: null
75 80
 
... ...
@@ -7,7 +7,7 @@ class Xmpp.Models.Contact extends Xmpp.Models.Model
7 7
     status: 'offline'
8 8
     message: ''
9 9
     avatar: ''
10
-    belongsTo: ''
10
+    belongsTo: []
11 11
 
12 12
   initialize: ->
13 13
     _.bindAll(this)
... ...
@@ -31,6 +31,7 @@ class Xmpp.Collections.ContactsCollection extends Backbone.Collection
31 31
     @activeList.setAsActiveChatGroup()
32 32
 
33 33
     @on('add', @appendContact)
34
+    @on('change:belongsTo', @mergeContacts)
34 35
 
35 36
     Backbone.Events.on('openChat', (chat) =>
36 37
       @moveToInactiveList('all')
... ...
@@ -42,8 +43,21 @@ class Xmpp.Collections.ContactsCollection extends Backbone.Collection
42 42
     )
43 43
 
44 44
   appendContact: (contact) ->
45
+    App.debug ['apppend', contact]
45 46
     @friendsList.appendContact(contact)
46 47
 
48
+  mergeContacts: (contact) ->
49
+    App.debug ['merging contacts', contact]
50
+    #TODO: ak ten isty clovek je vo viacerych rosteroch, treba nastavit spravne belongsTo
51
+
52
+    existingContact = @get(contact.get('id'))
53
+    if existingContact?
54
+      alreadyBelongsTo = existingContact.get('belongsTo')
55
+      willBelongTo = contact.get('belongsTo')
56
+      existingContact.set(belongsTo: _.union(alreadyBelongsTo, willBelongTo))
57
+    else
58
+      @add(contact)
59
+
47 60
   moveToActiveList: (contact) ->
48 61
     @_switchContactBelongingList(@get(contact), @friendsList, @activeList)
49 62
 
... ...
@@ -61,6 +75,30 @@ class Xmpp.Collections.ContactsCollection extends Backbone.Collection
61 61
   udpateVcard: (response) ->
62 62
     @get(response.jid).set(name: response.vcard.name, avatar: response.vcard.avatar)
63 63
 
64
+  subscriptionChanged: (newSubscription) ->
65
+    if (newSubscription.action == 'subscribed')
66
+      App.debug ['subscribed', newSubscription]
67
+
68
+      newContact = new Xmpp.Models.Contact(
69
+        id: newSubscription.jid
70
+        jid: newSubscription.jid
71
+        belongsTo: [newSubscription.belongsTo]
72
+      )
73
+      @mergeContacts(newContact)
74
+    else if (newSubscription.action == 'unsubscribed')
75
+      # TODO: show info and ask if user want to remove him from roster
76
+      App.debug ['unsubscribed', newSubscription]
77
+
78
+      contact = @get(newSubscription.jid)
79
+      App.Com.removeContactRemote(contact.get('jid'), newSubscription.belongsTo)
80
+
81
+      nowBelongsTo = _.without(contact.get('belongsTo'), newSubscription.belongsTo)
82
+      if (nowBelongsTo.length == 0)
83
+        Backbone.Events.trigger('removeContact', contact)
84
+        @remove(contact)
85
+
86
+    # ignore 'unsubscribe' type of action
87
+
64 88
   _switchContactBelongingList: (contact, fromList, toList) ->
65 89
     if !contact?
66 90
       false
... ...
@@ -13,6 +13,8 @@ class Xmpp.Views.Contacts.ListView extends Backbone.View
13 13
     @activeGroup = false
14 14
     @contactViews = []
15 15
 
16
+    Backbone.Events.on('removeContact', @detachContact)
17
+
16 18
   createListContainer: ->
17 19
     $(@el).html(@template(title: @title, isActiveGroup: @activeGroup))
18 20
 
... ...
@@ -81,6 +81,19 @@ class WsRosterController < WebsocketRails::BaseController
81 81
     # Zacni pocuvat zmeny stavov v rosteri
82 82
     def start_polling_contacts_state
83 83
         connection_store[:rosters].each do |roster|
84
+            roster.add_subscription_callback do |roster_item, stanza|
85
+                # stan unsubscribe = niekto uz ma nesubscribuje
86
+                # stan unsubscribed = niekto ma uz nesubscribuje a aj si ma vymazal
87
+                # stan subscribed = niekto moze vidiet moj stav
88
+
89
+                client = connection_store[:link_roster_client][roster]
90
+
91
+                send_message 'app.roster.subscriptionChanged',
92
+                             action: stanza.type,
93
+                             jid: roster_item.jid.strip.to_s,
94
+                             belongsTo: client.jid.strip.to_s
95
+            end
96
+
84 97
             roster.add_presence_callback do |roster_item, old_presence, new_presence|
85 98
                 if new_presence.type == :unavailable
86 99
                     result = {status: :offline, message: ''}
... ...
@@ -108,6 +121,22 @@ class WsRosterController < WebsocketRails::BaseController
108 108
         end
109 109
     end
110 110
 
111
+    def remove_contact
112
+        jid = message[:jid]
113
+        client_jid = message[:client]
114
+
115
+        found_roster_item = nil
116
+        connection_store[:rosters].each do |roster|
117
+            roster_item = roster.find(jid)
118
+            if roster_item.first[1] && connection_store[:link_roster_client][roster].jid.strip.to_s == client_jid
119
+                found_roster_item = roster_item.first[1]
120
+                break
121
+            end
122
+        end
123
+
124
+        found_roster_item && found_roster_item.remove()
125
+    end
126
+
111 127
     ##
112 128
     # Ziskaj informacie o mne (meno, stav, status...)
113 129
     def myself
... ...
@@ -54,6 +54,7 @@ WebsocketRails::EventMap.describe do
54 54
             subscribe :startPolling,        to: WsRosterController, with_method: :start_polling_contacts_state
55 55
             subscribe :setPresence,         to: WsRosterController, with_method: :set_presence
56 56
             subscribe :myself,              to: WsRosterController, with_method: :myself
57
+            subscribe :removeContact,       to: WsRosterController, with_method: :remove_contact
57 58
         end
58 59
 
59 60
     end