Browse code

Prijimanie a posielanie sprav

Cinan Rakosnik authored on 18/04/2013 at 09:31:19
Showing 7 changed files
... ...
@@ -16,7 +16,7 @@ this.App =
16 16
       App._dispatcher = new WebSocketRails('www.xmpp.dev:3000/websocket')
17 17
       App._dispatcher.on_open = =>
18 18
         @_setupBackboneComponents()
19
-        @_bindEvents()
19
+        @_bindListeners()
20 20
         callback?()
21 21
 
22 22
     trigger: (options) ->
... ...
@@ -43,6 +43,9 @@ this.App =
43 43
     startPollingRoster: ->
44 44
       App.Com.trigger(event: 'app.roster.startPolling')
45 45
 
46
+    startPollingMessages: ->
47
+      App.Com.trigger(event: 'app.chat.startPollingMessages')
48
+
46 49
     setPresence: ->
47 50
       App.Com.trigger(event: 'app.roster.setPresence')
48 51
 
... ...
@@ -77,7 +80,7 @@ this.App =
77 77
 
78 78
       App.Views.tabbar = new Xmpp.Views.Tabbar.TabbarView()
79 79
 
80
-    _bindEvents: ->
80
+    _bindListeners: ->
81 81
       App._dispatcher.bind('app.roster.statusChanged', (result) ->
82 82
         App.debug 'change contact state'
83 83
         App.Collections.contacts.updateStatus(result)
... ...
@@ -93,6 +96,36 @@ this.App =
93 93
         App.Collections.contacts.subscriptionChanged(subscription)
94 94
       )
95 95
 
96
+      App._dispatcher.bind('app.chat.messageReceived', (result) ->
97
+        App.debug 'message received'
98
+
99
+        contact = App.Collections.contacts.findByJid(result.from)
100
+
101
+        if contact
102
+          contactView = App.Collections.contacts.friendsList.hasContact(contact) ||
103
+            App.Collections.contacts.activeList.hasContact(contact)
104
+        else
105
+          contact = new Xmpp.Models.Contact(
106
+            id: result.from
107
+            jid: result.from
108
+            belongsTo: [result.to]
109
+          )
110
+          App.Collections.contacts.add(contact, merge: true)
111
+          contactView = App.Collections.contacts.friendsList.hasContact(contact)
112
+
113
+        contactView.startChat()
114
+
115
+        tab = _.find(App.Views.tabbar.tabs, (tab) ->
116
+          tab.hasParticipants(App.Models.me, contact)
117
+        )
118
+
119
+        tab.chatWindow.appendMessage(contact, new Date(), result.message)
120
+
121
+#        if (result.chat_id)
122
+#          #todo otvorit chat podla chat_id. Spytat sa servera, kto vsetko je ucastnikom
123
+#          #chat = App.Collections.chats.findById(result.chat_id)
124
+      )
125
+
96 126
   Models:
97 127
     me: null
98 128
 
... ...
@@ -17,4 +17,5 @@ App.Com.connect(->
17 17
   App.Com.setPresence()
18 18
 
19 19
   App.Com.getMe()
20
+  App.Com.startPollingMessages()
20 21
 )
... ...
@@ -30,5 +30,8 @@ class Xmpp.Collections.ChatsCollection extends Backbone.Collection
30 30
       chat.get('who') == who && chat.get('withWhom') == withWhom
31 31
     )
32 32
 
33
+  findById: (id) ->
34
+    @findWhere(chatId: id)
35
+
33 36
   removeChat: (chat) ->
34 37
     @models = _.without(@openedChats, chat)
35 38
\ No newline at end of file
... ...
@@ -6,7 +6,7 @@ class Xmpp.Models.Contact extends Xmpp.Models.Model
6 6
     name: ''
7 7
     status: 'offline'
8 8
     message: ''
9
-    avatar: ''
9
+    avatar: 'assets/avatar.png'
10 10
     belongsTo: []
11 11
 
12 12
   initialize: ->
... ...
@@ -100,10 +100,14 @@ class Xmpp.Collections.ContactsCollection extends Backbone.Collection
100 100
 
101 101
     # ignore 'unsubscribe' type of action
102 102
 
103
+  # toto sem asi nepatri, ale asi do views. Nic to v skutocnosti nerobi s modelmi.
103 104
   filter: (searchTerm) ->
104 105
     @friendsList.filter(searchTerm)
105 106
     @activeList.filter(searchTerm)
106 107
 
108
+  findByJid: (jid) ->
109
+    @get(jid)
110
+
107 111
   _switchContactBelongingList: (contact, fromList, toList) ->
108 112
     if !contact?
109 113
       false
... ...
@@ -52,3 +52,5 @@ class Xmpp.Views.Contacts.ContactView extends Backbone.View
52 52
 
53 53
     Backbone.Events.trigger('openChat', chat)
54 54
 
55
+  openChatById: (chatId) ->
56
+    chat = App.Collections.chats.findById(chatId)
55 57
\ No newline at end of file
... ...
@@ -1,5 +1,28 @@
1 1
 class WsChatController < WsController
2 2
 
3
+    def start_polling_messages
4
+        connection_store[:clients].each do |client|
5
+            client.add_message_callback do |message|
6
+                if message.body
7
+                    send_message 'app.chat.messageReceived',
8
+                                 from: message.from.strip.to_s,
9
+                                 to: message.to.strip.to_s,
10
+                                 message: message.body,
11
+                                 chat_id: if message.attribute(:is_simulating) then message.attribute(:chat_id) end
12
+                end
13
+                #TODO: upozornit na pisanie spravy
14
+                #TODO: odoslat informaciu o tom, ze pisem spravu
15
+                #else
16
+                #    send_message 'app.chat.messageState',
17
+                #                 state: message.chat_state,
18
+                #                 from: message.from.strip.to_s,
19
+                #                 to: message.to.strip.to_s,
20
+                #                 message: message.body,
21
+                #                 chat_id: if message.attribute(:is_simulating) then message.attribute(:chat_id) end
22
+            end
23
+        end
24
+    end
25
+
3 26
     def open_chat
4 27
         id = Time.now.to_f
5 28
         me = message[:chatOwner]
... ...
@@ -63,6 +63,7 @@ WebsocketRails::EventMap.describe do
63 63
             subscribe :newChatId,           to: WsChatController, with_method: :open_chat
64 64
             subscribe :addToChat,           to: WsChatController, with_method: :add_to_chat
65 65
             subscribe :sendMessage,         to: WsChatController, with_method: :send_chat_message
66
+            subscribe :startPollingMessages,to: WsChatController, with_method: :start_polling_messages
66 67
         end
67 68
     end
68 69
 end