1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.ActivityPub.Visibility do
10 alias Pleroma.Web.ActivityPub.Utils
12 require Pleroma.Constants
14 @spec is_public?(Object.t() | Activity.t() | map()) :: boolean()
15 def is_public?(%Object{data: %{"type" => "Tombstone"}}), do: false
16 def is_public?(%Object{data: data}), do: is_public?(data)
17 def is_public?(%Activity{data: %{"type" => "Move"}}), do: true
18 def is_public?(%Activity{data: data}), do: is_public?(data)
19 def is_public?(%{"directMessage" => true}), do: false
21 def is_public?(data) do
22 Utils.label_in_message?(Pleroma.Constants.as_public(), data) or
23 Utils.label_in_message?(Pleroma.Constants.as_local_public(), data)
26 def is_local_public?(%Object{data: data}), do: is_local_public?(data)
27 def is_local_public?(%Activity{data: data}), do: is_local_public?(data)
29 def is_local_public?(data) do
30 Utils.label_in_message?(Pleroma.Constants.as_local_public(), data) and
31 not Utils.label_in_message?(Pleroma.Constants.as_public(), data)
34 def is_private?(activity) do
35 with false <- is_public?(activity),
36 %User{follower_address: follower_address} <-
37 User.get_cached_by_ap_id(activity.data["actor"]) do
38 follower_address in activity.data["to"]
44 def is_announceable?(activity, user, public \\ true) do
45 is_public?(activity) ||
46 (!public && is_private?(activity) && activity.data["actor"] == user.ap_id)
49 def is_direct?(%Activity{data: %{"directMessage" => true}}), do: true
50 def is_direct?(%Object{data: %{"directMessage" => true}}), do: true
52 def is_direct?(activity) do
53 !is_public?(activity) && !is_private?(activity)
56 def is_list?(%{data: %{"listMessage" => _}}), do: true
57 def is_list?(_), do: false
59 @spec visible_for_user?(Object.t() | Activity.t() | nil, User.t() | nil) :: boolean()
60 def visible_for_user?(%Activity{actor: ap_id}, %User{ap_id: ap_id}), do: true
61 def visible_for_user?(%Object{data: %{"actor" => ap_id}}, %User{ap_id: ap_id}), do: true
62 def visible_for_user?(nil, _), do: false
63 def visible_for_user?(%Activity{data: %{"listMessage" => _}}, nil), do: false
65 def visible_for_user?(
66 %Activity{data: %{"listMessage" => list_ap_id}} = activity,
69 user.ap_id in activity.data["to"] ||
71 |> Pleroma.List.get_by_ap_id()
72 |> Pleroma.List.member?(user)
75 def visible_for_user?(%{__struct__: module} = message, nil)
76 when module in [Activity, Object] do
77 if restrict_unauthenticated_access?(message),
79 else: is_public?(message) and not is_local_public?(message)
82 def visible_for_user?(%{__struct__: module} = message, user)
83 when module in [Activity, Object] do
84 x = [user.ap_id | User.following(user)]
85 y = [message.data["actor"]] ++ message.data["to"] ++ (message.data["cc"] || [])
86 is_public?(message) || Enum.any?(x, &(&1 in y))
89 def entire_thread_visible_for_user?(%Activity{} = activity, %User{} = user) do
90 {:ok, %{rows: [[result]]}} =
91 Ecto.Adapters.SQL.query(Repo, "SELECT thread_visibility($1, $2)", [
99 def restrict_unauthenticated_access?(%Activity{local: local}) do
100 restrict_unauthenticated_access_to_activity?(local)
103 def restrict_unauthenticated_access?(%Object{} = object) do
106 |> restrict_unauthenticated_access_to_activity?()
109 def restrict_unauthenticated_access?(%User{} = user) do
110 User.visible_for(user, _reading_user = nil)
113 defp restrict_unauthenticated_access_to_activity?(local?) when is_boolean(local?) do
114 cfg_key = if local?, do: :local, else: :remote
116 Pleroma.Config.restrict_unauthenticated_access?(:activities, cfg_key)
119 def get_visibility(object) do
120 to = object.data["to"] || []
121 cc = object.data["cc"] || []
124 Pleroma.Constants.as_public() in to ->
127 Pleroma.Constants.as_public() in cc ->
130 Pleroma.Constants.as_local_public() in to ->
133 # this should use the sql for the object's activity
134 Enum.any?(to, &String.contains?(&1, "/followers")) ->
137 object.data["directMessage"] == true ->
140 is_binary(object.data["listMessage"]) ->