]> git.openstreetmap.org Git - rails.git/blob - app/controllers/diary_comments_controller.rb
Merge remote-tracking branch 'upstream/pull/4995'
[rails.git] / app / controllers / diary_comments_controller.rb
1 class DiaryCommentsController < ApplicationController
2   include UserMethods
3   include PaginationMethods
4
5   layout "site"
6
7   before_action :authorize_web
8   before_action :set_locale
9   before_action :check_database_readable
10
11   authorize_resource
12
13   before_action :lookup_user, :only => :index
14   before_action :check_database_writable, :only => [:create, :hide, :unhide]
15
16   allow_thirdparty_images :only => :index
17
18   def index
19     @title = t ".title", :user => @user.display_name
20
21     comments = DiaryComment.where(:user => @user)
22     comments = comments.visible unless can? :unhide, DiaryComment
23
24     @params = params.permit(:display_name, :before, :after)
25
26     @comments, @newer_comments_id, @older_comments_id = get_page_items(comments, :includes => [:user])
27   end
28
29   def create
30     @entry = DiaryEntry.find(params[:id])
31     @comments = @entry.visible_comments
32     @diary_comment = @entry.comments.build(comment_params)
33     @diary_comment.user = current_user
34     if @diary_comment.save
35
36       # Notify current subscribers of the new comment
37       @entry.subscribers.visible.each do |user|
38         UserMailer.diary_comment_notification(@diary_comment, user).deliver_later if current_user != user
39       end
40
41       # Add the commenter to the subscribers if necessary
42       @entry.subscriptions.create(:user => current_user) unless @entry.subscribers.exists?(current_user.id)
43
44       redirect_to diary_entry_path(@entry.user, @entry)
45     else
46       render :action => "new"
47     end
48   rescue ActiveRecord::RecordNotFound
49     render "diary_entries/no_such_entry", :status => :not_found
50   end
51
52   def hide
53     comment = DiaryComment.find(params[:comment])
54     comment.update(:visible => false)
55     redirect_to diary_entry_path(comment.diary_entry.user, comment.diary_entry)
56   end
57
58   def unhide
59     comment = DiaryComment.find(params[:comment])
60     comment.update(:visible => true)
61     redirect_to diary_entry_path(comment.diary_entry.user, comment.diary_entry)
62   end
63
64   private
65
66   ##
67   # return permitted diary comment parameters
68   def comment_params
69     params.require(:diary_comment).permit(:body)
70   end
71 end