1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2022 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(Pleroma.Web.ApiSpec.CastAndValidate, replace_params: false)
19 defdelegate open_api_operation(action), to: ApiSpec.UserImportOperation
22 %{private: %{open_api_spex: %{body_params: %{list: %Plug.Upload{path: path}}}}} = conn,
25 list = File.read!(path)
29 def follow(%{private: %{open_api_spex: %{body_params: %{list: list}}}} = conn, _),
30 do: do_follow(conn, list)
32 def do_follow(%{assigns: %{user: follower}} = conn, list) do
36 |> Enum.map(&(&1 |> String.split(",") |> List.first()))
37 |> List.delete("Account address")
38 |> Enum.map(&(&1 |> String.trim() |> String.trim_leading("@")))
39 |> Enum.reject(&(&1 == ""))
41 User.Import.follow_import(follower, identifiers)
42 json(conn, "job started")
46 %{private: %{open_api_spex: %{body_params: %{list: %Plug.Upload{path: path}}}}} = conn,
49 list = File.read!(path)
53 def blocks(%{private: %{open_api_spex: %{body_params: %{list: list}}}} = conn, _),
54 do: do_block(conn, list)
56 defp do_block(%{assigns: %{user: blocker}} = conn, list) do
57 User.Import.blocks_import(blocker, prepare_user_identifiers(list))
58 json(conn, "job started")
62 %{private: %{open_api_spex: %{body_params: %{list: %Plug.Upload{path: path}}}}} = conn,
65 list = File.read!(path)
69 def mutes(%{private: %{open_api_spex: %{body_params: %{list: list}}}} = conn, _),
70 do: do_mute(conn, list)
72 defp do_mute(%{assigns: %{user: user}} = conn, list) do
73 User.Import.mutes_import(user, prepare_user_identifiers(list))
74 json(conn, "job started")
77 defp prepare_user_identifiers(list) do
80 |> Enum.map(&String.trim_leading(&1, "@"))