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.ObjectValidators.CommonFixes do
6 alias Pleroma.EctoType.ActivityPub.ObjectValidators
7 alias Pleroma.Object.Containment
9 alias Pleroma.Web.ActivityPub.Transmogrifier
10 alias Pleroma.Web.ActivityPub.Utils
12 def cast_and_filter_recipients(message, field, follower_collection, field_fallback \\ []) do
13 {:ok, data} = ObjectValidators.Recipients.cast(message[field] || field_fallback)
16 Enum.reject(data, fn x ->
17 String.ends_with?(x, "/followers") and x != follower_collection
20 Map.put(message, field, data)
23 def fix_object_defaults(data) do
24 %{data: %{"id" => context}, id: context_id} =
25 Utils.create_context(data["context"] || data["conversation"])
27 %User{follower_address: follower_collection} = User.get_cached_by_ap_id(data["attributedTo"])
30 |> Map.put("context", context)
31 |> Map.put("context_id", context_id)
32 |> cast_and_filter_recipients("to", follower_collection)
33 |> cast_and_filter_recipients("cc", follower_collection)
34 |> cast_and_filter_recipients("bto", follower_collection)
35 |> cast_and_filter_recipients("bcc", follower_collection)
36 |> Transmogrifier.fix_implicit_addressing(follower_collection)
39 def fix_activity_addressing(activity, _meta) do
40 %User{follower_address: follower_collection} = User.get_cached_by_ap_id(activity["actor"])
43 |> cast_and_filter_recipients("to", follower_collection)
44 |> cast_and_filter_recipients("cc", follower_collection)
45 |> cast_and_filter_recipients("bto", follower_collection)
46 |> cast_and_filter_recipients("bcc", follower_collection)
47 |> Transmogrifier.fix_implicit_addressing(follower_collection)
50 def fix_actor(data) do
53 |> Map.put_new("actor", data["attributedTo"])
54 |> Containment.get_actor()
57 |> Map.put("actor", actor)
58 |> Map.put("attributedTo", actor)