about summary refs log tree commit diff
path: root/app/policies/status_policy.rb
blob: f4a5e7c6c01791d49424ca26f648aaefb8f0bd7d (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
# frozen_string_literal: true

class StatusPolicy
  attr_reader :account, :status

  def initialize(account, status)
    @account = account
    @status = status
  end

  def show?
    return false if local_only? && account.nil?

    if direct?
      owned? || status.mentions.where(account: account).exists?
    elsif private?
      owned? || account&.following?(status.account) || status.mentions.where(account: account).exists?
    else
      account.nil? || !status.account.blocking?(account)
    end
  end

  def reblog?
    !direct? && !private? && show?
  end

  def destroy?
    admin? || owned?
  end

  alias unreblog? destroy?

  private

  def admin?
    account&.user&.admin?
  end

  def direct?
    status.direct_visibility?
  end

  def owned?
    status.account.id == account&.id
  end

  def private?
    status.private_visibility?
  end
  
  def local_only?
    status.local_only?
  end
end