]> git.openstreetmap.org Git - rails.git/blob - app/models/user_block.rb
Merge remote-tracking branch 'upstream/pull/7015'
[rails.git] / app / models / user_block.rb
1 # frozen_string_literal: true
2
3 # == Schema Information
4 #
5 # Table name: user_blocks
6 #
7 #  id             :integer          not null, primary key
8 #  user_id        :bigint           not null
9 #  creator_id     :bigint           not null
10 #  reason         :text             not null
11 #  ends_at        :datetime         not null
12 #  needs_view     :boolean          default(FALSE), not null
13 #  revoker_id     :bigint
14 #  created_at     :datetime
15 #  updated_at     :datetime
16 #  reason_format  :enum             default("markdown"), not null
17 #  deactivates_at :datetime
18 #
19 # Indexes
20 #
21 #  index_user_blocks_on_creator_id_and_id  (creator_id,id)
22 #  index_user_blocks_on_user_id            (user_id)
23 #
24 # Foreign Keys
25 #
26 #  user_blocks_moderator_id_fkey  (creator_id => users.id)
27 #  user_blocks_revoker_id_fkey    (revoker_id => users.id)
28 #  user_blocks_user_id_fkey       (user_id => users.id)
29 #
30
31 class UserBlock < ApplicationRecord
32   validate :moderator_permissions
33   validates :reason, :characters => true, :length => { :maximum => 10000 }, :presence => true
34   validates :deactivates_at, :comparison => { :greater_than_or_equal_to => :ends_at }, :unless => -> { needs_view }
35   validates :deactivates_at, :absence => true, :if => -> { needs_view }
36   validates :ends_at, :presence => true
37
38   belongs_to :user, :class_name => "User"
39   belongs_to :creator, :class_name => "User"
40   belongs_to :revoker, :class_name => "User", :optional => true
41
42   PERIODS = Settings.user_block_periods
43
44   ##
45   # scope to match active blocks
46   def self.active
47     where("needs_view or ends_at > ?", Time.now.utc)
48   end
49
50   ##
51   # return a renderable version of the reason text.
52   def reason
53     RichText.new(self[:reason_format], self[:reason])
54   end
55
56   ##
57   # returns true if the block is currently active (i.e: the user can't
58   # use the API).
59   def active?
60     needs_view || ends_at > Time.now.utc
61   end
62
63   ##
64   # returns true if the block is a "zero hour" block
65   def zero_hour?
66     # if the times differ more than 1 minute we probably have more important issues
67     needs_view && (ends_at.to_i - updated_at.to_i) < 60
68   end
69
70   ##
71   # revokes the block, allowing the user to use the API again. the argument
72   # is the user object who is revoking the ban.
73   def revoke!(revoker)
74     update(
75       :ends_at => Time.now.utc,
76       :deactivates_at => Time.now.utc,
77       :revoker_id => revoker.id,
78       :needs_view => false
79     )
80   end
81
82   private
83
84   ##
85   # validate that only moderators are allowed to change the
86   # block. this should be caught and dealt with in the controller,
87   # but i've also included it here just in case.
88   def moderator_permissions
89     errors.add(:base, I18n.t("user_blocks.model.non_moderator_update")) if creator_id_changed? && !creator.moderator?
90     errors.add(:base, I18n.t("user_blocks.model.non_moderator_revoke")) if revoker_id_changed? && !revoker_id.nil? && !revoker.moderator?
91   end
92 end