2019-07-10 05:13:23 +00:00
|
|
|
# Pleroma: A lightweight social networking server
|
2023-01-02 20:38:50 +00:00
|
|
|
# Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
|
2019-07-10 05:13:23 +00:00
|
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2019-02-22 12:29:52 +00:00
|
|
|
defmodule Pleroma.Web.ActivityPub.Visibility do
|
|
|
|
alias Pleroma.Activity
|
|
|
|
alias Pleroma.Object
|
2019-03-25 00:38:28 +00:00
|
|
|
alias Pleroma.Repo
|
2019-02-22 12:29:52 +00:00
|
|
|
alias Pleroma.User
|
2019-10-23 01:50:25 +00:00
|
|
|
alias Pleroma.Web.ActivityPub.Utils
|
2019-02-22 12:29:52 +00:00
|
|
|
|
2019-07-29 02:43:19 +00:00
|
|
|
require Pleroma.Constants
|
2019-07-23 19:15:48 +00:00
|
|
|
|
|
|
|
@spec is_public?(Object.t() | Activity.t() | map()) :: boolean()
|
2019-02-22 12:29:52 +00:00
|
|
|
def is_public?(%Object{data: %{"type" => "Tombstone"}}), do: false
|
|
|
|
def is_public?(%Object{data: data}), do: is_public?(data)
|
2019-10-30 11:21:49 +00:00
|
|
|
def is_public?(%Activity{data: %{"type" => "Move"}}), do: true
|
2019-02-22 12:29:52 +00:00
|
|
|
def is_public?(%Activity{data: data}), do: is_public?(data)
|
|
|
|
def is_public?(%{"directMessage" => true}), do: false
|
2020-10-02 17:00:50 +00:00
|
|
|
|
|
|
|
def is_public?(data) do
|
|
|
|
Utils.label_in_message?(Pleroma.Constants.as_public(), data) or
|
2021-05-31 18:39:15 +00:00
|
|
|
Utils.label_in_message?(Utils.as_local_public(), data)
|
2020-10-02 17:00:50 +00:00
|
|
|
end
|
2019-02-22 12:29:52 +00:00
|
|
|
|
2020-11-11 14:47:57 +00:00
|
|
|
def is_local_public?(%Object{data: data}), do: is_local_public?(data)
|
|
|
|
def is_local_public?(%Activity{data: data}), do: is_local_public?(data)
|
|
|
|
|
|
|
|
def is_local_public?(data) do
|
2021-05-31 18:39:15 +00:00
|
|
|
Utils.label_in_message?(Utils.as_local_public(), data) and
|
2020-11-11 14:47:57 +00:00
|
|
|
not Utils.label_in_message?(Pleroma.Constants.as_public(), data)
|
|
|
|
end
|
|
|
|
|
2019-02-22 12:29:52 +00:00
|
|
|
def is_private?(activity) do
|
2019-05-08 16:08:50 +00: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 12:29:52 +00:00
|
|
|
else
|
2019-05-08 16:08:50 +00:00
|
|
|
_ -> false
|
2019-02-22 12:29:52 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-10-01 15:49:52 +00:00
|
|
|
def is_announceable?(activity, user, public \\ true) do
|
|
|
|
is_public?(activity) ||
|
|
|
|
(!public && is_private?(activity) && activity.data["actor"] == user.ap_id)
|
2019-02-24 17:45:29 +00:00
|
|
|
end
|
|
|
|
|
2019-02-22 12:29:52 +00:00
|
|
|
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
|
|
|
|
|
2019-07-11 12:29:24 +00:00
|
|
|
def is_list?(%{data: %{"listMessage" => _}}), do: true
|
|
|
|
def is_list?(_), do: false
|
|
|
|
|
2021-01-21 16:45:42 +00:00
|
|
|
@spec visible_for_user?(Object.t() | Activity.t() | nil, User.t() | nil) :: boolean()
|
2021-07-27 13:54:01 +00:00
|
|
|
def visible_for_user?(%Object{data: %{"type" => "Tombstone"}}, _), do: false
|
2020-10-05 20:48:00 +00:00
|
|
|
def visible_for_user?(%Activity{actor: ap_id}, %User{ap_id: ap_id}), do: true
|
2021-01-21 16:45:42 +00:00
|
|
|
def visible_for_user?(%Object{data: %{"actor" => ap_id}}, %User{ap_id: ap_id}), do: true
|
2020-06-24 11:29:08 +00:00
|
|
|
def visible_for_user?(nil, _), do: false
|
2020-10-05 20:48:00 +00:00
|
|
|
def visible_for_user?(%Activity{data: %{"listMessage" => _}}, nil), do: false
|
2020-06-24 11:29:08 +00:00
|
|
|
|
2020-10-05 20:48:00 +00:00
|
|
|
def visible_for_user?(
|
|
|
|
%Activity{data: %{"listMessage" => list_ap_id}} = activity,
|
|
|
|
%User{} = user
|
|
|
|
) do
|
2019-07-15 07:54:40 +00:00
|
|
|
user.ap_id in activity.data["to"] ||
|
|
|
|
list_ap_id
|
|
|
|
|> Pleroma.List.get_by_ap_id()
|
|
|
|
|> Pleroma.List.member?(user)
|
2019-07-11 12:29:24 +00:00
|
|
|
end
|
|
|
|
|
2021-01-21 16:45:42 +00:00
|
|
|
def visible_for_user?(%{__struct__: module} = message, nil)
|
|
|
|
when module in [Activity, Object] do
|
|
|
|
if restrict_unauthenticated_access?(message),
|
2020-03-20 10:04:37 +00:00
|
|
|
do: false,
|
2021-01-21 16:45:42 +00:00
|
|
|
else: is_public?(message) and not is_local_public?(message)
|
2019-02-22 12:29:52 +00:00
|
|
|
end
|
|
|
|
|
2021-01-21 16:45:42 +00:00
|
|
|
def visible_for_user?(%{__struct__: module} = message, user)
|
|
|
|
when module in [Activity, Object] do
|
2019-10-10 19:35:32 +00:00
|
|
|
x = [user.ap_id | User.following(user)]
|
2021-01-21 16:45:42 +00:00
|
|
|
y = [message.data["actor"]] ++ message.data["to"] ++ (message.data["cc"] || [])
|
2022-05-05 22:07:30 +00:00
|
|
|
|
|
|
|
user_is_local = user.local
|
|
|
|
federatable = not is_local_public?(message)
|
|
|
|
(is_public?(message) || Enum.any?(x, &(&1 in y))) and (user_is_local || federatable)
|
2019-02-22 12:29:52 +00:00
|
|
|
end
|
|
|
|
|
2019-03-25 00:38:28 +00:00
|
|
|
def entire_thread_visible_for_user?(%Activity{} = activity, %User{} = user) do
|
|
|
|
{:ok, %{rows: [[result]]}} =
|
|
|
|
Ecto.Adapters.SQL.query(Repo, "SELECT thread_visibility($1, $2)", [
|
|
|
|
user.ap_id,
|
|
|
|
activity.data["id"]
|
|
|
|
])
|
2019-02-22 12:29:52 +00:00
|
|
|
|
2019-03-25 00:38:28 +00:00
|
|
|
result
|
2019-02-22 12:29:52 +00:00
|
|
|
end
|
2019-05-15 14:30:08 +00:00
|
|
|
|
2020-10-05 20:48:00 +00:00
|
|
|
def restrict_unauthenticated_access?(%Activity{local: local}) do
|
|
|
|
restrict_unauthenticated_access_to_activity?(local)
|
|
|
|
end
|
|
|
|
|
|
|
|
def restrict_unauthenticated_access?(%Object{} = object) do
|
|
|
|
object
|
|
|
|
|> Object.local?()
|
|
|
|
|> restrict_unauthenticated_access_to_activity?()
|
|
|
|
end
|
|
|
|
|
|
|
|
def restrict_unauthenticated_access?(%User{} = user) do
|
|
|
|
User.visible_for(user, _reading_user = nil)
|
|
|
|
end
|
|
|
|
|
|
|
|
defp restrict_unauthenticated_access_to_activity?(local?) when is_boolean(local?) do
|
|
|
|
cfg_key = if local?, do: :local, else: :remote
|
|
|
|
|
|
|
|
Pleroma.Config.restrict_unauthenticated_access?(:activities, cfg_key)
|
|
|
|
end
|
|
|
|
|
2019-05-15 14:30:08 +00:00
|
|
|
def get_visibility(object) do
|
|
|
|
to = object.data["to"] || []
|
|
|
|
cc = object.data["cc"] || []
|
|
|
|
|
|
|
|
cond do
|
2019-07-29 02:43:19 +00:00
|
|
|
Pleroma.Constants.as_public() in to ->
|
2019-05-15 14:30:08 +00:00
|
|
|
"public"
|
|
|
|
|
2019-07-29 02:43:19 +00:00
|
|
|
Pleroma.Constants.as_public() in cc ->
|
2019-05-15 14:30:08 +00:00
|
|
|
"unlisted"
|
|
|
|
|
2021-05-31 18:39:15 +00:00
|
|
|
Utils.as_local_public() in to ->
|
2020-11-11 14:47:57 +00:00
|
|
|
"local"
|
|
|
|
|
2019-05-15 14:30:08 +00:00
|
|
|
# this should use the sql for the object's activity
|
|
|
|
Enum.any?(to, &String.contains?(&1, "/followers")) ->
|
|
|
|
"private"
|
|
|
|
|
2019-06-01 03:23:21 +00:00
|
|
|
object.data["directMessage"] == true ->
|
|
|
|
"direct"
|
|
|
|
|
2019-07-11 12:29:24 +00:00
|
|
|
is_binary(object.data["listMessage"]) ->
|
|
|
|
"list"
|
|
|
|
|
2019-05-15 14:30:08 +00:00
|
|
|
length(cc) > 0 ->
|
|
|
|
"private"
|
|
|
|
|
|
|
|
true ->
|
|
|
|
"direct"
|
|
|
|
end
|
|
|
|
end
|
2019-02-22 12:29:52 +00:00
|
|
|
end
|