Class: MessagingController

Inherits:
KitController
  • Object
show all
Defined in:
app/controllers/messaging_controller.rb

Defined Under Namespace

Classes: Msg

Constant Summary collapse

@@invoke_count =
0

Constants inherited from KitController

KitController::Pagebase

Instance Attribute Summary

Attributes inherited from KitController

#is_image_request, #kit_request, #layout_being_used, #requested_url, #template_being_used

Instance Method Summary collapse

Methods inherited from KitController

#anti_spam_okay?, #app_name, #can_moderate, #can_use, #captcha_okay?, #check_and_record_goal, #check_user, #csv_headers, #dif, #edit_page_path, #feature?, #get_asset, #get_view_content, #host_name, #index_name, #info_page_path, #kit_layout_in_use, #kit_render, #kit_session, #kit_session_end, #link_to, #mailchimp_connect, #mobile_template, #no_read, #no_write, #not_found, #not_found_404, #offline, #page_path, #pref, #rails_app_name, #render, #render_error, #render_page, #render_page_by_url, #routing_error, #sanity_check_okay?, #session_id, #set_requested_url, #show_form, #stylesheets, #super_render, #user_sees_menu?

Instance Method Details

#build_response(requested_convos) ⇒ Object



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'app/controllers/messaging_controller.rb', line 119

def build_response(requested_convos)
  conversations = []

  requested_convos.each do |id, convo_details|
    next unless convo_details
    current_user.conversations.where(:id=>convo_details["conversation_id"]).each do |conversation|
      messages = conversation.messages.order("id desc").where("id>#{convo_details["last_message"]}")
      messages = messages.limit(5) if convo_details["last_message"].to_i==0
      messages.reverse.each do |message| 
        conversations << Msg.make(conversation.id, current_user.display_name, message.display_message, message.id) 
      end
      ConversationUser.connection.execute("update conversation_users set last_seen_message_id = #{messages.last.id} where conversation_id = #{conversation.id} and user_id = #{current_user.id}") if messages.last
    end
  end

  return conversations
end

#game_availabilityObject



60
61
62
63
64
65
66
67
68
# File 'app/controllers/messaging_controller.rb', line 60

def game_availability
  o = Opponent.where(:user_id => current_user.id).where(:game_id=>params[:id]).first
  if params[:available]=="true"
    Opponent.create(:user_id=>current_user.id, :game_id=>params[:id]) unless o
  else
    o.destroy if o
  end
  render :js=>";"
end

#game_startObject



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
# File 'app/controllers/messaging_controller.rb', line 25

def game_start
  o = Opponent.where(:user_id=>params[:opponent_id]).where(:game_id=>params[:game_id]).first
  result = ""
  unless o
    render :js => "game_start_error('Opponent does not exist');"
    return
  end

  cu = Conversation.find_by_sql("select c.* from conversations c, conversation_users cu1, conversation_users cu2 where c.id = cu1.conversation_id and c.id = cu2.conversation_id and cu1.user_id = #{current_user.id} and cu2.user_id = #{o.user_id}")

  if cu.size>0
    render :js => "game_created(#{cu.first.id}, '#{cu.first.name}');"
    return
  end

  c = Conversation.new
  c.user_id = current_user.id
  c.name = current_user.display_name + "&" + o.user.display_name
  c.is_public = false
  c.game_id = o.game_id
  c.save
  cu = ConversationUser.new
  cu.user_id = current_user.id
  cu.conversation_name_for_this_user = o.user.display_name
  cu.conversation_id = c.id
  cu.save
  cu2 = ConversationUser.new
  cu2.user_id = o.user_id
  cu2.conversation_name_for_this_user = current_user.display_name
  cu2.conversation_id = c.id
  cu2.save
  Conversation.connection.execute("update conversation_users set updated_at = now() where id in (#{cu.id}, #{cu2.id})")
  render :js => "game_created(#{c.id},'#{c.name}');"
end

#indexObject



70
71
72
73
74
# File 'app/controllers/messaging_controller.rb', line 70

def index
  get_conversations
  @conversations = Conversation.where(:is_public=>1).all
  @conversations += current_user.conversations if current_user
end

#load_userObject



5
6
7
# File 'app/controllers/messaging_controller.rb', line 5

def load_user
  authenticate_user!
end

#messageObject



113
114
115
116
117
# File 'app/controllers/messaging_controller.rb', line 113

def message
  conversation_id = params[:id]
  Message.create(:system_id=>_sid, :conversation_id=>params[:id], :user_id=>current_user.id, :message=>params[:message])
  render :json => nil
end

#pollObject



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'app/controllers/messaging_controller.rb', line 84

def poll
  @@invoke_count += 1  
  current_user.heard_from!
  if Rails.env.development? || @@invoke_count > 10
    @@invoke_count = 0
    Conversation.flush
  end
  r = {}
  r[:conversations] =  build_response(ActiveSupport::JSON.decode(params[:conversations]))
  get_conversations
  r[:list] = @conversations.as_json(:only=>[:id, :name])

  if params[:selected_game_id]
    opponents = []
    # opponents who have been heard from in teh last minute, that aren't this user, up to 50
    Opponent.where(:game_id=>params[:selected_game_id]).joins(:user).where("users.last_heard_from > date_sub(now(), interval 1 minute)").includes(:user).order("opponents.updated_at desc").limit(50).where("opponents.user_id<>#{current_user.id}").each do |opponent|
      if Conversation.find_by_sql("select c.* from conversations c, conversation_users cu1, conversation_users cu2 where c.id = cu1.conversation_id and c.id = cu2.conversation_id and cu1.user_id = #{current_user.id} and cu2.user_id = #{opponent.user_id} and c.game_id = #{opponent.game_id}").size==0
        opponents <<   { :user_id=>opponent.user_id, :user_name=>opponent.display_name, :game_id=>opponent.game_id } 
      end
    end
    r[:opponents] = opponents
    r[:me_available] = Opponent.where(:game_id=>params[:selected_game_id]).where(:user_id=>current_user.id).count == 1 ? "true" : "false"
  else
    r[:opponents] = nil
  end

  render :json => r
end

#show_conversationObject



76
77
78
79
80
# File 'app/controllers/messaging_controller.rb', line 76

def show_conversation
  @conversation = Conversation.find_sys_id(_sid, params[:id])

  render "show_conversation", :layout=>false
end

#suggest_gameObject



137
138
139
140
141
142
143
# File 'app/controllers/messaging_controller.rb', line 137

def suggest_game
  game = Game.new
  game.name = params[:suggestion_name]
  game.is_live = 0
  game.save
  render :js=>"thanks_for_suggestion();"
end