logo

pleroma

My custom branche(s) on git.pleroma.social/pleroma/pleroma git clone https://hacktivis.me/git/pleroma.git

user_import_controller.ex (2552B)


  1. # Pleroma: A lightweight social networking server
  2. # Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
  3. # SPDX-License-Identifier: AGPL-3.0-only
  4. defmodule Pleroma.Web.PleromaAPI.UserImportController do
  5. use Pleroma.Web, :controller
  6. require Logger
  7. alias Pleroma.User
  8. alias Pleroma.Web.ApiSpec
  9. alias Pleroma.Web.Plugs.OAuthScopesPlug
  10. plug(OAuthScopesPlug, %{scopes: ["follow", "write:follows"]} when action == :follow)
  11. plug(OAuthScopesPlug, %{scopes: ["follow", "write:blocks"]} when action == :blocks)
  12. plug(OAuthScopesPlug, %{scopes: ["follow", "write:mutes"]} when action == :mutes)
  13. plug(Pleroma.Web.ApiSpec.CastAndValidate, replace_params: false)
  14. defdelegate open_api_operation(action), to: ApiSpec.UserImportOperation
  15. def follow(
  16. %{private: %{open_api_spex: %{body_params: %{list: %Plug.Upload{path: path}}}}} = conn,
  17. _
  18. ) do
  19. list = File.read!(path)
  20. do_follow(conn, list)
  21. end
  22. def follow(%{private: %{open_api_spex: %{body_params: %{list: list}}}} = conn, _),
  23. do: do_follow(conn, list)
  24. def do_follow(%{assigns: %{user: follower}} = conn, list) do
  25. identifiers =
  26. list
  27. |> String.split("\n")
  28. |> Enum.map(&(&1 |> String.split(",") |> List.first()))
  29. |> List.delete("Account address")
  30. |> Enum.map(&(&1 |> String.trim() |> String.trim_leading("@")))
  31. |> Enum.reject(&(&1 == ""))
  32. User.Import.follow_import(follower, identifiers)
  33. json(conn, "job started")
  34. end
  35. def blocks(
  36. %{private: %{open_api_spex: %{body_params: %{list: %Plug.Upload{path: path}}}}} = conn,
  37. _
  38. ) do
  39. list = File.read!(path)
  40. do_block(conn, list)
  41. end
  42. def blocks(%{private: %{open_api_spex: %{body_params: %{list: list}}}} = conn, _),
  43. do: do_block(conn, list)
  44. defp do_block(%{assigns: %{user: blocker}} = conn, list) do
  45. User.Import.blocks_import(blocker, prepare_user_identifiers(list))
  46. json(conn, "job started")
  47. end
  48. def mutes(
  49. %{private: %{open_api_spex: %{body_params: %{list: %Plug.Upload{path: path}}}}} = conn,
  50. _
  51. ) do
  52. list = File.read!(path)
  53. do_mute(conn, list)
  54. end
  55. def mutes(%{private: %{open_api_spex: %{body_params: %{list: list}}}} = conn, _),
  56. do: do_mute(conn, list)
  57. defp do_mute(%{assigns: %{user: user}} = conn, list) do
  58. User.Import.mutes_import(user, prepare_user_identifiers(list))
  59. json(conn, "job started")
  60. end
  61. defp prepare_user_identifiers(list) do
  62. list
  63. |> String.split()
  64. |> Enum.map(&String.trim_leading(&1, "@"))
  65. end
  66. end