jam-cloud/web/app/controllers/api_invitations_controller.rb

73 lines
2.4 KiB
Ruby
Raw Permalink Normal View History

2012-10-27 22:26:45 +00:00
class ApiInvitationsController < ApiController
# have to be signed in currently to see this screen
2012-11-14 05:37:50 +00:00
before_filter :api_signed_in_user
2012-10-27 22:26:45 +00:00
respond_to :json
def index
conditions = {}
sender_id = params[:sender]
receiver_id = params[:receiver]
if !sender_id.nil?
if current_user.id != sender_id
raise JamPermissionError, "You can only ask for your own sent invitations"
2012-10-27 22:26:45 +00:00
end
2014-01-14 04:33:32 +00:00
if session_id = params[:session_id]
@invitations = Invitation.where(:sender_id => sender_id, :music_session_id => session_id).to_a.uniq { |i| i.receiver_id }
2014-01-14 04:33:32 +00:00
else
@invitations = Invitation.where(:sender_id => current_user.id).to_a.uniq { |i| i.receiver_id }
2014-01-14 04:33:32 +00:00
end
2012-10-27 22:26:45 +00:00
elsif !receiver_id.nil?
if current_user.id != receiver_id
raise JamPermissionError, "You can only ask for your own received invitations"
2012-10-27 22:26:45 +00:00
end
@invitations = Invitation.where(:receiver_id => current_user.id).to_a.uniq { |i| i.receiver_id }
2012-10-27 22:26:45 +00:00
else
# default to invitations you've received
@invitations = Invitation.where(:receiver_id => current_user.id).to_a.uniq { |i| i.receiver_id }
2012-10-27 22:26:45 +00:00
end
end
def create
music_session = MusicSession.find(params[:music_session])
receiver = User.find(params[:receiver])
sender = current_user
join_request = JoinRequest.find(params[:join_request]) unless params[:join_request].nil?
2012-10-27 22:26:45 +00:00
2014-01-14 20:06:43 +00:00
@invitation = Invitation.new
@invitation.music_session = music_session
@invitation.sender = sender
@invitation.receiver = receiver
@invitation.join_request = join_request
@invitation.save
2012-10-27 22:26:45 +00:00
2014-01-14 20:06:43 +00:00
unless @invitation.errors.any?
User.save_session_settings(current_user, music_session)
2014-01-14 20:06:43 +00:00
# send notification
Notification.send_session_invitation(receiver, current_user, music_session.id)
respond_with @invitation, :responder => ApiResponder, :location => api_invitation_detail_url(@invitation)
2012-10-27 22:26:45 +00:00
2014-01-14 20:06:43 +00:00
else
# we have to do this because api_invitation_detail_url will fail with a bad @invitation
response.status = :unprocessable_entity
respond_with @invitation
2012-10-27 22:26:45 +00:00
end
2014-01-14 20:06:43 +00:00
2012-10-27 22:26:45 +00:00
end
def show
@invitation = Invitation.where(["receiver_id = ? or sender_id = ?", current_user.id, current_user.id]).find(params[:id])
2012-10-27 22:26:45 +00:00
end
def delete
@invitation = Invitation.where(["sender_id = ?", current_user.id]).find(params[:id])
2012-10-27 22:26:45 +00:00
@invitation.delete
respond_with @invitation, responder => ApiResponder
end
end