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