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