gab-social/app/policies/status_policy.rb

105 lines
2.1 KiB
Ruby
Raw Permalink Normal View History

2019-07-02 08:10:25 +01:00
# frozen_string_literal: true
class StatusPolicy < ApplicationPolicy
def initialize(current_account, record, preloaded_relations = {})
super(current_account, record)
@preloaded_relations = preloaded_relations
end
def index?
staff?
end
def show?
return true if owned? || staff?
if private?
if record.group_id
private_group_member?
else
following_author?
end
elsif requires_mention?
mention_exists?
2019-07-02 08:10:25 +01:00
else
current_account.nil? || !author_blocking?
end
end
def reblog?
!requires_mention? && (!private? || owned?) && show? && !blocking_author?
end
def favourite?
show? && !blocking_author?
end
def destroy?
staff? || owned?
end
alias unreblog? destroy?
def update?
2019-08-15 23:04:16 +01:00
staff? || owned?
2019-07-02 08:10:25 +01:00
end
private
def requires_mention?
2020-11-15 18:48:32 +00:00
record.limited_visibility?
2019-07-02 08:10:25 +01:00
end
def owned?
author.id == current_account&.id
end
def private?
record.hidden?
2019-07-02 08:10:25 +01:00
end
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?
return false if owned?
2019-07-02 08:10:25 +01:00
@preloaded_relations[:blocking] ? @preloaded_relations[:blocking][author.id] : current_account.blocking?(author)
end
def author_blocking?
return false if current_account.nil?
return false if owned?
2019-07-02 08:10:25 +01:00
@preloaded_relations[:blocked_by] ? @preloaded_relations[:blocked_by][author.id] : author.blocking?(current_account)
end
def following_author?
return false if current_account.nil?
return true if owned?
2019-07-02 08:10:25 +01:00
@preloaded_relations[:following] ? @preloaded_relations[:following][author.id] : current_account.following?(author)
end
def private_group_member?
return false if current_account.nil?
return false if record.group_id.nil?
return true if owned?
GroupAccount.where(group_id: record.group_id, account: current_account).exists?
end
2019-07-02 08:10:25 +01:00
def author
record.account
end
end