Browse code

Zobrazovanie ludi na frontende

Zdaleka nie je dokoncene. Funguje vlastne len zobrazovanie jid.

Cinan Rakosnik authored on 23/03/2013 at 17:11:26
Showing 14 changed files
... ...
@@ -6,9 +6,28 @@ this.App =
6 6
       $("#height-setter-2").css({height: height - $('#tabbar').outerHeight() - $('#msg-writer').outerHeight() - 57})
7 7
 
8 8
   Com:
9
-    connect: ->
10
-      App._dispatcher = new WebSocketRails('xmpp.dev:3000/websocket')
11
-      App._dispatcher.trigger('app.roster.connect')
9
+    connect: (callback) ->
10
+      App._dispatcher = new WebSocketRails('www.xmpp.dev:3000/websocket')
11
+      App._dispatcher.on_open = =>
12
+        @_setupBackboneComponents()
13
+        callback?()
12 14
 
13 15
     fetch: (options) ->
16
+      _.defaults(options, {data: {} })
14 17
       App._dispatcher.trigger(options.event, options.data, options.success, options.error)
18
+
19
+    getRoster: ->
20
+      App.Com.fetch({
21
+        event: 'app.roster.contacts'
22
+        success: (data, contacts = data.contacts) ->
23
+          _.each(contacts, (contact) ->
24
+            newContact = new Xmpp.Models.Contact(contact)
25
+            App.Collections.Contacts.add newContact
26
+          )
27
+      })
28
+
29
+    _setupBackboneComponents: ->
30
+      App.Collections.Contacts = new Xmpp.Collections.ContactsCollection()
31
+
32
+  Collections:
33
+    Contacts: null
... ...
@@ -1,5 +1,3 @@
1 1
 #= require jquery
2 2
 #= require jquery_ujs
3
-#= require _main
4
-#= require chat
5 3
 
... ...
@@ -1,8 +1,16 @@
1 1
 #= require websocket_rails/main
2
+#= require hamlcoffee
2 3
 #= require underscore
3 4
 #= require backbone
4 5
 #= require backbone_rails_sync
5 6
 #= require backbone_datalink
6 7
 #= require backbone/xmpp
7 8
 
8
-App.Com.connect()
9
+#= require _main
10
+#= require chat
11
+
12
+App.Com.connect(->
13
+  App.Com.getRoster()
14
+  #= require i18n
15
+  #= require i18n/translations
16
+)
... ...
@@ -2,16 +2,23 @@ class Xmpp.Models.Contact extends Xmpp.Models.Model
2 2
   namespace: 'app.roster'
3 3
 
4 4
   defaults:
5
-    name: 'unnamed'
6
-    age: 0
5
+    status: ''
6
+    state: 'online'
7 7
 
8 8
   initialize: ->
9
-    console.log "Welcome stranger"
10
-
11
-    @on("change:name", (model) ->
12
-      name = model.get("name");
13
-      console.log "Changed my name to " + name
14
-    )
9
+    _.bindAll(this)
15 10
 
16 11
 class Xmpp.Collections.ContactsCollection extends Backbone.Collection
17 12
   model: Xmpp.Models.Contact
13
+
14
+  initialize: ->
15
+    _.bindAll(this)
16
+    @counter = 0
17
+    @friendsList = new Xmpp.Views.Contacts.ListView(collection: this, attributes: {title: 'chat.roster.friends', id: 'js-inactive-friends'})
18
+#    @activeList  = new Xmpp.Views.Contacts.ListView(collection: this, attributes: {title: 'chat.roster.chat-group', id: 'js-active-friends'})
19
+
20
+    @on("add", @appendContact)
21
+
22
+  appendContact: (contact) ->
23
+    @counter++
24
+    @friendsList.appendContact(contact)
18 25
\ No newline at end of file
... ...
@@ -1,6 +1,4 @@
1 1
 class Xmpp.Models.Model extends Backbone.Model
2
-  communicator: App.Com
3
-
4 2
   sync: (method, model, options) ->
5 3
     params = {}
6 4
 
... ...
@@ -24,10 +22,7 @@ class Xmpp.Models.Model extends Backbone.Model
24 24
         error(model, xhr, options)
25 25
       model.trigger('error', model, xhr, options);
26 26
 
27
-    if (! @communicator?)
28
-      throw new Error('A communicator object must be specified');
29
-
30
-    result = options.result = @communicator.fetch(_.extend(params, options))
27
+    result = options.result = App.Com.fetch(_.extend(params, options))
31 28
 
32 29
     model.trigger('request', model, result, options)
33 30
     return result
34 31
\ No newline at end of file
35 32
new file mode 100644
... ...
@@ -0,0 +1,31 @@
0
+%li.clear.user
1
+  .avatar
2
+    %img{src: 'assets/avatar.png', alt: ("t chat.roster.avatar_alt") }
3
+  %h1
4
+    #{@jid}
5
+    -#{@state}
6
+    %span.status.icon-record{class: "#{@state}"}
7
+  %h2 #{@jid}
8
+  -#%span.action
9
+    %span.icon-bubbles
10
+      %span invite
11
+-#%li.clear.user
12
+-#  .avatar
13
+-#    %img{src: 'assets/avatar.png', alt: t("chat.avatar_alt")}
14
+-#  %h1
15
+-#    Bob Awesome
16
+-#    %span.status.dnd.icon-record
17
+-#  %h2 sales manager
18
+-#  %span.action
19
+-#    %span.icon-x-full
20
+-#      %span kick
21
+-#%li.clear.user
22
+-#  .avatar
23
+-#    %img{src: 'assets/avatar.png', alt: t("chat.avatar_alt")}
24
+-#  %h1
25
+-#    Bob Awesome
26
+-#    %span.status.dnd.icon-record
27
+-#  %h2 sales manager
28
+-#  %span.action
29
+-#    %span.icon-bubble
30
+-#      %span chat
0 31
new file mode 100644
... ...
@@ -0,0 +1,4 @@
0
+%h1.group-header #{@title}
1
+%ul
2
+  -for contact in @contacts
3
+    != JST['backbone/templates/contacts/contact'](contact)
0 4
new file mode 100644
... ...
@@ -0,0 +1,18 @@
0
+Xmpp.Views.Contacts ||= {}
1
+
2
+class Xmpp.Views.Contacts.ContactView extends Backbone.View
3
+  template: JST["backbone/templates/contacts/contact"]
4
+
5
+#  events:
6
+#    'click li.user': 'something'
7
+
8
+  initialize: () ->
9
+    _.bindAll(this)
10
+
11
+    @el = '#' + @attributes['listView'].attributes['id'] + ' ul'
12
+
13
+    @render()
14
+
15
+  render: ->
16
+    contact = @model.toJSON()
17
+    $(@el).append(@template(contact))
0 18
new file mode 100644
... ...
@@ -0,0 +1,26 @@
0
+Xmpp.Views.Contacts ||= {}
1
+
2
+class Xmpp.Views.Contacts.ListView extends Backbone.View
3
+  template: JST["backbone/templates/contacts/contact_list"]
4
+
5
+  initialize: () ->
6
+    _.bindAll(this)
7
+
8
+    @el = '#' + @attributes['id']
9
+    @title = @attributes['title']
10
+
11
+    @contacts = []
12
+
13
+    @render()
14
+
15
+  appendContact: (contact) ->
16
+    @contacts.push = new Xmpp.Views.Contacts.ContactView(model: contact, attributes: {listView: this})
17
+
18
+  render: (contacts) ->
19
+    contacts ||= []
20
+    json_contacts = new Array(contacts.length)
21
+    _.each(contacts, (i, v) ->
22
+      json_contacts[i] = v.toJSON()
23
+    )
24
+
25
+    $(@el).html(@template(title: @title, contacts: json_contacts))
0 26
\ No newline at end of file
... ...
@@ -4,7 +4,7 @@ class SessionsController < ApplicationController
4 4
 	end
5 5
 
6 6
 	def create
7
-		require_dependency "signin.rb"
7
+		#require_dependency "signin.rb"
8 8
 
9 9
 		begin
10 10
 			Signin.try_login(params[:jid].downcase, params[:password])
... ...
@@ -1,31 +1,71 @@
1 1
 class WsRosterController < WebsocketRails::BaseController
2
-    #include Jabber
2
+    require 'xmpp4r/roster'
3
+    require 'xmpp4r/vcard'
3 4
 
4
-    def initialize_session
5
-        #controller_store[:friends] = {}
5
+    def initialize
6
+        super
7
+        @storages = [:contacts, :clients, :rosters]
8
+    end
9
+
10
+    def initialize_storage
11
+        @storages.each do |storage|
12
+            connection_store[storage] = {} if connection_store[storage].nil?
13
+            connection_store[storage][client_id] = []
14
+        end
6 15
     end
7 16
 
8 17
     def connect
18
+        initialize_storage()
19
+
20
+        clients = Token.fing_user_accounts_having_to_token(session[:token])
21
+
22
+        clients.each do |credentials|
23
+            begin
24
+                client = Signin.try_login(credentials["jid"], credentials["pass"])
25
+                client.send(Jabber::Presence.new.set_type(:available))
9 26
 
27
+                connection_store[:clients][client_id] << client
28
+                connection_store[:rosters][client_id] << Jabber::Roster::Helper.new(client)
29
+            rescue Signin::LoginError
30
+                trigger_failure msg: credentials["jid"]
31
+            end
32
+        end
10 33
     end
11 34
 
12 35
     ##
13 36
     # Vrat vsetkych priatelov v rosteri
14
-    def get_roster
15
-        #controller_store[:friends]
37
+    def get_contacts
38
+        all_contacts = []
39
+
40
+        connection_store[:rosters][client_id].each do |roster|
41
+            roster.wait_for_roster
42
+            roster.get_roster()
43
+
44
+            roster.items.each do |jid, contact|
45
+                all_contacts << {
46
+                    jid: jid.to_s
47
+                }
48
+            end
49
+        end
50
+
51
+        trigger_success contacts: all_contacts
16 52
     end
17 53
 
18
-    ##
19
-    # Pridaj noveho priatela do zoznamu
20
-    def add_friend(data)
54
+    def disconnect
55
+        if connection_store[:clients][client_id]
56
+            connection_store[:clients][client_id].each do |client|
57
+                client.send(Jabber::Presence.new.set_type(:unavailable))
58
+            end
59
+        end
21 60
 
61
+        @storages.each do |storage|
62
+            connection_store.delete(storage)
63
+        end
22 64
     end
23 65
 
24
-    private
66
+    ##
67
+    # Pridaj noveho priatela do zoznamu
68
+    def add_friend(data)
25 69
 
26
-    def login
27
-        Rails.logger.info session[:token].inspect
28
-        #user_id = Token.find_by(token: session[:token]).user_id
29
-        #Rails.logger.info "user ID" + user_id.inspect
30 70
     end
31 71
 end
... ...
@@ -21,7 +21,7 @@
21 21
             %span.icon-arrow-down.drop-down
22 22
       .friends
23 23
         %h1.group-header.active Chat group
24
-        %ul.active-group.border.no-left-border.left-border
24
+        %ul.active-group.border.no-left-border.left-border#js-active-friends
25 25
           %li.clear.user
26 26
             .avatar
27 27
               %img{src: 'assets/avatar.png', alt: t("chat.avatar_alt")}
... ...
@@ -44,39 +44,7 @@
44 44
                 %span kick
45 45
               %span.icon-bubbles
46 46
                 %span invite
47
-        %h1.group-header Friends
48
-        %ul
49
-          %li.clear.user
50
-            .avatar
51
-              %img{src: 'assets/avatar.png', alt: t("chat.avatar_alt")}
52
-            %h1
53
-              Ashley Bond
54
-              %span.status.offline.icon-record
55
-            %h2 sexretary
56
-            %span.action
57
-              %span.icon-bubbles
58
-                %span invite
59
-          %li.clear.user
60
-            .avatar
61
-              %img{src: 'assets/avatar.png', alt: t("chat.avatar_alt")}
62
-            %h1
63
-              Bob Awesome
64
-              %span.status.dnd.icon-record
65
-            %h2 sales manager
66
-            %span.action
67
-              %span.icon-x-full
68
-                %span kick
69
-          %li.clear.user
70
-            .avatar
71
-              %img{src: 'assets/avatar.png', alt: t("chat.avatar_alt")}
72
-            %h1
73
-              Bob Awesome
74
-              %span.status.dnd.icon-record
75
-            %h2 sales manager
76
-            %span.action
77
-              %span.icon-bubble
78
-                %span chat
79
-
47
+        #js-inactive-friends a
80 48
       .toolbox
81 49
         %input{type: "search", placeholder: "Search contact"}
82 50
         %a{ href: "#"}
... ...
@@ -42,11 +42,14 @@ WebsocketRails::EventMap.describe do
42 42
   #   end
43 43
   # The above will handle an event triggered on the client like `product.new`.
44 44
 
45
+    subscribe :client_connected, to: WsRosterController, with_method: :connect
46
+    subscribe :client_disconnected, to: WsRosterController, with_method: :disconnect
47
+
45 48
     namespace :app do
46 49
 
47 50
         namespace :roster do
48 51
             subscribe :connect, to: WsRosterController, with_method: :connect
49
-            subscribe :read, to: WsRosterController, with_method: :get
52
+            subscribe :contacts, to: WsRosterController, with_method: :get_contacts
50 53
         end
51 54
 
52 55
     end
... ...
@@ -15,4 +15,8 @@ en:
15 15
       form-send: "Login"
16 16
 
17 17
   chat:
18
-    title: "Chat"
19 18
\ No newline at end of file
19
+    title: "Chat"
20
+    roster:
21
+      friends: "Friends"
22
+      chat-group: "Chat group"
23
+      avatar_alt: "Avatar"
20 24
\ No newline at end of file