Add importer module

This commit is contained in:
tusooa 2022-11-06 22:22:00 -05:00
parent 8d704d384d
commit 08c983db3f
No known key found for this signature in database
GPG key ID: 7B467EDE43A08224
3 changed files with 123 additions and 11 deletions

View file

@ -42,17 +42,18 @@ defmodule Pleroma.Constants do
]
)
const(updatable_object_types,
do: [
"Note",
"Question",
"Audio",
"Video",
"Event",
"Article",
"Page"
]
)
@status_types [
"Note",
"Question",
"Audio",
"Video",
"Event",
"Article",
"Page"
]
const(updatable_object_types, do: @status_types)
const(status_types, do: @status_types)
const(actor_types,
do: [

View file

@ -0,0 +1,64 @@
# Pleroma: A lightweight social networking server
# Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only
defmodule Pleroma.Web.ActivityPub.Importer do
alias Pleroma.User
alias Pleroma.Web.ActivityPub.ActivityPub
require Pleroma.Constants
def import_object(
%{
"type" => type,
"published" => published,
"context" => context
} = object,
%User{} = user
)
when type in Pleroma.Constants.status_types() do
fixed_object =
object
|> strip_ap_id()
|> rewrite_actor(user)
|> strip_recipients()
ActivityPub.create(%{
actor: user,
published: published,
object: fixed_object,
to: [],
local: true,
context: context
})
end
def import_activity(%{"type" => "Create", "object" => object} = _activity, %User{} = user) do
import_object(object, user)
end
def import_activity(_, _), do: nil
def import_one(%{"type" => type} = object, %User{} = user) do
if type in Pleroma.Constants.status_types() do
import_object(object, user)
else
import_activity(object, user)
end
end
defp strip_ap_id(object), do: object |> Map.drop(["id"])
defp rewrite_actor(object, user) do
object
|> Map.put("actor", user.ap_id)
end
defp strip_recipients(object) do
object
|> Map.put("to", [])
|> Map.put("cc", [])
|> Map.put("bto", [])
|> Map.put("bcc", [])
end
end

View file

@ -0,0 +1,47 @@
# Pleroma: A lightweight social networking server
# Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only
defmodule Pleroma.Web.ActivityPub.ImporterTest do
use Pleroma.DataCase
alias Pleroma.Web.ActivityPub.Importer
alias Pleroma.Web.ActivityPub.Transmogrifier
alias Pleroma.Web.CommonAPI
require Pleroma.Constants
import Pleroma.Factory
describe "import_one/2" do
test "it imports an activity" do
user = insert(:user)
{:ok, activity} = CommonAPI.post(user, %{status: "mew"})
{:ok, activity_for_import} = Transmogrifier.prepare_outgoing(activity.data)
importing_user = insert(:user)
assert {:ok, imported_activity} = Importer.import_one(activity_for_import, importing_user)
assert imported_activity.id != activity.id
assert imported_activity.actor == importing_user.ap_id
assert imported_activity.object.data["actor"] == importing_user.ap_id
assert imported_activity.object.data["content"] == activity.object.data["content"]
assert imported_activity.object.data["published"] == activity.object.data["published"]
end
test "it strips all mentions" do
user = insert(:user)
user2 = insert(:user)
{:ok, activity} = CommonAPI.post(user, %{status: "mew @#{user2.nickname}"})
assert user2.ap_id in activity.recipients
{:ok, activity_for_import} = Transmogrifier.prepare_outgoing(activity.data)
importing_user = insert(:user)
assert {:ok, imported_activity} = Importer.import_one(activity_for_import, importing_user)
assert [importing_user.ap_id] == imported_activity.recipients
assert [] == imported_activity.object.data["to"]
assert [] == imported_activity.object.data["cc"]
end
end
end