2020-08-18 00:11:38 +00:00
|
|
|
# Pleroma: A lightweight social networking server
|
2021-01-13 06:49:20 +00:00
|
|
|
# Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
|
2020-08-18 00:11:38 +00:00
|
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
|
|
|
defmodule Pleroma.Web.ActivityPub.ObjectValidators.CommonFixes do
|
2020-09-10 09:08:05 +00:00
|
|
|
alias Pleroma.EctoType.ActivityPub.ObjectValidators
|
2020-08-20 00:00:04 +00:00
|
|
|
alias Pleroma.Object.Containment
|
2020-09-10 09:08:05 +00:00
|
|
|
alias Pleroma.User
|
|
|
|
alias Pleroma.Web.ActivityPub.Transmogrifier
|
2020-08-18 00:11:38 +00:00
|
|
|
alias Pleroma.Web.ActivityPub.Utils
|
|
|
|
|
2020-09-15 15:22:08 +00:00
|
|
|
def cast_and_filter_recipients(message, field, follower_collection, field_fallback \\ []) do
|
2020-09-10 17:45:42 +00:00
|
|
|
{:ok, data} = ObjectValidators.Recipients.cast(message[field] || field_fallback)
|
|
|
|
|
2020-09-15 15:22:08 +00:00
|
|
|
data =
|
|
|
|
Enum.reject(data, fn x ->
|
|
|
|
String.ends_with?(x, "/followers") and x != follower_collection
|
|
|
|
end)
|
|
|
|
|
2020-09-10 17:45:42 +00:00
|
|
|
Map.put(message, field, data)
|
|
|
|
end
|
|
|
|
|
2020-09-10 09:08:05 +00:00
|
|
|
def fix_object_defaults(data) do
|
2020-08-18 00:11:38 +00:00
|
|
|
%{data: %{"id" => context}, id: context_id} =
|
|
|
|
Utils.create_context(data["context"] || data["conversation"])
|
|
|
|
|
2020-09-10 09:08:05 +00:00
|
|
|
%User{follower_address: follower_collection} = User.get_cached_by_ap_id(data["attributedTo"])
|
|
|
|
|
2020-08-18 00:11:38 +00:00
|
|
|
data
|
2020-08-25 00:18:33 +00:00
|
|
|
|> Map.put("context", context)
|
|
|
|
|> Map.put("context_id", context_id)
|
2020-09-15 15:22:08 +00:00
|
|
|
|> cast_and_filter_recipients("to", follower_collection)
|
|
|
|
|> cast_and_filter_recipients("cc", follower_collection)
|
|
|
|
|> cast_and_filter_recipients("bto", follower_collection)
|
|
|
|
|> cast_and_filter_recipients("bcc", follower_collection)
|
2020-09-10 09:08:05 +00:00
|
|
|
|> Transmogrifier.fix_implicit_addressing(follower_collection)
|
2020-08-18 00:11:38 +00:00
|
|
|
end
|
|
|
|
|
2020-09-10 17:45:42 +00:00
|
|
|
def fix_activity_addressing(activity, _meta) do
|
|
|
|
%User{follower_address: follower_collection} = User.get_cached_by_ap_id(activity["actor"])
|
2020-09-10 09:08:05 +00:00
|
|
|
|
2020-09-10 09:11:10 +00:00
|
|
|
activity
|
2020-09-15 15:22:08 +00:00
|
|
|
|> cast_and_filter_recipients("to", follower_collection)
|
|
|
|
|> cast_and_filter_recipients("cc", follower_collection)
|
|
|
|
|> cast_and_filter_recipients("bto", follower_collection)
|
|
|
|
|> cast_and_filter_recipients("bcc", follower_collection)
|
2020-09-10 17:45:42 +00:00
|
|
|
|> Transmogrifier.fix_implicit_addressing(follower_collection)
|
2020-08-18 00:11:38 +00:00
|
|
|
end
|
2020-08-20 00:00:04 +00:00
|
|
|
|
|
|
|
def fix_actor(data) do
|
2020-09-10 09:08:05 +00:00
|
|
|
actor =
|
|
|
|
data
|
|
|
|
|> Map.put_new("actor", data["attributedTo"])
|
|
|
|
|> Containment.get_actor()
|
2020-08-20 00:00:04 +00:00
|
|
|
|
|
|
|
data
|
|
|
|
|> Map.put("actor", actor)
|
|
|
|
|> Map.put("attributedTo", actor)
|
|
|
|
end
|
2020-08-18 00:11:38 +00:00
|
|
|
end
|