make bulk user creation from admin works as a transaction
[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 unless is_public?(activity) do
17 follower_address = User.get_cached_by_ap_id(activity.data["actor"]).follower_address
18 Enum.any?(activity.data["to"], &(&1 == follower_address))
19 else
20 false
21 end
22 end
23
24 def is_direct?(%Activity{data: %{"directMessage" => true}}), do: true
25 def is_direct?(%Object{data: %{"directMessage" => true}}), do: true
26
27 def is_direct?(activity) do
28 !is_public?(activity) && !is_private?(activity)
29 end
30
31 def visible_for_user?(activity, nil) do
32 is_public?(activity)
33 end
34
35 def visible_for_user?(activity, user) do
36 x = [user.ap_id | user.following]
37 y = [activity.actor] ++ activity.data["to"] ++ (activity.data["cc"] || [])
38 visible_for_user?(activity, nil) || Enum.any?(x, &(&1 in y))
39 end
40
41 # guard
42 def entire_thread_visible_for_user?(nil, _user), do: false
43
44 # 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
45 # credo:disable-for-previous-line Credo.Check.Readability.MaxLineLength
46
47 def entire_thread_visible_for_user?(
48 %Activity{} = tail,
49 # %Activity{data: %{"object" => %{"inReplyTo" => parent_id}}} = tail,
50 user
51 ) do
52 case Object.normalize(tail) do
53 %{data: %{"inReplyTo" => parent_id}} when is_binary(parent_id) ->
54 parent = Activity.get_in_reply_to_activity(tail)
55 visible_for_user?(tail, user) && entire_thread_visible_for_user?(parent, user)
56
57 _ ->
58 visible_for_user?(tail, user)
59 end
60 end
61
62 def get_visibility(object) do
63 public = "https://www.w3.org/ns/activitystreams#Public"
64 to = object.data["to"] || []
65 cc = object.data["cc"] || []
66
67 cond do
68 public in to ->
69 "public"
70
71 public in cc ->
72 "unlisted"
73
74 # this should use the sql for the object's activity
75 Enum.any?(to, &String.contains?(&1, "/followers")) ->
76 "private"
77
78 length(cc) > 0 ->
79 "private"
80
81 true ->
82 "direct"
83 end
84 end
85 end