Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class ChatsController < ApplicationController
- require 'json'
- before_filter :non_administrator_only
- before_filter :login_required
- before_filter :prepare_job, :only => [:show]
- before_filter :prepare_chatter, :only => [:show]
- before_filter :prepare_interaction
- def show
- if request.xhr?
- render :json => %Q({interaction_id: #{@interaction.id},
- my_id: #{current_user.id},
- my_full_name: "#{current_user.full_name}",
- my_email: "#{current_user.email}",
- partner_id: #{@partner.id},
- partner_full_name: "#{@partner.full_name}",
- partner_email: "#{@partner.email}"
- })
- #else
- # redirect_to_communicate_path
- end
- end
- def sync
- @chat = @interaction.chat_logs.find :all,
- :conditions => ["id > ?", params[:last_fetch_id]]
- render :json => @chat
- end
- def update
- if params[:upload_session]
- asset = Asset.find_by_upload_session(params[:upload_session])
- file_size = file_size_to_word(asset.transfer_file_size)
- url_download = download_assets_url :sess_download => asset.upload_session
- params[:message] = "#{current_user.full_name} has upload a file
- <a href='#{url_download}' target='_blank'>
- <b> #{asset.transfer_file_name}</b>
- </a> (#{file_size})"
- sender_id = 0
- end
- if params[:shut_down] || params[:rejected]
- params[:message] = "Good Bye"
- sender_id = 0
- interaction = Interaction.find params[:interaction_id]
- duration = (Time.now.utc - interaction.started_at).to_i
- interaction.update_attributes(:ended_at=>Time.now.utc, :duration=>duration,:rejected=>params[:rejected])
- end
- sender_id = sender_id == 0 ? 0 : current_user.id
- @interaction.chat_logs << ChatLog.create(
- :interaction_id => params[:interaction_id],
- :sender_id => sender_id,
- :message => params[:message]
- )
- @interaction.update_attribute(:started_at, Time.now) unless @interaction.started?
- if params[:rejected]
- redirect_to interaction_path(interaction.id)
- else
- render :nothing => true, :status => :ok
- end
- end
- protected
- def file_size_to_word(file_size)
- if file_size/1024 < 1
- file_size.to_s + " byte"
- elsif file_size/1000000 < 1 && file_size/1024 > 1
- (file_size/1024).to_s + " KB"
- else
- (file_size/1000000).to_s + " MB"
- end
- end
- def prepare_job
- @job = current_user.associated_jobs params[:job_id]
- end
- def prepare_chatter
- @partner = User.find params[:partner_id]
- if current_user.has_role? "Client"
- @client = current_user
- @expert = @partner
- else
- @client = @partner
- @expert = current_user
- end
- end
- def prepare_interaction
- if action_name == "show"
- @interaction = Interaction.find_or_create_unfinished_interaction(
- @job.id,
- @expert.id,
- @client.id,
- params[:interaction_type_id])
- else
- @interaction = Interaction.find_by_id params[:interaction_id]
- end
- if !@interaction.is_continue_activity
- @requester = current_user.id == @interaction.client_id ? @interaction.try(:expert).try(:full_name) : @interaction.try(:client).try(:full_name)
- end
- @interaction_type_id = @interaction ? @interaction.interaction_type_id : params[:interaction_type_id]
- @interaction_id = @interaction.id
- end
- end
Add Comment
Please, Sign In to add comment