Collection importing route (WIP)

Signed-off-by: marcin mikołajczak <git@mkljczk.pl>
This commit is contained in:
marcin mikołajczak 2023-08-08 23:53:21 +02:00
parent c39fae12bc
commit 822b9edbfe
4 changed files with 38 additions and 2 deletions

View file

@ -65,6 +65,15 @@ def import_one(%{"type" => type} = object, %User{} = user, opts \\ []) do
end
end
def import_collection(%{
"type" => type,
"orderedItems" => objects
}, user)
when type in ["OrderedCollection", "Collection"] do
objects
|> Enum.each(&import_one(&1, user))
end
defp strip_ap_id(object), do: object |> Map.drop(["id"])
defp rewrite_actor(object, user) do

View file

@ -58,12 +58,26 @@ def mutes_operation do
}
end
def collection_operation do
%Operation{
tags: ["Data import"],
summary: "Import collection",
operationId: "UserImportController.collection",
requestBody: request_body("Parameters", import_request(), required: true),
responses: %{
200 => ok_response(),
500 => Operation.response("Error", "application/json", ApiError)
},
security: [%{"oAuth" => ["write:statuses"]}]
}
end
defp import_request do
%Schema{
type: :object,
required: [:list],
required: [:collection],
properties: %{
list: %Schema{
collection: %Schema{
description:
"STRING or FILE containing a whitespace-separated list of accounts to import.",
anyOf: [

View file

@ -14,6 +14,7 @@ defmodule Pleroma.Web.PleromaAPI.UserImportController do
plug(OAuthScopesPlug, %{scopes: ["follow", "write:follows"]} when action == :follow)
plug(OAuthScopesPlug, %{scopes: ["follow", "write:blocks"]} when action == :blocks)
plug(OAuthScopesPlug, %{scopes: ["follow", "write:mutes"]} when action == :mutes)
plug(OAuthScopesPlug, %{scopes: ["follow", "write:statuses"]} when action == :collection)
plug(Pleroma.Web.ApiSpec.CastAndValidate, replace_params: false)
defdelegate open_api_operation(action), to: ApiSpec.UserImportOperation
@ -74,6 +75,17 @@ defp do_mute(%{assigns: %{user: user}} = conn, list) do
json(conn, "job started")
end
def collection(%{body_params: %{list: %Plug.Upload{path: path}}} = conn, _) do
collection(%Plug.Conn{conn | body_params: %{list: File.read!(path)}}, %{})
end
def collection(%{assigns: %{user: user}, body_params: %{collection: collection}} = conn, _) do
collection
|> Jason.decode!()
|> Pleroma.Web.ActivityPub.Importer.import_collection(user)
json(conn, "job started")
end
defp prepare_user_identifiers(list) do
list
|> String.split()

View file

@ -567,6 +567,7 @@ defmodule Pleroma.Web.Router do
post("/mutes_import", UserImportController, :mutes)
post("/blocks_import", UserImportController, :blocks)
post("/follow_import", UserImportController, :follow)
post("/collection_import", UserImportController, :collection)
get("/accounts/mfa", TwoFactorAuthenticationController, :settings)
get("/accounts/mfa/backup_codes", TwoFactorAuthenticationController, :backup_codes)