1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 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
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])
24 if Pleroma.Config.get([:instance, :registrations_open]) do
25 create_user(params, opts)
27 create_user_with_invite(params, opts)
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)
38 nil -> {:error, "Invalid token"}
39 _ -> {:error, "Expired token"}
43 defp create_user(params, opts) do
44 changeset = User.register_changeset(%User{}, params, opts)
46 case User.register(changeset) do
48 maybe_notify_admins(user)
51 {:error, changeset} ->
54 |> Ecto.Changeset.traverse_errors(fn {msg, _opts} -> msg end)
61 defp maybe_notify_admins(%User{} = account) do
62 if Pleroma.Config.get([:instance, :account_approval_required]) do
64 |> Enum.filter(fn user -> not is_nil(user.email) end)
65 |> Enum.each(fn superuser ->
67 |> Pleroma.Emails.AdminEmail.new_unapproved_registration(account)
68 |> Pleroma.Emails.Mailer.deliver_async()
73 def password_reset(nickname_or_email) do
74 with true <- is_binary(nickname_or_email),
75 %User{local: true, email: email, deactivated: false} = user when is_binary(email) <-
76 User.get_by_nickname_or_email(nickname_or_email),
77 {:ok, token_record} <- Pleroma.PasswordResetToken.create_token(user) do
79 |> UserEmail.password_reset_email(token_record.token)
80 |> Mailer.deliver_async()
89 def validate_captcha(app, params) do
90 if app.trusted || not Pleroma.Captcha.enabled?() do
93 do_validate_captcha(params)
97 defp do_validate_captcha(params) do
98 with :ok <- validate_captcha_presence(params),
100 Pleroma.Captcha.validate(
101 params[:captcha_token],
102 params[:captcha_solution],
103 params[:captcha_answer_data]
107 {:error, :captcha_error} ->
108 captcha_error(dgettext("errors", "CAPTCHA Error"))
110 {:error, :invalid} ->
111 captcha_error(dgettext("errors", "Invalid CAPTCHA"))
113 {:error, :kocaptcha_service_unavailable} ->
114 captcha_error(dgettext("errors", "Kocaptcha service unavailable"))
116 {:error, :expired} ->
117 captcha_error(dgettext("errors", "CAPTCHA expired"))
119 {:error, :already_used} ->
120 captcha_error(dgettext("errors", "CAPTCHA already used"))
122 {:error, :invalid_answer_data} ->
123 captcha_error(dgettext("errors", "Invalid answer data"))
130 defp validate_captcha_presence(params) do
131 [:captcha_solution, :captcha_token, :captcha_answer_data]
132 |> Enum.find_value(:ok, fn key ->
133 unless is_binary(params[key]) do
134 error = dgettext("errors", "Invalid CAPTCHA (Missing parameter: %{name})", name: key)
140 # For some reason FE expects error message to be a serialized JSON
141 defp captcha_error(error), do: {:error, Jason.encode!(%{captcha: [error]})}