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 alias Pleroma.Emails.Mailer
7 alias Pleroma.Emails.UserEmail
10 alias Pleroma.UserInviteToken
12 require Pleroma.Constants
14 def register_user(params, opts \\ []) do
26 |> Map.put(:bio, User.parse_bio(params[:bio] || ""))
27 |> Map.put(:name, params.fullname)
28 |> Map.put(:password_confirmation, params[:confirm])
30 case validate_captcha(params) do
32 if Pleroma.Config.get([:instance, :registrations_open]) do
33 create_user(params, opts)
35 create_user_with_invite(params, opts)
39 # I have no idea how this error handling works
40 {:error, %{error: Jason.encode!(%{captcha: [error]})}}
44 defp validate_captcha(params) do
45 if Pleroma.Config.get([Pleroma.Captcha, :enabled]) do
46 Pleroma.Captcha.validate(
48 params.captcha_solution,
49 params.captcha_answer_data
56 defp create_user_with_invite(params, opts) do
57 with %{token: token} when is_binary(token) <- params,
58 %UserInviteToken{} = invite <- Repo.get_by(UserInviteToken, %{token: token}),
59 true <- UserInviteToken.valid_invite?(invite) do
60 UserInviteToken.update_usage!(invite)
61 create_user(params, opts)
63 nil -> {:error, "Invalid token"}
64 _ -> {:error, "Expired token"}
68 defp create_user(params, opts) do
69 changeset = User.register_changeset(%User{}, params, opts)
71 case User.register(changeset) do
75 {:error, changeset} ->
77 Ecto.Changeset.traverse_errors(changeset, fn {msg, _opts} -> msg end)
80 {:error, %{error: errors}}
84 def password_reset(nickname_or_email) do
85 with true <- is_binary(nickname_or_email),
86 %User{local: true, email: email} = user when not is_nil(email) <-
87 User.get_by_nickname_or_email(nickname_or_email),
88 {:ok, token_record} <- Pleroma.PasswordResetToken.create_token(user) do
90 |> UserEmail.password_reset_email(token_record.token)
91 |> Mailer.deliver_async()
96 {:error, "bad user identifier"}
98 %User{local: true, email: nil} ->
101 %User{local: false} ->
102 {:error, "remote user"}
105 {:error, "unknown user"}