app/models/user.rb
531ecd5f
 class User
5fa8b94d
 	include Mongoid::Document
 
d33f6448
     embeds_many :accounts, cascade_callbacks: true
ca631758
     accepts_nested_attributes_for :accounts
 
     index({ 'accounts.jid' => 1 })
10098bcb
 
     def update_pass(jid, pass)
ca631758
         account_credentials = accounts.detect do |f|
0c651ead
             f[:jid] == jid || f['jid'] == jid
10098bcb
         end
 
         account_credentials[:pass] = pass
         save
     end
 
     def self.existing_jid(jid)
ca631758
         where('accounts.jid' => jid).only(:accounts).first
10098bcb
     end
 
     def self.create_jid(jid)
ca631758
         new_user = new()
         new_user.add_account(jid)
10098bcb
         new_user.save
         new_user
     end
 
ca631758
     def add_account(another_jid, password = nil)
         accounts << Account.new(jid: another_jid, pass: password)
10098bcb
         save
     end
0c651ead
 
     def self.crendentials_for_token(token)
d33f6448
         user = find_by_token(token)
         return user ? user.accounts : []
     end
 
     def self.find_by_token(token)
0c651ead
         found = Token.where(token: token).only(:user_id).limit(1).first
d33f6448
 
0c651ead
         if found
d33f6448
             return find(found.user_id)
0c651ead
         else
d33f6448
             return nil
0c651ead
         end
ca631758
     end
 
f6071db7
     def self.can_save_conversation?(my_jid, friend_jid, chat_id = nil)
d33f6448
         if chat_id.blank?
             found = User.where('accounts.jid' => my_jid)
                         .elem_match('accounts.conversation_settings' => {jid: friend_jid, value: 0})
         else
             found = User.where('accounts.jid' => my_jid)
                         .elem_match('accounts.conversation_settings' => {chat_id: chat_id, value: 0})
         end
ca631758
 
d33f6448
         found.first.nil?
0c651ead
     end
f6071db7
 
     def self.set_history_saving(enable, my_jid, friend_jid, chat_id = nil)
         Rails.logger.debug [ enable, my_jid, friend_jid, chat_id]
         if chat_id.blank?
             user = User.where('accounts.jid' => my_jid)
                            .elem_match('accounts.conversation_settings' => {jid: friend_jid})
                            .only('accounts.$.jid')
                            .first
 
             if user.nil?
                 user = User.where('accounts.jid' => my_jid).only('accounts.$.jid').first
                 new_settings = {
                     jid: friend_jid,
                     value: enable
                 }
                 user.accounts.first.conversation_settings << new_settings
             else
                 settings = user.accounts.first.conversation_settings.find do |friend|
                     friend['jid'] == friend_jid
                 end
                 settings['value'] = enable
             end
 
             user.save
         else
             user = User.where('accounts.jid' => my_jid)
                         .elem_match('accounts.conversation_settings' => {chat_id: chat_id})
                         .only('accounts.$.jid')
                         .first
 
             if user.nil?
                 user = User.where('accounts.jid' => my_jid).only('accounts.$.jid').first
                 new_settings = {
                     chat_id: chat_id,
                     value: enable
                 }
                 user.accounts.first.conversation_settings << new_settings
             else
                 settings = user.accounts.first.conversation_settings.find do |chat|
                     chat['chat_id'] == chat_id
                 end
                 settings['value'] = enable
             end
 
             user.save
         end
     end
5fa8b94d
 end