1 class UserBlocksController < ApplicationController
3 include PaginationMethods
7 before_action :authorize_web
8 before_action :set_locale
12 before_action :lookup_user, :only => [:new, :create]
13 before_action :lookup_user_block, :only => [:show, :edit, :update]
14 before_action :require_valid_params, :only => [:create, :update]
15 before_action :check_database_readable
16 before_action :check_database_writable, :only => [:create, :update]
19 @params = params.permit
21 user_blocks = UserBlock.all
23 @user_blocks, @newer_user_blocks_id, @older_user_blocks_id = get_page_items(user_blocks, :includes => [:user, :creator, :revoker])
25 @show_user_name = true
26 @show_creator_name = true
28 render :partial => "page" if turbo_frame_request_id == "pagination"
32 if current_user && current_user == @user_block.user && !@user_block.deactivates_at
33 @user_block.needs_view = false
34 @user_block.deactivates_at = [@user_block.ends_at, Time.now.utc].max
40 @user_block = UserBlock.new
44 params[:user_block_period] = ((@user_block.ends_at - Time.now.utc) / 1.hour).ceil.to_s
50 @user_block = UserBlock.new(
52 :creator => current_user,
53 :reason => params[:user_block][:reason],
55 :ends_at => now + @block_period.hours,
56 :needs_view => params[:user_block][:needs_view]
58 @user_block.deactivates_at = @user_block.ends_at unless @user_block.needs_view
61 flash[:notice] = t(".flash", :name => @user.display_name)
62 redirect_to @user_block
64 render :action => "new"
67 redirect_to new_user_block_path(params[:display_name])
73 if cannot?(:update, @user_block)
74 flash[:error] = @user_block.revoker ? t(".only_creator_or_revoker_can_edit") : t(".only_creator_can_edit")
75 redirect_to :action => "edit"
77 user_block_was_active = @user_block.active?
78 @user_block.reason = params[:user_block][:reason]
79 @user_block.needs_view = params[:user_block][:needs_view]
80 @user_block.ends_at = Time.now.utc + @block_period.hours
81 @user_block.deactivates_at = (@user_block.ends_at unless @user_block.needs_view)
82 @user_block.revoker = current_user if user_block_was_active && !@user_block.active?
83 if user_block_was_active && @user_block.active? && current_user != @user_block.creator
84 flash.now[:error] = t(".only_creator_can_edit_without_revoking")
85 render :action => "edit"
86 elsif !user_block_was_active && @user_block.active?
87 flash.now[:error] = t(".inactive_block_cannot_be_reactivated")
88 render :action => "edit"
90 unless user_block_was_active
91 @user_block.ends_at = @user_block.ends_at_was
92 @user_block.deactivates_at = @user_block.deactivates_at_was
93 @user_block.deactivates_at = [@user_block.ends_at, @user_block.updated_at].max unless @user_block.deactivates_at # take updated_at into account before deactivates_at is backfilled
96 flash[:notice] = t(".success")
97 redirect_to @user_block
99 render :action => "edit"
104 redirect_to edit_user_block_path(params[:id])
111 # ensure that there is a "user_block" instance variable
112 def lookup_user_block
113 @user_block = UserBlock.find(params[:id])
114 rescue ActiveRecord::RecordNotFound
115 render :action => "not_found", :status => :not_found
119 # check that the input parameters are valid, setting an instance
120 # variable if not. note that this doesn't do any redirection, as it's
121 # called before two different actions, each of which should redirect
122 # to a different place.
123 def require_valid_params
124 @block_period = params[:user_block_period].to_i
125 @valid_params = false
127 if UserBlock::PERIODS.exclude?(@block_period)
128 flash[:error] = t("user_blocks.filter.block_period")