Merge remote-tracking branch 'upstream/develop' into admin-create-users
[akkoma] / lib / pleroma / web / activity_pub / visibility.ex
1 defmodule Pleroma.Web.ActivityPub.Visibility do
2 alias Pleroma.Activity
3 alias Pleroma.Object
4 alias Pleroma.Repo
5 alias Pleroma.User
6
7 def is_public?(%Object{data: %{"type" => "Tombstone"}}), do: false
8 def is_public?(%Object{data: data}), do: is_public?(data)
9 def is_public?(%Activity{data: data}), do: is_public?(data)
10 def is_public?(%{"directMessage" => true}), do: false
11
12 def is_public?(data) do
13 "https://www.w3.org/ns/activitystreams#Public" in (data["to"] ++ (data["cc"] || []))
14 end
15
16 def is_private?(activity) do
17 with false <- is_public?(activity),
18 %User{follower_address: follower_address} <-
19 User.get_cached_by_ap_id(activity.data["actor"]) do
20 follower_address in activity.data["to"]
21 else
22 _ -> false
23 end
24 end
25
26 def is_direct?(%Activity{data: %{"directMessage" => true}}), do: true
27 def is_direct?(%Object{data: %{"directMessage" => true}}), do: true
28
29 def is_direct?(activity) do
30 !is_public?(activity) && !is_private?(activity)
31 end
32
33 def visible_for_user?(activity, nil) do
34 is_public?(activity)
35 end
36
37 def visible_for_user?(activity, user) do
38 x = [user.ap_id | user.following]
39 y = [activity.actor] ++ activity.data["to"] ++ (activity.data["cc"] || [])
40 visible_for_user?(activity, nil) || Enum.any?(x, &(&1 in y))
41 end
42
43 def entire_thread_visible_for_user?(%Activity{} = activity, %User{} = user) do
44 {:ok, %{rows: [[result]]}} =
45 Ecto.Adapters.SQL.query(Repo, "SELECT thread_visibility($1, $2)", [
46 user.ap_id,
47 activity.data["id"]
48 ])
49
50 result
51 end
52
53 def get_visibility(object) do
54 public = "https://www.w3.org/ns/activitystreams#Public"
55 to = object.data["to"] || []
56 cc = object.data["cc"] || []
57
58 cond do
59 public in to ->
60 "public"
61
62 public in cc ->
63 "unlisted"
64
65 # this should use the sql for the object's activity
66 Enum.any?(to, &String.contains?(&1, "/followers")) ->
67 "private"
68
69 length(cc) > 0 ->
70 "private"
71
72 true ->
73 "direct"
74 end
75 end
76 end