Transmogrifier: Simplify fix_explicit_addressing and fix_implicit_addressing

This commit is contained in:
Haelwenn (lanodan) Monnier 2020-09-10 11:09:11 +02:00
parent ef36f7fa5c
commit e56779dd8d
No known key found for this signature in database
GPG key ID: D5B7A8E43C997DEE
2 changed files with 19 additions and 38 deletions

View file

@ -72,17 +72,21 @@ defmodule Pleroma.Web.ActivityPub.Transmogrifier do
end end
end end
def fix_explicit_addressing( # if directMessage flag is set to true, leave the addressing alone
%{"to" => to, "cc" => cc} = object, def fix_explicit_addressing(%{"directMessage" => true} = object, _follower_collection),
explicit_mentions, do: object
follower_collection
) do
explicit_to = Enum.filter(to, fn x -> x in explicit_mentions end)
def fix_explicit_addressing(%{"to" => to, "cc" => cc} = object, follower_collection) do
explicit_mentions =
Utils.determine_explicit_mentions(object) ++
[Pleroma.Constants.as_public(), follower_collection]
explicit_to = Enum.filter(to, fn x -> x in explicit_mentions end)
explicit_cc = Enum.filter(to, fn x -> x not in explicit_mentions end) explicit_cc = Enum.filter(to, fn x -> x not in explicit_mentions end)
final_cc = final_cc =
(cc ++ explicit_cc) (cc ++ explicit_cc)
|> Enum.filter(& &1)
|> Enum.reject(fn x -> String.ends_with?(x, "/followers") and x != follower_collection end) |> Enum.reject(fn x -> String.ends_with?(x, "/followers") and x != follower_collection end)
|> Enum.uniq() |> Enum.uniq()
@ -91,29 +95,6 @@ defmodule Pleroma.Web.ActivityPub.Transmogrifier do
|> Map.put("cc", final_cc) |> Map.put("cc", final_cc)
end end
def fix_explicit_addressing(object, _explicit_mentions, _followers_collection), do: object
# if directMessage flag is set to true, leave the addressing alone
def fix_explicit_addressing(%{"directMessage" => true} = object), do: object
def fix_explicit_addressing(object) do
explicit_mentions = Utils.determine_explicit_mentions(object)
%User{follower_address: follower_collection} =
object
|> Containment.get_actor()
|> User.get_cached_by_ap_id()
explicit_mentions =
explicit_mentions ++
[
Pleroma.Constants.as_public(),
follower_collection
]
fix_explicit_addressing(object, explicit_mentions, follower_collection)
end
# if as:Public is addressed, then make sure the followers collection is also addressed # if as:Public is addressed, then make sure the followers collection is also addressed
# so that the activities will be delivered to local users. # so that the activities will be delivered to local users.
def fix_implicit_addressing(%{"to" => to, "cc" => cc} = object, followers_collection) do def fix_implicit_addressing(%{"to" => to, "cc" => cc} = object, followers_collection) do
@ -137,19 +118,19 @@ defmodule Pleroma.Web.ActivityPub.Transmogrifier do
end end
end end
def fix_implicit_addressing(object, _), do: object
def fix_addressing(object) do def fix_addressing(object) do
{:ok, %User{} = user} = User.get_or_fetch_by_ap_id(object["actor"]) {:ok, %User{follower_address: follower_collection}} =
followers_collection = User.ap_followers(user) object
|> Containment.get_actor()
|> User.get_or_fetch_by_ap_id()
object object
|> fix_addressing_list("to") |> fix_addressing_list("to")
|> fix_addressing_list("cc") |> fix_addressing_list("cc")
|> fix_addressing_list("bto") |> fix_addressing_list("bto")
|> fix_addressing_list("bcc") |> fix_addressing_list("bcc")
|> fix_explicit_addressing() |> fix_explicit_addressing(follower_collection)
|> fix_implicit_addressing(followers_collection) |> fix_implicit_addressing(follower_collection)
end end
def fix_actor(%{"attributedTo" => actor} = object) do def fix_actor(%{"attributedTo" => actor} = object) do

View file

@ -446,7 +446,7 @@ defmodule Pleroma.Web.ActivityPub.TransmogrifierTest do
end) end)
} }
fixed_object = Transmogrifier.fix_explicit_addressing(object) fixed_object = Transmogrifier.fix_explicit_addressing(object, user.follower_address)
assert Enum.all?(explicitly_mentioned_actors, &(&1 in fixed_object["to"])) assert Enum.all?(explicitly_mentioned_actors, &(&1 in fixed_object["to"]))
refute "https://social.beepboop.ga/users/dirb" in fixed_object["to"] refute "https://social.beepboop.ga/users/dirb" in fixed_object["to"]
assert "https://social.beepboop.ga/users/dirb" in fixed_object["cc"] assert "https://social.beepboop.ga/users/dirb" in fixed_object["cc"]
@ -459,7 +459,7 @@ defmodule Pleroma.Web.ActivityPub.TransmogrifierTest do
"cc" => [] "cc" => []
} }
fixed_object = Transmogrifier.fix_explicit_addressing(object) fixed_object = Transmogrifier.fix_explicit_addressing(object, user.follower_address)
assert user.follower_address in fixed_object["to"] assert user.follower_address in fixed_object["to"]
refute user.follower_address in fixed_object["cc"] refute user.follower_address in fixed_object["cc"]
end end
@ -473,7 +473,7 @@ defmodule Pleroma.Web.ActivityPub.TransmogrifierTest do
"cc" => [user.follower_address, recipient.follower_address] "cc" => [user.follower_address, recipient.follower_address]
} }
fixed_object = Transmogrifier.fix_explicit_addressing(object) fixed_object = Transmogrifier.fix_explicit_addressing(object, user.follower_address)
assert user.follower_address in fixed_object["cc"] assert user.follower_address in fixed_object["cc"]
refute recipient.follower_address in fixed_object["cc"] refute recipient.follower_address in fixed_object["cc"]