simple_policy.ex 3.45 KB
Newer Older
1
# Pleroma: A lightweight social networking server
kaniini's avatar
kaniini committed
2
# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
3
4
# SPDX-License-Identifier: AGPL-3.0-only

5
6
defmodule Pleroma.Web.ActivityPub.MRF.SimplePolicy do
  alias Pleroma.User
7
  @moduledoc "Filter activities depending on their origin instance"
href's avatar
href committed
8
  @behaviour Pleroma.Web.ActivityPub.MRF
9

href's avatar
href committed
10
11
  defp check_accept(%{host: actor_host} = _actor_info, object) do
    accepts = Pleroma.Config.get([:mrf_simple, :accept])
12

href's avatar
href committed
13
14
    cond do
      accepts == [] -> {:ok, object}
href's avatar
href committed
15
      actor_host == Pleroma.Config.get([Pleroma.Web.Endpoint, :url, :host]) -> {:ok, object}
href's avatar
href committed
16
17
18
      Enum.member?(accepts, actor_host) -> {:ok, object}
      true -> {:reject, nil}
    end
kaniini's avatar
kaniini committed
19
20
  end

href's avatar
href committed
21
22
23
24
25
26
  defp check_reject(%{host: actor_host} = _actor_info, object) do
    if Enum.member?(Pleroma.Config.get([:mrf_simple, :reject]), actor_host) do
      {:reject, nil}
    else
      {:ok, object}
    end
27
28
  end

href's avatar
href committed
29
30
  defp check_media_removal(
         %{host: actor_host} = _actor_info,
31
         %{"type" => "Create", "object" => %{"attachment" => child_attachment}} = object
href's avatar
href committed
32
       )
href's avatar
href committed
33
       when length(child_attachment) > 0 do
href's avatar
href committed
34
35
36
37
38
39
40
    object =
      if Enum.member?(Pleroma.Config.get([:mrf_simple, :media_removal]), actor_host) do
        child_object = Map.delete(object["object"], "attachment")
        Map.put(object, "object", child_object)
      else
        object
      end
41
42

    {:ok, object}
43
44
  end

href's avatar
href committed
45
  defp check_media_removal(_actor_info, object), do: {:ok, object}
46

47
  defp check_media_nsfw(
href's avatar
href committed
48
         %{host: actor_host} = _actor_info,
49
50
51
52
53
         %{
           "type" => "Create",
           "object" => %{"attachment" => child_attachment} = child_object
         } = object
       )
href's avatar
href committed
54
55
56
57
58
59
60
61
62
63
64
       when length(child_attachment) > 0 do
    object =
      if Enum.member?(Pleroma.Config.get([:mrf_simple, :media_nsfw]), actor_host) do
        tags = (child_object["tag"] || []) ++ ["nsfw"]
        child_object = Map.put(child_object, "tags", tags)
        child_object = Map.put(child_object, "sensitive", true)
        Map.put(object, "object", child_object)
      else
        object
      end

65
    {:ok, object}
66
67
  end

href's avatar
href committed
68
  defp check_media_nsfw(_actor_info, object), do: {:ok, object}
69

href's avatar
href committed
70
  defp check_ftl_removal(%{host: actor_host} = _actor_info, object) do
71
    object =
href's avatar
href committed
72
73
74
75
76
      with true <-
             Enum.member?(
               Pleroma.Config.get([:mrf_simple, :federated_timeline_removal]),
               actor_host
             ),
href's avatar
href committed
77
           user <- User.get_cached_by_ap_id(object["actor"]),
href's avatar
href committed
78
79
           true <- "https://www.w3.org/ns/activitystreams#Public" in object["to"],
           true <- user.follower_address in object["cc"] do
80
81
82
83
84
85
86
87
88
89
90
91
        to =
          List.delete(object["to"], "https://www.w3.org/ns/activitystreams#Public") ++
            [user.follower_address]

        cc =
          List.delete(object["cc"], user.follower_address) ++
            ["https://www.w3.org/ns/activitystreams#Public"]

        object
        |> Map.put("to", to)
        |> Map.put("cc", cc)
      else
href's avatar
href committed
92
        _ -> object
93
94
95
      end

    {:ok, object}
96
97
  end

href's avatar
href committed
98
  @impl true
99
100
101
  def filter(object) do
    actor_info = URI.parse(object["actor"])

kaniini's avatar
kaniini committed
102
103
    with {:ok, object} <- check_accept(actor_info, object),
         {:ok, object} <- check_reject(actor_info, object),
104
105
106
107
108
         {:ok, object} <- check_media_removal(actor_info, object),
         {:ok, object} <- check_media_nsfw(actor_info, object),
         {:ok, object} <- check_ftl_removal(actor_info, object) do
      {:ok, object}
    else
feld's avatar
feld committed
109
      _e -> {:reject, nil}
110
111
112
    end
  end
end