Move admin approval email logic into User.post_register_action/1
[akkoma] / lib / pleroma / web / twitter_api / twitter_api.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Web.TwitterAPI.TwitterAPI do
6 import Pleroma.Web.Gettext
7
8 alias Pleroma.Emails.Mailer
9 alias Pleroma.Emails.UserEmail
10 alias Pleroma.Repo
11 alias Pleroma.User
12 alias Pleroma.UserInviteToken
13
14 def register_user(params, opts \\ []) do
15 params =
16 params
17 |> Map.take([:email, :token, :password])
18 |> Map.put(:bio, params |> Map.get(:bio, "") |> User.parse_bio())
19 |> Map.put(:nickname, params[:username])
20 |> Map.put(:name, Map.get(params, :fullname, params[:username]))
21 |> Map.put(:password_confirmation, params[:password])
22 |> Map.put(:registration_reason, params[:reason])
23
24 if Pleroma.Config.get([:instance, :registrations_open]) do
25 create_user(params, opts)
26 else
27 create_user_with_invite(params, opts)
28 end
29 end
30
31 defp create_user_with_invite(params, opts) do
32 with %{token: token} when is_binary(token) <- params,
33 %UserInviteToken{} = invite <- Repo.get_by(UserInviteToken, %{token: token}),
34 true <- UserInviteToken.valid_invite?(invite) do
35 UserInviteToken.update_usage!(invite)
36 create_user(params, opts)
37 else
38 nil -> {:error, "Invalid token"}
39 _ -> {:error, "Expired token"}
40 end
41 end
42
43 defp create_user(params, opts) do
44 changeset = User.register_changeset(%User{}, params, opts)
45
46 case User.register(changeset) do
47 {:ok, user} ->
48 {:ok, user}
49
50 {:error, changeset} ->
51 errors =
52 changeset
53 |> Ecto.Changeset.traverse_errors(fn {msg, _opts} -> msg end)
54 |> Jason.encode!()
55
56 {:error, errors}
57 end
58 end
59
60 def password_reset(nickname_or_email) do
61 with true <- is_binary(nickname_or_email),
62 %User{local: true, email: email, deactivated: false} = user when is_binary(email) <-
63 User.get_by_nickname_or_email(nickname_or_email),
64 {:ok, token_record} <- Pleroma.PasswordResetToken.create_token(user) do
65 user
66 |> UserEmail.password_reset_email(token_record.token)
67 |> Mailer.deliver_async()
68
69 {:ok, :enqueued}
70 else
71 _ ->
72 {:ok, :noop}
73 end
74 end
75
76 def validate_captcha(app, params) do
77 if app.trusted || not Pleroma.Captcha.enabled?() do
78 :ok
79 else
80 do_validate_captcha(params)
81 end
82 end
83
84 defp do_validate_captcha(params) do
85 with :ok <- validate_captcha_presence(params),
86 :ok <-
87 Pleroma.Captcha.validate(
88 params[:captcha_token],
89 params[:captcha_solution],
90 params[:captcha_answer_data]
91 ) do
92 :ok
93 else
94 {:error, :captcha_error} ->
95 captcha_error(dgettext("errors", "CAPTCHA Error"))
96
97 {:error, :invalid} ->
98 captcha_error(dgettext("errors", "Invalid CAPTCHA"))
99
100 {:error, :kocaptcha_service_unavailable} ->
101 captcha_error(dgettext("errors", "Kocaptcha service unavailable"))
102
103 {:error, :expired} ->
104 captcha_error(dgettext("errors", "CAPTCHA expired"))
105
106 {:error, :already_used} ->
107 captcha_error(dgettext("errors", "CAPTCHA already used"))
108
109 {:error, :invalid_answer_data} ->
110 captcha_error(dgettext("errors", "Invalid answer data"))
111
112 {:error, error} ->
113 captcha_error(error)
114 end
115 end
116
117 defp validate_captcha_presence(params) do
118 [:captcha_solution, :captcha_token, :captcha_answer_data]
119 |> Enum.find_value(:ok, fn key ->
120 unless is_binary(params[key]) do
121 error = dgettext("errors", "Invalid CAPTCHA (Missing parameter: %{name})", name: key)
122 {:error, error}
123 end
124 end)
125 end
126
127 # For some reason FE expects error message to be a serialized JSON
128 defp captcha_error(error), do: {:error, Jason.encode!(%{captcha: [error]})}
129 end