Do not fail when user has no email

This commit is contained in:
Egor Kislitsyn 2020-02-27 17:27:49 +04:00
parent fda6f35a46
commit cb60a9c42f
No known key found for this signature in database
GPG key ID: 1B49CB15B71E7805
8 changed files with 81 additions and 9 deletions

View file

@ -1,5 +1,5 @@
# Pleroma: A lightweight social networking server
# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only
defmodule Pleroma.Web.ActivityPub.ActivityPub do
@ -536,7 +536,9 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do
{:ok, activity} <- insert(flag_data, local),
{:ok, stripped_activity} <- strip_report_status_data(activity),
:ok <- maybe_federate(stripped_activity) do
Enum.each(User.all_superusers(), fn superuser ->
User.all_superusers()
|> Enum.filter(fn user -> not is_nil(user.email) end)
|> Enum.each(fn superuser ->
superuser
|> Pleroma.Emails.AdminEmail.report(actor, account, statuses, content)
|> Pleroma.Emails.Mailer.deliver_async()

View file

@ -1,5 +1,5 @@
# Pleroma: A lightweight social networking server
# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only
defmodule Pleroma.Web.TwitterAPI.TwitterAPI do
@ -99,7 +99,8 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPI do
def password_reset(nickname_or_email) do
with true <- is_binary(nickname_or_email),
%User{local: true} = user <- User.get_by_nickname_or_email(nickname_or_email),
%User{local: true, email: email} = user when not is_nil(email) <-
User.get_by_nickname_or_email(nickname_or_email),
{:ok, token_record} <- Pleroma.PasswordResetToken.create_token(user) do
user
|> UserEmail.password_reset_email(token_record.token)
@ -110,6 +111,9 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPI do
false ->
{:error, "bad user identifier"}
%User{local: true, email: nil} ->
{:ok, :noop}
%User{local: false} ->
{:error, "remote user"}

View file

@ -1,5 +1,5 @@
# Pleroma: A lightweight social networking server
# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only
defmodule Pleroma.Workers.Cron.DigestEmailsWorker do
@ -31,6 +31,7 @@ defmodule Pleroma.Workers.Cron.DigestEmailsWorker do
from(u in inactive_users_query,
where: fragment(~s(? ->'digest' @> 'true'), u.email_notifications),
where: not is_nil(u.email),
where: u.last_digest_emailed_at < datetime_add(^now, ^negative_interval, "day"),
select: u
)

View file

@ -51,6 +51,7 @@ defmodule Pleroma.Workers.Cron.NewUsersDigestWorker do
if users_and_statuses != [] do
%{is_admin: true}
|> User.Query.build()
|> where([u], not is_nil(u.email))
|> Repo.all()
|> Enum.map(&Pleroma.Emails.NewUsersDigestEmail.new_users(&1, users_and_statuses))
|> Enum.each(&Pleroma.Emails.Mailer.deliver/1)

View file

@ -1,5 +1,5 @@
# Pleroma: A lightweight social networking server
# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only
defmodule Pleroma.Web.MastodonAPI.AuthControllerTest do
@ -85,6 +85,37 @@ defmodule Pleroma.Web.MastodonAPI.AuthControllerTest do
end
end
describe "POST /auth/password, with nickname" do
test "it returns 204", %{conn: conn} do
user = insert(:user)
assert conn
|> post("/auth/password?nickname=#{user.nickname}")
|> json_response(:no_content)
ObanHelpers.perform_all()
token_record = Repo.get_by(Pleroma.PasswordResetToken, user_id: user.id)
email = Pleroma.Emails.UserEmail.password_reset_email(user, token_record.token)
notify_email = Config.get([:instance, :notify_email])
instance_name = Config.get([:instance, :name])
assert_email_sent(
from: {instance_name, notify_email},
to: {user.name, user.email},
html_body: email.html_body
)
end
test "it doesn't fail when a user has no email", %{conn: conn} do
user = insert(:user, %{email: nil})
assert conn
|> post("/auth/password?nickname=#{user.nickname}")
|> json_response(:no_content)
end
end
describe "POST /auth/password, with invalid parameters" do
setup do
user = insert(:user)

View file

@ -1,5 +1,5 @@
# Pleroma: A lightweight social networking server
# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only
defmodule Pleroma.Web.MastodonAPI.ReportControllerTest do
@ -75,4 +75,13 @@ defmodule Pleroma.Web.MastodonAPI.ReportControllerTest do
assert json_response(conn, 400) == %{"error" => "Account not found"}
end
test "doesn't fail if an admin has no email", %{conn: conn, target_user: target_user} do
insert(:user, %{is_admin: true, email: nil})
assert %{"action_taken" => false, "id" => _} =
conn
|> post("/api/v1/reports", %{"account_id" => target_user.id})
|> json_response(200)
end
end

View file

@ -1,5 +1,5 @@
# Pleroma: A lightweight social networking server
# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only
defmodule Pleroma.Workers.Cron.DigestEmailsWorkerTest do
@ -13,7 +13,7 @@ defmodule Pleroma.Workers.Cron.DigestEmailsWorkerTest do
clear_config([:email_notifications, :digest])
test "it sends digest emails" do
setup do
Pleroma.Config.put([:email_notifications, :digest], %{
active: true,
inactivity_threshold: 7,
@ -31,6 +31,10 @@ defmodule Pleroma.Workers.Cron.DigestEmailsWorkerTest do
{:ok, _} = User.switch_email_notifications(user2, "digest", true)
CommonAPI.post(user, %{"status" => "hey @#{user2.nickname}!"})
{:ok, user2: user2}
end
test "it sends digest emails", %{user2: user2} do
Pleroma.Workers.Cron.DigestEmailsWorker.perform(:opts, :pid)
# Performing job(s) enqueued at previous step
ObanHelpers.perform_all()
@ -39,4 +43,12 @@ defmodule Pleroma.Workers.Cron.DigestEmailsWorkerTest do
assert email.to == [{user2.name, user2.email}]
assert email.subject == "Your digest from #{Pleroma.Config.get(:instance)[:name]}"
end
test "it doesn't fail when a user has no email", %{user2: user2} do
{:ok, _} = user2 |> Ecto.Changeset.change(%{email: nil}) |> Pleroma.Repo.update()
Pleroma.Workers.Cron.DigestEmailsWorker.perform(:opts, :pid)
# Performing job(s) enqueued at previous step
ObanHelpers.perform_all()
end
end

View file

@ -29,4 +29,16 @@ defmodule Pleroma.Workers.Cron.NewUsersDigestWorkerTest do
assert email.html_body =~ user2.nickname
assert email.html_body =~ "cofe"
end
test "it doesn't fail when admin has no email" do
yesterday = NaiveDateTime.utc_now() |> Timex.shift(days: -1)
insert(:user, %{is_admin: true, email: nil})
insert(:user, %{inserted_at: yesterday})
user = insert(:user, %{inserted_at: yesterday})
CommonAPI.post(user, %{"status" => "cofe"})
NewUsersDigestWorker.perform(nil, nil)
ObanHelpers.perform_all()
end
end