activitypub: visibility: use SQL thread_visibility() function instead of manually...
[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 unless is_public?(activity) do
18 follower_address = User.get_cached_by_ap_id(activity.data["actor"]).follower_address
19 Enum.any?(activity.data["to"], &(&1 == follower_address))
20 else
21 false
22 end
23 end
24
25 def is_direct?(%Activity{data: %{"directMessage" => true}}), do: true
26 def is_direct?(%Object{data: %{"directMessage" => true}}), do: true
27
28 def is_direct?(activity) do
29 !is_public?(activity) && !is_private?(activity)
30 end
31
32 def visible_for_user?(activity, nil) do
33 is_public?(activity)
34 end
35
36 def visible_for_user?(activity, user) do
37 x = [user.ap_id | user.following]
38 y = [activity.actor] ++ activity.data["to"] ++ (activity.data["cc"] || [])
39 visible_for_user?(activity, nil) || Enum.any?(x, &(&1 in y))
40 end
41
42 def entire_thread_visible_for_user?(%Activity{} = activity, %User{} = user) do
43 {:ok, %{rows: [[result]]}} =
44 Ecto.Adapters.SQL.query(Repo, "SELECT thread_visibility($1, $2)", [
45 user.ap_id,
46 activity.data["id"]
47 ])
48
49 result
50 end
51
52 def get_visibility(object) do
53 public = "https://www.w3.org/ns/activitystreams#Public"
54 to = object.data["to"] || []
55 cc = object.data["cc"] || []
56
57 cond do
58 public in to ->
59 "public"
60
61 public in cc ->
62 "unlisted"
63
64 # this should use the sql for the object's activity
65 Enum.any?(to, &String.contains?(&1, "/followers")) ->
66 "private"
67
68 length(cc) > 0 ->
69 "private"
70
71 true ->
72 "direct"
73 end
74 end
75 end