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.PleromaAPI.UserImportController do
6 use Pleroma.Web, :controller
11 alias Pleroma.Web.ApiSpec
12 alias Pleroma.Web.Plugs.OAuthScopesPlug
14 plug(OAuthScopesPlug, %{scopes: ["follow", "write:follows"]} when action == :follow)
15 plug(OAuthScopesPlug, %{scopes: ["follow", "write:blocks"]} when action == :blocks)
16 plug(OAuthScopesPlug, %{scopes: ["follow", "write:mutes"]} when action == :mutes)
18 plug(OpenApiSpex.Plug.CastAndValidate)
19 defdelegate open_api_operation(action), to: ApiSpec.UserImportOperation
21 def follow(%{body_params: %{list: %Plug.Upload{path: path}}} = conn, _) do
22 follow(%Plug.Conn{conn | body_params: %{list: File.read!(path)}}, %{})
25 def follow(%{assigns: %{user: follower}, body_params: %{list: list}} = conn, _) do
29 |> Enum.map(&(&1 |> String.split(",") |> List.first()))
30 |> List.delete("Account address")
31 |> Enum.map(&(&1 |> String.trim() |> String.trim_leading("@")))
32 |> Enum.reject(&(&1 == ""))
34 User.Import.follow_import(follower, identifiers)
35 json(conn, "job started")
38 def blocks(%{body_params: %{list: %Plug.Upload{path: path}}} = conn, _) do
39 blocks(%Plug.Conn{conn | body_params: %{list: File.read!(path)}}, %{})
42 def blocks(%{assigns: %{user: blocker}, body_params: %{list: list}} = conn, _) do
43 User.Import.blocks_import(blocker, prepare_user_identifiers(list))
44 json(conn, "job started")
47 def mutes(%{body_params: %{list: %Plug.Upload{path: path}}} = conn, _) do
48 mutes(%Plug.Conn{conn | body_params: %{list: File.read!(path)}}, %{})
51 def mutes(%{assigns: %{user: user}, body_params: %{list: list}} = conn, _) do
52 User.Import.mutes_import(user, prepare_user_identifiers(list))
53 json(conn, "job started")
56 defp prepare_user_identifiers(list) do
59 |> Enum.map(&String.trim_leading(&1, "@"))