about summary refs log tree commit diff
path: root/app/models/concerns/user_roles.rb
blob: 2da039efd4823e68b967e772218d6d079ef907f2 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# frozen_string_literal: true

module UserRoles
  extend ActiveSupport::Concern

  included do
    scope :admins, -> { where(admin: true) }
    scope :moderators, -> { where(moderator: true) }
    scope :halfmods, -> { where(halfmod: true) }
    scope :staff, -> { admins.or(moderators) }
  end

  def staff?
    admin? || moderator?
  end

  def can_moderate?
    staff? || halfmod?
  end

  def role
    if admin?
      'admin'
    elsif moderator?
      'moderator'
    elsif halfmod?
      'halfmod'
    else
      'user'
    end
  end

  def role?(role)
    case role
    when 'user'
      true
    when 'halfmod'
      halfmod?
    when 'moderator'
      staff?
    when 'admin'
      admin?
    else
      false
    end
  end

  def has_more_authority_than?(other_user)
    if admin?
      !other_user&.admin?
    elsif moderator?
      !other_user&.staff?
    elsif halfmod?
      !other_user&.can_moderate?
    else
      false
    end
  end

  def promote!
    if halfmod?
      update!(halfmod: false, moderator: true, admin: false)
    elsif moderator?
      update!(halfmod: false, moderator: false, admin: true)
    elsif !admin?
      update!(halfmod: true, moderator: false, admin: false)
    end
  end

  def demote!
    if admin?
      update!(halfmod: false, moderator: true, admin: false)
    elsif moderator?
      update!(halfmod: true, moderator: false, admin: false)
    elsif halfmod?
      update!(halfmod: false, moderator: false, admin: false)
    end
  end

  def fangs_out!
    update!(defanged: false, last_fanged_at: Time.now.utc)
    LogWorker.perform_async("\u23eb <#{self.account.username}> switched to fanged #{role} mode.")
  end

  def defang!
    update!(defanged: true, last_fanged_at: nil)
    LogWorker.perform_async("\u23ec <#{self.account.username}> is no longer in fanged #{role} mode.")
  end
end