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
|
2015-04-20 14:50:33 +00:00
|
|
|
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]
|
2016-07-17 15:16:27 +00:00
|
|
|
@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
|
2016-07-17 15:16:27 +00:00
|
|
|
@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
|
2015-04-20 14:50:33 +00:00
|
|
|
raise JamPermissionError, "You can only ask for your own received invitations"
|
2012-10-27 22:26:45 +00:00
|
|
|
end
|
|
|
|
|
|
2016-07-17 15:16:27 +00:00
|
|
|
@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
|
2016-07-17 15:16:27 +00:00
|
|
|
@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
|
2012-11-30 15:30:30 +00:00
|
|
|
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)
|
2013-03-22 00:17:28 +00:00
|
|
|
|
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
|
2016-07-17 15:16:27 +00:00
|
|
|
@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
|
2016-07-17 15:16:27 +00:00
|
|
|
@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
|