Browse code

Ukladanie historie konverzacii

Cinan Rakosnik authored on 12/05/2013 at 22:51:26
Showing 3 changed files
... ...
@@ -126,6 +126,13 @@ class WsChatController < WsController
126 126
                 client.send(message)
127 127
             end
128 128
 
129
+            stripped_me = client.jid.strip.to_s
130
+            stripped_to = Jabber::JID.new(message[:to]).strip!.to_s
131
+            if User.can_save_conversation(stripped_me, stripped_to, chat_id)
132
+                Rails.logger.debug ['saving send message', stripped_me]
133
+                History.save_message(stripped_me, stripped_me, message[:message], stripped_to, chat_id)
134
+            end
135
+
129 136
             trigger_success message[:message]
130 137
         else
131 138
             trigger_failure
... ...
@@ -196,10 +203,18 @@ class WsChatController < WsController
196 196
 
197 197
     private
198 198
 
199
-    def process_incoming_message(from, to, body, chat_id = nil)
199
+    def process_incoming_message(from, me, body, chat_id = nil)
200
+        stripped_me = me.strip.to_s
201
+        stripped_from = from.strip.to_s
202
+
203
+        if User.can_save_conversation(stripped_me, stripped_from, chat_id)
204
+            Rails.logger.debug ['saving received message', stripped_me, chat_id]
205
+            History.save_message(stripped_me, stripped_from, body, stripped_from, chat_id)
206
+        end
207
+
200 208
         send_message 'app.chat.messageReceived',
201 209
                      from: from.to_s,
202
-                     to: to.strip.to_s,
210
+                     to: stripped_me,
203 211
                      message: body,
204 212
                      chat_id: chat_id
205 213
     end
... ...
@@ -3,6 +3,7 @@ class Account
3 3
 
4 4
     field :jid, type: String
5 5
     field :pass, type: String
6
+    field :conversation_settings, type: Array, default: []
6 7
 
7 8
     embedded_in :user
8 9
 end
9 10
\ No newline at end of file
... ...
@@ -1,7 +1,7 @@
1 1
 class User
2 2
 	include Mongoid::Document
3 3
 
4
-    embeds_many :accounts
4
+    embeds_many :accounts, cascade_callbacks: true
5 5
     accepts_nested_attributes_for :accounts
6 6
 
7 7
     index({ 'accounts.jid' => 1 })
... ...
@@ -32,18 +32,29 @@ class User
32 32
     end
33 33
 
34 34
     def self.crendentials_for_token(token)
35
+        user = find_by_token(token)
36
+        return user ? user.accounts : []
37
+    end
38
+
39
+    def self.find_by_token(token)
35 40
         found = Token.where(token: token).only(:user_id).limit(1).first
41
+
36 42
         if found
37
-            user_id = found.user_id
43
+            return find(found.user_id)
38 44
         else
39
-            return []
45
+            return nil
40 46
         end
41
-
42
-        user = where(id: user_id).only(:accounts).first
43
-        return user ? user.accounts : []
44 47
     end
45 48
 
46 49
     def self.can_save_conversation(my_jid, friend_jid, chat_id = nil)
50
+        if chat_id.blank?
51
+            found = User.where('accounts.jid' => my_jid)
52
+                        .elem_match('accounts.conversation_settings' => {jid: friend_jid, value: 0})
53
+        else
54
+            found = User.where('accounts.jid' => my_jid)
55
+                        .elem_match('accounts.conversation_settings' => {chat_id: chat_id, value: 0})
56
+        end
47 57
 
58
+        found.first.nil?
48 59
     end
49 60
 end
50 61
\ No newline at end of file