Class: Api::PostsController

Inherits:
ApplicationController show all
Includes:
Hooks, Recommendations, Relations, Upload, Utils
Defined in:
app/controllers/api/posts_controller.rb

Instance Method Summary collapse

Methods included from Recommendations

#recommended, #similar

Methods included from Utils

#get_id_from_short_title, #is_admin, #is_member, #is_reviewer, #nickname_exist, #short_title_exist

Methods included from Hooks

#create_external_hook, #delete_external_hook, #get_external_hooks, #hook_new_member, #hook_new_need, #hook_new_post, #hook_new_project, #hook_new_project_challenge, #update_external_hook

Methods included from Upload

#remove_avatar, #remove_banner, #remove_document, #upload_avatar, #upload_banner, #upload_document

Methods included from Relations

#clap, #clappers, #follow, #review, #reviewed_object, #save, #saved_objects

Methods included from Response

#json_response

Instance Method Details

#commentObject



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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'app/controllers/api/posts_controller.rb', line 86

def comment
  case request.method_symbol
  when :post
    @comment = Comment.new(user_id: current_user.id,
                           post_id: @post.id,
                           content: params[:comment][:content])
    object = @post.from_object.constantize.find(@post.from_id)
    # change "workgroup" wording to "group"
    post_from_object_name = @post.from_object == 'Workgroup' ? 'group' : @post.from_object.downcase
    the_url = "post/#{@post.id}"
    # if @post.from_object == 'Need'
    # 	the_url = "project/" + @post.from_need_project_id + "#needs"
    # else
    # 	the_url = @post.from_object.downcase + '/' + @post.from_id.to_s + "#news"
    # end
    if @comment.save
      @post.comments << @comment
      current_user.add_edge(@post, 'has_commented_on')
      @comment.add_edge(@post, 'is_comment_of')
      current_user.add_edge(@comment, 'is_author_of')
      # First let's render the JSON because the post went through
      render json: { data: 'Your comment has been published' }, status: :created
      save_mentions(params[:comment][:mentions])
    end
  when :patch
    @comment = Comment.find(params[:comment_id])
    is_allowed @comment
    render json: { data: 'Cannot find comment id' } if @comment.nil?
    @comment.update(content: params[:comment][:content])
    render json: { data: 'Your post has been updated' }, status: :ok
    save_mentions(params[:comment][:mentions])
  when :delete
    @comment = Comment.find(params[:comment_id])
    is_allowed @comment
    @comment.destroy
    current_user.remove_edge(@post, 'has_commented_on')
    @comment.remove_edge(@post, 'is_comment_of')
    current_user.remove_edge(@comment, 'is_author_of')
    render json: { data: 'Comment deleted' }, status: :ok
  end
end

#createObject



24
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
59
60
61
62
63
64
65
66
67
# File 'app/controllers/api/posts_controller.rb', line 24

def create
  current_user.active_at! unless current_user.nil?

  feed = Feed.find(params.dig(:post, :feed_id))
  from = feed.feedable
  can_post_to?(from)

  # NOTE: keep @post as instance variable for Merit gem
  @post = Post.new(post_params)
  @post.user_id = current_user.id
  @post.from = from

  @post.from_name = from.title
  @post.from_need_project_id = from.project.id if from.instance_of?(Need)

  raise ApiExceptions::UnprocessableEntity.new(message: 'Something went wrong') unless @post.save && !feed.nil? && !from.nil?

  current_user.add_edge(@post, 'is_author_of')
  current_user.add_edge(from, 'has_posted_on')
  @post.add_edge(from, 'is_post_of')
  feed.posts << @post

  if from.is_a?(User)
    from.notif_new_post_on_profile_feed
  else
    feed.notif_new_post_in_feed(@post, from)
    feed.notif_admins_about_new_post(@post)
  end

  # if posting to a need, we cross-post it to the need's project, apparently
  if from.is_a?(Need)
    from.project.feed.posts << @post
    from.project.feed.notif_new_post_in_feed(@post, from.project)
    from.project.feed.notif_admins_about_new_post(@post)
    hook_new_post(from.project.externalhooks, @post)
  elsif from.is_a?(Project) || from.is_a?(Challenge)
    hook_new_post(from.externalhooks, @post)
  end

  # First let's render the JSON because the post went through
  render json: { id: @post.id, data: 'Your post has been published' }, status: :created
  # Then we handle the different mentions "offline"
  save_mentions(params[:post][:mentions])
end

#destroyObject



77
78
79
80
81
82
83
84
# File 'app/controllers/api/posts_controller.rb', line 77

def destroy
  if @post.destroy
    current_user.remove_edge(@post, 'is_author_of')
    current_user.remove_edge(@post.from, 'has_posted_on')
    @post.remove_edge(@post.from, 'is_post_of')
    render json: { data: 'Your post has been deleted' }, status: :ok
  end
end

#showObject



20
21
22
# File 'app/controllers/api/posts_controller.rb', line 20

def show
  render json: @post, includes: %i[relations], show_objects: true, status: :ok
end

#updateObject



69
70
71
72
73
74
75
# File 'app/controllers/api/posts_controller.rb', line 69

def update
  current_user.active_at! unless current_user.nil?

  render json: { data: 'Post has been updated' }, status: :ok if @post.update(post_params)
  # Now we handle the mentions
  save_mentions(params[:post][:mentions])
end