Merge branch 'develop' of git.pleroma.social:pleroma/pleroma into feature/local-only...
[akkoma] / lib / pleroma / web / activity_pub / visibility.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 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?(Pleroma.Constants.as_local_public(), data)
24 end
25
26 def is_private?(activity) do
27 with false <- is_public?(activity),
28 %User{follower_address: follower_address} <-
29 User.get_cached_by_ap_id(activity.data["actor"]) do
30 follower_address in activity.data["to"]
31 else
32 _ -> false
33 end
34 end
35
36 def is_announceable?(activity, user, public \\ true) do
37 is_public?(activity) ||
38 (!public && is_private?(activity) && activity.data["actor"] == user.ap_id)
39 end
40
41 def is_direct?(%Activity{data: %{"directMessage" => true}}), do: true
42 def is_direct?(%Object{data: %{"directMessage" => true}}), do: true
43
44 def is_direct?(activity) do
45 !is_public?(activity) && !is_private?(activity)
46 end
47
48 def is_list?(%{data: %{"listMessage" => _}}), do: true
49 def is_list?(_), do: false
50
51 @spec visible_for_user?(Activity.t() | nil, User.t() | nil) :: boolean()
52 def visible_for_user?(%Activity{actor: ap_id}, %User{ap_id: ap_id}), do: true
53
54 def visible_for_user?(nil, _), do: false
55
56 def visible_for_user?(%Activity{data: %{"listMessage" => _}}, nil), do: false
57
58 def visible_for_user?(
59 %Activity{data: %{"listMessage" => list_ap_id}} = activity,
60 %User{} = user
61 ) do
62 user.ap_id in activity.data["to"] ||
63 list_ap_id
64 |> Pleroma.List.get_by_ap_id()
65 |> Pleroma.List.member?(user)
66 end
67
68 def visible_for_user?(%Activity{} = activity, nil) do
69 if restrict_unauthenticated_access?(activity),
70 do: false,
71 else: is_public?(activity)
72 end
73
74 def visible_for_user?(%Activity{} = activity, user) do
75 x = [user.ap_id | User.following(user)]
76 y = [activity.actor] ++ activity.data["to"] ++ (activity.data["cc"] || [])
77 is_public?(activity) || Enum.any?(x, &(&1 in y))
78 end
79
80 def entire_thread_visible_for_user?(%Activity{} = activity, %User{} = user) do
81 {:ok, %{rows: [[result]]}} =
82 Ecto.Adapters.SQL.query(Repo, "SELECT thread_visibility($1, $2)", [
83 user.ap_id,
84 activity.data["id"]
85 ])
86
87 result
88 end
89
90 def restrict_unauthenticated_access?(%Activity{local: local}) do
91 restrict_unauthenticated_access_to_activity?(local)
92 end
93
94 def restrict_unauthenticated_access?(%Object{} = object) do
95 object
96 |> Object.local?()
97 |> restrict_unauthenticated_access_to_activity?()
98 end
99
100 def restrict_unauthenticated_access?(%User{} = user) do
101 User.visible_for(user, _reading_user = nil)
102 end
103
104 defp restrict_unauthenticated_access_to_activity?(local?) when is_boolean(local?) do
105 cfg_key = if local?, do: :local, else: :remote
106
107 Pleroma.Config.restrict_unauthenticated_access?(:activities, cfg_key)
108 end
109
110 def get_visibility(object) do
111 to = object.data["to"] || []
112 cc = object.data["cc"] || []
113
114 cond do
115 Pleroma.Constants.as_public() in to ->
116 "public"
117
118 Pleroma.Constants.as_public() in cc ->
119 "unlisted"
120
121 # this should use the sql for the object's activity
122 Enum.any?(to, &String.contains?(&1, "/followers")) ->
123 "private"
124
125 object.data["directMessage"] == true ->
126 "direct"
127
128 is_binary(object.data["listMessage"]) ->
129 "list"
130
131 length(cc) > 0 ->
132 "private"
133
134 true ->
135 "direct"
136 end
137 end
138 end