Merge branch 'develop' of git.pleroma.social:pleroma/pleroma into remake-remodel
[akkoma] / lib / pleroma / web / activity_pub / visibility.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 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: data}), do: is_public?(data)
18 def is_public?(%{"directMessage" => true}), do: false
19 def is_public?(data), do: Utils.label_in_message?(Pleroma.Constants.as_public(), data)
20
21 def is_private?(activity) do
22 with false <- is_public?(activity),
23 %User{follower_address: follower_address} <-
24 User.get_cached_by_ap_id(activity.data["actor"]) do
25 follower_address in activity.data["to"]
26 else
27 _ -> false
28 end
29 end
30
31 def is_announceable?(activity, user, public \\ true) do
32 is_public?(activity) ||
33 (!public && is_private?(activity) && activity.data["actor"] == user.ap_id)
34 end
35
36 def is_direct?(%Activity{data: %{"directMessage" => true}}), do: true
37 def is_direct?(%Object{data: %{"directMessage" => true}}), do: true
38
39 def is_direct?(activity) do
40 !is_public?(activity) && !is_private?(activity)
41 end
42
43 def is_list?(%{data: %{"listMessage" => _}}), do: true
44 def is_list?(_), do: false
45
46 def visible_for_user?(%{actor: ap_id}, %User{ap_id: ap_id}), do: true
47
48 def visible_for_user?(%{data: %{"listMessage" => list_ap_id}} = activity, %User{} = user) do
49 user.ap_id in activity.data["to"] ||
50 list_ap_id
51 |> Pleroma.List.get_by_ap_id()
52 |> Pleroma.List.member?(user)
53 end
54
55 def visible_for_user?(%{data: %{"listMessage" => _}}, nil), do: false
56
57 def visible_for_user?(activity, nil) do
58 is_public?(activity)
59 end
60
61 def visible_for_user?(activity, user) do
62 x = [user.ap_id | User.following(user)]
63 y = [activity.actor] ++ activity.data["to"] ++ (activity.data["cc"] || [])
64 visible_for_user?(activity, nil) || Enum.any?(x, &(&1 in y))
65 end
66
67 def entire_thread_visible_for_user?(%Activity{} = activity, %User{} = user) do
68 {:ok, %{rows: [[result]]}} =
69 Ecto.Adapters.SQL.query(Repo, "SELECT thread_visibility($1, $2)", [
70 user.ap_id,
71 activity.data["id"]
72 ])
73
74 result
75 end
76
77 def get_visibility(object) do
78 to = object.data["to"] || []
79 cc = object.data["cc"] || []
80
81 cond do
82 Pleroma.Constants.as_public() in to ->
83 "public"
84
85 Pleroma.Constants.as_public() in cc ->
86 "unlisted"
87
88 # this should use the sql for the object's activity
89 Enum.any?(to, &String.contains?(&1, "/followers")) ->
90 "private"
91
92 object.data["directMessage"] == true ->
93 "direct"
94
95 is_binary(object.data["listMessage"]) ->
96 "list"
97
98 length(cc) > 0 ->
99 "private"
100
101 true ->
102 "direct"
103 end
104 end
105 end