1a0c9a46c1c62b26c9addd9a2e868b48bc4733b2
[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(), User.t() | nil) :: boolean()
52 def visible_for_user?(%{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?(%{data: %{"listMessage" => _}}, nil), do: false
57
58 def visible_for_user?(%{data: %{"listMessage" => list_ap_id}} = activity, %User{} = user) do
59 user.ap_id in activity.data["to"] ||
60 list_ap_id
61 |> Pleroma.List.get_by_ap_id()
62 |> Pleroma.List.member?(user)
63 end
64
65 def visible_for_user?(%{local: local} = activity, nil) do
66 cfg_key = if local, do: :local, else: :remote
67
68 if Pleroma.Config.restrict_unauthenticated_access?(:activities, cfg_key),
69 do: false,
70 else: is_public?(activity)
71 end
72
73 def visible_for_user?(activity, user) do
74 x = [user.ap_id | User.following(user)]
75 y = [activity.actor] ++ activity.data["to"] ++ (activity.data["cc"] || [])
76 is_public?(activity) || Enum.any?(x, &(&1 in y))
77 end
78
79 def entire_thread_visible_for_user?(%Activity{} = activity, %User{} = user) do
80 {:ok, %{rows: [[result]]}} =
81 Ecto.Adapters.SQL.query(Repo, "SELECT thread_visibility($1, $2)", [
82 user.ap_id,
83 activity.data["id"]
84 ])
85
86 result
87 end
88
89 def get_visibility(object) do
90 to = object.data["to"] || []
91 cc = object.data["cc"] || []
92
93 cond do
94 Pleroma.Constants.as_public() in to ->
95 "public"
96
97 Pleroma.Constants.as_public() in cc ->
98 "unlisted"
99
100 # this should use the sql for the object's activity
101 Enum.any?(to, &String.contains?(&1, "/followers")) ->
102 "private"
103
104 object.data["directMessage"] == true ->
105 "direct"
106
107 is_binary(object.data["listMessage"]) ->
108 "list"
109
110 length(cc) > 0 ->
111 "private"
112
113 true ->
114 "direct"
115 end
116 end
117 end