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