Class: ChatsController
- Inherits:
-
ApplicationController
- Object
- ApplicationController
- ChatsController
- Includes:
- ActionView::Helpers::SanitizeHelper
- Defined in:
- lib/rails/generators/chatter/templates/chats_controller.rb
Overview
Don’t forget to set the session after login
Instance Method Summary collapse
- #chat_heartbeat ⇒ Object
- #close_chat ⇒ Object
- #samplea ⇒ Object
- #send_chat ⇒ Object
- #start_chat_session ⇒ Object
Instance Method Details
#chat_heartbeat ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/rails/generators/chatter/templates/chats_controller.rb', line 23 def chat_heartbeat items = [] Chat.find(:all, :conditions => {:to => session[:username], :recd => 0}, :order => 'id ASC').each do |chat| items = session[:chat_history][chat.from] if session[:open_chat_boxes][chat.from].nil? and session[:chat_history][chat.from] chat. = sanitize(chat.) items << {:s => 0, :f => chat.from, :m => chat.} session[:chat_history][chat.from] = [] if session[:chat_history][chat.from].nil? session[:chat_history][chat.from] << {:s => 0, :f => chat.from, :m => chat.} session[:ts_chat_boxes][chat.from] = nil session[:open_chat_boxes][chat.from] = chat.sent chat.recd = 1 chat.save! end if !session[:open_chat_boxes].empty? session[:open_chat_boxes].each do |chatbox, value| if session[:ts_chat_boxes][chatbox].nil? time = DateTime.parse(value.to_s) now = (DateTime.now - time) * 86400 stime = time.strftime("%I:%MAM %b #{time.day.ordinalize}") = "Sent at #{stime}" if now > 120 items << {:s => 2, :f => chatbox, :m => "{#{}}"} session[:chat_history][chatbox] = [] if session[:chat_history][chatbox].nil? session[:chat_history][chatbox] << {:s => 2, :f => chatbox, :m => "{#{}}"} session[:ts_chat_boxes][chatbox] = 1; end end end end render :json => {:items => items}.to_json end |
#close_chat ⇒ Object
100 101 102 103 104 |
# File 'lib/rails/generators/chatter/templates/chats_controller.rb', line 100 def close_chat #session[:open_chat_boxes][params[:chatbox]] = nil render :text => 1 end |
#samplea ⇒ Object
8 9 10 11 12 13 14 15 16 17 18 19 |
# File 'lib/rails/generators/chatter/templates/chats_controller.rb', line 8 def samplea if user_signed_in? Chat.update_status(current_user) session[:username] = current_user.username @onlineusers = User.find(:all, :conditions => ["status= 'online' and id != '#{current_user.id}'"]) end end |
#send_chat ⇒ Object
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/rails/generators/chatter/templates/chats_controller.rb', line 79 def send_chat from = current_user.username to = params[:to] = params[:message] session[:open_chat_boxes][to] = DateTime.now.strftime("%Y-%m-%d %H:%M:%S") = sanitize() session[:chat_history][to] = [] if session[:chat_history][to].nil? session[:chat_history][to] << {:s => 1, :f => "{#{to}}", :m => "#{}"} session[:ts_chat_boxes][to] = nil Chat.create(:from => from, :to => to, :message => , :sent => DateTime.now) render :text => 1 end |
#start_chat_session ⇒ Object
68 69 70 71 72 73 74 75 76 77 |
# File 'lib/rails/generators/chatter/templates/chats_controller.rb', line 68 def start_chat_session items = [] if !session[:open_chat_boxes].empty? session[:open_chat_boxes].each do |chatbox, item| items += chat_box_session(chatbox) end end render :json => {:username => current_user.try(:username).to_s, :items => items}.to_json end |