Browse code

Pridavanie novych kontaktov do rostera

Upravene boli styly pre popup, aplikacia dokaze prijimat a potvrdzovat ziadosti o priatelstvo od cudzich ludi.

Cinan Rakosnik authored on 20/05/2013 at 13:25:58
Showing 14 changed files
... ...
@@ -130,6 +130,12 @@ this.App =
130 130
     removeContactRemote: (contact, client) ->
131 131
       App.Com.trigger(event: 'app.roster.removeContact', data: {jid: contact, client: client})
132 132
 
133
+    addContact: (jid) ->
134
+      App.Com.trigger(event: 'app.roster.addContact', data: {jid: jid})
135
+
136
+    answerFriendRequest: (jid, answer) ->
137
+      App.Com.trigger(event: 'app.roster.answerFriendRequest', data: { jid: jid, answer: answer})
138
+
133 139
     _setupBackboneComponents: ->
134 140
       App.Collections.contacts = new Xmpp.Collections.ContactsCollection()
135 141
       App.Collections.chats = new Xmpp.Collections.ChatsCollection()
... ...
@@ -190,6 +196,12 @@ this.App =
190 190
           chat.set('withWhom', attendants)
191 191
       )
192 192
 
193
+      App._dispatcher.bind('app.roster.friendRequest', (request) ->
194
+        App.debug ['receiving friend request', request]
195
+        popup = new Xmpp.Views.Popup.FriendRequestView(jid: request.jid, name: request.name)
196
+        popup.render()
197
+      )
198
+
193 199
       App._dispatcher.bind('app.chat.importChat', (chatData) ->
194 200
         chatId = chatData.chat_id
195 201
         chat = App.Collections.chats.findById(chatId)
196 202
deleted file mode 100644
... ...
@@ -1,11 +0,0 @@
1
-%h1 Add new account
2
-%form{ action: '#', id: 'asd' }
3
-  %label{for: 'jid'}
4
-    %strong Jabber ID
5
-  %input{type: 'text', name: 'jid', class: 'text', placeholder: 'nick@example.com'}
6
-
7
-  %label{for: 'pass'}
8
-    %strong Password
9
-  %input{type: 'password', name: 'password', class: 'text'}
10
-
11
-  %input{type: 'submit', value: "#{I18n.t('sessions.new.form-send')}", class: 'button'}
12 1
\ No newline at end of file
13 2
new file mode 100644
... ...
@@ -0,0 +1,7 @@
0
+%h1 Add a new contact
1
+%form{ action: '#' }
2
+  %label{for: 'jid'}
3
+    %strong Jabber ID
4
+  %input{type: 'text', name: 'jid', class: 'text', placeholder: 'nick@example.com'}
5
+
6
+  %input{type: 'submit', value: "Send friend request", class: 'button'}
0 7
\ No newline at end of file
1 8
new file mode 100644
... ...
@@ -0,0 +1,7 @@
0
+%h1 Friend request
1
+%form{ action: '#' }
2
+  %strong #{@new_contact}
3
+  wants to add you to his contacts. Allow?
4
+  %br
5
+  %input.left{type: 'submit', value: "Yes", name: '1', class: 'button', style: 'width: 60%'}
6
+  %input.close.inactive{type: 'button', value: "No", style: 'width: 30%; padding: 0'}
0 7
\ No newline at end of file
... ...
@@ -4,5 +4,16 @@ class Xmpp.Views.Popup.PopupView extends Backbone.View
4 4
   superTemplate: JST["backbone/templates/popup/popup"]
5 5
   el: $('#js-popup')
6 6
 
7
+  superEvents:
8
+    'click .close': 'close'
9
+
7 10
   render: (childHtml) ->
8
-    $(@el).html(@superTemplate()).append(childHtml)
9 11
\ No newline at end of file
12
+    $(@el).html(@superTemplate()).append(childHtml)
13
+
14
+  close: (e) ->
15
+    e.preventDefault()
16
+    @remove()
17
+
18
+  remove: ->
19
+    @stopListening()
20
+    $(@el).empty()
10 21
\ No newline at end of file
11 22
new file mode 100644
... ...
@@ -0,0 +1,25 @@
0
+Xmpp.Views.Popup ||= {}
1
+
2
+class Xmpp.Views.Popup.AddContactView extends Xmpp.Views.Popup.PopupView
3
+  template: JST["backbone/templates/popup/add_contact"]
4
+
5
+  events:
6
+    'submit form': 'addContact'
7
+
8
+  initialize: ->
9
+    @events = _.extend(@events, @superEvents)
10
+
11
+  render: ->
12
+    super(@template())
13
+    $(@el).css(width: '20%', 'z-index': 999)
14
+    $(@el).find('input[type=text]').focus()
15
+    this
16
+
17
+  addContact: (e) ->
18
+    e.preventDefault()
19
+    jid = $(@el).find('input[name=jid]').val()
20
+    r = new RegExp('^.+@.+\..+$')
21
+    match = r.exec(jid)
22
+    if match and (match.length > 0)
23
+      jid = App.stripJid(match[0])
24
+      App.Com.addContact(jid)
0 25
new file mode 100644
... ...
@@ -0,0 +1,31 @@
0
+Xmpp.Views.Popup ||= {}
1
+
2
+class Xmpp.Views.Popup.FriendRequestView extends Xmpp.Views.Popup.PopupView
3
+  template: JST["backbone/templates/popup/friend_request"]
4
+
5
+  events:
6
+    'submit form': 'approve'
7
+
8
+  initialize: (options) ->
9
+    @name = options.name
10
+    @jid  = options.jid
11
+
12
+    @events = _.extend(@events, @superEvents)
13
+
14
+  render: ->
15
+    super(@template(new_contact: @name))
16
+    $(@el).css(width: '20%', 'z-index': 999)
17
+    $(@el).find('input[type=text]').focus()
18
+    this
19
+
20
+  approve: (e) ->
21
+    e.preventDefault()
22
+    App.debug 'approve request'
23
+    App.Com.answerFriendRequest(@jid, true)
24
+    @remove()
25
+
26
+  close: (e) ->
27
+    e.preventDefault()
28
+    App.debug 'cancel request'
29
+    App.Com.answerFriendRequest(@jid, false)
30
+    @remove()
0 31
\ No newline at end of file
1 32
deleted file mode 100644
... ...
@@ -1,26 +0,0 @@
1
-Xmpp.Views.Popup ||= {}
2
-
3
-class Xmpp.Views.Popup.NewAccountView extends Xmpp.Views.Popup.PopupView
4
-  template: JST["backbone/templates/popup/add_account"]
5
-
6
-  events:
7
-    'submit form': 'addAccount'
8
-
9
-  render: ->
10
-    super(@template())
11
-    $(@el).css(width: '20%')
12
-
13
-  addAccount: (e) ->
14
-    e.preventDefault()
15
-    jid = $(@el).find('input[name=jid]').val()
16
-    pass = $(@el).find('input[name=password]').val()
17
-    $.ajax(
18
-      url: '/sessions'
19
-      type: 'POST'
20
-      data:
21
-        jid: jid,
22
-        password: pass,
23
-        override: 1
24
-      success: (msg) ->
25
-        App.debug msg
26
-    )
27 1
\ No newline at end of file
... ...
@@ -10,8 +10,15 @@ $().ready(->
10 10
     App.UI.filterContacts(searchTerm)
11 11
   )
12 12
 
13
-  $('.chat-alert').on('click', '.close', (e) ->
13
+#  $('.chat-alert').on('click', '.close', (e) ->
14
+#    e.preventDefault()
15
+#    $('.chat-alert').fadeOut(250, -> $(this).html(''))
16
+#  )
17
+
18
+  $('#add-contact').click((e) ->
14 19
     e.preventDefault()
15
-    $('.chat-alert').fadeOut(250, -> $(this).html(''))
20
+    $('#js-popup .close').click()
21
+    popup = new Xmpp.Views.Popup.AddContactView()
22
+    popup.render()
16 23
   )
17 24
 )
18 25
\ No newline at end of file
... ...
@@ -37,15 +37,28 @@ input {
37 37
   height: 30px;
38 38
   @include box-sizing(border-box);
39 39
 
40
-  &[type=submit] {
40
+  &[type=submit], &[type=button] {
41 41
     text-transform:lowercase;
42 42
     font-variant: small-caps;
43 43
     font-weight: bold;
44 44
     font-size: 18px;
45
+    margin-top: 16px;
46
+  }
47
+
48
+  &[type=submit] {
45 49
     background: $app-color;
46 50
     color: #f9f9f9;
47 51
     border: none 0px #000000;
48
-    margin-top: 16px;
52
+  }
53
+
54
+  &[type=button] {
55
+    background: $app-color;
56
+    color: #f9f9f9;
57
+    border: none 0px #000000;
58
+  }
59
+
60
+  &.inactive {
61
+    background-color: $inactive-color;
49 62
   }
50 63
 
51 64
   &:focus {
... ...
@@ -378,10 +378,10 @@
378 378
     color: $app-color;
379 379
     border: 1px solid #dde0e3;
380 380
     border-radius: 12px;
381
-    padding: 5px 4px 4px 5px;
381
+    padding: 4px 4px 4px 5px;
382 382
     margin-left: 0;
383 383
     width: 13px;
384
-    height: 13px;
384
+    height: 14px;
385 385
     background: white;
386 386
     display: inline-block;
387 387
   }
... ...
@@ -87,7 +87,7 @@ class WsRosterController < WsController
87 87
 
88 88
             roster.items.each do |jid, contact|
89 89
                 Thread.new do
90
-                    vcard = get_vcard(client, jid.to_s)
90
+                    vcard = get_vcard_info(client, jid.to_s)
91 91
                     send_message 'app.roster.vcard', jid: jid.to_s, vcard: vcard
92 92
                 end
93 93
             end
... ...
@@ -102,6 +102,7 @@ class WsRosterController < WsController
102 102
         connection_store[:rosters].each do |roster|
103 103
             start_polling_subscription(roster)
104 104
             start_polling_presence(roster)
105
+            start_polling_friend_requests(roster)
105 106
         end
106 107
     end
107 108
 
... ...
@@ -150,7 +151,7 @@ class WsRosterController < WsController
150 150
         jid = presence = ''
151 151
 
152 152
         connection_store[:clients].each do |client|
153
-            vcard = get_vcard(client)
153
+            vcard = get_vcard_info(client)
154 154
             jid = client.jid.strip.to_s
155 155
             presence = uniform_presence(connection_store[:my_presences][client].show)
156 156
         end
... ...
@@ -223,10 +224,30 @@ class WsRosterController < WsController
223 223
         end
224 224
     end
225 225
 
226
-    ##
227
-    # Pridaj noveho priatela do zoznamu
228
-    def add_friend(data)
226
+    def add_contact(jid = nil)
227
+        new_jid = message[:jid] || jid
228
+
229
+        if is_valid_jid? new_jid
230
+            connection_store[:rosters].each do |roster|
231
+                Thread.new do
232
+                    roster.add(new_jid, nil, true)
233
+                end
234
+            end
235
+        end
236
+    end
237
+
238
+    def answer_friend_request
239
+        jid = message[:jid]
240
+        answer = message[:answer]
229 241
 
242
+        connection_store[:rosters].each do |roster|
243
+            if answer
244
+                roster.accept_subscription(jid)
245
+                add_contact(jid)
246
+            else
247
+                roster.decline_subscription(jid)
248
+            end
249
+        end
230 250
     end
231 251
 
232 252
     private
... ...
@@ -243,7 +264,7 @@ class WsRosterController < WsController
243 243
         end].max.second
244 244
     end
245 245
 
246
-    def get_vcard(me, contact_jid = nil)
246
+    def get_vcard_info(me, contact_jid = nil)
247 247
         vcard = Jabber::Vcard::Helper.get(me, contact_jid)
248 248
 
249 249
         { name: pull_name_from_vcard(vcard) || contact_jid,
... ...
@@ -333,7 +354,21 @@ class WsRosterController < WsController
333 333
         end
334 334
     end
335 335
 
336
+    def start_polling_friend_requests(roster)
337
+        client = connection_store[:link_roster_client][roster]
338
+        roster.add_subscription_request_callback do |roster_item, stanza|
339
+            jid = stanza.from.to_s
340
+            send_message 'app.roster.friendRequest',
341
+                         jid: jid,
342
+                         name: get_vcard_info(client, jid)[:name]
343
+        end
344
+    end
345
+
336 346
     def ask_if_using_this_app(client, contact)
337 347
         client.send(MessageBuilder::control_question(client.jid.to_s, contact))
338 348
     end
349
+
350
+    def is_valid_jid?(jid)
351
+        !! jid.scan(/^\w+@\w+\.\w+$/).first
352
+    end
339 353
 end
... ...
@@ -22,8 +22,8 @@
22 22
           #js-inactive-friends
23 23
           .toolbox
24 24
             %input#js-search-contacts{type: 'search', placeholder: "#{I18n.t('chat.roster.search')}", name: 'contact'}
25
-            %a{ href: '#'}
26
-              %span.icon-cog
25
+            %a{ href: '#', title: 'Add new contact', id: 'add-contact' }
26
+              %span.icon-plus
27 27
   .chatting-window.left.rightside
28 28
     .tabbar#tabbar
29 29
       .wrap#js-tabbar
... ...
@@ -57,6 +57,8 @@ 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 :addContact,          to: WsRosterController, with_method: :add_contact
61
+            subscribe :answerFriendRequest, to: WsRosterController, with_method: :answer_friend_request
60 62
         end
61 63
 
62 64
         namespace :chat do