2017-05-29 19:22:22 +03:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-11-11 21:23:33 +02:00
|
|
|
class StatusPolicy < ApplicationPolicy
|
2018-05-03 11:41:58 +03:00
|
|
|
def initialize(current_account, record, preloaded_relations = {})
|
|
|
|
super(current_account, record)
|
|
|
|
|
|
|
|
@preloaded_relations = preloaded_relations
|
|
|
|
end
|
|
|
|
|
2017-11-11 21:23:33 +02:00
|
|
|
def index?
|
|
|
|
staff?
|
2017-05-29 19:22:22 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
def show?
|
2017-05-30 16:16:14 +03:00
|
|
|
if direct?
|
2018-05-03 11:41:58 +03:00
|
|
|
owned? || mention_exists?
|
2017-05-30 16:16:14 +03:00
|
|
|
elsif private?
|
2018-05-03 11:41:58 +03:00
|
|
|
owned? || following_author? || mention_exists?
|
2017-05-29 19:22:22 +03:00
|
|
|
else
|
2018-05-03 11:41:58 +03:00
|
|
|
current_account.nil? || !author_blocking?
|
2017-05-29 19:22:22 +03:00
|
|
|
end
|
|
|
|
end
|
2017-05-30 16:16:14 +03:00
|
|
|
|
|
|
|
def reblog?
|
2018-05-03 11:41:58 +03:00
|
|
|
!direct? && (!private? || owned?) && show? && !blocking_author?
|
2018-05-02 16:50:20 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
def favourite?
|
2018-05-03 11:41:58 +03:00
|
|
|
show? && !blocking_author?
|
2017-05-30 16:16:14 +03:00
|
|
|
end
|
|
|
|
|
2017-05-30 23:56:31 +03:00
|
|
|
def destroy?
|
2017-11-11 21:23:33 +02:00
|
|
|
staff? || owned?
|
2017-05-30 23:56:31 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
alias unreblog? destroy?
|
|
|
|
|
2017-11-11 21:23:33 +02:00
|
|
|
def update?
|
|
|
|
staff?
|
2017-05-30 23:56:31 +03:00
|
|
|
end
|
|
|
|
|
2017-11-11 21:23:33 +02:00
|
|
|
private
|
|
|
|
|
2017-05-30 16:16:14 +03:00
|
|
|
def direct?
|
2017-11-11 21:23:33 +02:00
|
|
|
record.direct_visibility?
|
2017-05-30 16:16:14 +03:00
|
|
|
end
|
|
|
|
|
2017-05-30 23:56:31 +03:00
|
|
|
def owned?
|
2017-11-11 21:23:33 +02:00
|
|
|
author.id == current_account&.id
|
2017-05-30 23:56:31 +03:00
|
|
|
end
|
|
|
|
|
2017-05-30 16:16:14 +03:00
|
|
|
def private?
|
2017-11-11 21:23:33 +02:00
|
|
|
record.private_visibility?
|
|
|
|
end
|
|
|
|
|
2018-05-03 11:41:58 +03:00
|
|
|
def mention_exists?
|
|
|
|
return false if current_account.nil?
|
|
|
|
|
|
|
|
if record.mentions.loaded?
|
|
|
|
record.mentions.any? { |mention| mention.account_id == current_account.id }
|
|
|
|
else
|
|
|
|
record.mentions.where(account: current_account).exists?
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def blocking_author?
|
|
|
|
return false if current_account.nil?
|
|
|
|
|
|
|
|
@preloaded_relations[:blocking] ? @preloaded_relations[:blocking][author.id] : current_account.blocking?(author)
|
|
|
|
end
|
|
|
|
|
|
|
|
def author_blocking?
|
|
|
|
return false if current_account.nil?
|
|
|
|
|
|
|
|
@preloaded_relations[:blocked_by] ? @preloaded_relations[:blocked_by][author.id] : author.blocking?(current_account)
|
|
|
|
end
|
|
|
|
|
|
|
|
def following_author?
|
|
|
|
return false if current_account.nil?
|
|
|
|
|
|
|
|
@preloaded_relations[:following] ? @preloaded_relations[:following][author.id] : current_account.following?(author)
|
|
|
|
end
|
|
|
|
|
2017-11-11 21:23:33 +02:00
|
|
|
def author
|
|
|
|
record.account
|
2017-05-30 16:16:14 +03:00
|
|
|
end
|
2017-05-29 19:22:22 +03:00
|
|
|
end
|