Merge remote-tracking branch 'origin/develop' into conversations-import
[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.User
5
6 def is_public?(%Object{data: %{"type" => "Tombstone"}}), do: false
7 def is_public?(%Object{data: data}), do: is_public?(data)
8 def is_public?(%Activity{data: data}), do: is_public?(data)
9 def is_public?(%{"directMessage" => true}), do: false
10
11 def is_public?(data) do
12 "https://www.w3.org/ns/activitystreams#Public" in (data["to"] ++ (data["cc"] || []))
13 end
14
15 def is_private?(activity) do
16 with false <- is_public?(activity),
17 %User{follower_address: follower_address} <-
18 User.get_cached_by_ap_id(activity.data["actor"]) do
19 follower_address in activity.data["to"]
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 # guard
43 def entire_thread_visible_for_user?(nil, _user), do: false
44
45 # XXX: Probably even more inefficient than the previous implementation intended to be a placeholder untill https://git.pleroma.social/pleroma/pleroma/merge_requests/971 is in develop
46 # credo:disable-for-previous-line Credo.Check.Readability.MaxLineLength
47
48 def entire_thread_visible_for_user?(
49 %Activity{} = tail,
50 # %Activity{data: %{"object" => %{"inReplyTo" => parent_id}}} = tail,
51 user
52 ) do
53 case Object.normalize(tail) do
54 %{data: %{"inReplyTo" => parent_id}} when is_binary(parent_id) ->
55 parent = Activity.get_in_reply_to_activity(tail)
56 visible_for_user?(tail, user) && entire_thread_visible_for_user?(parent, user)
57
58 _ ->
59 visible_for_user?(tail, user)
60 end
61 end
62
63 def get_visibility(object) do
64 public = "https://www.w3.org/ns/activitystreams#Public"
65 to = object.data["to"] || []
66 cc = object.data["cc"] || []
67
68 cond do
69 public in to ->
70 "public"
71
72 public in cc ->
73 "unlisted"
74
75 # this should use the sql for the object's activity
76 Enum.any?(to, &String.contains?(&1, "/followers")) ->
77 "private"
78
79 length(cc) > 0 ->
80 "private"
81
82 true ->
83 "direct"
84 end
85 end
86 end