Merge remote-tracking branch 'upstream/develop' into restrict-origin
[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 import Pleroma.Web.Gettext
7
8 alias Pleroma.Emails.Mailer
9 alias Pleroma.Emails.UserEmail
10 alias Pleroma.Repo
11 alias Pleroma.User
12 alias Pleroma.UserInviteToken
13
14 def register_user(params, opts \\ []) do
15 params =
16 params
17 |> Map.take([:email, :token, :password])
18 |> Map.put(:bio, params |> Map.get(:bio, "") |> User.parse_bio())
19 |> Map.put(:nickname, params[:username])
20 |> Map.put(:name, Map.get(params, :fullname, params[:username]))
21 |> Map.put(:password_confirmation, params[:password])
22 |> Map.put(:registration_reason, params[:reason])
23
24 if Pleroma.Config.get([:instance, :registrations_open]) do
25 create_user(params, opts)
26 else
27 create_user_with_invite(params, opts)
28 end
29 end
30
31 defp create_user_with_invite(params, opts) do
32 with %{token: token} when is_binary(token) <- params,
33 %UserInviteToken{} = invite <- Repo.get_by(UserInviteToken, %{token: token}),
34 true <- UserInviteToken.valid_invite?(invite) do
35 UserInviteToken.update_usage!(invite)
36 create_user(params, opts)
37 else
38 nil -> {:error, "Invalid token"}
39 _ -> {:error, "Expired token"}
40 end
41 end
42
43 defp create_user(params, opts) do
44 changeset = User.register_changeset(%User{}, params, opts)
45
46 case User.register(changeset) do
47 {:ok, user} ->
48 maybe_notify_admins(user)
49 {:ok, user}
50
51 {:error, changeset} ->
52 errors =
53 changeset
54 |> Ecto.Changeset.traverse_errors(fn {msg, _opts} -> msg end)
55 |> Jason.encode!()
56
57 {:error, errors}
58 end
59 end
60
61 defp maybe_notify_admins(%User{} = account) do
62 if Pleroma.Config.get([:instance, :account_approval_required]) do
63 User.all_superusers()
64 |> Enum.filter(fn user -> not is_nil(user.email) end)
65 |> Enum.each(fn superuser ->
66 superuser
67 |> Pleroma.Emails.AdminEmail.new_unapproved_registration(account)
68 |> Pleroma.Emails.Mailer.deliver_async()
69 end)
70 end
71 end
72
73 def password_reset(nickname_or_email) do
74 with true <- is_binary(nickname_or_email),
75 %User{local: true, email: email, deactivated: false} = user when is_binary(email) <-
76 User.get_by_nickname_or_email(nickname_or_email),
77 {:ok, token_record} <- Pleroma.PasswordResetToken.create_token(user) do
78 user
79 |> UserEmail.password_reset_email(token_record.token)
80 |> Mailer.deliver_async()
81
82 {:ok, :enqueued}
83 else
84 _ ->
85 {:ok, :noop}
86 end
87 end
88
89 def validate_captcha(app, params) do
90 if app.trusted || not Pleroma.Captcha.enabled?() do
91 :ok
92 else
93 do_validate_captcha(params)
94 end
95 end
96
97 defp do_validate_captcha(params) do
98 with :ok <- validate_captcha_presence(params),
99 :ok <-
100 Pleroma.Captcha.validate(
101 params[:captcha_token],
102 params[:captcha_solution],
103 params[:captcha_answer_data]
104 ) do
105 :ok
106 else
107 {:error, :captcha_error} ->
108 captcha_error(dgettext("errors", "CAPTCHA Error"))
109
110 {:error, :invalid} ->
111 captcha_error(dgettext("errors", "Invalid CAPTCHA"))
112
113 {:error, :kocaptcha_service_unavailable} ->
114 captcha_error(dgettext("errors", "Kocaptcha service unavailable"))
115
116 {:error, :expired} ->
117 captcha_error(dgettext("errors", "CAPTCHA expired"))
118
119 {:error, :already_used} ->
120 captcha_error(dgettext("errors", "CAPTCHA already used"))
121
122 {:error, :invalid_answer_data} ->
123 captcha_error(dgettext("errors", "Invalid answer data"))
124
125 {:error, error} ->
126 captcha_error(error)
127 end
128 end
129
130 defp validate_captcha_presence(params) do
131 [:captcha_solution, :captcha_token, :captcha_answer_data]
132 |> Enum.find_value(:ok, fn key ->
133 unless is_binary(params[key]) do
134 error = dgettext("errors", "Invalid CAPTCHA (Missing parameter: %{name})", name: key)
135 {:error, error}
136 end
137 end)
138 end
139
140 # For some reason FE expects error message to be a serialized JSON
141 defp captcha_error(error), do: {:error, Jason.encode!(%{captcha: [error]})}
142 end