1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.ActivityPub.MRF.HashtagPolicy do
6 require Pleroma.Constants
12 Reject, TWKN-remove or Set-Sensitive messsages with specific hashtags (without the leading #)
14 Note: This MRF Policy is always enabled, if you want to disable it you have to set empty lists.
17 @behaviour Pleroma.Web.ActivityPub.MRF.Policy
20 def history_awareness, do: :manual
22 defp check_reject(message, hashtags) do
23 if Enum.any?(Config.get([:mrf_hashtag, :reject]), fn match -> match in hashtags end) do
24 {:reject, "[HashtagPolicy] Matches with rejected keyword"}
30 defp check_ftl_removal(%{"to" => to} = message, hashtags) do
31 if Pleroma.Constants.as_public() in to and
32 Enum.any?(Config.get([:mrf_hashtag, :federated_timeline_removal]), fn match ->
35 to = List.delete(to, Pleroma.Constants.as_public())
36 cc = [Pleroma.Constants.as_public() | message["cc"] || []]
42 |> Kernel.put_in(["object", "to"], to)
43 |> Kernel.put_in(["object", "cc"], cc)
51 defp check_ftl_removal(message, _hashtags), do: {:ok, message}
53 defp check_sensitive(message) do
55 Object.Updater.do_with_history(message["object"], fn object ->
56 hashtags = Object.hashtags(%Object{data: object})
58 if Enum.any?(Config.get([:mrf_hashtag, :sensitive]), fn match -> match in hashtags end) do
59 {:ok, Map.put(object, "sensitive", true)}
65 {:ok, Map.put(message, "object", new_object)}
69 def filter(%{"type" => type, "object" => object} = message) when type in ["Create", "Update"] do
71 with %{"formerRepresentations" => %{"orderedItems" => items}} <- object do
78 Enum.reduce(history_items, [], fn item, acc ->
79 acc ++ Object.hashtags(%Object{data: item})
82 hashtags = Object.hashtags(%Object{data: object}) ++ historical_hashtags
85 with {:ok, message} <- check_reject(message, hashtags),
87 (if "type" == "Create" do
88 check_ftl_removal(message, hashtags)
92 {:ok, message} <- check_sensitive(message) do
101 def filter(message), do: {:ok, message}
106 Config.get(:mrf_hashtag)
109 {:ok, %{mrf_hashtag: mrf_hashtag}}
113 def config_description do
116 related_policy: "Pleroma.Web.ActivityPub.MRF.HashtagPolicy",
117 label: "MRF Hashtag",
118 description: @moduledoc,
122 type: {:list, :string},
123 description: "A list of hashtags which result in message being rejected.",
127 key: :federated_timeline_removal,
128 type: {:list, :string},
130 "A list of hashtags which result in message being removed from federated timelines (a.k.a unlisted).",
135 type: {:list, :string},
137 "A list of hashtags which result in message being set as sensitive (a.k.a NSFW/R-18)",
138 suggestions: ["nsfw", "r18"]