097fceb08f9ae92cc3d81aaf60c4b7a847f04272
[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 @public "https://www.w3.org/ns/activitystreams#Public"
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: @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_direct?(%Activity{data: %{"directMessage" => true}}), do: true
31 def is_direct?(%Object{data: %{"directMessage" => true}}), do: true
32
33 def is_direct?(activity) do
34 !is_public?(activity) && !is_private?(activity)
35 end
36
37 def is_list?(%{data: %{"listMessage" => _}}), do: true
38 def is_list?(_), do: false
39
40 def visible_for_user?(%{actor: ap_id}, %User{ap_id: ap_id}), do: true
41
42 def visible_for_user?(%{data: %{"listMessage" => list_ap_id}} = activity, %User{} = user) do
43 user.ap_id in activity.data["to"] ||
44 list_ap_id
45 |> Pleroma.List.get_by_ap_id()
46 |> Pleroma.List.member?(user)
47 end
48
49 def visible_for_user?(%{data: %{"listMessage" => _}}, nil), do: false
50
51 def visible_for_user?(activity, nil) do
52 is_public?(activity)
53 end
54
55 def visible_for_user?(activity, user) do
56 x = [user.ap_id | user.following]
57 y = [activity.actor] ++ activity.data["to"] ++ (activity.data["cc"] || [])
58 visible_for_user?(activity, nil) || Enum.any?(x, &(&1 in y))
59 end
60
61 def entire_thread_visible_for_user?(%Activity{} = activity, %User{} = user) do
62 {:ok, %{rows: [[result]]}} =
63 Ecto.Adapters.SQL.query(Repo, "SELECT thread_visibility($1, $2)", [
64 user.ap_id,
65 activity.data["id"]
66 ])
67
68 result
69 end
70
71 def get_visibility(object) do
72 to = object.data["to"] || []
73 cc = object.data["cc"] || []
74
75 cond do
76 @public in to ->
77 "public"
78
79 @public in cc ->
80 "unlisted"
81
82 # this should use the sql for the object's activity
83 Enum.any?(to, &String.contains?(&1, "/followers")) ->
84 "private"
85
86 object.data["directMessage"] == true ->
87 "direct"
88
89 is_binary(object.data["listMessage"]) ->
90 "list"
91
92 length(cc) > 0 ->
93 "private"
94
95 true ->
96 "direct"
97 end
98 end
99 end