Browse code

Zistovanie, ci je klient schopny multchatu

Cinan Rakosnik authored on 28/04/2013 at 17:03:25
Showing 8 changed files
... ...
@@ -46,6 +46,9 @@ this.App =
46 46
     startPollingMessages: ->
47 47
       App.Com.trigger(event: 'app.chat.startPollingMessages')
48 48
 
49
+    whoUseThisApp: ->
50
+      App.Com.trigger(event: 'app.roster.whoUseThisApp')
51
+
49 52
     setPresence: ->
50 53
       App.Com.trigger(event: 'app.roster.setPresence')
51 54
 
... ...
@@ -106,6 +109,13 @@ this.App =
106 106
         App.Collections.contacts.subscriptionChanged(subscription)
107 107
       )
108 108
 
109
+      App._dispatcher.bind('app.roster.using_this_app', (person) ->
110
+        App.debug ['is using this app', person.jid]
111
+        contact = App.Collections.contacts.get(person.jid)
112
+        if (contact)
113
+          contact.setUsingMyApp()
114
+      )
115
+
109 116
       App._dispatcher.bind('app.chat.importChat', (chatData) ->
110 117
         newChat = new Xmpp.Models.Chat(
111 118
           chatId: chatData.chat_id,
... ...
@@ -14,6 +14,7 @@ App.Com.connect(->
14 14
   App.Com.initRoster()
15 15
   App.Com.startFetchingVcards()
16 16
   App.Com.startPollingRoster()
17
+  App.Com.whoUseThisApp()
17 18
   App.Com.setPresence()
18 19
 
19 20
   App.Com.getMe()
... ...
@@ -8,6 +8,7 @@ class Xmpp.Models.Contact extends Xmpp.Models.Model
8 8
     message: ''
9 9
     avatar: 'assets/avatar.png'
10 10
     belongsTo: []
11
+    usingMyApp: false
11 12
 
12 13
   initialize: ->
13 14
     _.bindAll(this)
... ...
@@ -20,6 +21,9 @@ class Xmpp.Models.Contact extends Xmpp.Models.Model
20 20
   firstname: ->
21 21
     @get('name').split(' ')[0]
22 22
 
23
+  setUsingMyApp: ->
24
+    @set('usingMyApp', true)
25
+
23 26
 class Xmpp.Collections.ContactsCollection extends Backbone.Collection
24 27
   model: Xmpp.Models.Contact
25 28
 
... ...
@@ -5,8 +5,9 @@
5 5
   %span.status.icon-record{class: "#{@status}"}
6 6
 %h2 #{@message}  
7 7
 %span.action
8
-  %span.icon-bubbles.chat
9
-    %span chat
8
+  -if @usingMyApp
9
+    %span.icon-bubbles.chat
10
+      %span chat
10 11
   -#%span.icon-bubbles.invite
11 12
     %span invite
12 13
 
... ...
@@ -198,6 +198,7 @@
198 198
   }
199 199
 
200 200
   .action {
201
+    display: none;
201 202
     margin-right: 2px;
202 203
     margin-top: -31px;
203 204
     float: right;
... ...
@@ -200,6 +200,18 @@ class WsRosterController < WsController
200 200
         end
201 201
     end
202 202
 
203
+    def ask_if_using_this_app
204
+        connection_store[:rosters].each do |roster|
205
+            client = connection_store[:link_roster_client][roster]
206
+
207
+            roster.items.each do |jid, contact|
208
+                client.send(MessageBuilder::control_question(client.jid.strip.to_s, jid))
209
+            end
210
+        end
211
+
212
+        start_polling_control_answer()
213
+    end
214
+
203 215
     ##
204 216
     # Pridaj noveho priatela do zoznamu
205 217
     def add_friend(data)
... ...
@@ -227,4 +239,17 @@ class WsRosterController < WsController
227 227
             else                 :online
228 228
         end
229 229
     end
230
+
231
+    def start_polling_control_answer
232
+        connection_store[:clients].each do |client|
233
+            client.add_message_callback do |message|
234
+                if message.attribute('i_am_using_same_app')
235
+                    send_message 'app.roster.using_this_app',
236
+                                 jid: message.from.node + '@' + message.from.domain
237
+                elsif message.attribute('are_you_using_my_app')
238
+                    client.send(MessageBuilder::control_answer(client.jid.strip.to_s, message.from))
239
+                end
240
+            end
241
+        end
242
+    end
230 243
 end
... ...
@@ -48,6 +48,22 @@ module MessageBuilder
48 48
         message
49 49
     end
50 50
 
51
+    def self.control_answer(from, to)
52
+        message = Jabber::Message.new(to)
53
+        message.from = from
54
+        message.add_attributes('i_am_using_same_app' => 'true')
55
+
56
+        message
57
+    end
58
+
59
+    def self.control_question(from, to)
60
+        message = Jabber::Message.new(to)
61
+        message.from = from
62
+        message.add_attributes('are_you_using_my_app' => '?')
63
+
64
+        message
65
+    end
66
+
51 67
     private
52 68
 
53 69
     def self.collect_contacts(to_element, contacts)
... ...
@@ -57,6 +57,7 @@ WebsocketRails::EventMap.describe do
57 57
             subscribe :updateMyStatus,      to: WsRosterController, with_method: :me_update_status
58 58
             subscribe :updateMyVcard,       to: WsRosterController, with_method: :me_update_vcard
59 59
             subscribe :removeContact,       to: WsRosterController, with_method: :remove_contact
60
+            subscribe :whoUseThisApp,       to: WsRosterController, with_method: :ask_if_using_this_app
60 61
         end
61 62
 
62 63
         namespace :chat do