Guest User

Untitled

a guest
Jul 17th, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.77 KB | None | 0 0
  1. class ChatsController < ApplicationController
  2. require 'json'
  3.  
  4. before_filter :non_administrator_only
  5. before_filter :login_required
  6. before_filter :prepare_job, :only => [:show]
  7. before_filter :prepare_chatter, :only => [:show]
  8. before_filter :prepare_interaction
  9.  
  10. def show
  11. if request.xhr?
  12. render :json => %Q({interaction_id: #{@interaction.id},
  13. my_id: #{current_user.id},
  14. my_full_name: "#{current_user.full_name}",
  15. my_email: "#{current_user.email}",
  16. partner_id: #{@partner.id},
  17. partner_full_name: "#{@partner.full_name}",
  18. partner_email: "#{@partner.email}"
  19. })
  20. #else
  21. # redirect_to_communicate_path
  22. end
  23. end
  24.  
  25.  
  26. def sync
  27. @chat = @interaction.chat_logs.find :all,
  28. :conditions => ["id > ?", params[:last_fetch_id]]
  29.  
  30. render :json => @chat
  31. end
  32.  
  33. def update
  34. if params[:upload_session]
  35. asset = Asset.find_by_upload_session(params[:upload_session])
  36. file_size = file_size_to_word(asset.transfer_file_size)
  37. url_download = download_assets_url :sess_download => asset.upload_session
  38. params[:message] = "#{current_user.full_name} has upload a file
  39. <a href='#{url_download}' target='_blank'>
  40. <b> #{asset.transfer_file_name}</b>
  41. </a> (#{file_size})"
  42. sender_id = 0
  43. end
  44.  
  45. if params[:shut_down] || params[:rejected]
  46. params[:message] = "Good Bye"
  47. sender_id = 0
  48. interaction = Interaction.find params[:interaction_id]
  49. duration = (Time.now.utc - interaction.started_at).to_i
  50. interaction.update_attributes(:ended_at=>Time.now.utc, :duration=>duration,:rejected=>params[:rejected])
  51. end
  52.  
  53. sender_id = sender_id == 0 ? 0 : current_user.id
  54.  
  55. @interaction.chat_logs << ChatLog.create(
  56. :interaction_id => params[:interaction_id],
  57. :sender_id => sender_id,
  58. :message => params[:message]
  59. )
  60. @interaction.update_attribute(:started_at, Time.now) unless @interaction.started?
  61.  
  62. if params[:rejected]
  63. redirect_to interaction_path(interaction.id)
  64. else
  65. render :nothing => true, :status => :ok
  66. end
  67. end
  68.  
  69. protected
  70.  
  71. def file_size_to_word(file_size)
  72. if file_size/1024 < 1
  73. file_size.to_s + " byte"
  74. elsif file_size/1000000 < 1 && file_size/1024 > 1
  75. (file_size/1024).to_s + " KB"
  76. else
  77. (file_size/1000000).to_s + " MB"
  78. end
  79. end
  80.  
  81. def prepare_job
  82. @job = current_user.associated_jobs params[:job_id]
  83. end
  84.  
  85. def prepare_chatter
  86. @partner = User.find params[:partner_id]
  87.  
  88. if current_user.has_role? "Client"
  89. @client = current_user
  90. @expert = @partner
  91. else
  92. @client = @partner
  93. @expert = current_user
  94. end
  95. end
  96.  
  97. def prepare_interaction
  98. if action_name == "show"
  99. @interaction = Interaction.find_or_create_unfinished_interaction(
  100. @job.id,
  101. @expert.id,
  102. @client.id,
  103. params[:interaction_type_id])
  104. else
  105. @interaction = Interaction.find_by_id params[:interaction_id]
  106. end
  107.  
  108. if !@interaction.is_continue_activity
  109. @requester = current_user.id == @interaction.client_id ? @interaction.try(:expert).try(:full_name) : @interaction.try(:client).try(:full_name)
  110. end
  111.  
  112. @interaction_type_id = @interaction ? @interaction.interaction_type_id : params[:interaction_type_id]
  113. @interaction_id = @interaction.id
  114. end
  115. end
Add Comment
Please, Sign In to add comment