Add accepts_newsletter
field to User, set during registration or account updates
This commit is contained in:
parent
b221d77a6d
commit
a0f2cba0d8
9 changed files with 89 additions and 7 deletions
|
@ -148,6 +148,7 @@ defmodule Pleroma.User do
|
||||||
field(:accepts_chat_messages, :boolean, default: nil)
|
field(:accepts_chat_messages, :boolean, default: nil)
|
||||||
field(:last_active_at, :naive_datetime)
|
field(:last_active_at, :naive_datetime)
|
||||||
field(:disclose_client, :boolean, default: true)
|
field(:disclose_client, :boolean, default: true)
|
||||||
|
field(:accepts_newsletter, :boolean, default: false)
|
||||||
|
|
||||||
embeds_one(
|
embeds_one(
|
||||||
:notification_settings,
|
:notification_settings,
|
||||||
|
@ -515,7 +516,8 @@ def update_changeset(struct, params \\ %{}) do
|
||||||
:is_discoverable,
|
:is_discoverable,
|
||||||
:actor_type,
|
:actor_type,
|
||||||
:accepts_chat_messages,
|
:accepts_chat_messages,
|
||||||
:disclose_client
|
:disclose_client,
|
||||||
|
:accepts_newsletter
|
||||||
]
|
]
|
||||||
)
|
)
|
||||||
|> unique_constraint(:nickname)
|
|> unique_constraint(:nickname)
|
||||||
|
@ -678,7 +680,8 @@ def register_changeset_ldap(struct, params = %{password: password})
|
||||||
:name,
|
:name,
|
||||||
:nickname,
|
:nickname,
|
||||||
:email,
|
:email,
|
||||||
:accepts_chat_messages
|
:accepts_chat_messages,
|
||||||
|
:accepts_newsletter
|
||||||
])
|
])
|
||||||
|> validate_required([:name, :nickname])
|
|> validate_required([:name, :nickname])
|
||||||
|> unique_constraint(:nickname)
|
|> unique_constraint(:nickname)
|
||||||
|
@ -722,7 +725,8 @@ def register_changeset(struct, params \\ %{}, opts \\ []) do
|
||||||
:password_confirmation,
|
:password_confirmation,
|
||||||
:emoji,
|
:emoji,
|
||||||
:accepts_chat_messages,
|
:accepts_chat_messages,
|
||||||
:registration_reason
|
:registration_reason,
|
||||||
|
:accepts_newsletter
|
||||||
])
|
])
|
||||||
|> validate_required([:name, :nickname, :password, :password_confirmation])
|
|> validate_required([:name, :nickname, :password, :password_confirmation])
|
||||||
|> validate_confirmation(:password)
|
|> validate_confirmation(:password)
|
||||||
|
@ -1709,7 +1713,8 @@ def purge_user_changeset(user) do
|
||||||
fields: [],
|
fields: [],
|
||||||
raw_fields: [],
|
raw_fields: [],
|
||||||
is_discoverable: false,
|
is_discoverable: false,
|
||||||
also_known_as: []
|
also_known_as: [],
|
||||||
|
accepts_newsletter: false
|
||||||
})
|
})
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -458,6 +458,11 @@ defp create_request do
|
||||||
type: :string,
|
type: :string,
|
||||||
nullable: true,
|
nullable: true,
|
||||||
description: "Invite token required when the registrations aren't public"
|
description: "Invite token required when the registrations aren't public"
|
||||||
|
},
|
||||||
|
accepts_newsletter: %Schema{
|
||||||
|
allOf: [BooleanLike],
|
||||||
|
description:
|
||||||
|
"Whether the user opts-in to receiving news and marketing updates from site admins. These should be presented to the user in order to allow them to consent before setting this parameter to TRUE."
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
example: %{
|
example: %{
|
||||||
|
@ -635,7 +640,12 @@ defp update_credentials_request do
|
||||||
description:
|
description:
|
||||||
"Discovery (listing, indexing) of this account by external services (search bots etc.) is allowed."
|
"Discovery (listing, indexing) of this account by external services (search bots etc.) is allowed."
|
||||||
},
|
},
|
||||||
actor_type: ActorType
|
actor_type: ActorType,
|
||||||
|
accepts_newsletter: %Schema{
|
||||||
|
allOf: [BooleanLike],
|
||||||
|
description:
|
||||||
|
"Whether the user opts-in to receiving news and marketing updates from site admins."
|
||||||
|
}
|
||||||
},
|
},
|
||||||
example: %{
|
example: %{
|
||||||
bot: false,
|
bot: false,
|
||||||
|
|
|
@ -185,7 +185,8 @@ def update_credentials(%{assigns: %{user: user}, body_params: params} = conn, _p
|
||||||
:skip_thread_containment,
|
:skip_thread_containment,
|
||||||
:allow_following_move,
|
:allow_following_move,
|
||||||
:also_known_as,
|
:also_known_as,
|
||||||
:accepts_chat_messages
|
:accepts_chat_messages,
|
||||||
|
:accepts_newsletter
|
||||||
]
|
]
|
||||||
|> Enum.reduce(%{}, fn key, acc ->
|
|> Enum.reduce(%{}, fn key, acc ->
|
||||||
Maps.put_if_present(acc, key, params[key], &{:ok, truthy_param?(&1)})
|
Maps.put_if_present(acc, key, params[key], &{:ok, truthy_param?(&1)})
|
||||||
|
|
|
@ -14,7 +14,7 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPI do
|
||||||
def register_user(params, opts \\ []) do
|
def register_user(params, opts \\ []) do
|
||||||
params =
|
params =
|
||||||
params
|
params
|
||||||
|> Map.take([:email, :token, :password])
|
|> Map.take([:email, :token, :password, :accepts_newsletter])
|
||||||
|> Map.put(:bio, params |> Map.get(:bio, "") |> User.parse_bio())
|
|> Map.put(:bio, params |> Map.get(:bio, "") |> User.parse_bio())
|
||||||
|> Map.put(:nickname, params[:username])
|
|> Map.put(:nickname, params[:username])
|
||||||
|> Map.put(:name, Map.get(params, :fullname, params[:username]))
|
|> Map.put(:name, Map.get(params, :fullname, params[:username]))
|
||||||
|
|
|
@ -0,0 +1,9 @@
|
||||||
|
defmodule Pleroma.Repo.Migrations.AddNewsletterFieldToUsers do
|
||||||
|
use Ecto.Migration
|
||||||
|
|
||||||
|
def change do
|
||||||
|
alter table(:users) do
|
||||||
|
add(:accepts_newsletter, :boolean, default: false)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
|
@ -663,6 +663,16 @@ test "it creates a confirmed user" do
|
||||||
|
|
||||||
assert user.is_confirmed
|
assert user.is_confirmed
|
||||||
end
|
end
|
||||||
|
|
||||||
|
test "it sets 'accepts_newsletter'" do
|
||||||
|
params = Map.put_new(@full_user_data, :accepts_newsletter, true)
|
||||||
|
changeset = User.register_changeset(%User{}, params)
|
||||||
|
assert changeset.valid?
|
||||||
|
|
||||||
|
{:ok, user} = Repo.insert(changeset)
|
||||||
|
|
||||||
|
assert user.accepts_newsletter
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
describe "user registration, with :account_activation_required" do
|
describe "user registration, with :account_activation_required" do
|
||||||
|
@ -737,6 +747,17 @@ test "it restricts length of registration reason" do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
describe "update_changeset/2" do
|
||||||
|
test "it sets :accepts_newsletter" do
|
||||||
|
changeset =
|
||||||
|
%User{accepts_newsletter: false}
|
||||||
|
|> User.update_changeset(%{accepts_newsletter: true})
|
||||||
|
|
||||||
|
assert changeset.valid?
|
||||||
|
assert %User{accepts_newsletter: true} = Ecto.Changeset.apply_changes(changeset)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
describe "get_or_fetch/1" do
|
describe "get_or_fetch/1" do
|
||||||
test "gets an existing user by nickname" do
|
test "gets an existing user by nickname" do
|
||||||
user = insert(:user)
|
user = insert(:user)
|
||||||
|
|
|
@ -1125,6 +1125,7 @@ test "registers and logs in without :account_activation_required / :account_appr
|
||||||
assert user
|
assert user
|
||||||
assert user.is_confirmed
|
assert user.is_confirmed
|
||||||
assert user.is_approved
|
assert user.is_approved
|
||||||
|
refute user.accepts_newsletter
|
||||||
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
|
||||||
|
@ -1356,6 +1357,20 @@ test "allow registration with an empty email", %{conn: conn, valid_params: valid
|
||||||
assert json_response_and_validate_schema(res, 200)
|
assert json_response_and_validate_schema(res, 200)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
test "registration with accepts_newsletter", %{conn: conn, valid_params: valid_params} do
|
||||||
|
app_token = insert(:oauth_token, user: nil)
|
||||||
|
conn = put_req_header(conn, "authorization", "Bearer " <> app_token.token)
|
||||||
|
|
||||||
|
res =
|
||||||
|
conn
|
||||||
|
|> put_req_header("content-type", "application/json")
|
||||||
|
|> Map.put(:remote_ip, {127, 0, 0, 9})
|
||||||
|
|> post("/api/v1/accounts", Map.put(valid_params, :accepts_newsletter, true))
|
||||||
|
|
||||||
|
assert json_response_and_validate_schema(res, 200)
|
||||||
|
assert %User{accepts_newsletter: true} = Repo.get_by(User, email: "lain@example.org")
|
||||||
|
end
|
||||||
|
|
||||||
test "returns forbidden if token is invalid", %{conn: conn, valid_params: valid_params} do
|
test "returns forbidden if token is invalid", %{conn: conn, valid_params: valid_params} do
|
||||||
res =
|
res =
|
||||||
conn
|
conn
|
||||||
|
|
|
@ -113,6 +113,13 @@ test "updates the user's chat acceptance status", %{conn: conn} do
|
||||||
assert user_data["pleroma"]["accepts_chat_messages"] == false
|
assert user_data["pleroma"]["accepts_chat_messages"] == false
|
||||||
end
|
end
|
||||||
|
|
||||||
|
test "updates the user's newsletter preference", %{user: user, conn: conn} do
|
||||||
|
conn = patch(conn, "/api/v1/accounts/update_credentials", %{accepts_newsletter: "true"})
|
||||||
|
|
||||||
|
assert json_response_and_validate_schema(conn, 200)
|
||||||
|
assert %User{accepts_newsletter: true} = User.get_by_id(user.id)
|
||||||
|
end
|
||||||
|
|
||||||
test "updates the user's allow_following_move", %{user: user, conn: conn} do
|
test "updates the user's allow_following_move", %{user: user, conn: conn} do
|
||||||
assert user.allow_following_move == true
|
assert user.allow_following_move == true
|
||||||
|
|
||||||
|
|
|
@ -146,6 +146,20 @@ test "it registers a new user and parses mentions in the bio" do
|
||||||
assert user2.bio == expected_text
|
assert user2.bio == expected_text
|
||||||
end
|
end
|
||||||
|
|
||||||
|
test "it registers a new user with accepts_newsletter." do
|
||||||
|
data = %{
|
||||||
|
:username => "lain",
|
||||||
|
:email => "lain@wired.jp",
|
||||||
|
:fullname => "lain iwakura",
|
||||||
|
:password => "bear",
|
||||||
|
:confirm => "bear",
|
||||||
|
:accepts_newsletter => true
|
||||||
|
}
|
||||||
|
|
||||||
|
{:ok, user} = TwitterAPI.register_user(data)
|
||||||
|
assert user.accepts_newsletter
|
||||||
|
end
|
||||||
|
|
||||||
describe "register with one time token" do
|
describe "register with one time token" do
|
||||||
setup do: clear_config([:instance, :registrations_open], false)
|
setup do: clear_config([:instance, :registrations_open], false)
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue