merge develop
[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 def is_public?(%Object{data: %{"type" => "Tombstone"}}), do: false
12 def is_public?(%Object{data: data}), do: is_public?(data)
13 def is_public?(%Activity{data: data}), do: is_public?(data)
14 def is_public?(%{"directMessage" => true}), do: false
15
16 def is_public?(data) do
17 "https://www.w3.org/ns/activitystreams#Public" in (data["to"] ++ (data["cc"] || []))
18 end
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 visible_for_user?(activity, nil) do
38 is_public?(activity)
39 end
40
41 def visible_for_user?(activity, user) do
42 x = [user.ap_id | user.following]
43 y = [activity.actor] ++ activity.data["to"] ++ (activity.data["cc"] || [])
44 visible_for_user?(activity, nil) || Enum.any?(x, &(&1 in y))
45 end
46
47 def entire_thread_visible_for_user?(%Activity{} = activity, %User{} = user) do
48 {:ok, %{rows: [[result]]}} =
49 Ecto.Adapters.SQL.query(Repo, "SELECT thread_visibility($1, $2)", [
50 user.ap_id,
51 activity.data["id"]
52 ])
53
54 result
55 end
56
57 def get_visibility(object) do
58 public = "https://www.w3.org/ns/activitystreams#Public"
59 to = object.data["to"] || []
60 cc = object.data["cc"] || []
61
62 cond do
63 public in to ->
64 "public"
65
66 public in cc ->
67 "unlisted"
68
69 # this should use the sql for the object's activity
70 Enum.any?(to, &String.contains?(&1, "/followers")) ->
71 "private"
72
73 object.data["directMessage"] == true ->
74 "direct"
75
76 length(cc) > 0 ->
77 "private"
78
79 true ->
80 "direct"
81 end
82 end
83 end