]> git.openstreetmap.org Git - rails.git/blob - app/controllers/notes_controller.rb
Merge remote-tracking branch 'upstream/pull/7633'
[rails.git] / app / controllers / notes_controller.rb
1 # frozen_string_literal: true
2
3 class NotesController < ApplicationController
4   include UserMethods
5
6   layout :map_layout
7
8   before_action :check_api_readable
9   before_action :authorize_web
10   before_action :set_locale
11   before_action :require_oauth
12
13   authorize_resource
14
15   before_action :lookup_user, :only => [:index]
16   around_action :web_timeout
17
18   PAGE_SIZE = 10
19
20   ##
21   # Display a list of notes by a specified user
22   def index
23     param! :page, Integer, :min => 1
24     param! :status, String, :in => %w[all open closed hidden]
25
26     @params = params.permit(:display_name, :status)
27     @title = t ".title", :user => @user.display_name
28     @page = (params[:page] || 1).to_i
29     @notes = @user.notes
30     @notes = @notes.visible unless current_user&.moderator?
31     @notes = @notes.where(:status => params[:status]) unless params[:status] == "all" || params[:status].blank?
32     @notes = @notes.order(:updated_at => :desc, :id => :asc).distinct.offset((@page - 1) * PAGE_SIZE).limit(PAGE_SIZE + 1).preload(:comments => :author)
33     @notes = @notes.to_a
34     if @notes.size > PAGE_SIZE
35       @notes.pop
36       @next_page = true
37     end
38
39     respond_to do |format|
40       format.html { render :layout => "site" }
41     end
42   end
43
44   def show
45     @type = "note"
46
47     if current_user&.moderator?
48       @note = Note.find(params.expect(:id))
49       @note_comments = @note.comments.unscope(:where => :visible)
50     else
51       @note = Note.visible.find(params.expect(:id))
52       @note_comments = @note.comments
53     end
54
55     @note_includes_anonymous = @note.author.nil? || @note_comments.find { |comment| comment.author.nil? }
56
57     @note_comments = @note_comments.drop(1) if @note_comments.first&.event == "opened"
58   rescue ActiveRecord::RecordNotFound
59     render :template => "browse/not_found", :status => :not_found
60   end
61
62   def new
63     @anonymous_notes_count = request.cookies["_osm_anonymous_notes_count"].to_i
64     render :action => :new_readonly if api_status != "online"
65   end
66 end