Don't crash when email settings are invalid
[akkoma] / lib / pleroma / web / pleroma_api / controllers / user_import_controller.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Web.PleromaAPI.UserImportController do
6 use Pleroma.Web, :controller
7
8 require Logger
9
10 alias Pleroma.User
11 alias Pleroma.Web.ApiSpec
12 alias Pleroma.Web.Plugs.OAuthScopesPlug
13
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)
17
18 plug(OpenApiSpex.Plug.CastAndValidate)
19 defdelegate open_api_operation(action), to: ApiSpec.UserImportOperation
20
21 def follow(%{body_params: %{list: %Plug.Upload{path: path}}} = conn, _) do
22 follow(%Plug.Conn{conn | body_params: %{list: File.read!(path)}}, %{})
23 end
24
25 def follow(%{assigns: %{user: follower}, body_params: %{list: list}} = conn, _) do
26 identifiers =
27 list
28 |> String.split("\n")
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 == ""))
33
34 User.Import.follow_import(follower, identifiers)
35 json(conn, "job started")
36 end
37
38 def blocks(%{body_params: %{list: %Plug.Upload{path: path}}} = conn, _) do
39 blocks(%Plug.Conn{conn | body_params: %{list: File.read!(path)}}, %{})
40 end
41
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")
45 end
46
47 def mutes(%{body_params: %{list: %Plug.Upload{path: path}}} = conn, _) do
48 mutes(%Plug.Conn{conn | body_params: %{list: File.read!(path)}}, %{})
49 end
50
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")
54 end
55
56 defp prepare_user_identifiers(list) do
57 list
58 |> String.split()
59 |> Enum.map(&String.trim_leading(&1, "@"))
60 end
61 end