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.TwitterAPI.TwitterAPI do
6 import Pleroma.Web.Gettext
8 alias Pleroma.Emails.Mailer
9 alias Pleroma.Emails.UserEmail
12 alias Pleroma.UserInviteToken
14 def register_user(params, opts \\ []) do
15 fallback_language = Gettext.get_locale()
19 |> Map.take([:email, :token, :password])
20 |> Map.put(:bio, params |> Map.get(:bio, "") |> User.parse_bio())
21 |> Map.put(:nickname, params[:username])
22 |> Map.put(:name, Map.get(params, :fullname, params[:username]))
23 |> Map.put(:password_confirmation, params[:password])
24 |> Map.put(:registration_reason, params[:reason])
25 |> Map.put(:birthday, params[:birthday])
28 Pleroma.Web.Gettext.normalize_locale(params[:language]) || fallback_language
31 if Pleroma.Config.get([:instance, :registrations_open]) do
32 create_user(params, opts)
34 create_user_with_invite(params, opts)
38 defp create_user_with_invite(params, opts) do
39 with %{token: token} when is_binary(token) <- params,
40 %UserInviteToken{} = invite <- Repo.get_by(UserInviteToken, %{token: token}),
41 true <- UserInviteToken.valid_invite?(invite) do
42 UserInviteToken.update_usage!(invite)
43 create_user(params, opts)
45 nil -> {:error, "Invalid token"}
46 _ -> {:error, "Expired token"}
50 defp create_user(params, opts) do
51 changeset = User.register_changeset(%User{}, params, opts)
53 case User.register(changeset) do
57 {:error, changeset} ->
60 |> Ecto.Changeset.traverse_errors(fn {msg, _opts} -> msg end)
67 def password_reset(nickname_or_email) do
68 with true <- is_binary(nickname_or_email),
69 %User{local: true, email: email, is_active: true} = user when is_binary(email) <-
70 User.get_by_nickname_or_email(nickname_or_email),
71 {:ok, token_record} <- Pleroma.PasswordResetToken.create_token(user) do
73 |> UserEmail.password_reset_email(token_record.token)
74 |> Mailer.deliver_async()
83 def validate_captcha(app, params) do
84 if app.trusted || not Pleroma.Captcha.enabled?() do
87 do_validate_captcha(params)
91 defp do_validate_captcha(params) do
92 with :ok <- validate_captcha_presence(params),
94 Pleroma.Captcha.validate(
95 params[:captcha_token],
96 params[:captcha_solution],
97 params[:captcha_answer_data]
101 {:error, :captcha_error} ->
102 captcha_error(dgettext("errors", "CAPTCHA Error"))
104 {:error, :invalid} ->
105 captcha_error(dgettext("errors", "Invalid CAPTCHA"))
107 {:error, :kocaptcha_service_unavailable} ->
108 captcha_error(dgettext("errors", "Kocaptcha service unavailable"))
110 {:error, :expired} ->
111 captcha_error(dgettext("errors", "CAPTCHA expired"))
113 {:error, :already_used} ->
114 captcha_error(dgettext("errors", "CAPTCHA already used"))
116 {:error, :invalid_answer_data} ->
117 captcha_error(dgettext("errors", "Invalid answer data"))
124 defp validate_captcha_presence(params) do
125 [:captcha_solution, :captcha_token, :captcha_answer_data]
126 |> Enum.find_value(:ok, fn key ->
127 unless is_binary(params[key]) do
128 error = dgettext("errors", "Invalid CAPTCHA (Missing parameter: %{name})", name: key)
134 # For some reason FE expects error message to be a serialized JSON
135 defp captcha_error(error), do: {:error, Jason.encode!(%{captcha: [error]})}