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