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"]
16 trusted_app? = params["trusted_app"]
19 nickname: params["nickname"],
20 name: params["fullname"],
21 bio: User.parse_bio(params["bio"]),
22 email: params["email"],
23 password: params["password"],
24 password_confirmation: params["confirm"],
25 captcha_solution: params["captcha_solution"],
26 captcha_token: params["captcha_token"],
27 captcha_answer_data: params["captcha_answer_data"]
30 captcha_enabled = Pleroma.Config.get([Pleroma.Captcha, :enabled])
31 # true if captcha is disabled or enabled and valid, false otherwise
33 if trusted_app? || not captcha_enabled do
36 Pleroma.Captcha.validate(
37 params[:captcha_token],
38 params[:captcha_solution],
39 params[:captcha_answer_data]
44 if captcha_ok != :ok do
45 {:error, error} = captcha_ok
46 # I have no idea how this error handling works
47 {:error, %{error: Jason.encode!(%{captcha: [error]})}}
52 registrations_open: Pleroma.Config.get([:instance, :registrations_open]),
60 defp registration_process(params, %{registrations_open: true}, opts) do
61 create_user(params, opts)
64 defp registration_process(params, %{token: token}, opts) do
66 unless is_nil(token) do
67 Repo.get_by(UserInviteToken, %{token: token})
70 valid_invite? = invite && UserInviteToken.valid_invite?(invite)
74 {:error, "Invalid token"}
76 invite when valid_invite? ->
77 UserInviteToken.update_usage!(invite)
78 create_user(params, opts)
81 {:error, "Expired token"}
85 defp create_user(params, opts) do
86 changeset = User.register_changeset(%User{}, params, opts)
88 case User.register(changeset) do
92 {:error, changeset} ->
94 Ecto.Changeset.traverse_errors(changeset, fn {msg, _opts} -> msg end)
97 {:error, %{error: errors}}
101 def password_reset(nickname_or_email) do
102 with true <- is_binary(nickname_or_email),
103 %User{local: true, email: email} = user when not is_nil(email) <-
104 User.get_by_nickname_or_email(nickname_or_email),
105 {:ok, token_record} <- Pleroma.PasswordResetToken.create_token(user) do
107 |> UserEmail.password_reset_email(token_record.token)
108 |> Mailer.deliver_async()
113 {:error, "bad user identifier"}
115 %User{local: true, email: nil} ->
118 %User{local: false} ->
119 {:error, "remote user"}
122 {:error, "unknown user"}