Class: Api::V1::PostInteractionsController

Inherits:
BaseController show all
Includes:
PostsHelper
Defined in:
app/controllers/api/v1/post_interactions_controller.rb

Instance Attribute Summary

Attributes included from OpenidConnect::ProtectedResourceEndpoint

#current_token

Instance Method Summary collapse

Methods included from PostsHelper

#post_iframe_url, #post_page_title

Methods included from OpenidConnect::ProtectedResourceEndpoint

#access_token?, #require_access_token

Instance Method Details

#hideObject


26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'app/controllers/api/v1/post_interactions_controller.rb', line 26

def hide
  return render_error(422, "Missing parameter") if params[:hide].nil?

  post = find_post
  hidden = current_user.is_shareable_hidden?(post)

  if (params[:hide] && !hidden) || (!params[:hide] && hidden)
    current_user.toggle_hidden_shareable(post)
    head :no_content
  else
    render_error(params[:hide] ? 409 : 410, params[:hide] ? "Post already hidden" : "Post not hidden")
  end
end

#muteObject


40
41
42
43
44
45
46
47
# File 'app/controllers/api/v1/post_interactions_controller.rb', line 40

def mute
  post = find_post
  participation = current_user.participations.find_by(target_id: post.id)
  return head :gone unless participation

  participation.destroy
  head :no_content
end

#reportObject


49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'app/controllers/api/v1/post_interactions_controller.rb', line 49

def report
  reason = params.require(:reason)
  post = find_post
  report = current_user.reports.new(
    item_id:   post.id,
    item_type: "Post",
    text:      reason
  )
  if report.save
    head :no_content
  else
    render_error 409, "Failed to create report on this post"
  end
rescue ActionController::ParameterMissing
  render_error 422, "Failed to create report on this post"
end

#subscribeObject


16
17
18
19
20
21
22
23
24
# File 'app/controllers/api/v1/post_interactions_controller.rb', line 16

def subscribe
  post = find_post
  return head :conflict if current_user.participations.find_by(target_id: post.id)

  current_user.participate!(post)
  head :no_content
rescue ActiveRecord::RecordInvalid
  render_error 422, "Cannot subscribe to this post"
end

#voteObject


66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'app/controllers/api/v1/post_interactions_controller.rb', line 66

def vote
  post = find_post
  begin
    poll_vote = poll_service.vote(post.id, params[:poll_answer])
  rescue ActiveRecord::RecordNotFound
    # This, but not the find_post above, should return a 422,
    # we just keep poll_vote nil so it goes into the else below
  end
  if poll_vote
    head :no_content
  else
    render_error 422, "Cant vote on this post"
  end
rescue ActiveRecord::RecordInvalid
  render_error 422, "Cant vote on this post"
end