]> git.openstreetmap.org Git - rails.git/blob - app/controllers/user_blocks_controller.rb
Use turbo for pagination
[rails.git] / app / controllers / user_blocks_controller.rb
1 class UserBlocksController < ApplicationController
2   include UserMethods
3   include PaginationMethods
4
5   layout "site"
6
7   before_action :authorize_web
8   before_action :set_locale
9
10   authorize_resource
11
12   before_action :lookup_user, :only => [:new, :create, :revoke_all, :blocks_on, :blocks_by]
13   before_action :lookup_user_block, :only => [:show, :edit, :update, :revoke]
14   before_action :require_valid_params, :only => [:create, :update]
15   before_action :check_database_readable
16   before_action :check_database_writable, :only => [:create, :update, :revoke, :revoke_all]
17
18   def index
19     @params = params.permit
20
21     user_blocks = UserBlock.all
22
23     @user_blocks, @newer_user_blocks_id, @older_user_blocks_id = get_page_items(user_blocks, :includes => [:user, :creator, :revoker])
24
25     @show_user_name = true
26     @show_creator_name = true
27
28     render :partial => "blocks" if turbo_frame_request_id == "pagination"
29   end
30
31   def show
32     if current_user && current_user == @user_block.user
33       @user_block.needs_view = false
34       @user_block.save!
35     end
36   end
37
38   def new
39     @user_block = UserBlock.new
40   end
41
42   def edit
43     params[:user_block_period] = ((@user_block.ends_at - Time.now.utc) / 1.hour).ceil.to_s
44   end
45
46   def create
47     if @valid_params
48       now = Time.now.utc
49       @user_block = UserBlock.new(
50         :user => @user,
51         :creator => current_user,
52         :reason => params[:user_block][:reason],
53         :created_at => now,
54         :ends_at => now + @block_period.hours,
55         :needs_view => params[:user_block][:needs_view]
56       )
57
58       if @user_block.save
59         flash[:notice] = t(".flash", :name => @user.display_name)
60         redirect_to @user_block
61       else
62         render :action => "new"
63       end
64     else
65       redirect_to new_user_block_path(:display_name => params[:display_name])
66     end
67   end
68
69   def update
70     if @valid_params
71       if current_user != @user_block.creator &&
72          current_user != @user_block.revoker
73         flash[:error] = t(@user_block.revoker ? ".only_creator_or_revoker_can_edit" : ".only_creator_can_edit")
74         redirect_to :action => "edit"
75       else
76         user_block_was_active = @user_block.active?
77         @user_block.reason = params[:user_block][:reason]
78         @user_block.needs_view = params[:user_block][:needs_view]
79         @user_block.ends_at = Time.now.utc + @block_period.hours
80         @user_block.revoker = current_user if user_block_was_active && !@user_block.active?
81         if !user_block_was_active && @user_block.active?
82           flash.now[:error] = t(".inactive_block_cannot_be_reactivated")
83           render :action => "edit"
84         else
85           @user_block.ends_at = @user_block.ends_at_was unless user_block_was_active
86           if @user_block.save
87             flash[:notice] = t(".success")
88             redirect_to @user_block
89           else
90             render :action => "edit"
91           end
92         end
93       end
94     else
95       redirect_to edit_user_block_path(:id => params[:id])
96     end
97   end
98
99   ##
100   # revokes the block, setting the end_time to now
101   def revoke
102     if request.post? && params[:confirm] && @user_block.revoke!(current_user)
103       flash[:notice] = t ".flash"
104       redirect_to(@user_block)
105     end
106   end
107
108   ##
109   # revokes all active blocks
110   def revoke_all
111     if request.post? && params[:confirm]
112       @user.blocks.active.each { |block| block.revoke!(current_user) }
113       flash[:notice] = t ".flash"
114       redirect_to user_blocks_on_path(@user)
115     end
116   end
117
118   ##
119   # shows a list of all the blocks on the given user
120   def blocks_on
121     @params = params.permit(:display_name)
122
123     user_blocks = UserBlock.where(:user => @user)
124
125     @user_blocks, @newer_user_blocks_id, @older_user_blocks_id = get_page_items(user_blocks, :includes => [:user, :creator, :revoker])
126
127     @show_user_name = false
128     @show_creator_name = true
129
130     render :partial => "blocks" if turbo_frame_request_id == "pagination"
131   end
132
133   ##
134   # shows a list of all the blocks by the given user.
135   def blocks_by
136     @params = params.permit(:display_name)
137
138     user_blocks = UserBlock.where(:creator => @user)
139
140     @user_blocks, @newer_user_blocks_id, @older_user_blocks_id = get_page_items(user_blocks, :includes => [:user, :creator, :revoker])
141
142     @show_user_name = true
143     @show_creator_name = false
144
145     render :partial => "blocks" if turbo_frame_request_id == "pagination"
146   end
147
148   private
149
150   ##
151   # ensure that there is a "user_block" instance variable
152   def lookup_user_block
153     @user_block = UserBlock.find(params[:id])
154   rescue ActiveRecord::RecordNotFound
155     render :action => "not_found", :status => :not_found
156   end
157
158   ##
159   # check that the input parameters are valid, setting an instance
160   # variable if not. note that this doesn't do any redirection, as it's
161   # called before two different actions, each of which should redirect
162   # to a different place.
163   def require_valid_params
164     @block_period = params[:user_block_period].to_i
165     @valid_params = false
166
167     if UserBlock::PERIODS.exclude?(@block_period)
168       flash[:error] = t("user_blocks.filter.block_period")
169
170     else
171       @valid_params = true
172     end
173   end
174 end