Change user.approval_pending field to user.is_approved

This commit is contained in:
Mark Felder 2020-10-16 21:44:25 +00:00
parent cf367fdbd5
commit f7e59c28ed
10 changed files with 74 additions and 54 deletions

View file

@ -112,7 +112,7 @@ defmodule Pleroma.User do
field(:is_locked, :boolean, default: false) field(:is_locked, :boolean, default: false)
field(:confirmation_pending, :boolean, default: false) field(:confirmation_pending, :boolean, default: false)
field(:password_reset_pending, :boolean, default: false) field(:password_reset_pending, :boolean, default: false)
field(:approval_pending, :boolean, default: false) field(:is_approved, :boolean, default: true)
field(:registration_reason, :string, default: nil) field(:registration_reason, :string, default: nil)
field(:confirmation_token, :string, default: nil) field(:confirmation_token, :string, default: nil)
field(:default_scope, :string, default: "public") field(:default_scope, :string, default: "public")
@ -288,7 +288,7 @@ defmodule Pleroma.User do
@spec account_status(User.t()) :: account_status() @spec account_status(User.t()) :: account_status()
def account_status(%User{deactivated: true}), do: :deactivated def account_status(%User{deactivated: true}), do: :deactivated
def account_status(%User{password_reset_pending: true}), do: :password_reset_pending def account_status(%User{password_reset_pending: true}), do: :password_reset_pending
def account_status(%User{local: true, approval_pending: true}), do: :approval_pending def account_status(%User{local: true, is_approved: false}), do: :approval_pending
def account_status(%User{local: true, confirmation_pending: true}) do def account_status(%User{local: true, confirmation_pending: true}) do
if Config.get([:instance, :account_activation_required]) do if Config.get([:instance, :account_activation_required]) do
@ -711,16 +711,16 @@ defmodule Pleroma.User do
opts[:need_confirmation] opts[:need_confirmation]
end end
need_approval? = approved? =
if is_nil(opts[:need_approval]) do if is_nil(opts[:approved]) do
Config.get([:instance, :account_approval_required]) !Config.get([:instance, :account_approval_required])
else else
opts[:need_approval] opts[:approved]
end end
struct struct
|> confirmation_changeset(need_confirmation: need_confirmation?) |> confirmation_changeset(need_confirmation: need_confirmation?)
|> approval_changeset(need_approval: need_approval?) |> approval_changeset(set_approval: approved?)
|> cast(params, [ |> cast(params, [
:bio, :bio,
:raw_bio, :raw_bio,
@ -814,14 +814,14 @@ defmodule Pleroma.User do
end end
end end
def post_register_action(%User{approval_pending: true} = user) do def post_register_action(%User{is_approved: false} = user) do
with {:ok, _} <- send_user_approval_email(user), with {:ok, _} <- send_user_approval_email(user),
{:ok, _} <- send_admin_approval_emails(user) do {:ok, _} <- send_admin_approval_emails(user) do
{:ok, user} {:ok, user}
end end
end end
def post_register_action(%User{approval_pending: false, confirmation_pending: false} = user) do def post_register_action(%User{is_approved: true, confirmation_pending: false} = user) do
with {:ok, user} <- autofollow_users(user), with {:ok, user} <- autofollow_users(user),
{:ok, _} <- autofollowing_users(user), {:ok, _} <- autofollowing_users(user),
{:ok, user} <- set_cache(user), {:ok, user} <- set_cache(user),
@ -1624,8 +1624,8 @@ defmodule Pleroma.User do
end) end)
end end
def approve(%User{approval_pending: true} = user) do def approve(%User{is_approved: false} = user) do
with chg <- change(user, approval_pending: false), with chg <- change(user, is_approved: true),
{:ok, user} <- update_and_set_cache(chg) do {:ok, user} <- update_and_set_cache(chg) do
post_register_action(user) post_register_action(user)
{:ok, user} {:ok, user}
@ -1684,7 +1684,7 @@ defmodule Pleroma.User do
is_locked: false, is_locked: false,
confirmation_pending: false, confirmation_pending: false,
password_reset_pending: false, password_reset_pending: false,
approval_pending: false, is_approved: true,
registration_reason: nil, registration_reason: nil,
confirmation_token: nil, confirmation_token: nil,
domain_blocks: [], domain_blocks: [],
@ -2327,9 +2327,9 @@ defmodule Pleroma.User do
end end
@spec approval_changeset(User.t(), keyword()) :: Changeset.t() @spec approval_changeset(User.t(), keyword()) :: Changeset.t()
def approval_changeset(user, need_approval: need_approval?) do def approval_changeset(user, set_approval: approved?) do
params = if need_approval?, do: %{approval_pending: true}, else: %{approval_pending: false} params = if approved?, do: %{is_approved: true}, else: %{is_approved: false}
cast(user, params, [:approval_pending]) cast(user, params, [:is_approved])
end end
def add_pinnned_activity(user, %Pleroma.Activity{id: id}) do def add_pinnned_activity(user, %Pleroma.Activity{id: id}) do

View file

@ -138,7 +138,7 @@ defmodule Pleroma.User.Query do
defp compose_query({:active, _}, query) do defp compose_query({:active, _}, query) do
User.restrict_deactivated(query) User.restrict_deactivated(query)
|> where([u], u.approval_pending == false) |> where([u], u.is_approved == true)
end end
defp compose_query({:legacy_active, _}, query) do defp compose_query({:legacy_active, _}, query) do
@ -159,7 +159,7 @@ defmodule Pleroma.User.Query do
end end
defp compose_query({:need_approval, _}, query) do defp compose_query({:need_approval, _}, query) do
where(query, [u], u.approval_pending) where(query, [u], u.is_approved == false)
end end
defp compose_query({:unconfirmed, _}, query) do defp compose_query({:unconfirmed, _}, query) do

View file

@ -78,7 +78,7 @@ defmodule Pleroma.Web.AdminAPI.AccountView do
"roles" => User.roles(user), "roles" => User.roles(user),
"tags" => user.tags || [], "tags" => user.tags || [],
"confirmation_pending" => user.confirmation_pending, "confirmation_pending" => user.confirmation_pending,
"approval_pending" => user.approval_pending, "is_approved" => user.is_approved,
"url" => user.uri || user.ap_id, "url" => user.uri || user.ap_id,
"registration_reason" => user.registration_reason, "registration_reason" => user.registration_reason,
"actor_type" => user.actor_type "actor_type" => user.actor_type

View file

@ -0,0 +1,20 @@
# Pleroma: A lightweight social networking server
# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only
defmodule Pleroma.Repo.Migrations.RefactorApprovalPendingUserField do
use Ecto.Migration
def up do
# Flip the values before we change the meaning of the column
execute("UPDATE users SET approval_pending = NOT approval_pending;")
execute("ALTER TABLE users RENAME COLUMN approval_pending TO is_approved;")
execute("ALTER TABLE users ALTER COLUMN is_approved SET DEFAULT true;")
end
def down do
execute("UPDATE users SET is_approved = NOT is_approved;")
execute("ALTER TABLE users RENAME COLUMN is_approved TO approval_pending;")
execute("ALTER TABLE users ALTER COLUMN approval_pending SET DEFAULT false;")
end
end

View file

@ -694,7 +694,7 @@ defmodule Pleroma.UserTest do
{:ok, user} = Repo.insert(changeset) {:ok, user} = Repo.insert(changeset)
assert user.approval_pending refute user.is_approved
assert user.registration_reason == "I'm a cool guy :)" assert user.registration_reason == "I'm a cool guy :)"
end end
@ -1388,17 +1388,17 @@ defmodule Pleroma.UserTest do
describe "approve" do describe "approve" do
test "approves a user" do test "approves a user" do
user = insert(:user, approval_pending: true) user = insert(:user, is_approved: false)
assert true == user.approval_pending refute user.is_approved
{:ok, user} = User.approve(user) {:ok, user} = User.approve(user)
assert false == user.approval_pending assert user.is_approved
end end
test "approves a list of users" do test "approves a list of users" do
unapproved_users = [ unapproved_users = [
insert(:user, approval_pending: true), insert(:user, is_approved: false),
insert(:user, approval_pending: true), insert(:user, is_approved: false),
insert(:user, approval_pending: true) insert(:user, is_approved: false)
] ]
{:ok, users} = User.approve(unapproved_users) {:ok, users} = User.approve(unapproved_users)
@ -1406,7 +1406,7 @@ defmodule Pleroma.UserTest do
assert Enum.count(users) == 3 assert Enum.count(users) == 3
Enum.each(users, fn user -> Enum.each(users, fn user ->
assert false == user.approval_pending assert user.is_approved
end) end)
end end
@ -1414,7 +1414,7 @@ defmodule Pleroma.UserTest do
clear_config([:welcome, :email, :enabled], true) clear_config([:welcome, :email, :enabled], true)
clear_config([:welcome, :email, :sender], "tester@test.me") clear_config([:welcome, :email, :sender], "tester@test.me")
user = insert(:user, approval_pending: true) user = insert(:user, is_approved: false)
welcome_user = insert(:user, email: "tester@test.me") welcome_user = insert(:user, email: "tester@test.me")
instance_name = Pleroma.Config.get([:instance, :name]) instance_name = Pleroma.Config.get([:instance, :name])
@ -1432,7 +1432,7 @@ defmodule Pleroma.UserTest do
test "approving an approved user does not trigger post-register actions" do test "approving an approved user does not trigger post-register actions" do
clear_config([:welcome, :email, :enabled], true) clear_config([:welcome, :email, :enabled], true)
user = insert(:user, approval_pending: false) user = insert(:user, is_approved: true)
User.approve(user) User.approve(user)
ObanHelpers.perform_all() ObanHelpers.perform_all()
@ -1465,9 +1465,9 @@ defmodule Pleroma.UserTest do
end) end)
end end
test "sends approval emails when `approval_pending: true`" do test "sends approval emails when `is_approved: false`" do
admin = insert(:user, is_admin: true) admin = insert(:user, is_admin: true)
user = insert(:user, confirmation_pending: true, approval_pending: true) user = insert(:user, confirmation_pending: true, is_approved: false)
User.confirm(user) User.confirm(user)
ObanHelpers.perform_all() ObanHelpers.perform_all()
@ -1494,7 +1494,7 @@ defmodule Pleroma.UserTest do
end end
test "confirming a confirmed user does not trigger post-register actions" do test "confirming a confirmed user does not trigger post-register actions" do
user = insert(:user, confirmation_pending: false, approval_pending: true) user = insert(:user, confirmation_pending: false, is_approved: false)
User.confirm(user) User.confirm(user)
ObanHelpers.perform_all() ObanHelpers.perform_all()
@ -1591,7 +1591,7 @@ defmodule Pleroma.UserTest do
end end
test "delete/1 when approval is pending deletes the user" do test "delete/1 when approval is pending deletes the user" do
user = insert(:user, approval_pending: true) user = insert(:user, is_approved: false)
{:ok, job} = User.delete(user) {:ok, job} = User.delete(user)
{:ok, _} = ObanHelpers.perform(job) {:ok, _} = ObanHelpers.perform(job)
@ -1618,7 +1618,7 @@ defmodule Pleroma.UserTest do
is_locked: true, is_locked: true,
confirmation_pending: true, confirmation_pending: true,
password_reset_pending: true, password_reset_pending: true,
approval_pending: true, is_approved: false,
registration_reason: "ahhhhh", registration_reason: "ahhhhh",
confirmation_token: "qqqq", confirmation_token: "qqqq",
domain_blocks: ["lain.com"], domain_blocks: ["lain.com"],
@ -1660,7 +1660,7 @@ defmodule Pleroma.UserTest do
is_locked: false, is_locked: false,
confirmation_pending: false, confirmation_pending: false,
password_reset_pending: false, password_reset_pending: false,
approval_pending: false, is_approved: true,
registration_reason: nil, registration_reason: nil,
confirmation_token: nil, confirmation_token: nil,
domain_blocks: [], domain_blocks: [],
@ -1755,10 +1755,10 @@ defmodule Pleroma.UserTest do
end end
test "returns :approval_pending for unapproved user" do test "returns :approval_pending for unapproved user" do
user = insert(:user, local: true, approval_pending: true) user = insert(:user, local: true, is_approved: false)
assert User.account_status(user) == :approval_pending assert User.account_status(user) == :approval_pending
user = insert(:user, local: true, confirmation_pending: true, approval_pending: true) user = insert(:user, local: true, confirmation_pending: true, is_approved: false)
assert User.account_status(user) == :approval_pending assert User.account_status(user) == :approval_pending
end end
end end

View file

@ -429,7 +429,7 @@ defmodule Pleroma.Web.AdminAPI.UserControllerTest do
describe "GET /api/pleroma/admin/users" do describe "GET /api/pleroma/admin/users" do
test "renders users array for the first page", %{conn: conn, admin: admin} do test "renders users array for the first page", %{conn: conn, admin: admin} do
user = insert(:user, local: false, tags: ["foo", "bar"]) user = insert(:user, local: false, tags: ["foo", "bar"])
user2 = insert(:user, approval_pending: true, registration_reason: "I'm a chill dude") user2 = insert(:user, is_approved: false, registration_reason: "I'm a chill dude")
conn = get(conn, "/api/pleroma/admin/users?page=1") conn = get(conn, "/api/pleroma/admin/users?page=1")
@ -444,7 +444,7 @@ defmodule Pleroma.Web.AdminAPI.UserControllerTest do
user2, user2,
%{ %{
"local" => true, "local" => true,
"approval_pending" => true, "is_approved" => false,
"registration_reason" => "I'm a chill dude", "registration_reason" => "I'm a chill dude",
"actor_type" => "Person" "actor_type" => "Person"
} }
@ -638,7 +638,7 @@ defmodule Pleroma.Web.AdminAPI.UserControllerTest do
sad_user = insert(:user, nickname: "sadboy", confirmation_pending: true) sad_user = insert(:user, nickname: "sadboy", confirmation_pending: true)
old_user = insert(:user, nickname: "oldboy", confirmation_pending: true) old_user = insert(:user, nickname: "oldboy", confirmation_pending: true)
insert(:user, nickname: "happyboy", approval_pending: false) insert(:user, nickname: "happyboy", is_approved: true)
insert(:user, confirmation_pending: false) insert(:user, confirmation_pending: false)
result = result =
@ -650,7 +650,7 @@ defmodule Pleroma.Web.AdminAPI.UserControllerTest do
Enum.map([old_user, sad_user], fn user -> Enum.map([old_user, sad_user], fn user ->
user_response(user, %{ user_response(user, %{
"confirmation_pending" => true, "confirmation_pending" => true,
"approval_pending" => false "is_approved" => true
}) })
end) end)
|> Enum.sort_by(& &1["nickname"]) |> Enum.sort_by(& &1["nickname"])
@ -662,18 +662,18 @@ defmodule Pleroma.Web.AdminAPI.UserControllerTest do
user = user =
insert(:user, insert(:user,
nickname: "sadboy", nickname: "sadboy",
approval_pending: true, is_approved: false,
registration_reason: "Plz let me in!" registration_reason: "Plz let me in!"
) )
insert(:user, nickname: "happyboy", approval_pending: false) insert(:user, nickname: "happyboy", is_approved: true)
conn = get(conn, "/api/pleroma/admin/users?filters=need_approval") conn = get(conn, "/api/pleroma/admin/users?filters=need_approval")
users = [ users = [
user_response( user_response(
user, user,
%{"approval_pending" => true, "registration_reason" => "Plz let me in!"} %{"is_approved" => false, "registration_reason" => "Plz let me in!"}
) )
] ]
@ -816,8 +816,8 @@ defmodule Pleroma.Web.AdminAPI.UserControllerTest do
end end
test "`active` filters out users pending approval", %{token: token} do test "`active` filters out users pending approval", %{token: token} do
insert(:user, approval_pending: true) insert(:user, is_approved: false)
%{id: user_id} = insert(:user, approval_pending: false) %{id: user_id} = insert(:user, is_approved: true)
%{id: admin_id} = token.user %{id: admin_id} = token.user
conn = conn =
@ -913,8 +913,8 @@ defmodule Pleroma.Web.AdminAPI.UserControllerTest do
end end
test "PATCH /api/pleroma/admin/users/approve", %{admin: admin, conn: conn} do test "PATCH /api/pleroma/admin/users/approve", %{admin: admin, conn: conn} do
user_one = insert(:user, approval_pending: true) user_one = insert(:user, is_approved: false)
user_two = insert(:user, approval_pending: true) user_two = insert(:user, is_approved: false)
conn = conn =
patch( patch(
@ -924,7 +924,7 @@ defmodule Pleroma.Web.AdminAPI.UserControllerTest do
) )
response = json_response(conn, 200) response = json_response(conn, 200)
assert Enum.map(response["users"], & &1["approval_pending"]) == [false, false] assert Enum.map(response["users"], & &1["is_approved"]) == [true, true]
log_entry = Repo.one(ModerationLog) log_entry = Repo.one(ModerationLog)
@ -961,7 +961,7 @@ defmodule Pleroma.Web.AdminAPI.UserControllerTest do
"avatar" => User.avatar_url(user) |> MediaProxy.url(), "avatar" => User.avatar_url(user) |> MediaProxy.url(),
"display_name" => HTML.strip_tags(user.name || user.nickname), "display_name" => HTML.strip_tags(user.name || user.nickname),
"confirmation_pending" => false, "confirmation_pending" => false,
"approval_pending" => false, "is_approved" => true,
"url" => user.ap_id, "url" => user.ap_id,
"registration_reason" => nil, "registration_reason" => nil,
"actor_type" => "Person" "actor_type" => "Person"

View file

@ -182,7 +182,7 @@ defmodule Pleroma.Web.AdminAPI.SearchTest do
end end
test "it returns unapproved user" do test "it returns unapproved user" do
unapproved = insert(:user, approval_pending: true) unapproved = insert(:user, is_approved: false)
insert(:user) insert(:user)
insert(:user) insert(:user)

View file

@ -1028,7 +1028,7 @@ defmodule Pleroma.Web.MastodonAPI.AccountControllerTest do
assert user assert user
refute user.confirmation_pending refute user.confirmation_pending
refute user.approval_pending assert user.is_approved
end end
test "registers but does not log in with :account_activation_required", %{conn: conn} do test "registers but does not log in with :account_activation_required", %{conn: conn} do
@ -1150,7 +1150,7 @@ defmodule Pleroma.Web.MastodonAPI.AccountControllerTest do
user = Repo.get_by(User, email: "lain@example.org") user = Repo.get_by(User, email: "lain@example.org")
assert user.approval_pending refute user.is_approved
assert user.registration_reason == "I'm a cool dude, bro" assert user.registration_reason == "I'm a cool dude, bro"
end end

View file

@ -1041,7 +1041,7 @@ defmodule Pleroma.Web.OAuth.OAuthControllerTest do
user = user =
insert(:user, insert(:user,
password_hash: Pleroma.Password.Pbkdf2.hash_pwd_salt(password), password_hash: Pleroma.Password.Pbkdf2.hash_pwd_salt(password),
approval_pending: true is_approved: false
) )
refute Pleroma.User.account_status(user) == :active refute Pleroma.User.account_status(user) == :active

View file

@ -97,7 +97,7 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPITest do
{:ok, user} = TwitterAPI.register_user(data) {:ok, user} = TwitterAPI.register_user(data)
ObanHelpers.perform_all() ObanHelpers.perform_all()
assert user.approval_pending refute user.is_approved
user_email = Pleroma.Emails.UserEmail.approval_pending_email(user) user_email = Pleroma.Emails.UserEmail.approval_pending_email(user)
admin_email = Pleroma.Emails.AdminEmail.new_unapproved_registration(admin, user) admin_email = Pleroma.Emails.AdminEmail.new_unapproved_registration(admin, user)