]> git.openstreetmap.org Git - rails.git/blob - app/controllers/diary_comments_controller.rb
Drop support for ruby 3.0
[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
28     render :partial => "page" if turbo_frame_request_id == "pagination"
29   end
30
31   def create
32     @entry = DiaryEntry.find(params[:id])
33     @comments = @entry.visible_comments
34     @diary_comment = @entry.comments.build(comment_params)
35     @diary_comment.user = current_user
36     if @diary_comment.save
37
38       # Notify current subscribers of the new comment
39       @entry.subscribers.visible.each do |user|
40         UserMailer.diary_comment_notification(@diary_comment, user).deliver_later if current_user != user
41       end
42
43       # Add the commenter to the subscribers if necessary
44       @entry.subscriptions.create(:user => current_user) unless @entry.subscribers.exists?(current_user.id)
45
46       redirect_to diary_entry_path(@entry.user, @entry, :anchor => "comment#{@diary_comment.id}")
47     else
48       render :action => "new"
49     end
50   rescue ActiveRecord::RecordNotFound
51     render "diary_entries/no_such_entry", :status => :not_found
52   end
53
54   def hide
55     comment = DiaryComment.find(params[:comment])
56     comment.update(:visible => false)
57     redirect_to diary_entry_path(comment.diary_entry.user, comment.diary_entry)
58   end
59
60   def unhide
61     comment = DiaryComment.find(params[:comment])
62     comment.update(:visible => true)
63     redirect_to diary_entry_path(comment.diary_entry.user, comment.diary_entry)
64   end
65
66   private
67
68   ##
69   # return permitted diary comment parameters
70   def comment_params
71     params.require(:diary_comment).permit(:body)
72   end
73 end