total rebase
[anni] / lib / pleroma / web / activity_pub / mrf / quote_to_link_tag_policy.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2023 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Web.ActivityPub.MRF.QuoteToLinkTagPolicy do
6   @moduledoc "Force a Link tag for posts quoting another post. (may break outgoing federation of quote posts with older Pleroma versions)"
7   @behaviour Pleroma.Web.ActivityPub.MRF.Policy
8
9   alias Pleroma.Web.ActivityPub.ObjectValidators.CommonFixes
10
11   require Pleroma.Constants
12
13   @impl Pleroma.Web.ActivityPub.MRF.Policy
14   def filter(%{"object" => %{"quoteUrl" => _} = object} = activity) do
15     {:ok, Map.put(activity, "object", filter_object(object))}
16   end
17
18   @impl Pleroma.Web.ActivityPub.MRF.Policy
19   def filter(object), do: {:ok, object}
20
21   @impl Pleroma.Web.ActivityPub.MRF.Policy
22   def describe, do: {:ok, %{}}
23
24   @impl Pleroma.Web.ActivityPub.MRF.Policy
25   def history_awareness, do: :auto
26
27   defp filter_object(%{"quoteUrl" => quote_url} = object) do
28     tags = object["tag"] || []
29
30     if Enum.any?(tags, fn tag ->
31          CommonFixes.object_link_tag?(tag) and tag["href"] == quote_url
32        end) do
33       object
34     else
35       object
36       |> Map.put(
37         "tag",
38         tags ++
39           [
40             %{
41               "type" => "Link",
42               "mediaType" => Pleroma.Constants.activity_json_canonical_mime_type(),
43               "href" => quote_url
44             }
45           ]
46       )
47     end
48   end
49 end