2019-02-22 05:29:52 -07:00
defmodule Pleroma.Web.ActivityPub.Visibility do
alias Pleroma.Activity
alias Pleroma.Object
alias Pleroma.User
def is_public? ( % Object { data : %{ " type " = > " Tombstone " } } ) , do : false
def is_public? ( % Object { data : data } ) , do : is_public? ( data )
def is_public? ( % Activity { data : data } ) , do : is_public? ( data )
def is_public? ( %{ " directMessage " = > true } ) , do : false
def is_public? ( data ) do
" https://www.w3.org/ns/activitystreams # Public " in ( data [ " to " ] ++ ( data [ " cc " ] || [ ] ) )
end
def is_private? ( activity ) do
2019-05-08 10:08:50 -06:00
with false <- is_public? ( activity ) ,
% User { follower_address : follower_address } <-
User . get_cached_by_ap_id ( activity . data [ " actor " ] ) do
follower_address in activity . data [ " to " ]
2019-02-22 05:29:52 -07:00
else
2019-05-08 10:08:50 -06:00
_ -> false
2019-02-22 05:29:52 -07:00
end
end
def is_direct? ( % Activity { data : %{ " directMessage " = > true } } ) , do : true
def is_direct? ( % Object { data : %{ " directMessage " = > true } } ) , do : true
def is_direct? ( activity ) do
! is_public? ( activity ) && ! is_private? ( activity )
end
def visible_for_user? ( activity , nil ) do
is_public? ( activity )
end
def visible_for_user? ( activity , user ) do
x = [ user . ap_id | user . following ]
y = [ activity . actor ] ++ activity . data [ " to " ] ++ ( activity . data [ " cc " ] || [ ] )
visible_for_user? ( activity , nil ) || Enum . any? ( x , & ( &1 in y ) )
end
# guard
def entire_thread_visible_for_user? ( nil , _user ) , do : false
2019-04-18 00:28:20 -06:00
# XXX: Probably even more inefficient than the previous implementation intended to be a placeholder untill https://git.pleroma.social/pleroma/pleroma/merge_requests/971 is in develop
# credo:disable-for-previous-line Credo.Check.Readability.MaxLineLength
2019-02-22 05:29:52 -07:00
def entire_thread_visible_for_user? (
2019-04-17 08:59:15 -06:00
% Activity { } = tail ,
# %Activity{data: %{"object" => %{"inReplyTo" => parent_id}}} = tail,
2019-02-22 05:29:52 -07:00
user
2019-04-17 08:59:15 -06:00
) do
case Object . normalize ( tail ) do
%{ data : %{ " inReplyTo " = > parent_id } } when is_binary ( parent_id ) ->
parent = Activity . get_in_reply_to_activity ( tail )
visible_for_user? ( tail , user ) && entire_thread_visible_for_user? ( parent , user )
_ ->
visible_for_user? ( tail , user )
end
2019-02-22 05:29:52 -07:00
end
end