1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.ActivityPub.MRF.TagPolicy do
7 @behaviour Pleroma.Web.ActivityPub.MRF
9 Apply policies based on user tags
11 This policy applies policies on a user activities depending on their tags
14 - `mrf_tag:media-force-nsfw`: Mark as sensitive on presence of attachments
15 - `mrf_tag:media-strip`: Remove attachments
16 - `mrf_tag:force-unlisted`: Mark as unlisted (removes from the federated timeline)
17 - `mrf_tag:sandbox`: Remove from public (local and federated) timelines
18 - `mrf_tag:disable-remote-subscription`: Reject non-local follow requests
19 - `mrf_tag:disable-any-subscription`: Reject any follow requests
22 defp get_tags(%User{tags: tags}) when is_list(tags), do: tags
23 defp get_tags(_), do: []
26 "mrf_tag:media-force-nsfw",
27 %{"type" => "Create", "object" => %{"attachment" => child_attachment} = object} = message
29 when length(child_attachment) > 0 do
30 tags = (object["tag"] || []) ++ ["nsfw"]
34 |> Map.put("tag", tags)
35 |> Map.put("sensitive", true)
37 message = Map.put(message, "object", object)
43 "mrf_tag:media-strip",
44 %{"type" => "Create", "object" => %{"attachment" => child_attachment} = object} = message
46 when length(child_attachment) > 0 do
47 object = Map.delete(object, "attachment")
48 message = Map.put(message, "object", object)
54 "mrf_tag:force-unlisted",
55 %{"type" => "Create", "to" => to, "cc" => cc, "actor" => actor} = message
57 user = User.get_cached_by_ap_id(actor)
59 if Enum.member?(to, "https://www.w3.org/ns/activitystreams#Public") do
61 List.delete(to, "https://www.w3.org/ns/activitystreams#Public") ++ [user.follower_address]
64 List.delete(cc, user.follower_address) ++ ["https://www.w3.org/ns/activitystreams#Public"]
75 |> Map.put("object", object)
85 %{"type" => "Create", "to" => to, "cc" => cc, "actor" => actor} = message
87 user = User.get_cached_by_ap_id(actor)
89 if Enum.member?(to, "https://www.w3.org/ns/activitystreams#Public") or
90 Enum.member?(cc, "https://www.w3.org/ns/activitystreams#Public") do
92 List.delete(to, "https://www.w3.org/ns/activitystreams#Public") ++ [user.follower_address]
94 cc = List.delete(cc, "https://www.w3.org/ns/activitystreams#Public")
105 |> Map.put("object", object)
114 "mrf_tag:disable-remote-subscription",
115 %{"type" => "Follow", "actor" => actor} = message
117 user = User.get_cached_by_ap_id(actor)
119 if user.local == true do
126 defp process_tag("mrf_tag:disable-any-subscription", %{"type" => "Follow"}), do: {:reject, nil}
128 defp process_tag(_, message), do: {:ok, message}
130 def filter_message(actor, message) do
131 User.get_cached_by_ap_id(actor)
133 |> Enum.reduce({:ok, message}, fn
134 tag, {:ok, message} ->
135 process_tag(tag, message)
143 def filter(%{"object" => target_actor, "type" => "Follow"} = message),
144 do: filter_message(target_actor, message)
147 def filter(%{"actor" => actor, "type" => "Create"} = message),
148 do: filter_message(actor, message)
151 def filter(message), do: {:ok, message}