Retrospring/app/controllers/ajax/inbox_controller.rb

89 lines
1.9 KiB
Ruby
Raw Normal View History

2014-11-11 07:19:20 -08:00
class Ajax::InboxController < ApplicationController
2014-12-09 14:21:41 -08:00
def create
unless user_signed_in?
@status = :noauth
@message = "requires authentication"
@success = false
return
end
question = Question.create!(content: QuestionGenerator.generate,
author_is_anonymous: true,
author_name: 'justask',
user: current_user)
inbox = Inbox.create!(user: current_user, question_id: question.id, new: true)
@status = :okay
@message = "Successfully added new question."
@success = true
@render = render_to_string(partial: 'inbox/entry', locals: { i: inbox })
inbox.update(new: false)
end
2014-11-11 07:19:20 -08:00
def destroy
params.require :id
params.require :answer
params.require :share
2014-11-11 07:19:20 -08:00
2014-11-11 09:02:59 -08:00
inbox = Inbox.find(params[:id])
2014-11-11 07:19:20 -08:00
2014-11-11 09:02:59 -08:00
unless current_user == inbox.user
2014-11-11 07:19:20 -08:00
@status = :fail
@message = "question not in your inbox"
@success = false
return
end
2014-12-12 14:53:23 -08:00
answer = nil
2014-12-12 14:45:49 -08:00
begin
2014-12-12 14:53:23 -08:00
answer = inbox.answer params[:answer], current_user
rescue
@status = :err
@message = "An error occurred"
@success = false
return
2014-11-11 09:09:28 -08:00
end
# sharing
2014-12-13 07:46:27 -08:00
begin
2014-12-13 06:38:16 -08:00
share_to = JSON.parse params[:share]
2014-12-12 14:53:23 -08:00
current_user.services.each do |service|
2014-12-13 06:38:16 -08:00
service.post(answer) if share_to.include? service.provider
2014-12-12 14:53:23 -08:00
end
2014-12-13 07:46:27 -08:00
rescue
2014-12-12 14:45:49 -08:00
end
2014-11-11 07:19:20 -08:00
@status = :okay
@message = "Successfully answered question."
@success = true
end
def remove
params.require :id
inbox = Inbox.find(params[:id])
unless current_user == inbox.user
@status = :fail
@message = "question not in your inbox"
@success = false
return
end
begin
inbox.remove
rescue
@status = :err
@message = "An error occurred"
@success = false
return
end
@status = :okay
@message = "Successfully deleted question."
@success = true
end
2014-11-11 07:19:20 -08:00
end