From b108b0565076d677a90ff38ec8926e1f3153b7f6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?marcin=20miko=C5=82ajczak?= Date: Tue, 18 Jan 2022 14:57:48 +0100 Subject: [PATCH 01/11] Birth dates, birthday reminders API, allow instance admins to require minimum age MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: marcin mikołajczak --- config/config.exs | 4 ++- config/description.exs | 10 ++++++ lib/pleroma/user.ex | 35 +++++++++++++++++-- lib/pleroma/user/query.ex | 16 ++++++++- lib/pleroma/web/activity_pub/activity_pub.ex | 3 +- .../web/activity_pub/views/user_view.ex | 8 ++++- .../api_spec/operations/account_operation.ex | 25 +++++++++++-- .../operations/pleroma_account_operation.ex | 29 +++++++++++++++ lib/pleroma/web/api_spec/schemas/account.ex | 5 ++- .../controllers/account_controller.ex | 4 ++- .../web/mastodon_api/views/account_view.ex | 8 ++++- .../controllers/account_controller.ex | 19 ++++++++++ lib/pleroma/web/router.ex | 2 ++ lib/pleroma/web/twitter_api/twitter_api.ex | 1 + ...29220116183110_add_birth_date_to_users.exs | 10 ++++++ priv/static/schemas/litepub-0.1.jsonld | 3 +- 16 files changed, 168 insertions(+), 14 deletions(-) create mode 100644 priv/repo/migrations/29220116183110_add_birth_date_to_users.exs diff --git a/config/config.exs b/config/config.exs index 1385ce5de..ec2407ea2 100644 --- a/config/config.exs +++ b/config/config.exs @@ -259,7 +259,9 @@ password_reset_token_validity: 60 * 60 * 24, profile_directory: true, privileged_staff: false, - max_endorsed_users: 20 + max_endorsed_users: 20, + birth_date_required: false, + birth_date_min_age: 0 config :pleroma, :welcome, direct_message: [ diff --git a/config/description.exs b/config/description.exs index 644c60a63..9a5242820 100644 --- a/config/description.exs +++ b/config/description.exs @@ -957,6 +957,16 @@ type: :boolean, description: "Let moderators access sensitive data (e.g. updating user credentials, get password reset token, delete users, index and read private statuses and chats)" + }, + %{ + key: :birth_date_required, + type: :boolean, + description: "Require users to provide birth day." + }, + %{ + key: :birth_date_min_age, + type: :integer, + description: "Min age for users to create account. Only makes sense if birth date is required." } ] }, diff --git a/lib/pleroma/user.ex b/lib/pleroma/user.ex index 0a5dfccc9..ca9986e28 100644 --- a/lib/pleroma/user.ex +++ b/lib/pleroma/user.ex @@ -154,6 +154,9 @@ defmodule Pleroma.User do field(:pinned_objects, :map, default: %{}) field(:is_suggested, :boolean, default: false) field(:last_status_at, :naive_datetime) + field(:birth_date, :date) + field(:hide_birth_date, :boolean, default: false) + embeds_one( :notification_settings, @@ -470,7 +473,8 @@ def remote_user_changeset(struct \\ %User{local: false}, params) do :actor_type, :also_known_as, :accepts_chat_messages, - :pinned_objects + :pinned_objects, + :birth_date ] ) |> cast(params, [:name], empty_values: []) @@ -531,9 +535,11 @@ def update_changeset(struct, params \\ %{}) do :is_discoverable, :actor_type, :accepts_chat_messages, - :disclose_client + :disclose_client, + :birth_date ] ) + |> validate_min_age() |> unique_constraint(:nickname) |> validate_format(:nickname, local_nickname_regex()) |> validate_length(:bio, max: bio_limit) @@ -738,7 +744,8 @@ def register_changeset(struct, params \\ %{}, opts \\ []) do :password_confirmation, :emoji, :accepts_chat_messages, - :registration_reason + :registration_reason, + :birth_date ]) |> validate_required([:name, :nickname, :password, :password_confirmation]) |> validate_confirmation(:password) @@ -760,6 +767,8 @@ def register_changeset(struct, params \\ %{}, opts \\ []) do |> validate_length(:name, min: 1, max: name_limit) |> validate_length(:registration_reason, max: reason_limit) |> maybe_validate_required_email(opts[:external]) + |> maybe_validate_required_birth_date + |> validate_min_age() |> put_password_hash |> put_ap_id() |> unique_constraint(:ap_id) @@ -776,6 +785,26 @@ def maybe_validate_required_email(changeset, _) do end end + defp maybe_validate_required_birth_date(changeset) do + if Config.get([:instance, :birth_date_required]) do + validate_required(changeset, [:birth_date]) + else + changeset + end + end + + defp validate_min_age(changeset) do + changeset + |> validate_change(:birth_date, fn :birth_date, birth_date -> + valid? = + Date.utc_today() + |> Date.diff(birth_date) >= + Config.get([:instance, :birth_date_min_age]) + + if valid?, do: [], else: [birth_date: "Invalid birth date"] + end) + end + defp put_ap_id(changeset) do ap_id = ap_id(%User{nickname: get_field(changeset, :nickname)}) put_change(changeset, :ap_id, ap_id) diff --git a/lib/pleroma/user/query.ex b/lib/pleroma/user/query.ex index bf78cb32d..9e594013a 100644 --- a/lib/pleroma/user/query.ex +++ b/lib/pleroma/user/query.ex @@ -59,7 +59,9 @@ defmodule Pleroma.User.Query do order_by: term(), select: term(), limit: pos_integer(), - actor_types: [String.t()] + actor_types: [String.t()], + birth_day: pos_integer(), + birth_month: pos_integer() } | map() @@ -230,6 +232,18 @@ defp compose_query({:internal, false}, query) do |> where([u], not like(u.nickname, "internal.%")) end + defp compose_query({:birth_day, day}, query) do + query + |> where([u], not is_nil(u.birth_date)) + |> where([u], fragment("date_part('day', ?)", u.birth_date) == ^day) + end + + defp compose_query({:birth_month, month}, query) do + query + |> where([u], not is_nil(u.birth_date)) + |> where([u], fragment("date_part('month', ?)", u.birth_date) == ^month) + end + defp compose_query(_unsupported_param, query), do: query defp location_query(query, local) do diff --git a/lib/pleroma/web/activity_pub/activity_pub.ex b/lib/pleroma/web/activity_pub/activity_pub.ex index 756096952..837f4b82a 100644 --- a/lib/pleroma/web/activity_pub/activity_pub.ex +++ b/lib/pleroma/web/activity_pub/activity_pub.ex @@ -1523,7 +1523,8 @@ defp object_to_user_data(data) do inbox: data["inbox"], shared_inbox: shared_inbox, accepts_chat_messages: accepts_chat_messages, - pinned_objects: pinned_objects + pinned_objects: pinned_objects, + birth_date: data["vcard:bday"] } # nickname can be nil because of virtual actors diff --git a/lib/pleroma/web/activity_pub/views/user_view.ex b/lib/pleroma/web/activity_pub/views/user_view.ex index 344da19d3..942f5685a 100644 --- a/lib/pleroma/web/activity_pub/views/user_view.ex +++ b/lib/pleroma/web/activity_pub/views/user_view.ex @@ -92,6 +92,11 @@ def render("user.json", %{user: user}) do %{} end + birth_date = + if !user.hide_birth_date, + do: user.birth_date, + else: nil + %{ "id" => user.ap_id, "type" => user.actor_type, @@ -116,7 +121,8 @@ def render("user.json", %{user: user}) do # Note: key name is indeed "discoverable" (not an error) "discoverable" => user.is_discoverable, "capabilities" => capabilities, - "alsoKnownAs" => user.also_known_as + "alsoKnownAs" => user.also_known_as, + "vcard:bday" => birth_date } |> Map.merge(maybe_make_image(&User.avatar_url/2, "icon", user)) |> Map.merge(maybe_make_image(&User.banner_url/2, "image", user)) diff --git a/lib/pleroma/web/api_spec/operations/account_operation.ex b/lib/pleroma/web/api_spec/operations/account_operation.ex index 768d3c720..dc2019b67 100644 --- a/lib/pleroma/web/api_spec/operations/account_operation.ex +++ b/lib/pleroma/web/api_spec/operations/account_operation.ex @@ -543,7 +543,13 @@ defp create_request do type: :string, nullable: true, description: "Invite token required when the registrations aren't public" - } + }, + birth_date: %Schema{ + type: :string, + nullable: true, + description: "User's birth date", + format: :date + }, }, example: %{ "username" => "cofe", @@ -720,7 +726,18 @@ defp update_credentials_request do description: "Discovery (listing, indexing) of this account by external services (search bots etc.) is allowed." }, - actor_type: ActorType + actor_type: ActorType, + birth_date: %Schema{ + type: :string, + nullable: true, + description: "User's birth date", + format: :date + }, + hide_birth_date: %Schema{ + allOf: [BooleanLike], + nullable: true, + description: "User's birth date will be hidden" + } }, example: %{ bot: false, @@ -740,7 +757,9 @@ defp update_credentials_request do allow_following_move: false, also_known_as: ["https://foo.bar/users/foo"], discoverable: false, - actor_type: "Person" + actor_type: "Person", + hide_birth_date: true, + birth_date: "2001-02-12" } } end diff --git a/lib/pleroma/web/api_spec/operations/pleroma_account_operation.ex b/lib/pleroma/web/api_spec/operations/pleroma_account_operation.ex index ed0db173e..23201a4ba 100644 --- a/lib/pleroma/web/api_spec/operations/pleroma_account_operation.ex +++ b/lib/pleroma/web/api_spec/operations/pleroma_account_operation.ex @@ -4,6 +4,7 @@ defmodule Pleroma.Web.ApiSpec.PleromaAccountOperation do alias OpenApiSpex.Operation + alias OpenApiSpex.Schema alias Pleroma.Web.ApiSpec.AccountOperation alias Pleroma.Web.ApiSpec.Schemas.AccountRelationship alias Pleroma.Web.ApiSpec.Schemas.ApiError @@ -112,6 +113,34 @@ def unsubscribe_operation do } end + def birthdays_operation do + %Operation{ + tags: ["Retrieve account information"], + summary: "Birthday reminders", + description: "Birthday reminders about users you follow.", + operationId: "PleromaAPI.AccountController.birthdays", + parameters: [ + Operation.parameter( + :day, + :query, + %Schema{type: :integer}, + "Day of users' birthdays" + ), + Operation.parameter( + :month, + :query, + %Schema{type: :integer}, + "Month of users' birthdays" + ) + ], + security: [%{"oAuth" => ["read:accounts"]}], + responses: %{ + 200 => + Operation.response("Accounts", "application/json", AccountOperation.array_of_accounts()) + } + } + end + defp id_param do Operation.parameter(:id, :path, FlakeID, "Account ID", example: "9umDrYheeY451cQnEe", diff --git a/lib/pleroma/web/api_spec/schemas/account.ex b/lib/pleroma/web/api_spec/schemas/account.ex index 548e70544..66c900b76 100644 --- a/lib/pleroma/web/api_spec/schemas/account.ex +++ b/lib/pleroma/web/api_spec/schemas/account.ex @@ -47,12 +47,14 @@ defmodule Pleroma.Web.ApiSpec.Schemas.Account do description: "whether the user allows automatically follow moved following accounts" }, background_image: %Schema{type: :string, nullable: true, format: :uri}, + birth_date: %Schema{type: :string, format: :date}, chat_token: %Schema{type: :string}, is_confirmed: %Schema{ type: :boolean, description: "whether the user account is waiting on email confirmation to be activated" }, + hide_birth_date: %Schema{type: :boolean}, hide_favorites: %Schema{type: :boolean}, hide_followers_count: %Schema{ type: :boolean, @@ -202,7 +204,8 @@ defmodule Pleroma.Web.ApiSpec.Schemas.Account do }, "settings_store" => %{ "pleroma-fe" => %{} - } + }, + "birth_date" => "2001-02-12" }, "source" => %{ "fields" => [], diff --git a/lib/pleroma/web/mastodon_api/controllers/account_controller.ex b/lib/pleroma/web/mastodon_api/controllers/account_controller.ex index a90833bf0..64034fbef 100644 --- a/lib/pleroma/web/mastodon_api/controllers/account_controller.ex +++ b/lib/pleroma/web/mastodon_api/controllers/account_controller.ex @@ -191,7 +191,8 @@ def update_credentials(%{assigns: %{user: user}, body_params: params} = conn, _p :skip_thread_containment, :allow_following_move, :also_known_as, - :accepts_chat_messages + :accepts_chat_messages, + :hide_birth_date ] |> Enum.reduce(%{}, fn key, acc -> Maps.put_if_present(acc, key, params[key], &{:ok, Params.truthy_param?(&1)}) @@ -219,6 +220,7 @@ def update_credentials(%{assigns: %{user: user}, body_params: params} = conn, _p |> Maps.put_if_present(:is_locked, params[:locked]) # Note: param name is indeed :discoverable (not an error) |> Maps.put_if_present(:is_discoverable, params[:discoverable]) + |> Maps.put_if_present(:birth_date, params[:birth_date]) # What happens here: # diff --git a/lib/pleroma/web/mastodon_api/views/account_view.ex b/lib/pleroma/web/mastodon_api/views/account_view.ex index b964fdc54..f42d13aa6 100644 --- a/lib/pleroma/web/mastodon_api/views/account_view.ex +++ b/lib/pleroma/web/mastodon_api/views/account_view.ex @@ -249,6 +249,11 @@ defp do_render("show.json", %{user: user} = opts) do nil end + birth_date = + if !user.hide_birth_date or opts[:for] == user, + do: user.birth_date, + else: nil + %{ id: to_string(user.id), username: username_from_nickname(user.nickname), @@ -297,7 +302,8 @@ defp do_render("show.json", %{user: user} = opts) do skip_thread_containment: user.skip_thread_containment, background_image: image_url(user.background) |> MediaProxy.url(), accepts_chat_messages: user.accepts_chat_messages, - favicon: favicon + favicon: favicon, + birth_date: birth_date } } |> maybe_put_role(user, opts[:for]) diff --git a/lib/pleroma/web/pleroma_api/controllers/account_controller.ex b/lib/pleroma/web/pleroma_api/controllers/account_controller.ex index 66a8d1c1c..ca6c9c3a2 100644 --- a/lib/pleroma/web/pleroma_api/controllers/account_controller.ex +++ b/lib/pleroma/web/pleroma_api/controllers/account_controller.ex @@ -51,6 +51,11 @@ defmodule Pleroma.Web.PleromaAPI.AccountController do when action == :endorsements ) + plug( + OAuthScopesPlug, + %{scopes: ["read:accounts"]} when action == :birthdays + ) + plug(RateLimiter, [name: :account_confirmation_resend] when action == :confirmation_resend) plug( @@ -137,4 +142,18 @@ def unsubscribe(%{assigns: %{user: user, account: subscription_target}} = conn, {:error, message} -> json_response(conn, :forbidden, %{error: message}) end end + + @doc "GET /api/v1/pleroma/birthday_reminders" + def birthdays(%{assigns: %{user: %User{} = user}} = conn, %{day: day, month: month} = _params) do + birthdays = + User.Query.build(%{friends: user, deactivated: false, birth_day: day, birth_month: month}) + |> Pleroma.Repo.all() + + conn + |> render("index.json", + for: user, + users: birthdays, + as: :user + ) + end end diff --git a/lib/pleroma/web/router.ex b/lib/pleroma/web/router.ex index 67c1a3e5c..2d896fdd4 100644 --- a/lib/pleroma/web/router.ex +++ b/lib/pleroma/web/router.ex @@ -448,6 +448,8 @@ defmodule Pleroma.Web.Router do post("/accounts/:id/subscribe", AccountController, :subscribe) post("/accounts/:id/unsubscribe", AccountController, :unsubscribe) + + get("/birthday_reminders", AccountController, :birthdays) end post("/accounts/confirmation_resend", AccountController, :confirmation_resend) diff --git a/lib/pleroma/web/twitter_api/twitter_api.ex b/lib/pleroma/web/twitter_api/twitter_api.ex index 76ca82d20..91d10d260 100644 --- a/lib/pleroma/web/twitter_api/twitter_api.ex +++ b/lib/pleroma/web/twitter_api/twitter_api.ex @@ -20,6 +20,7 @@ def register_user(params, opts \\ []) do |> Map.put(:name, Map.get(params, :fullname, params[:username])) |> Map.put(:password_confirmation, params[:password]) |> Map.put(:registration_reason, params[:reason]) + |> Map.put(:birth_date, params[:birth_date]) if Pleroma.Config.get([:instance, :registrations_open]) do create_user(params, opts) diff --git a/priv/repo/migrations/29220116183110_add_birth_date_to_users.exs b/priv/repo/migrations/29220116183110_add_birth_date_to_users.exs new file mode 100644 index 000000000..f610eeb10 --- /dev/null +++ b/priv/repo/migrations/29220116183110_add_birth_date_to_users.exs @@ -0,0 +1,10 @@ +defmodule Pleroma.Repo.Migrations.AddBirthDateToUsers do + use Ecto.Migration + + def change do + alter table(:users) do + add_if_not_exists(:birth_date, :date) + add_if_not_exists(:hide_birth_date, :boolean, default: false, null: false) + end + end +end diff --git a/priv/static/schemas/litepub-0.1.jsonld b/priv/static/schemas/litepub-0.1.jsonld index e7722cf72..946099a6e 100644 --- a/priv/static/schemas/litepub-0.1.jsonld +++ b/priv/static/schemas/litepub-0.1.jsonld @@ -35,7 +35,8 @@ "alsoKnownAs": { "@id": "as:alsoKnownAs", "@type": "@id" - } + }, + "vcard": "http://www.w3.org/2006/vcard/ns#" } ] } From 397f67fef8658000be26fd3b9bd86f5968fcaf52 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?marcin=20miko=C5=82ajczak?= Date: Tue, 18 Jan 2022 18:02:01 +0100 Subject: [PATCH 02/11] Format code, expose instance configuration related to birth dates MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: marcin mikołajczak --- config/description.exs | 3 ++- lib/pleroma/user.ex | 3 +-- .../web/api_spec/operations/account_operation.ex | 2 +- lib/pleroma/web/api_spec/schemas/account.ex | 2 +- lib/pleroma/web/mastodon_api/views/instance_view.ex | 4 +++- .../pleroma_api/controllers/account_controller.ex | 12 ++++++------ .../web/mastodon_api/views/account_view_test.exs | 2 ++ 7 files changed, 16 insertions(+), 12 deletions(-) diff --git a/config/description.exs b/config/description.exs index 9a5242820..867112b8e 100644 --- a/config/description.exs +++ b/config/description.exs @@ -966,7 +966,8 @@ %{ key: :birth_date_min_age, type: :integer, - description: "Min age for users to create account. Only makes sense if birth date is required." + description: + "Min age for users to create account. Only makes sense if birth date is required." } ] }, diff --git a/lib/pleroma/user.ex b/lib/pleroma/user.ex index ca9986e28..cb7740292 100644 --- a/lib/pleroma/user.ex +++ b/lib/pleroma/user.ex @@ -157,7 +157,6 @@ defmodule Pleroma.User do field(:birth_date, :date) field(:hide_birth_date, :boolean, default: false) - embeds_one( :notification_settings, Pleroma.User.NotificationSetting, @@ -799,7 +798,7 @@ defp validate_min_age(changeset) do valid? = Date.utc_today() |> Date.diff(birth_date) >= - Config.get([:instance, :birth_date_min_age]) + Config.get([:instance, :birth_date_min_age]) if valid?, do: [], else: [birth_date: "Invalid birth date"] end) diff --git a/lib/pleroma/web/api_spec/operations/account_operation.ex b/lib/pleroma/web/api_spec/operations/account_operation.ex index dc2019b67..e0ef45027 100644 --- a/lib/pleroma/web/api_spec/operations/account_operation.ex +++ b/lib/pleroma/web/api_spec/operations/account_operation.ex @@ -549,7 +549,7 @@ defp create_request do nullable: true, description: "User's birth date", format: :date - }, + } }, example: %{ "username" => "cofe", diff --git a/lib/pleroma/web/api_spec/schemas/account.ex b/lib/pleroma/web/api_spec/schemas/account.ex index 66c900b76..a20964342 100644 --- a/lib/pleroma/web/api_spec/schemas/account.ex +++ b/lib/pleroma/web/api_spec/schemas/account.ex @@ -47,7 +47,7 @@ defmodule Pleroma.Web.ApiSpec.Schemas.Account do description: "whether the user allows automatically follow moved following accounts" }, background_image: %Schema{type: :string, nullable: true, format: :uri}, - birth_date: %Schema{type: :string, format: :date}, + birth_date: %Schema{type: :string, nullable: true, format: :date}, chat_token: %Schema{type: :string}, is_confirmed: %Schema{ type: :boolean, diff --git a/lib/pleroma/web/mastodon_api/views/instance_view.ex b/lib/pleroma/web/mastodon_api/views/instance_view.ex index 8e657ee0f..f7c76f06b 100644 --- a/lib/pleroma/web/mastodon_api/views/instance_view.ex +++ b/lib/pleroma/web/mastodon_api/views/instance_view.ex @@ -46,7 +46,9 @@ def render("show.json", _) do federation: federation(), fields_limits: fields_limits(), post_formats: Config.get([:instance, :allowed_post_formats]), - privileged_staff: Config.get([:instance, :privileged_staff]) + privileged_staff: Config.get([:instance, :privileged_staff]), + birth_date_required: Config.get([:instance, :birth_date_required]), + birth_date_min_age: Config.get([:instance, :birth_date_min_age]) }, stats: %{mau: Pleroma.User.active_user_count()}, vapid_public_key: Keyword.get(Pleroma.Web.Push.vapid_config(), :public_key) diff --git a/lib/pleroma/web/pleroma_api/controllers/account_controller.ex b/lib/pleroma/web/pleroma_api/controllers/account_controller.ex index ca6c9c3a2..4a833275e 100644 --- a/lib/pleroma/web/pleroma_api/controllers/account_controller.ex +++ b/lib/pleroma/web/pleroma_api/controllers/account_controller.ex @@ -149,11 +149,11 @@ def birthdays(%{assigns: %{user: %User{} = user}} = conn, %{day: day, month: mon User.Query.build(%{friends: user, deactivated: false, birth_day: day, birth_month: month}) |> Pleroma.Repo.all() - conn - |> render("index.json", - for: user, - users: birthdays, - as: :user - ) + conn + |> render("index.json", + for: user, + users: birthdays, + as: :user + ) end end diff --git a/test/pleroma/web/mastodon_api/views/account_view_test.exs b/test/pleroma/web/mastodon_api/views/account_view_test.exs index c23ffb966..da8761355 100644 --- a/test/pleroma/web/mastodon_api/views/account_view_test.exs +++ b/test/pleroma/web/mastodon_api/views/account_view_test.exs @@ -79,6 +79,7 @@ test "Represent a user account" do ap_id: user.ap_id, also_known_as: ["https://shitposter.zone/users/shp"], background_image: "https://example.com/images/asuka_hospital.png", + birth_date: nil, favicon: nil, is_confirmed: true, tags: [], @@ -181,6 +182,7 @@ test "Represent a Service(bot) account" do ap_id: user.ap_id, also_known_as: [], background_image: nil, + birth_date: nil, favicon: nil, is_confirmed: true, tags: [], From dfb28085356aea63c2b0c755d7861946315c3931 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?marcin=20miko=C5=82ajczak?= Date: Tue, 18 Jan 2022 19:29:21 +0100 Subject: [PATCH 03/11] Birth dates: Add tests MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: marcin mikołajczak --- lib/pleroma/user.ex | 12 ++++- lib/pleroma/user/query.ex | 2 + lib/pleroma/web/api_spec/schemas/account.ex | 2 +- .../web/mastodon_api/views/account_view.ex | 26 ++++++--- .../controllers/account_controller.ex | 2 +- test/pleroma/user_test.exs | 48 +++++++++++++++++ .../controllers/account_controller_test.exs | 54 +++++++++++++++++++ .../mastodon_api/update_credentials_test.exs | 20 +++++++ .../controllers/account_controller_test.exs | 53 ++++++++++++++++++ 9 files changed, 210 insertions(+), 9 deletions(-) diff --git a/lib/pleroma/user.ex b/lib/pleroma/user.ex index cb7740292..5655762ac 100644 --- a/lib/pleroma/user.ex +++ b/lib/pleroma/user.ex @@ -535,7 +535,8 @@ def update_changeset(struct, params \\ %{}) do :actor_type, :accepts_chat_messages, :disclose_client, - :birth_date + :birth_date, + :hide_birth_date ] ) |> validate_min_age() @@ -2583,4 +2584,13 @@ def update_last_status_at(user) do _ -> {:error, user} end end + + def get_friends_birthdays_query(%User{} = user, day, month) do + User.Query.build(%{ + friends: user, + deactivated: false, + birth_day: day, + birth_month: month + }) + end end diff --git a/lib/pleroma/user/query.ex b/lib/pleroma/user/query.ex index 9e594013a..ea1706c9e 100644 --- a/lib/pleroma/user/query.ex +++ b/lib/pleroma/user/query.ex @@ -234,12 +234,14 @@ defp compose_query({:internal, false}, query) do defp compose_query({:birth_day, day}, query) do query + |> where([u], u.hide_birth_date == false) |> where([u], not is_nil(u.birth_date)) |> where([u], fragment("date_part('day', ?)", u.birth_date) == ^day) end defp compose_query({:birth_month, month}, query) do query + |> where([u], u.hide_birth_date == false) |> where([u], not is_nil(u.birth_date)) |> where([u], fragment("date_part('month', ?)", u.birth_date) == ^month) end diff --git a/lib/pleroma/web/api_spec/schemas/account.ex b/lib/pleroma/web/api_spec/schemas/account.ex index a20964342..3796bccfd 100644 --- a/lib/pleroma/web/api_spec/schemas/account.ex +++ b/lib/pleroma/web/api_spec/schemas/account.ex @@ -54,7 +54,7 @@ defmodule Pleroma.Web.ApiSpec.Schemas.Account do description: "whether the user account is waiting on email confirmation to be activated" }, - hide_birth_date: %Schema{type: :boolean}, + hide_birth_date: %Schema{type: :boolean, nullable: true}, hide_favorites: %Schema{type: :boolean}, hide_followers_count: %Schema{ type: :boolean, diff --git a/lib/pleroma/web/mastodon_api/views/account_view.ex b/lib/pleroma/web/mastodon_api/views/account_view.ex index f42d13aa6..07b3c776f 100644 --- a/lib/pleroma/web/mastodon_api/views/account_view.ex +++ b/lib/pleroma/web/mastodon_api/views/account_view.ex @@ -249,11 +249,6 @@ defp do_render("show.json", %{user: user} = opts) do nil end - birth_date = - if !user.hide_birth_date or opts[:for] == user, - do: user.birth_date, - else: nil - %{ id: to_string(user.id), username: username_from_nickname(user.nickname), @@ -303,7 +298,8 @@ defp do_render("show.json", %{user: user} = opts) do background_image: image_url(user.background) |> MediaProxy.url(), accepts_chat_messages: user.accepts_chat_messages, favicon: favicon, - birth_date: birth_date + birth_date: user.birth_date, + hide_birth_date: user.hide_birth_date } } |> maybe_put_role(user, opts[:for]) @@ -317,6 +313,7 @@ defp do_render("show.json", %{user: user} = opts) do |> maybe_put_unread_conversation_count(user, opts[:for]) |> maybe_put_unread_notification_count(user, opts[:for]) |> maybe_put_email_address(user, opts[:for]) + |> maybe_hide_birth_date(user, opts[:for]) end defp username_from_nickname(string) when is_binary(string) do @@ -438,6 +435,23 @@ defp maybe_put_email_address(data, %User{id: user_id}, %User{id: user_id} = user defp maybe_put_email_address(data, _, _), do: data + defp maybe_hide_birth_date(data, %User{id: user_id}, %User{id: user_id}) do + data + end + + defp maybe_hide_birth_date(data, %User{hide_birth_date: true}, _) do + data + |> Kernel.pop_in([:pleroma, :birth_date]) + |> Kernel.pop_in([:pleroma, :hide_birth_date]) + |> elem(1) + end + + defp maybe_hide_birth_date(data, _, _) do + data + |> Kernel.pop_in([:pleroma, :hide_birth_date]) + |> elem(1) + end + defp image_url(%{"url" => [%{"href" => href} | _]}), do: href defp image_url(_), do: nil end diff --git a/lib/pleroma/web/pleroma_api/controllers/account_controller.ex b/lib/pleroma/web/pleroma_api/controllers/account_controller.ex index 4a833275e..20697fa46 100644 --- a/lib/pleroma/web/pleroma_api/controllers/account_controller.ex +++ b/lib/pleroma/web/pleroma_api/controllers/account_controller.ex @@ -146,7 +146,7 @@ def unsubscribe(%{assigns: %{user: user, account: subscription_target}} = conn, @doc "GET /api/v1/pleroma/birthday_reminders" def birthdays(%{assigns: %{user: %User{} = user}} = conn, %{day: day, month: month} = _params) do birthdays = - User.Query.build(%{friends: user, deactivated: false, birth_day: day, birth_month: month}) + User.get_friends_birthdays_query(user, day, month) |> Pleroma.Repo.all() conn diff --git a/test/pleroma/user_test.exs b/test/pleroma/user_test.exs index 0345a9290..263c2b274 100644 --- a/test/pleroma/user_test.exs +++ b/test/pleroma/user_test.exs @@ -755,6 +755,54 @@ test "it restricts length of registration reason" do end end + describe "user registration, with :birth_date_required and :birth_date_min_age" do + @full_user_data %{ + bio: "A guy", + name: "my name", + nickname: "nick", + password: "test", + password_confirmation: "test", + email: "email@example.com" + } + + setup do + clear_config([:instance, :birth_date_required], true) + clear_config([:instance, :birth_date_min_age], 18 * 365) + end + + test "it passes when correct birth date is provided" do + today = Date.utc_today() + birth_date = Date.add(today, -19 * 365) + + params = + @full_user_data + |> Map.put(:birth_date, birth_date) + + changeset = User.register_changeset(%User{}, params) + + assert changeset.valid? + end + + test "it fails when birth date is not provided" do + changeset = User.register_changeset(%User{}, @full_user_data) + + refute changeset.valid? + end + + test "it fails when provided invalid birth date" do + today = Date.utc_today() + birth_date = Date.add(today, -17 * 365) + + params = + @full_user_data + |> Map.put(:birth_date, birth_date) + + changeset = User.register_changeset(%User{}, params) + + refute changeset.valid? + end + end + describe "get_or_fetch/1" do test "gets an existing user by nickname" do user = insert(:user) diff --git a/test/pleroma/web/mastodon_api/controllers/account_controller_test.exs b/test/pleroma/web/mastodon_api/controllers/account_controller_test.exs index bba528d83..d8ebd98cc 100644 --- a/test/pleroma/web/mastodon_api/controllers/account_controller_test.exs +++ b/test/pleroma/web/mastodon_api/controllers/account_controller_test.exs @@ -1586,6 +1586,60 @@ test "returns an error if captcha is invalid", %{conn: conn} do end end + describe "create account with required birth date" do + setup %{conn: conn} do + clear_config([:instance, :birth_date_required], true) + clear_config([:instance, :birth_date_min_age], 18 * 365) + + app_token = insert(:oauth_token, user: nil) + + conn = + conn + |> put_req_header("authorization", "Bearer " <> app_token.token) + |> put_req_header("content-type", "multipart/form-data") + + [conn: conn] + end + + test "creates an account if provided valid birth date", %{conn: conn} do + birth_date = + Date.utc_today() + |> Date.add(-19 * 365) + |> Date.to_string() + + params = %{ + username: "mkljczk", + email: "mkljczk@example.org", + password: "dupa.8", + agreement: true, + birth_date: birth_date + } + + res = + conn + |> post("/api/v1/accounts", params) + + assert json_response_and_validate_schema(res, 200) + end + + test "returns an error if missing birth date", %{conn: conn} do + params = %{ + username: "mkljczk", + email: "mkljczk@example.org", + password: "dupa.8", + agreement: true + } + + res = + conn + |> post("/api/v1/accounts", params) + + assert json_response_and_validate_schema(res, 400) == %{ + "error" => "{\"birth_date\":[\"can't be blank\"]}" + } + end + end + describe "GET /api/v1/accounts/:id/lists - account_lists" do test "returns lists to which the account belongs" do %{user: user, conn: conn} = oauth_access(["read:lists"]) diff --git a/test/pleroma/web/mastodon_api/update_credentials_test.exs b/test/pleroma/web/mastodon_api/update_credentials_test.exs index 1d2027899..e89f597a9 100644 --- a/test/pleroma/web/mastodon_api/update_credentials_test.exs +++ b/test/pleroma/web/mastodon_api/update_credentials_test.exs @@ -370,6 +370,26 @@ test "update fields", %{conn: conn} do ] end + test "updates birth date", %{conn: conn, user: user} do + res = + patch(conn, "/api/v1/accounts/update_credentials", %{ + "birth_date" => "2001-02-12" + }) + + assert user_data = json_response_and_validate_schema(res, 200) + assert user_data["pleroma"]["birth_date"] == "2001-02-12" + end + + test "updates the user's hide_birth_date status", %{conn: conn} do + res = + patch(conn, "/api/v1/accounts/update_credentials", %{ + "hide_birth_date" => true + }) + + assert user_data = json_response_and_validate_schema(res, 200) + assert user_data["pleroma"]["hide_birth_date"] == true + end + test "emojis in fields labels", %{conn: conn} do fields = [ %{"name" => ":firefox:", "value" => "is best 2hu"}, diff --git a/test/pleroma/web/pleroma_api/controllers/account_controller_test.exs b/test/pleroma/web/pleroma_api/controllers/account_controller_test.exs index d9aa8ce55..5b74bb0b2 100644 --- a/test/pleroma/web/pleroma_api/controllers/account_controller_test.exs +++ b/test/pleroma/web/pleroma_api/controllers/account_controller_test.exs @@ -304,4 +304,57 @@ test "returns 404 error when specified user is not exist", %{conn: conn} do assert json_response_and_validate_schema(conn, 404) == %{"error" => "Record not found"} end end + + describe "birthday reminders" do + test "returns a list of friends having birthday on specified day" do + %{user: user, conn: conn} = oauth_access(["read:accounts"]) + + %{id: id1} = + user1 = + insert(:user, %{ + birth_date: "2001-02-12" + }) + + user2 = + insert(:user, %{ + birth_date: "2001-02-14" + }) + + user3 = insert(:user) + + CommonAPI.follow(user, user1) + CommonAPI.follow(user, user2) + CommonAPI.follow(user, user3) + + [%{"id" => ^id1}] = + conn + |> get("/api/v1/pleroma/birthday_reminders?day=12&month=2") + |> json_response_and_validate_schema(:ok) + end + + test "the list doesn't list friends with hidden birth date" do + %{user: user, conn: conn} = oauth_access(["read:accounts"]) + + user1 = + insert(:user, %{ + birth_date: "2001-02-12", + hide_birth_date: true + }) + + %{id: id2} = + user2 = + insert(:user, %{ + birth_date: "2001-02-12", + hide_birth_date: false + }) + + CommonAPI.follow(user, user1) + CommonAPI.follow(user, user2) + + [%{"id" => ^id2}] = + conn + |> get("/api/v1/pleroma/birthday_reminders?day=12&month=2") + |> json_response_and_validate_schema(:ok) + end + end end From c180f9276fe9c6d6e7c5d25165bf87606cd1c966 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?marcin=20miko=C5=82ajczak?= Date: Wed, 19 Jan 2022 17:19:28 +0100 Subject: [PATCH 04/11] check if remote bday is valid MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: marcin mikołajczak --- lib/pleroma/web/activity_pub/activity_pub.ex | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/lib/pleroma/web/activity_pub/activity_pub.ex b/lib/pleroma/web/activity_pub/activity_pub.ex index 837f4b82a..e1f94ba9c 100644 --- a/lib/pleroma/web/activity_pub/activity_pub.ex +++ b/lib/pleroma/web/activity_pub/activity_pub.ex @@ -1501,6 +1501,16 @@ defp object_to_user_data(data) do nil end + birth_date = + if data["vcard:bday"] do + case Date.from_iso8601(data["vcard:bday"]) do + {:ok, date} -> date + {:error, _} -> nil + end + else + nil + end + user_data = %{ ap_id: data["id"], uri: get_actor_url(data["url"]), @@ -1524,7 +1534,7 @@ defp object_to_user_data(data) do shared_inbox: shared_inbox, accepts_chat_messages: accepts_chat_messages, pinned_objects: pinned_objects, - birth_date: data["vcard:bday"] + birth_date: birth_date } # nickname can be nil because of virtual actors From 74cf0f0355fbfd21859e0ed374c263762279d89e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?marcin=20miko=C5=82ajczak?= Date: Wed, 19 Jan 2022 19:51:30 +0100 Subject: [PATCH 05/11] Update changelog MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: marcin mikołajczak --- CHANGELOG.md | 3 +++ lib/pleroma/web/mastodon_api/views/account_view.ex | 1 + 2 files changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 79b669782..9ddfa36d5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -25,6 +25,9 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). - Ability to log slow Ecto queries by configuring `:pleroma, :telemetry, :slow_queries_logging` - Added Phoenix LiveDashboard at `/phoenix/live_dashboard` - Added `/manifest.json` for progressive web apps. +- MastoAPI: Support for `birth_date` and `show_birth_date` field in `/api/v1/accounts/update_credentials`. +- Configuration: Add `birth_date_required` and `birth_date_min_age` settings to provide a way to require users to enter their birth date. +- PleromaAPI: Add `GET /api/v1/pleroma/birthday_reminders` API endpoint ### Fixed - Subscription(Bell) Notifications: Don't create from Pipeline Ingested replies diff --git a/lib/pleroma/web/mastodon_api/views/account_view.ex b/lib/pleroma/web/mastodon_api/views/account_view.ex index 07b3c776f..6c7b4f7c5 100644 --- a/lib/pleroma/web/mastodon_api/views/account_view.ex +++ b/lib/pleroma/web/mastodon_api/views/account_view.ex @@ -442,6 +442,7 @@ defp maybe_hide_birth_date(data, %User{id: user_id}, %User{id: user_id}) do defp maybe_hide_birth_date(data, %User{hide_birth_date: true}, _) do data |> Kernel.pop_in([:pleroma, :birth_date]) + |> elem(1) |> Kernel.pop_in([:pleroma, :hide_birth_date]) |> elem(1) end From 66e8c6f90fa0ca9ab01cc58c865344694548e4d6 Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Sat, 22 Jan 2022 13:21:55 -0600 Subject: [PATCH 06/11] Birthdays: birth_date --> birthday --- CHANGELOG.md | 4 +-- config/config.exs | 4 +-- config/description.exs | 4 +-- lib/pleroma/user.ex | 32 +++++++++---------- lib/pleroma/user/query.ex | 20 ++++++------ lib/pleroma/web/activity_pub/activity_pub.ex | 6 ++-- .../web/activity_pub/views/user_view.ex | 8 ++--- .../api_spec/operations/account_operation.ex | 16 +++++----- lib/pleroma/web/api_spec/schemas/account.ex | 6 ++-- .../controllers/account_controller.ex | 4 +-- .../web/mastodon_api/views/account_view.ex | 18 +++++------ .../web/mastodon_api/views/instance_view.ex | 4 +-- lib/pleroma/web/twitter_api/twitter_api.ex | 2 +- ...29220116183110_add_birth_date_to_users.exs | 4 +-- test/pleroma/user_test.exs | 14 ++++---- .../controllers/account_controller_test.exs | 10 +++--- .../mastodon_api/update_credentials_test.exs | 12 +++---- .../mastodon_api/views/account_view_test.exs | 4 +-- .../controllers/account_controller_test.exs | 12 +++---- 19 files changed, 92 insertions(+), 92 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9ddfa36d5..99941b9ee 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -25,8 +25,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). - Ability to log slow Ecto queries by configuring `:pleroma, :telemetry, :slow_queries_logging` - Added Phoenix LiveDashboard at `/phoenix/live_dashboard` - Added `/manifest.json` for progressive web apps. -- MastoAPI: Support for `birth_date` and `show_birth_date` field in `/api/v1/accounts/update_credentials`. -- Configuration: Add `birth_date_required` and `birth_date_min_age` settings to provide a way to require users to enter their birth date. +- MastoAPI: Support for `birthday` and `show_birthday` field in `/api/v1/accounts/update_credentials`. +- Configuration: Add `birthday_required` and `birthday_min_age` settings to provide a way to require users to enter their birth date. - PleromaAPI: Add `GET /api/v1/pleroma/birthday_reminders` API endpoint ### Fixed diff --git a/config/config.exs b/config/config.exs index ec2407ea2..cefc8a98f 100644 --- a/config/config.exs +++ b/config/config.exs @@ -260,8 +260,8 @@ profile_directory: true, privileged_staff: false, max_endorsed_users: 20, - birth_date_required: false, - birth_date_min_age: 0 + birthday_required: false, + birthday_min_age: 0 config :pleroma, :welcome, direct_message: [ diff --git a/config/description.exs b/config/description.exs index 867112b8e..f0c94c1f7 100644 --- a/config/description.exs +++ b/config/description.exs @@ -959,12 +959,12 @@ "Let moderators access sensitive data (e.g. updating user credentials, get password reset token, delete users, index and read private statuses and chats)" }, %{ - key: :birth_date_required, + key: :birthday_required, type: :boolean, description: "Require users to provide birth day." }, %{ - key: :birth_date_min_age, + key: :birthday_min_age, type: :integer, description: "Min age for users to create account. Only makes sense if birth date is required." diff --git a/lib/pleroma/user.ex b/lib/pleroma/user.ex index 5655762ac..d608525e8 100644 --- a/lib/pleroma/user.ex +++ b/lib/pleroma/user.ex @@ -154,8 +154,8 @@ defmodule Pleroma.User do field(:pinned_objects, :map, default: %{}) field(:is_suggested, :boolean, default: false) field(:last_status_at, :naive_datetime) - field(:birth_date, :date) - field(:hide_birth_date, :boolean, default: false) + field(:birthday, :date) + field(:hide_birthday, :boolean, default: false) embeds_one( :notification_settings, @@ -473,7 +473,7 @@ def remote_user_changeset(struct \\ %User{local: false}, params) do :also_known_as, :accepts_chat_messages, :pinned_objects, - :birth_date + :birthday ] ) |> cast(params, [:name], empty_values: []) @@ -535,8 +535,8 @@ def update_changeset(struct, params \\ %{}) do :actor_type, :accepts_chat_messages, :disclose_client, - :birth_date, - :hide_birth_date + :birthday, + :hide_birthday ] ) |> validate_min_age() @@ -745,7 +745,7 @@ def register_changeset(struct, params \\ %{}, opts \\ []) do :emoji, :accepts_chat_messages, :registration_reason, - :birth_date + :birthday ]) |> validate_required([:name, :nickname, :password, :password_confirmation]) |> validate_confirmation(:password) @@ -767,7 +767,7 @@ def register_changeset(struct, params \\ %{}, opts \\ []) do |> validate_length(:name, min: 1, max: name_limit) |> validate_length(:registration_reason, max: reason_limit) |> maybe_validate_required_email(opts[:external]) - |> maybe_validate_required_birth_date + |> maybe_validate_required_birthday |> validate_min_age() |> put_password_hash |> put_ap_id() @@ -785,9 +785,9 @@ def maybe_validate_required_email(changeset, _) do end end - defp maybe_validate_required_birth_date(changeset) do - if Config.get([:instance, :birth_date_required]) do - validate_required(changeset, [:birth_date]) + defp maybe_validate_required_birthday(changeset) do + if Config.get([:instance, :birthday_required]) do + validate_required(changeset, [:birthday]) else changeset end @@ -795,13 +795,13 @@ defp maybe_validate_required_birth_date(changeset) do defp validate_min_age(changeset) do changeset - |> validate_change(:birth_date, fn :birth_date, birth_date -> + |> validate_change(:birthday, fn :birthday, birthday -> valid? = Date.utc_today() - |> Date.diff(birth_date) >= - Config.get([:instance, :birth_date_min_age]) + |> Date.diff(birthday) >= + Config.get([:instance, :birthday_min_age]) - if valid?, do: [], else: [birth_date: "Invalid birth date"] + if valid?, do: [], else: [birthday: "Invalid age"] end) end @@ -2589,8 +2589,8 @@ def get_friends_birthdays_query(%User{} = user, day, month) do User.Query.build(%{ friends: user, deactivated: false, - birth_day: day, - birth_month: month + birthday_day: day, + birthday_month: month }) end end diff --git a/lib/pleroma/user/query.ex b/lib/pleroma/user/query.ex index ea1706c9e..dddfe07bf 100644 --- a/lib/pleroma/user/query.ex +++ b/lib/pleroma/user/query.ex @@ -60,8 +60,8 @@ defmodule Pleroma.User.Query do select: term(), limit: pos_integer(), actor_types: [String.t()], - birth_day: pos_integer(), - birth_month: pos_integer() + birthday_day: pos_integer(), + birthday_month: pos_integer() } | map() @@ -232,18 +232,18 @@ defp compose_query({:internal, false}, query) do |> where([u], not like(u.nickname, "internal.%")) end - defp compose_query({:birth_day, day}, query) do + defp compose_query({:birthday_day, day}, query) do query - |> where([u], u.hide_birth_date == false) - |> where([u], not is_nil(u.birth_date)) - |> where([u], fragment("date_part('day', ?)", u.birth_date) == ^day) + |> where([u], u.hide_birthday == false) + |> where([u], not is_nil(u.birthday)) + |> where([u], fragment("date_part('day', ?)", u.birthday) == ^day) end - defp compose_query({:birth_month, month}, query) do + defp compose_query({:birthday_month, month}, query) do query - |> where([u], u.hide_birth_date == false) - |> where([u], not is_nil(u.birth_date)) - |> where([u], fragment("date_part('month', ?)", u.birth_date) == ^month) + |> where([u], u.hide_birthday == false) + |> where([u], not is_nil(u.birthday)) + |> where([u], fragment("date_part('month', ?)", u.birthday) == ^month) end defp compose_query(_unsupported_param, query), do: query diff --git a/lib/pleroma/web/activity_pub/activity_pub.ex b/lib/pleroma/web/activity_pub/activity_pub.ex index e1f94ba9c..bdbcc93f4 100644 --- a/lib/pleroma/web/activity_pub/activity_pub.ex +++ b/lib/pleroma/web/activity_pub/activity_pub.ex @@ -1501,8 +1501,8 @@ defp object_to_user_data(data) do nil end - birth_date = - if data["vcard:bday"] do + birthday = + if is_binary(data["vcard:bday"]) do case Date.from_iso8601(data["vcard:bday"]) do {:ok, date} -> date {:error, _} -> nil @@ -1534,7 +1534,7 @@ defp object_to_user_data(data) do shared_inbox: shared_inbox, accepts_chat_messages: accepts_chat_messages, pinned_objects: pinned_objects, - birth_date: birth_date + birthday: birthday } # nickname can be nil because of virtual actors diff --git a/lib/pleroma/web/activity_pub/views/user_view.ex b/lib/pleroma/web/activity_pub/views/user_view.ex index 942f5685a..8ab516214 100644 --- a/lib/pleroma/web/activity_pub/views/user_view.ex +++ b/lib/pleroma/web/activity_pub/views/user_view.ex @@ -92,9 +92,9 @@ def render("user.json", %{user: user}) do %{} end - birth_date = - if !user.hide_birth_date, - do: user.birth_date, + birthday = + if !user.hide_birthday, + do: user.birthday, else: nil %{ @@ -122,7 +122,7 @@ def render("user.json", %{user: user}) do "discoverable" => user.is_discoverable, "capabilities" => capabilities, "alsoKnownAs" => user.also_known_as, - "vcard:bday" => birth_date + "vcard:bday" => birthday } |> Map.merge(maybe_make_image(&User.avatar_url/2, "icon", user)) |> Map.merge(maybe_make_image(&User.banner_url/2, "image", user)) diff --git a/lib/pleroma/web/api_spec/operations/account_operation.ex b/lib/pleroma/web/api_spec/operations/account_operation.ex index e0ef45027..1b2bffa3e 100644 --- a/lib/pleroma/web/api_spec/operations/account_operation.ex +++ b/lib/pleroma/web/api_spec/operations/account_operation.ex @@ -544,10 +544,10 @@ defp create_request do nullable: true, description: "Invite token required when the registrations aren't public" }, - birth_date: %Schema{ + birthday: %Schema{ type: :string, nullable: true, - description: "User's birth date", + description: "User's birthday", format: :date } }, @@ -727,16 +727,16 @@ defp update_credentials_request do "Discovery (listing, indexing) of this account by external services (search bots etc.) is allowed." }, actor_type: ActorType, - birth_date: %Schema{ + birthday: %Schema{ type: :string, nullable: true, - description: "User's birth date", + description: "User's birthday", format: :date }, - hide_birth_date: %Schema{ + hide_birthday: %Schema{ allOf: [BooleanLike], nullable: true, - description: "User's birth date will be hidden" + description: "User's birthday will be hidden" } }, example: %{ @@ -758,8 +758,8 @@ defp update_credentials_request do also_known_as: ["https://foo.bar/users/foo"], discoverable: false, actor_type: "Person", - hide_birth_date: true, - birth_date: "2001-02-12" + hide_birthday: true, + birthday: "2001-02-12" } } end diff --git a/lib/pleroma/web/api_spec/schemas/account.ex b/lib/pleroma/web/api_spec/schemas/account.ex index 3796bccfd..2113f0d31 100644 --- a/lib/pleroma/web/api_spec/schemas/account.ex +++ b/lib/pleroma/web/api_spec/schemas/account.ex @@ -47,14 +47,14 @@ defmodule Pleroma.Web.ApiSpec.Schemas.Account do description: "whether the user allows automatically follow moved following accounts" }, background_image: %Schema{type: :string, nullable: true, format: :uri}, - birth_date: %Schema{type: :string, nullable: true, format: :date}, + birthday: %Schema{type: :string, nullable: true, format: :date}, chat_token: %Schema{type: :string}, is_confirmed: %Schema{ type: :boolean, description: "whether the user account is waiting on email confirmation to be activated" }, - hide_birth_date: %Schema{type: :boolean, nullable: true}, + hide_birthday: %Schema{type: :boolean, nullable: true}, hide_favorites: %Schema{type: :boolean}, hide_followers_count: %Schema{ type: :boolean, @@ -205,7 +205,7 @@ defmodule Pleroma.Web.ApiSpec.Schemas.Account do "settings_store" => %{ "pleroma-fe" => %{} }, - "birth_date" => "2001-02-12" + "birthday" => "2001-02-12" }, "source" => %{ "fields" => [], diff --git a/lib/pleroma/web/mastodon_api/controllers/account_controller.ex b/lib/pleroma/web/mastodon_api/controllers/account_controller.ex index 64034fbef..60c9f7d69 100644 --- a/lib/pleroma/web/mastodon_api/controllers/account_controller.ex +++ b/lib/pleroma/web/mastodon_api/controllers/account_controller.ex @@ -192,7 +192,7 @@ def update_credentials(%{assigns: %{user: user}, body_params: params} = conn, _p :allow_following_move, :also_known_as, :accepts_chat_messages, - :hide_birth_date + :hide_birthday ] |> Enum.reduce(%{}, fn key, acc -> Maps.put_if_present(acc, key, params[key], &{:ok, Params.truthy_param?(&1)}) @@ -220,7 +220,7 @@ def update_credentials(%{assigns: %{user: user}, body_params: params} = conn, _p |> Maps.put_if_present(:is_locked, params[:locked]) # Note: param name is indeed :discoverable (not an error) |> Maps.put_if_present(:is_discoverable, params[:discoverable]) - |> Maps.put_if_present(:birth_date, params[:birth_date]) + |> Maps.put_if_present(:birthday, params[:birthday]) # What happens here: # diff --git a/lib/pleroma/web/mastodon_api/views/account_view.ex b/lib/pleroma/web/mastodon_api/views/account_view.ex index 6c7b4f7c5..e0137a112 100644 --- a/lib/pleroma/web/mastodon_api/views/account_view.ex +++ b/lib/pleroma/web/mastodon_api/views/account_view.ex @@ -298,8 +298,8 @@ defp do_render("show.json", %{user: user} = opts) do background_image: image_url(user.background) |> MediaProxy.url(), accepts_chat_messages: user.accepts_chat_messages, favicon: favicon, - birth_date: user.birth_date, - hide_birth_date: user.hide_birth_date + birthday: user.birthday, + hide_birthday: user.hide_birthday } } |> maybe_put_role(user, opts[:for]) @@ -313,7 +313,7 @@ defp do_render("show.json", %{user: user} = opts) do |> maybe_put_unread_conversation_count(user, opts[:for]) |> maybe_put_unread_notification_count(user, opts[:for]) |> maybe_put_email_address(user, opts[:for]) - |> maybe_hide_birth_date(user, opts[:for]) + |> maybe_hide_birthday(user, opts[:for]) end defp username_from_nickname(string) when is_binary(string) do @@ -435,21 +435,21 @@ defp maybe_put_email_address(data, %User{id: user_id}, %User{id: user_id} = user defp maybe_put_email_address(data, _, _), do: data - defp maybe_hide_birth_date(data, %User{id: user_id}, %User{id: user_id}) do + defp maybe_hide_birthday(data, %User{id: user_id}, %User{id: user_id}) do data end - defp maybe_hide_birth_date(data, %User{hide_birth_date: true}, _) do + defp maybe_hide_birthday(data, %User{hide_birthday: true}, _) do data - |> Kernel.pop_in([:pleroma, :birth_date]) + |> Kernel.pop_in([:pleroma, :birthday]) |> elem(1) - |> Kernel.pop_in([:pleroma, :hide_birth_date]) + |> Kernel.pop_in([:pleroma, :hide_birthday]) |> elem(1) end - defp maybe_hide_birth_date(data, _, _) do + defp maybe_hide_birthday(data, _, _) do data - |> Kernel.pop_in([:pleroma, :hide_birth_date]) + |> Kernel.pop_in([:pleroma, :hide_birthday]) |> elem(1) end diff --git a/lib/pleroma/web/mastodon_api/views/instance_view.ex b/lib/pleroma/web/mastodon_api/views/instance_view.ex index f7c76f06b..f24979048 100644 --- a/lib/pleroma/web/mastodon_api/views/instance_view.ex +++ b/lib/pleroma/web/mastodon_api/views/instance_view.ex @@ -47,8 +47,8 @@ def render("show.json", _) do fields_limits: fields_limits(), post_formats: Config.get([:instance, :allowed_post_formats]), privileged_staff: Config.get([:instance, :privileged_staff]), - birth_date_required: Config.get([:instance, :birth_date_required]), - birth_date_min_age: Config.get([:instance, :birth_date_min_age]) + birthday_required: Config.get([:instance, :birthday_required]), + birthday_min_age: Config.get([:instance, :birthday_min_age]) }, stats: %{mau: Pleroma.User.active_user_count()}, vapid_public_key: Keyword.get(Pleroma.Web.Push.vapid_config(), :public_key) diff --git a/lib/pleroma/web/twitter_api/twitter_api.ex b/lib/pleroma/web/twitter_api/twitter_api.ex index 91d10d260..aa4dfb145 100644 --- a/lib/pleroma/web/twitter_api/twitter_api.ex +++ b/lib/pleroma/web/twitter_api/twitter_api.ex @@ -20,7 +20,7 @@ def register_user(params, opts \\ []) do |> Map.put(:name, Map.get(params, :fullname, params[:username])) |> Map.put(:password_confirmation, params[:password]) |> Map.put(:registration_reason, params[:reason]) - |> Map.put(:birth_date, params[:birth_date]) + |> Map.put(:birthday, params[:birthday]) if Pleroma.Config.get([:instance, :registrations_open]) do create_user(params, opts) diff --git a/priv/repo/migrations/29220116183110_add_birth_date_to_users.exs b/priv/repo/migrations/29220116183110_add_birth_date_to_users.exs index f610eeb10..be0ed2bbc 100644 --- a/priv/repo/migrations/29220116183110_add_birth_date_to_users.exs +++ b/priv/repo/migrations/29220116183110_add_birth_date_to_users.exs @@ -3,8 +3,8 @@ defmodule Pleroma.Repo.Migrations.AddBirthDateToUsers do def change do alter table(:users) do - add_if_not_exists(:birth_date, :date) - add_if_not_exists(:hide_birth_date, :boolean, default: false, null: false) + add_if_not_exists(:birthday, :date) + add_if_not_exists(:hide_birthday, :boolean, default: false, null: false) end end end diff --git a/test/pleroma/user_test.exs b/test/pleroma/user_test.exs index 263c2b274..9a902e8b1 100644 --- a/test/pleroma/user_test.exs +++ b/test/pleroma/user_test.exs @@ -755,7 +755,7 @@ test "it restricts length of registration reason" do end end - describe "user registration, with :birth_date_required and :birth_date_min_age" do + describe "user registration, with :birthday_required and :birthday_min_age" do @full_user_data %{ bio: "A guy", name: "my name", @@ -766,17 +766,17 @@ test "it restricts length of registration reason" do } setup do - clear_config([:instance, :birth_date_required], true) - clear_config([:instance, :birth_date_min_age], 18 * 365) + clear_config([:instance, :birthday_required], true) + clear_config([:instance, :birthday_min_age], 18 * 365) end test "it passes when correct birth date is provided" do today = Date.utc_today() - birth_date = Date.add(today, -19 * 365) + birthday = Date.add(today, -19 * 365) params = @full_user_data - |> Map.put(:birth_date, birth_date) + |> Map.put(:birthday, birthday) changeset = User.register_changeset(%User{}, params) @@ -791,11 +791,11 @@ test "it fails when birth date is not provided" do test "it fails when provided invalid birth date" do today = Date.utc_today() - birth_date = Date.add(today, -17 * 365) + birthday = Date.add(today, -17 * 365) params = @full_user_data - |> Map.put(:birth_date, birth_date) + |> Map.put(:birthday, birthday) changeset = User.register_changeset(%User{}, params) diff --git a/test/pleroma/web/mastodon_api/controllers/account_controller_test.exs b/test/pleroma/web/mastodon_api/controllers/account_controller_test.exs index d8ebd98cc..19d706958 100644 --- a/test/pleroma/web/mastodon_api/controllers/account_controller_test.exs +++ b/test/pleroma/web/mastodon_api/controllers/account_controller_test.exs @@ -1588,8 +1588,8 @@ test "returns an error if captcha is invalid", %{conn: conn} do describe "create account with required birth date" do setup %{conn: conn} do - clear_config([:instance, :birth_date_required], true) - clear_config([:instance, :birth_date_min_age], 18 * 365) + clear_config([:instance, :birthday_required], true) + clear_config([:instance, :birthday_min_age], 18 * 365) app_token = insert(:oauth_token, user: nil) @@ -1602,7 +1602,7 @@ test "returns an error if captcha is invalid", %{conn: conn} do end test "creates an account if provided valid birth date", %{conn: conn} do - birth_date = + birthday = Date.utc_today() |> Date.add(-19 * 365) |> Date.to_string() @@ -1612,7 +1612,7 @@ test "creates an account if provided valid birth date", %{conn: conn} do email: "mkljczk@example.org", password: "dupa.8", agreement: true, - birth_date: birth_date + birthday: birthday } res = @@ -1635,7 +1635,7 @@ test "returns an error if missing birth date", %{conn: conn} do |> post("/api/v1/accounts", params) assert json_response_and_validate_schema(res, 400) == %{ - "error" => "{\"birth_date\":[\"can't be blank\"]}" + "error" => "{\"birthday\":[\"can't be blank\"]}" } end end diff --git a/test/pleroma/web/mastodon_api/update_credentials_test.exs b/test/pleroma/web/mastodon_api/update_credentials_test.exs index e89f597a9..5507a77b0 100644 --- a/test/pleroma/web/mastodon_api/update_credentials_test.exs +++ b/test/pleroma/web/mastodon_api/update_credentials_test.exs @@ -370,24 +370,24 @@ test "update fields", %{conn: conn} do ] end - test "updates birth date", %{conn: conn, user: user} do + test "updates birth date", %{conn: conn} do res = patch(conn, "/api/v1/accounts/update_credentials", %{ - "birth_date" => "2001-02-12" + "birthday" => "2001-02-12" }) assert user_data = json_response_and_validate_schema(res, 200) - assert user_data["pleroma"]["birth_date"] == "2001-02-12" + assert user_data["pleroma"]["birthday"] == "2001-02-12" end - test "updates the user's hide_birth_date status", %{conn: conn} do + test "updates the user's hide_birthday status", %{conn: conn} do res = patch(conn, "/api/v1/accounts/update_credentials", %{ - "hide_birth_date" => true + "hide_birthday" => true }) assert user_data = json_response_and_validate_schema(res, 200) - assert user_data["pleroma"]["hide_birth_date"] == true + assert user_data["pleroma"]["hide_birthday"] == true end test "emojis in fields labels", %{conn: conn} do diff --git a/test/pleroma/web/mastodon_api/views/account_view_test.exs b/test/pleroma/web/mastodon_api/views/account_view_test.exs index da8761355..329813994 100644 --- a/test/pleroma/web/mastodon_api/views/account_view_test.exs +++ b/test/pleroma/web/mastodon_api/views/account_view_test.exs @@ -79,7 +79,7 @@ test "Represent a user account" do ap_id: user.ap_id, also_known_as: ["https://shitposter.zone/users/shp"], background_image: "https://example.com/images/asuka_hospital.png", - birth_date: nil, + birthday: nil, favicon: nil, is_confirmed: true, tags: [], @@ -182,7 +182,7 @@ test "Represent a Service(bot) account" do ap_id: user.ap_id, also_known_as: [], background_image: nil, - birth_date: nil, + birthday: nil, favicon: nil, is_confirmed: true, tags: [], diff --git a/test/pleroma/web/pleroma_api/controllers/account_controller_test.exs b/test/pleroma/web/pleroma_api/controllers/account_controller_test.exs index 5b74bb0b2..8f3e565ee 100644 --- a/test/pleroma/web/pleroma_api/controllers/account_controller_test.exs +++ b/test/pleroma/web/pleroma_api/controllers/account_controller_test.exs @@ -312,12 +312,12 @@ test "returns a list of friends having birthday on specified day" do %{id: id1} = user1 = insert(:user, %{ - birth_date: "2001-02-12" + birthday: "2001-02-12" }) user2 = insert(:user, %{ - birth_date: "2001-02-14" + birthday: "2001-02-14" }) user3 = insert(:user) @@ -337,15 +337,15 @@ test "the list doesn't list friends with hidden birth date" do user1 = insert(:user, %{ - birth_date: "2001-02-12", - hide_birth_date: true + birthday: "2001-02-12", + hide_birthday: true }) %{id: id2} = user2 = insert(:user, %{ - birth_date: "2001-02-12", - hide_birth_date: false + birthday: "2001-02-12", + hide_birthday: false }) CommonAPI.follow(user, user1) From 98ce239ebc99444f7f418040acaba2e6cc990c29 Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Sat, 22 Jan 2022 13:28:00 -0600 Subject: [PATCH 07/11] Update description.exs --- config/description.exs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/config/description.exs b/config/description.exs index f0c94c1f7..3f66877e4 100644 --- a/config/description.exs +++ b/config/description.exs @@ -961,13 +961,13 @@ %{ key: :birthday_required, type: :boolean, - description: "Require users to provide birth day." + description: "Require users to enter their birthday." }, %{ key: :birthday_min_age, type: :integer, description: - "Min age for users to create account. Only makes sense if birth date is required." + "Minimum required age for users to create account. Only used if birthday is required." } ] }, From 0266bc3c96c30cfee929c55babdca679ca17a479 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?marcin=20miko=C5=82ajczak?= Date: Sun, 23 Jan 2022 08:42:18 +0100 Subject: [PATCH 08/11] Birthdays: hide_birthday -> show_birthday MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: marcin mikołajczak --- CHANGELOG.md | 2 +- lib/pleroma/user.ex | 7 ++++--- lib/pleroma/user/query.ex | 4 ++-- lib/pleroma/web/activity_pub/activity_pub.ex | 5 ++++- .../web/activity_pub/views/user_view.ex | 2 +- .../api_spec/operations/account_operation.ex | 6 +++--- lib/pleroma/web/api_spec/schemas/account.ex | 2 +- .../controllers/account_controller.ex | 2 +- .../web/mastodon_api/views/account_view.ex | 20 ++++++++----------- .../controllers/account_controller.ex | 2 +- lib/pleroma/web/router.ex | 2 +- ...29220116183110_add_birth_date_to_users.exs | 2 +- .../mastodon_api/update_credentials_test.exs | 6 +++--- .../controllers/account_controller_test.exs | 14 +++++++------ 14 files changed, 39 insertions(+), 37 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 53cdc6e79..9e6e0fdf2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -28,7 +28,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). - Added `/manifest.json` for progressive web apps. - MastoAPI: Support for `birthday` and `show_birthday` field in `/api/v1/accounts/update_credentials`. - Configuration: Add `birthday_required` and `birthday_min_age` settings to provide a way to require users to enter their birth date. -- PleromaAPI: Add `GET /api/v1/pleroma/birthday_reminders` API endpoint +- PleromaAPI: Add `GET /api/v1/pleroma/birthdays` API endpoint ### Fixed - Subscription(Bell) Notifications: Don't create from Pipeline Ingested replies diff --git a/lib/pleroma/user.ex b/lib/pleroma/user.ex index 8bb4fb204..0d209c5a8 100644 --- a/lib/pleroma/user.ex +++ b/lib/pleroma/user.ex @@ -155,7 +155,7 @@ defmodule Pleroma.User do field(:is_suggested, :boolean, default: false) field(:last_status_at, :naive_datetime) field(:birthday, :date) - field(:hide_birthday, :boolean, default: false) + field(:show_birthday, :boolean, default: false) embeds_one( :notification_settings, @@ -473,7 +473,8 @@ def remote_user_changeset(struct \\ %User{local: false}, params) do :also_known_as, :accepts_chat_messages, :pinned_objects, - :birthday + :birthday, + :show_birthday ] ) |> cast(params, [:name], empty_values: []) @@ -536,7 +537,7 @@ def update_changeset(struct, params \\ %{}) do :accepts_chat_messages, :disclose_client, :birthday, - :hide_birthday + :show_birthday ] ) |> validate_min_age() diff --git a/lib/pleroma/user/query.ex b/lib/pleroma/user/query.ex index dddfe07bf..bd11d287c 100644 --- a/lib/pleroma/user/query.ex +++ b/lib/pleroma/user/query.ex @@ -234,14 +234,14 @@ defp compose_query({:internal, false}, query) do defp compose_query({:birthday_day, day}, query) do query - |> where([u], u.hide_birthday == false) + |> where([u], u.show_birthday == true) |> where([u], not is_nil(u.birthday)) |> where([u], fragment("date_part('day', ?)", u.birthday) == ^day) end defp compose_query({:birthday_month, month}, query) do query - |> where([u], u.hide_birthday == false) + |> where([u], u.show_birthday == true) |> where([u], not is_nil(u.birthday)) |> where([u], fragment("date_part('month', ?)", u.birthday) == ^month) end diff --git a/lib/pleroma/web/activity_pub/activity_pub.ex b/lib/pleroma/web/activity_pub/activity_pub.ex index 7551dd56d..e6475a2b7 100644 --- a/lib/pleroma/web/activity_pub/activity_pub.ex +++ b/lib/pleroma/web/activity_pub/activity_pub.ex @@ -1511,6 +1511,8 @@ defp object_to_user_data(data) do nil end + show_birthday = !!birthday + user_data = %{ ap_id: data["id"], uri: get_actor_url(data["url"]), @@ -1534,7 +1536,8 @@ defp object_to_user_data(data) do shared_inbox: shared_inbox, accepts_chat_messages: accepts_chat_messages, pinned_objects: pinned_objects, - birthday: birthday + birthday: birthday, + show_birthday: show_birthday } # nickname can be nil because of virtual actors diff --git a/lib/pleroma/web/activity_pub/views/user_view.ex b/lib/pleroma/web/activity_pub/views/user_view.ex index 8ab516214..5ed08d7f6 100644 --- a/lib/pleroma/web/activity_pub/views/user_view.ex +++ b/lib/pleroma/web/activity_pub/views/user_view.ex @@ -93,7 +93,7 @@ def render("user.json", %{user: user}) do end birthday = - if !user.hide_birthday, + if user.show_birthday, do: user.birthday, else: nil diff --git a/lib/pleroma/web/api_spec/operations/account_operation.ex b/lib/pleroma/web/api_spec/operations/account_operation.ex index 1b2bffa3e..03efa3c38 100644 --- a/lib/pleroma/web/api_spec/operations/account_operation.ex +++ b/lib/pleroma/web/api_spec/operations/account_operation.ex @@ -733,10 +733,10 @@ defp update_credentials_request do description: "User's birthday", format: :date }, - hide_birthday: %Schema{ + show_birthday: %Schema{ allOf: [BooleanLike], nullable: true, - description: "User's birthday will be hidden" + description: "User's birthday will be visible" } }, example: %{ @@ -758,7 +758,7 @@ defp update_credentials_request do also_known_as: ["https://foo.bar/users/foo"], discoverable: false, actor_type: "Person", - hide_birthday: true, + show_birthday: false, birthday: "2001-02-12" } } diff --git a/lib/pleroma/web/api_spec/schemas/account.ex b/lib/pleroma/web/api_spec/schemas/account.ex index 2113f0d31..029c6f6cf 100644 --- a/lib/pleroma/web/api_spec/schemas/account.ex +++ b/lib/pleroma/web/api_spec/schemas/account.ex @@ -54,7 +54,7 @@ defmodule Pleroma.Web.ApiSpec.Schemas.Account do description: "whether the user account is waiting on email confirmation to be activated" }, - hide_birthday: %Schema{type: :boolean, nullable: true}, + show_birthday: %Schema{type: :boolean, nullable: true}, hide_favorites: %Schema{type: :boolean}, hide_followers_count: %Schema{ type: :boolean, diff --git a/lib/pleroma/web/mastodon_api/controllers/account_controller.ex b/lib/pleroma/web/mastodon_api/controllers/account_controller.ex index 60c9f7d69..8e6d49168 100644 --- a/lib/pleroma/web/mastodon_api/controllers/account_controller.ex +++ b/lib/pleroma/web/mastodon_api/controllers/account_controller.ex @@ -192,7 +192,7 @@ def update_credentials(%{assigns: %{user: user}, body_params: params} = conn, _p :allow_following_move, :also_known_as, :accepts_chat_messages, - :hide_birthday + :show_birthday ] |> Enum.reduce(%{}, fn key, acc -> Maps.put_if_present(acc, key, params[key], &{:ok, Params.truthy_param?(&1)}) diff --git a/lib/pleroma/web/mastodon_api/views/account_view.ex b/lib/pleroma/web/mastodon_api/views/account_view.ex index e0137a112..e73d03f06 100644 --- a/lib/pleroma/web/mastodon_api/views/account_view.ex +++ b/lib/pleroma/web/mastodon_api/views/account_view.ex @@ -298,8 +298,7 @@ defp do_render("show.json", %{user: user} = opts) do background_image: image_url(user.background) |> MediaProxy.url(), accepts_chat_messages: user.accepts_chat_messages, favicon: favicon, - birthday: user.birthday, - hide_birthday: user.hide_birthday + birthday: user.birthday } } |> maybe_put_role(user, opts[:for]) @@ -313,7 +312,7 @@ defp do_render("show.json", %{user: user} = opts) do |> maybe_put_unread_conversation_count(user, opts[:for]) |> maybe_put_unread_notification_count(user, opts[:for]) |> maybe_put_email_address(user, opts[:for]) - |> maybe_hide_birthday(user, opts[:for]) + |> maybe_show_birthday(user, opts[:for]) end defp username_from_nickname(string) when is_binary(string) do @@ -347,6 +346,7 @@ defp maybe_put_settings( |> Kernel.put_in([:source, :privacy], user.default_scope) |> Kernel.put_in([:source, :pleroma, :show_role], user.show_role) |> Kernel.put_in([:source, :pleroma, :no_rich_text], user.no_rich_text) + |> Kernel.put_in([:source, :pleroma, :show_birthday], user.show_birthday) end defp maybe_put_settings(data, _, _, _), do: data @@ -435,22 +435,18 @@ defp maybe_put_email_address(data, %User{id: user_id}, %User{id: user_id} = user defp maybe_put_email_address(data, _, _), do: data - defp maybe_hide_birthday(data, %User{id: user_id}, %User{id: user_id}) do + defp maybe_show_birthday(data, %User{id: user_id} = user, %User{id: user_id}) do data + |> Kernel.put_in([:pleroma, :birthday], user.birthday) end - defp maybe_hide_birthday(data, %User{hide_birthday: true}, _) do + defp maybe_show_birthday(data, %User{show_birthday: true} = user, _) do data - |> Kernel.pop_in([:pleroma, :birthday]) - |> elem(1) - |> Kernel.pop_in([:pleroma, :hide_birthday]) - |> elem(1) + |> Kernel.put_in([:pleroma, :birthday], user.birthday) end - defp maybe_hide_birthday(data, _, _) do + defp maybe_show_birthday(data, _, _) do data - |> Kernel.pop_in([:pleroma, :hide_birthday]) - |> elem(1) end defp image_url(%{"url" => [%{"href" => href} | _]}), do: href diff --git a/lib/pleroma/web/pleroma_api/controllers/account_controller.ex b/lib/pleroma/web/pleroma_api/controllers/account_controller.ex index 20697fa46..d78ebbe2e 100644 --- a/lib/pleroma/web/pleroma_api/controllers/account_controller.ex +++ b/lib/pleroma/web/pleroma_api/controllers/account_controller.ex @@ -143,7 +143,7 @@ def unsubscribe(%{assigns: %{user: user, account: subscription_target}} = conn, end end - @doc "GET /api/v1/pleroma/birthday_reminders" + @doc "GET /api/v1/pleroma/birthdays" def birthdays(%{assigns: %{user: %User{} = user}} = conn, %{day: day, month: month} = _params) do birthdays = User.get_friends_birthdays_query(user, day, month) diff --git a/lib/pleroma/web/router.ex b/lib/pleroma/web/router.ex index 2d896fdd4..26706a6b8 100644 --- a/lib/pleroma/web/router.ex +++ b/lib/pleroma/web/router.ex @@ -449,7 +449,7 @@ defmodule Pleroma.Web.Router do post("/accounts/:id/subscribe", AccountController, :subscribe) post("/accounts/:id/unsubscribe", AccountController, :unsubscribe) - get("/birthday_reminders", AccountController, :birthdays) + get("/birthdays", AccountController, :birthdays) end post("/accounts/confirmation_resend", AccountController, :confirmation_resend) diff --git a/priv/repo/migrations/29220116183110_add_birth_date_to_users.exs b/priv/repo/migrations/29220116183110_add_birth_date_to_users.exs index be0ed2bbc..57fedaee2 100644 --- a/priv/repo/migrations/29220116183110_add_birth_date_to_users.exs +++ b/priv/repo/migrations/29220116183110_add_birth_date_to_users.exs @@ -4,7 +4,7 @@ defmodule Pleroma.Repo.Migrations.AddBirthDateToUsers do def change do alter table(:users) do add_if_not_exists(:birthday, :date) - add_if_not_exists(:hide_birthday, :boolean, default: false, null: false) + add_if_not_exists(:show_birthday, :boolean, default: false, null: false) end end end diff --git a/test/pleroma/web/mastodon_api/update_credentials_test.exs b/test/pleroma/web/mastodon_api/update_credentials_test.exs index 5507a77b0..9073cd771 100644 --- a/test/pleroma/web/mastodon_api/update_credentials_test.exs +++ b/test/pleroma/web/mastodon_api/update_credentials_test.exs @@ -380,14 +380,14 @@ test "updates birth date", %{conn: conn} do assert user_data["pleroma"]["birthday"] == "2001-02-12" end - test "updates the user's hide_birthday status", %{conn: conn} do + test "updates the user's show_birthday status", %{conn: conn} do res = patch(conn, "/api/v1/accounts/update_credentials", %{ - "hide_birthday" => true + "show_birthday" => true }) assert user_data = json_response_and_validate_schema(res, 200) - assert user_data["pleroma"]["hide_birthday"] == true + assert user_data["pleroma"]["source"]["show_birthday"] == true end test "emojis in fields labels", %{conn: conn} do diff --git a/test/pleroma/web/pleroma_api/controllers/account_controller_test.exs b/test/pleroma/web/pleroma_api/controllers/account_controller_test.exs index 8f3e565ee..15682e40a 100644 --- a/test/pleroma/web/pleroma_api/controllers/account_controller_test.exs +++ b/test/pleroma/web/pleroma_api/controllers/account_controller_test.exs @@ -312,12 +312,14 @@ test "returns a list of friends having birthday on specified day" do %{id: id1} = user1 = insert(:user, %{ - birthday: "2001-02-12" + birthday: "2001-02-12", + show_birthday: true }) user2 = insert(:user, %{ - birthday: "2001-02-14" + birthday: "2001-02-14", + show_birthday: true }) user3 = insert(:user) @@ -328,7 +330,7 @@ test "returns a list of friends having birthday on specified day" do [%{"id" => ^id1}] = conn - |> get("/api/v1/pleroma/birthday_reminders?day=12&month=2") + |> get("/api/v1/pleroma/birthdays?day=12&month=2") |> json_response_and_validate_schema(:ok) end @@ -338,14 +340,14 @@ test "the list doesn't list friends with hidden birth date" do user1 = insert(:user, %{ birthday: "2001-02-12", - hide_birthday: true + show_birthday: false }) %{id: id2} = user2 = insert(:user, %{ birthday: "2001-02-12", - hide_birthday: false + show_birthday: true }) CommonAPI.follow(user, user1) @@ -353,7 +355,7 @@ test "the list doesn't list friends with hidden birth date" do [%{"id" => ^id2}] = conn - |> get("/api/v1/pleroma/birthday_reminders?day=12&month=2") + |> get("/api/v1/pleroma/birthdays?day=12&month=2") |> json_response_and_validate_schema(:ok) end end From e3d394eef64f96210b144c40d3055d0662da7109 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?marcin=20miko=C5=82ajczak?= Date: Sun, 23 Jan 2022 09:41:21 +0100 Subject: [PATCH 09/11] Birthdays: Fix tests, add test for misskey MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: marcin mikołajczak --- test/fixtures/birthdays/misskey-user.json | 1 + .../web/activity_pub/activity_pub_test.exs | 20 +++++++++++++++++++ .../mastodon_api/update_credentials_test.exs | 2 +- 3 files changed, 22 insertions(+), 1 deletion(-) create mode 100644 test/fixtures/birthdays/misskey-user.json diff --git a/test/fixtures/birthdays/misskey-user.json b/test/fixtures/birthdays/misskey-user.json new file mode 100644 index 000000000..4ffee3910 --- /dev/null +++ b/test/fixtures/birthdays/misskey-user.json @@ -0,0 +1 @@ +{"@context":["https://www.w3.org/ns/activitystreams","https://w3id.org/security/v1",{"manuallyApprovesFollowers":"as:manuallyApprovesFollowers","sensitive":"as:sensitive","Hashtag":"as:Hashtag","quoteUrl":"as:quoteUrl","toot":"http://joinmastodon.org/ns#","Emoji":"toot:Emoji","featured":"toot:featured","discoverable":"toot:discoverable","schema":"http://schema.org#","PropertyValue":"schema:PropertyValue","value":"schema:value","misskey":"https://misskey.io/ns#","_misskey_content":"misskey:_misskey_content","_misskey_quote":"misskey:_misskey_quote","_misskey_reaction":"misskey:_misskey_reaction","_misskey_votes":"misskey:_misskey_votes","_misskey_talk":"misskey:_misskey_talk","isCat":"misskey:isCat","vcard":"http://www.w3.org/2006/vcard/ns#"}],"type":"Person","id":"https://misskey.io/users/8dhi2ne167","inbox":"https://misskey.io/users/8dhi2ne167/inbox","outbox":"https://misskey.io/users/8dhi2ne167/outbox","followers":"https://misskey.io/users/8dhi2ne167/followers","following":"https://misskey.io/users/8dhi2ne167/following","sharedInbox":"https://misskey.io/inbox","endpoints":{"sharedInbox":"https://misskey.io/inbox"},"url":"https://misskey.io/@mkljczk","preferredUsername":"mkljczk","name":null,"summary":null,"icon":null,"image":null,"tag":[],"manuallyApprovesFollowers":false,"discoverable":true,"publicKey":{"id":"https://misskey.io/users/8dhi2ne167#main-key","type":"Key","owner":"https://misskey.io/users/8dhi2ne167","publicKeyPem":"-----BEGIN PUBLIC KEY-----\nMIICIjANBgkqhkiG9w0BAQEFAAOCAg8AMIICCgKCAgEA7CI3Ol1M0TDdLL+E8Uhd\nJ8l/RTEtxl39MKxsqSCZr9itf/EBn4dGTifK9LN3XZD2fjmX4hdwaxndp2HYVDqn\ndc6O57u8dHxFv9wTwXQrLzEonOzbrBec6WB42ZpkFHi4XEyqg8iYGu5Yy7ttXJ21\nOfWqi+eytttcTErKuu4z8MX1L1IlmpfSmH1trMyDZLFMRqVJ0416/qI0K3l3cmIf\n8cuWbJ57UxVbYxp9242der/3vrNIU24rAouYQYe1atUgFPKil3w8dCY7magy36Wg\nOXC1hdRsFcsVW54/3cSQ9fc/+1HIg16/zlS+AWb4dVDhrAUJLYIBrkMPRnu/cDuI\ndvyL+KtZUxhDBoSO0JLrd1+GZGt0WD+mfutCugJS8IGlWQmGq8WRmM2vYfZgEYkq\nCv4392VSsWvg4iluKz0eX+8l7QKHseJwGBvk89Txlz6f7QkooBXYuuyHZS1ZLZBW\nfooK+RNAquDU+cVUu1gVt1V5yt3IxF1qvMRtlElNJKN5NUJT9/K2YcVX6UoMXhDd\noSOpARqPm9E2pdjI62pAOBbCplMSoBprhoCYm0iozf9QhNyUBGWDcTsFDDgqOwy4\nYjGQ5jsnCrkhSzRkTViWD+Pgw+Ar4fxcjySGUf0x7HkNfteDPSdLMD8J2vTJXfoB\nGAQQmGMZmFgONC62FrDphlsCAwEAAQ==\n-----END PUBLIC KEY-----\n"},"isCat":true,"vcard:bday":"2001-02-12"} \ No newline at end of file diff --git a/test/pleroma/web/activity_pub/activity_pub_test.exs b/test/pleroma/web/activity_pub/activity_pub_test.exs index 3d152b4d0..9789d7704 100644 --- a/test/pleroma/web/activity_pub/activity_pub_test.exs +++ b/test/pleroma/web/activity_pub/activity_pub_test.exs @@ -389,6 +389,26 @@ test "fetches user featured collection without embedded object" do assert %{data: %{"id" => ^object_url}} = Object.get_by_ap_id(object_url) end + + test "fetches user birthday information from misskey" do + user_id = "https://misskey.io/@mkljczk" + + Tesla.Mock.mock(fn + %{ + method: :get, + url: ^user_id + } -> + %Tesla.Env{ + status: 200, + body: File.read!("test/fixtures/birthdays/misskey-user.json"), + headers: [{"content-type", "application/activity+json"}] + } + end) + + {:ok, user} = ActivityPub.make_user_from_ap_id(user_id) + + assert user.birthday == ~D[2001-02-12] + end end test "it fetches the appropriate tag-restricted posts" do diff --git a/test/pleroma/web/mastodon_api/update_credentials_test.exs b/test/pleroma/web/mastodon_api/update_credentials_test.exs index 9073cd771..f0618885a 100644 --- a/test/pleroma/web/mastodon_api/update_credentials_test.exs +++ b/test/pleroma/web/mastodon_api/update_credentials_test.exs @@ -387,7 +387,7 @@ test "updates the user's show_birthday status", %{conn: conn} do }) assert user_data = json_response_and_validate_schema(res, 200) - assert user_data["pleroma"]["source"]["show_birthday"] == true + assert user_data["source"]["pleroma"]["show_birthday"] == true end test "emojis in fields labels", %{conn: conn} do From 61bae9a407ae9480e46e3a33d0b7d1f4c940b02c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?marcin=20miko=C5=82ajczak?= Date: Sun, 23 Jan 2022 09:54:24 +0100 Subject: [PATCH 10/11] Create index for `show_birthday` MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: marcin mikołajczak --- ..._to_users.exs => 20220116183110_add_birthday_to_users.exs} | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) rename priv/repo/migrations/{29220116183110_add_birth_date_to_users.exs => 20220116183110_add_birthday_to_users.exs} (63%) diff --git a/priv/repo/migrations/29220116183110_add_birth_date_to_users.exs b/priv/repo/migrations/20220116183110_add_birthday_to_users.exs similarity index 63% rename from priv/repo/migrations/29220116183110_add_birth_date_to_users.exs rename to priv/repo/migrations/20220116183110_add_birthday_to_users.exs index 57fedaee2..0b22ecc69 100644 --- a/priv/repo/migrations/29220116183110_add_birth_date_to_users.exs +++ b/priv/repo/migrations/20220116183110_add_birthday_to_users.exs @@ -1,4 +1,4 @@ -defmodule Pleroma.Repo.Migrations.AddBirthDateToUsers do +defmodule Pleroma.Repo.Migrations.AddBirthdayToUsers do use Ecto.Migration def change do @@ -6,5 +6,7 @@ def change do add_if_not_exists(:birthday, :date) add_if_not_exists(:show_birthday, :boolean, default: false, null: false) end + + create_if_not_exists(index(:users, [:show_birthday])) end end From 249fe88d12345c1c93f1812871cd70424b9e9a0d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?marcin=20miko=C5=82ajczak?= Date: Tue, 25 Jan 2022 10:45:32 +0100 Subject: [PATCH 11/11] Birthdays: users_birthday_month_day_index MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: marcin mikołajczak --- ...25104429_add_birthday_month_day_index_to_users.exs | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 priv/repo/migrations/20220125104429_add_birthday_month_day_index_to_users.exs diff --git a/priv/repo/migrations/20220125104429_add_birthday_month_day_index_to_users.exs b/priv/repo/migrations/20220125104429_add_birthday_month_day_index_to_users.exs new file mode 100644 index 000000000..8ce4c77c5 --- /dev/null +++ b/priv/repo/migrations/20220125104429_add_birthday_month_day_index_to_users.exs @@ -0,0 +1,11 @@ +defmodule Pleroma.Repo.Migrations.AddBirthdayMonthDayIndexToUsers do + use Ecto.Migration + + def change do + create( + index(:users, ["date_part('month', birthday)", "date_part('day', birthday)"], + name: :users_birthday_month_day_index + ) + ) + end +end