activitypub: mrf: tag policy: add support for processing follow requests
[akkoma] / lib / pleroma / web / activity_pub / mrf / tag_policy.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.TagPolicy do
6 alias Pleroma.User
7 @behaviour Pleroma.Web.ActivityPub.MRF
8
9 defp get_tags(%User{tags: tags}) when is_list(tags), do: tags
10 defp get_tags(_), do: []
11
12 defp process_tag(
13 "mrf_tag:media-force-nsfw",
14 %{"type" => "Create", "object" => %{"attachment" => child_attachment} = object} = message
15 )
16 when length(child_attachment) > 0 do
17 tags = (object["tag"] || []) ++ ["nsfw"]
18
19 object =
20 object
21 |> Map.put("tags", tags)
22 |> Map.put("sensitive", true)
23
24 message = Map.put(message, "object", object)
25
26 {:ok, message}
27 end
28
29 defp process_tag(
30 "mrf_tag:media-strip",
31 %{"type" => "Create", "object" => %{"attachment" => child_attachment} = object} = message
32 )
33 when length(child_attachment) > 0 do
34 object = Map.delete(object, "attachment")
35 message = Map.put(message, "object", object)
36
37 {:ok, message}
38 end
39
40 defp process_tag(
41 "mrf_tag:force-unlisted",
42 %{"type" => "Create", "to" => to, "cc" => cc, "actor" => actor} = message
43 ) do
44 user = User.get_cached_by_ap_id(actor)
45
46 if Enum.member?(to, "https://www.w3.org/ns/activitystreams#Public") do
47 to =
48 List.delete(to, "https://www.w3.org/ns/activitystreams#Public") ++ [user.follower_address]
49
50 cc =
51 List.delete(cc, user.follower_address) ++ ["https://www.w3.org/ns/activitystreams#Public"]
52
53 message =
54 message
55 |> Map.put("to", to)
56 |> Map.put("cc", cc)
57
58 {:ok, message}
59 else
60 {:ok, message}
61 end
62 end
63
64 defp process_tag(
65 "mrf_tag:sandbox",
66 %{"type" => "Create", "to" => to, "cc" => cc, "actor" => actor} = message
67 ) do
68 user = User.get_cached_by_ap_id(actor)
69
70 if Enum.member?(to, "https://www.w3.org/ns/activitystreams#Public") or
71 Enum.member?(cc, "https://www.w3.org/ns/activitystreams#Public") do
72 to =
73 List.delete(to, "https://www.w3.org/ns/activitystreams#Public") ++ [user.follower_address]
74
75 cc = List.delete(cc, "https://www.w3.org/ns/activitystreams#Public")
76
77 message =
78 message
79 |> Map.put("to", to)
80 |> Map.put("cc", cc)
81
82 {:ok, message}
83 else
84 {:ok, message}
85 end
86 end
87
88 defp process_tag(_, message), do: {:ok, message}
89
90 @impl true
91 def filter(%{"object" => target_actor, "type" => "Follow"} = message) do
92 User.get_cached_by_ap_id(target_actor)
93 |> get_tags()
94 |> Enum.reduce({:ok, message}, fn
95 tag, {:ok, message} ->
96 process_tag(tag, message)
97
98 _, error ->
99 error
100 end)
101 end
102
103 @impl true
104 def filter(%{"actor" => actor, "type" => "Create"} = message) do
105 User.get_cached_by_ap_id(actor)
106 |> get_tags()
107 |> Enum.reduce({:ok, message}, fn
108 tag, {:ok, message} ->
109 process_tag(tag, message)
110
111 _, error ->
112 error
113 end)
114 end
115
116 @impl true
117 def filter(message), do: {:ok, message}
118 end