7c57f88f99b939c50a669461b42d659d1c947cf6
[anni] / lib / pleroma / web / activity_pub / visibility.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Web.ActivityPub.Visibility do
6   alias Pleroma.Activity
7   alias Pleroma.Object
8   alias Pleroma.Repo
9   alias Pleroma.User
10   alias Pleroma.Web.ActivityPub.Utils
11
12   require Pleroma.Constants
13
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
20
21   def is_public?(data) do
22     Utils.label_in_message?(Pleroma.Constants.as_public(), data) or
23       Utils.label_in_message?(Utils.as_local_public(), data)
24   end
25
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)
28
29   def is_local_public?(data) do
30     Utils.label_in_message?(Utils.as_local_public(), data) and
31       not Utils.label_in_message?(Pleroma.Constants.as_public(), data)
32   end
33
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"]
39     else
40       _ -> false
41     end
42   end
43
44   def is_announceable?(activity, user, public \\ true) do
45     is_public?(activity) ||
46       (!public && is_private?(activity) && activity.data["actor"] == user.ap_id)
47   end
48
49   def is_direct?(%Activity{data: %{"directMessage" => true}}), do: true
50   def is_direct?(%Object{data: %{"directMessage" => true}}), do: true
51
52   def is_direct?(activity) do
53     !is_public?(activity) && !is_private?(activity)
54   end
55
56   def is_list?(%{data: %{"listMessage" => _}}), do: true
57   def is_list?(_), do: false
58
59   @spec visible_for_user?(Object.t() | Activity.t() | nil, User.t() | nil) :: boolean()
60   def visible_for_user?(%Object{data: %{"type" => "Tombstone"}}, _), do: false
61   def visible_for_user?(%Activity{actor: ap_id}, %User{ap_id: ap_id}), do: true
62   def visible_for_user?(%Object{data: %{"actor" => ap_id}}, %User{ap_id: ap_id}), do: true
63   def visible_for_user?(nil, _), do: false
64   def visible_for_user?(%Activity{data: %{"listMessage" => _}}, nil), do: false
65
66   def visible_for_user?(
67         %Activity{data: %{"listMessage" => list_ap_id}} = activity,
68         %User{} = user
69       ) do
70     user.ap_id in activity.data["to"] ||
71       list_ap_id
72       |> Pleroma.List.get_by_ap_id()
73       |> Pleroma.List.member?(user)
74   end
75
76   def visible_for_user?(%{__struct__: module} = message, nil)
77       when module in [Activity, Object] do
78     if restrict_unauthenticated_access?(message),
79       do: false,
80       else: is_public?(message) and not is_local_public?(message)
81   end
82
83   def visible_for_user?(%{__struct__: module} = message, user)
84       when module in [Activity, Object] do
85     x = [user.ap_id | User.following(user)]
86     y = [message.data["actor"]] ++ message.data["to"] ++ (message.data["cc"] || [])
87
88     user_is_local = user.local
89     federatable = not is_local_public?(message)
90     (is_public?(message) || Enum.any?(x, &(&1 in y))) and (user_is_local || federatable)
91   end
92
93   def entire_thread_visible_for_user?(%Activity{} = activity, %User{} = user) do
94     {:ok, %{rows: [[result]]}} =
95       Ecto.Adapters.SQL.query(Repo, "SELECT thread_visibility($1, $2)", [
96         user.ap_id,
97         activity.data["id"]
98       ])
99
100     result
101   end
102
103   def restrict_unauthenticated_access?(%Activity{local: local}) do
104     restrict_unauthenticated_access_to_activity?(local)
105   end
106
107   def restrict_unauthenticated_access?(%Object{} = object) do
108     object
109     |> Object.local?()
110     |> restrict_unauthenticated_access_to_activity?()
111   end
112
113   def restrict_unauthenticated_access?(%User{} = user) do
114     User.visible_for(user, _reading_user = nil)
115   end
116
117   defp restrict_unauthenticated_access_to_activity?(local?) when is_boolean(local?) do
118     cfg_key = if local?, do: :local, else: :remote
119
120     Pleroma.Config.restrict_unauthenticated_access?(:activities, cfg_key)
121   end
122
123   def get_visibility(object) do
124     to = object.data["to"] || []
125     cc = object.data["cc"] || []
126
127     cond do
128       Pleroma.Constants.as_public() in to ->
129         "public"
130
131       Pleroma.Constants.as_public() in cc ->
132         "unlisted"
133
134       Utils.as_local_public() in to ->
135         "local"
136
137       # this should use the sql for the object's activity
138       Enum.any?(to, &String.contains?(&1, "/followers")) ->
139         "private"
140
141       object.data["directMessage"] == true ->
142         "direct"
143
144       is_binary(object.data["listMessage"]) ->
145         "list"
146
147       length(cc) > 0 ->
148         "private"
149
150       true ->
151         "direct"
152     end
153   end
154 end