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
15 token = params["token"]
18 nickname: params["nickname"],
19 name: params["fullname"],
20 bio: User.parse_bio(params["bio"]),
21 email: params["email"],
22 password: params["password"],
23 password_confirmation: params["confirm"],
24 captcha_solution: params["captcha_solution"],
25 captcha_token: params["captcha_token"],
26 captcha_answer_data: params["captcha_answer_data"]
29 captcha_enabled = Pleroma.Config.get([Pleroma.Captcha, :enabled])
30 # true if captcha is disabled or enabled and valid, false otherwise
32 if not captcha_enabled do
35 Pleroma.Captcha.validate(
36 params[:captcha_token],
37 params[:captcha_solution],
38 params[:captcha_answer_data]
43 if captcha_ok != :ok do
44 {:error, error} = captcha_ok
45 # I have no idea how this error handling works
46 {:error, %{error: Jason.encode!(%{captcha: [error]})}}
51 registrations_open: Pleroma.Config.get([:instance, :registrations_open]),
59 defp registration_process(params, %{registrations_open: true}, opts) do
60 create_user(params, opts)
63 defp registration_process(params, %{token: token}, opts) do
65 unless is_nil(token) do
66 Repo.get_by(UserInviteToken, %{token: token})
69 valid_invite? = invite && UserInviteToken.valid_invite?(invite)
73 {:error, "Invalid token"}
75 invite when valid_invite? ->
76 UserInviteToken.update_usage!(invite)
77 create_user(params, opts)
80 {:error, "Expired token"}
84 defp create_user(params, opts) do
85 changeset = User.register_changeset(%User{}, params, opts)
87 case User.register(changeset) do
91 {:error, changeset} ->
93 Ecto.Changeset.traverse_errors(changeset, fn {msg, _opts} -> msg end)
96 {:error, %{error: errors}}
100 def password_reset(nickname_or_email) do
101 with true <- is_binary(nickname_or_email),
102 %User{local: true, email: email} = user when not is_nil(email) <-
103 User.get_by_nickname_or_email(nickname_or_email),
104 {:ok, token_record} <- Pleroma.PasswordResetToken.create_token(user) do
106 |> UserEmail.password_reset_email(token_record.token)
107 |> Mailer.deliver_async()
112 {:error, "bad user identifier"}
114 %User{local: true, email: nil} ->
117 %User{local: false} ->
118 {:error, "remote user"}
121 {:error, "unknown user"}