Merge remote-tracking branch 'pleroma/develop' into feature/disable-account
[akkoma] / lib / pleroma / web / activity_pub / mrf / reject_non_public.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.MRF.RejectNonPublic do
6 alias Pleroma.User
7 @moduledoc "Rejects non-public (followers-only, direct) activities"
8 @behaviour Pleroma.Web.ActivityPub.MRF
9
10 @impl true
11 def filter(%{"type" => "Create"} = object) do
12 user = User.get_cached_by_ap_id(object["actor"])
13 public = "https://www.w3.org/ns/activitystreams#Public"
14
15 # Determine visibility
16 visibility =
17 cond do
18 public in object["to"] -> "public"
19 public in object["cc"] -> "unlisted"
20 user.follower_address in object["to"] -> "followers"
21 true -> "direct"
22 end
23
24 policy = Pleroma.Config.get(:mrf_rejectnonpublic)
25
26 case visibility do
27 "public" ->
28 {:ok, object}
29
30 "unlisted" ->
31 {:ok, object}
32
33 "followers" ->
34 with true <- Keyword.get(policy, :allow_followersonly) do
35 {:ok, object}
36 else
37 _e -> {:reject, nil}
38 end
39
40 "direct" ->
41 with true <- Keyword.get(policy, :allow_direct) do
42 {:ok, object}
43 else
44 _e -> {:reject, nil}
45 end
46 end
47 end
48
49 @impl true
50 def filter(object), do: {:ok, object}
51 end