Merge branch 'feature/mastofe-admin-scope' into 'develop'
[akkoma] / lib / pleroma / web / twitter_api / twitter_api.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Web.TwitterAPI.TwitterAPI do
6 alias Pleroma.Emails.Mailer
7 alias Pleroma.Emails.UserEmail
8 alias Pleroma.Repo
9 alias Pleroma.User
10 alias Pleroma.UserInviteToken
11
12 require Pleroma.Constants
13
14 def register_user(params, opts \\ []) do
15 token = params["token"]
16
17 params = %{
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"]
27 }
28
29 captcha_enabled = Pleroma.Config.get([Pleroma.Captcha, :enabled])
30 # true if captcha is disabled or enabled and valid, false otherwise
31 captcha_ok =
32 if not captcha_enabled do
33 :ok
34 else
35 Pleroma.Captcha.validate(
36 params[:captcha_token],
37 params[:captcha_solution],
38 params[:captcha_answer_data]
39 )
40 end
41
42 # Captcha invalid
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]})}}
47 else
48 registration_process(
49 params,
50 %{
51 registrations_open: Pleroma.Config.get([:instance, :registrations_open]),
52 token: token
53 },
54 opts
55 )
56 end
57 end
58
59 defp registration_process(params, %{registrations_open: true}, opts) do
60 create_user(params, opts)
61 end
62
63 defp registration_process(params, %{token: token}, opts) do
64 invite =
65 unless is_nil(token) do
66 Repo.get_by(UserInviteToken, %{token: token})
67 end
68
69 valid_invite? = invite && UserInviteToken.valid_invite?(invite)
70
71 case invite do
72 nil ->
73 {:error, "Invalid token"}
74
75 invite when valid_invite? ->
76 UserInviteToken.update_usage!(invite)
77 create_user(params, opts)
78
79 _ ->
80 {:error, "Expired token"}
81 end
82 end
83
84 defp create_user(params, opts) do
85 changeset = User.register_changeset(%User{}, params, opts)
86
87 case User.register(changeset) do
88 {:ok, user} ->
89 {:ok, user}
90
91 {:error, changeset} ->
92 errors =
93 Ecto.Changeset.traverse_errors(changeset, fn {msg, _opts} -> msg end)
94 |> Jason.encode!()
95
96 {:error, %{error: errors}}
97 end
98 end
99
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
105 user
106 |> UserEmail.password_reset_email(token_record.token)
107 |> Mailer.deliver_async()
108
109 {:ok, :enqueued}
110 else
111 false ->
112 {:error, "bad user identifier"}
113
114 %User{local: true, email: nil} ->
115 {:ok, :noop}
116
117 %User{local: false} ->
118 {:error, "remote user"}
119
120 nil ->
121 {:error, "unknown user"}
122 end
123 end
124 end