Browse code

Zmena nastaveni ukladania historie z frontendu

Cinan Rakosnik authored on 25/05/2013 at 15:47:15
Showing 7 changed files
... ...
@@ -137,6 +137,12 @@ this.App =
137 137
     answerFriendRequest: (jid, answer) ->
138 138
       App.Com.trigger(event: 'app.roster.answerFriendRequest', data: { jid: jid, answer: answer})
139 139
 
140
+    checkSavingHistory: (me, chatId, attendant, onSuccess) ->
141
+      App.Com.trigger(event: 'app.chat.canSaveHistory', data: {me: me, chatId: chatId, attendant: attendant}, success: onSuccess)
142
+
143
+    setHistory: (me, chatId, attendant, state, onSuccess) ->
144
+      App.Com.trigger(event: 'app.chat.setHistorySaving', data: {me: me, chatId: chatId, attendant: attendant, enable: state}, success: onSuccess)
145
+
140 146
     _setupBackboneComponents: ->
141 147
       App.Collections.contacts = new Xmpp.Collections.ContactsCollection()
142 148
       App.Collections.chats = new Xmpp.Collections.ChatsCollection()
... ...
@@ -2,9 +2,11 @@
2 2
   .history-settings
3 3
     %a.left{href: '#'} Load history
4 4
 
5
-    %input.right{ type: 'checkbox', name: 'enable-history', value: '1', id: 'ihistory' }
6
-    %label.right{ for: 'ihistory' } Disable history
7
-    %span.right.current History recording is enabled.
5
+    %input.hidden{ type: 'checkbox', name: 'enable-history', value: '1', id: 'ihistory', checked: 'checked'}
6
+    %label.right.action.enabled{ for: 'ihistory' } Disable history
7
+    %label.right.action.disabled.hidden{ for: 'ihistory' } Enable history
8
+    %span.right.current.enabled History recording is enabled.
9
+    %span.right.current.disabled.hidden History recording is disabled.
8 10
   .messages#height-setter-2
9 11
     -for view in @history
10 12
       #{view}
... ...
@@ -6,20 +6,31 @@ class Xmpp.Views.Chat.WindowView extends Backbone.View
6 6
   inputSelector: 'input[type=text]'
7 7
 
8 8
   maxHistoryLength: 20
9
+  savingHistory: true
9 10
 
10 11
   events:
11 12
     'submit #msg-writer': (e) -> @sendMessage(e)
13
+    'change .history-settings input': 'changeHistorySettings'
12 14
 
13
-  initialize: () ->
15
+  initialize: (options) ->
14 16
     _.bindAll(this)
15
-    @tab = @attributes['tab']
16 17
     @historyStack = []
18
+    @tab = options.tab
19
+
20
+    chatId = @tab.getChatId()
21
+    attendant = if not chatId then @tab.getAttendant().getPreferredResource() else null
22
+    App.Com.checkSavingHistory(App.Models.me.get('jid'), chatId, attendant, (canSave) =>
23
+      @savingHistory = canSave
24
+      @_changeHistoryVisibly(@savingHistory)
25
+    )
17 26
 
18 27
   render: ->
19 28
     historyStackHtml = _.map(@historyStack, (view) ->
20 29
       view.render().el.outerHTML
21 30
     )
22 31
     $(@el).html(@template(showWriter: true, history: historyStackHtml))
32
+    @_changeHistoryVisibly(@savingHistory)
33
+
23 34
     @show()
24 35
     return this
25 36
 
... ...
@@ -80,4 +91,26 @@ class Xmpp.Views.Chat.WindowView extends Backbone.View
80 80
     if (@historyStack.length + 1 >= @maxHistoryLength)
81 81
       @historyStack = @historyStack.slice(@historyStack.length - @maxHistoryLength + 1)
82 82
 
83
-    @historyStack.push(view)
84 83
\ No newline at end of file
84
+    @historyStack.push(view)
85
+
86
+  changeHistorySettings: (e) ->
87
+    $input = $(e.currentTarget)
88
+    state = $input.is(':checked')
89
+
90
+    chatId = @tab.getChatId()
91
+    attendant = if not chatId then @tab.getAttendant().getPreferredResource() else null
92
+    App.Com.setHistory(App.Models.me.get('jid'), chatId, attendant, state, =>
93
+      @savingHistory = state
94
+      @_changeHistoryVisibly(state)
95
+    )
96
+
97
+  _changeHistoryVisibly: (currentState) ->
98
+    if currentState
99
+      toEnable = '.enabled'
100
+      toDisable = '.disabled'
101
+    else
102
+      toEnable = '.disabled'
103
+      toDisable = '.enabled'
104
+
105
+    $(@el).find(".action#{toEnable}, .current#{toEnable}").show()
106
+    $(@el).find(".action#{toDisable}, .current#{toDisable}").hide()
85 107
\ No newline at end of file
... ...
@@ -56,7 +56,7 @@ class Xmpp.Views.Tabbar.TabView extends Backbone.View
56 56
 
57 57
   showChat: ->
58 58
     if !@chatWindow
59
-      @chatWindow = new Xmpp.Views.Chat.WindowView(attributes: {tab: this})
59
+      @chatWindow = new Xmpp.Views.Chat.WindowView(tab: this)
60 60
       App.debug 'zobrazujem NOVY chat window'
61 61
     else
62 62
       App.debug 'zobrazujem EXISTUJUCI chat window'
... ...
@@ -128,7 +128,7 @@ class WsChatController < WsController
128 128
 
129 129
             stripped_me = client.jid.strip.to_s
130 130
             stripped_to = Jabber::JID.new(message[:to]).strip!.to_s
131
-            if User.can_save_conversation(stripped_me, stripped_to, chat_id)
131
+            if can_save_conversation?(stripped_me, stripped_to, chat_id)
132 132
                 Rails.logger.debug ['saving send message', stripped_me]
133 133
                 History.save_message(stripped_me, stripped_me, message[:message], stripped_to, chat_id)
134 134
             end
... ...
@@ -201,13 +201,40 @@ class WsChatController < WsController
201 201
         client.send(MessageBuilder::make_new_owner(client.jid.to_s, new_owner_jid, chat_id)) if new_owner_jid
202 202
     end
203 203
 
204
+    def can_save_conversation?(stripped_me = nil, stripped_attendant = nil, chat_id = nil)
205
+        me = message[:me] || stripped_me
206
+        attendant = message[:attendant] || stripped_attendant
207
+        _chat_id = message[:chatId] || chat_id
208
+
209
+        result = User.can_save_conversation?(me, attendant, _chat_id)
210
+
211
+        if message[:me]
212
+            trigger_success result
213
+        else
214
+            result
215
+        end
216
+    end
217
+
218
+    def set_history_saving
219
+        save = message[:enable]
220
+        me = message[:me]
221
+        attendant = message[:attendant]
222
+        chat_id = message[:chatId]
223
+
224
+        trigger_failure unless find_client(me)
225
+
226
+        save = save.is_a?(FalseClass) ? 0 : 1
227
+
228
+        User.set_history_saving(save, me, attendant, chat_id) && trigger_success
229
+    end
230
+
204 231
     private
205 232
 
206 233
     def process_incoming_message(from, me, body, chat_id = nil)
207 234
         stripped_me = me.strip.to_s
208 235
         stripped_from = from.strip.to_s
209 236
 
210
-        if User.can_save_conversation(stripped_me, stripped_from, chat_id)
237
+        if can_save_conversation?(stripped_me, stripped_from, chat_id)
211 238
             Rails.logger.debug ['saving received message', stripped_me, chat_id]
212 239
             History.save_message(stripped_me, stripped_from, body, stripped_from, chat_id)
213 240
         end
... ...
@@ -46,7 +46,7 @@ class User
46 46
         end
47 47
     end
48 48
 
49
-    def self.can_save_conversation(my_jid, friend_jid, chat_id = nil)
49
+    def self.can_save_conversation?(my_jid, friend_jid, chat_id = nil)
50 50
         if chat_id.blank?
51 51
             found = User.where('accounts.jid' => my_jid)
52 52
                         .elem_match('accounts.conversation_settings' => {jid: friend_jid, value: 0})
... ...
@@ -57,4 +57,51 @@ class User
57 57
 
58 58
         found.first.nil?
59 59
     end
60
+
61
+    def self.set_history_saving(enable, my_jid, friend_jid, chat_id = nil)
62
+        Rails.logger.debug [ enable, my_jid, friend_jid, chat_id]
63
+        if chat_id.blank?
64
+            user = User.where('accounts.jid' => my_jid)
65
+                           .elem_match('accounts.conversation_settings' => {jid: friend_jid})
66
+                           .only('accounts.$.jid')
67
+                           .first
68
+
69
+            if user.nil?
70
+                user = User.where('accounts.jid' => my_jid).only('accounts.$.jid').first
71
+                new_settings = {
72
+                    jid: friend_jid,
73
+                    value: enable
74
+                }
75
+                user.accounts.first.conversation_settings << new_settings
76
+            else
77
+                settings = user.accounts.first.conversation_settings.find do |friend|
78
+                    friend['jid'] == friend_jid
79
+                end
80
+                settings['value'] = enable
81
+            end
82
+
83
+            user.save
84
+        else
85
+            user = User.where('accounts.jid' => my_jid)
86
+                        .elem_match('accounts.conversation_settings' => {chat_id: chat_id})
87
+                        .only('accounts.$.jid')
88
+                        .first
89
+
90
+            if user.nil?
91
+                user = User.where('accounts.jid' => my_jid).only('accounts.$.jid').first
92
+                new_settings = {
93
+                    chat_id: chat_id,
94
+                    value: enable
95
+                }
96
+                user.accounts.first.conversation_settings << new_settings
97
+            else
98
+                settings = user.accounts.first.conversation_settings.find do |chat|
99
+                    chat['chat_id'] == chat_id
100
+                end
101
+                settings['value'] = enable
102
+            end
103
+
104
+            user.save
105
+        end
106
+    end
60 107
 end
61 108
\ No newline at end of file
... ...
@@ -70,6 +70,8 @@ WebsocketRails::EventMap.describe do
70 70
             subscribe :iClosedMultichat,      to: WsChatController, with_method: :i_closed_multichat
71 71
             subscribe :kickFromMultichat,     to: WsChatController, with_method: :kick_from_multichat
72 72
             subscribe :switchOwnership,       to: WsChatController, with_method: :switch_ownership
73
+            subscribe :canSaveHistory,        to: WsChatController, with_method: :can_save_conversation?
74
+            subscribe :setHistorySaving,      to: WsChatController, with_method: :set_history_saving
73 75
         end
74 76
     end
75 77
 end