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
27 |> Map.put(:bio, User.parse_bio(params[:bio] || ""))
28 |> Map.put(:name, params.fullname)
29 |> Map.put(:password_confirmation, params[:confirm])
31 case validate_captcha(params) do
33 if Pleroma.Config.get([:instance, :registrations_open]) do
34 create_user(params, opts)
36 create_user_with_invite(params, opts)
40 # I have no idea how this error handling works
41 {:error, %{error: Jason.encode!(%{captcha: [error]})}}
45 defp validate_captcha(params) do
46 if params[:trusted_app] || not Pleroma.Config.get([Pleroma.Captcha, :enabled]) do
49 Pleroma.Captcha.validate(
51 params.captcha_solution,
52 params.captcha_answer_data
57 defp create_user_with_invite(params, opts) do
58 with %{token: token} when is_binary(token) <- params,
59 %UserInviteToken{} = invite <- Repo.get_by(UserInviteToken, %{token: token}),
60 true <- UserInviteToken.valid_invite?(invite) do
61 UserInviteToken.update_usage!(invite)
62 create_user(params, opts)
64 nil -> {:error, "Invalid token"}
65 _ -> {:error, "Expired token"}
69 defp create_user(params, opts) do
70 changeset = User.register_changeset(%User{}, params, opts)
72 case User.register(changeset) do
76 {:error, changeset} ->
78 Ecto.Changeset.traverse_errors(changeset, fn {msg, _opts} -> msg end)
81 {:error, %{error: errors}}
85 def password_reset(nickname_or_email) do
86 with true <- is_binary(nickname_or_email),
87 %User{local: true, email: email} = user when not is_nil(email) <-
88 User.get_by_nickname_or_email(nickname_or_email),
89 {:ok, token_record} <- Pleroma.PasswordResetToken.create_token(user) do
91 |> UserEmail.password_reset_email(token_record.token)
92 |> Mailer.deliver_async()
97 {:error, "bad user identifier"}
99 %User{local: true, email: nil} ->
102 %User{local: false} ->
103 {:error, "remote user"}
106 {:error, "unknown user"}