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] 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 @@ defmodule Pleroma.User do :also_known_as, :accepts_chat_messages, :pinned_objects, - :birthday + :birthday, + :show_birthday ] ) |> cast(params, [:name], empty_values: []) @@ -536,7 +537,7 @@ defmodule Pleroma.User 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 @@ defmodule Pleroma.User.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 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do nil end + show_birthday = !!birthday + user_data = %{ ap_id: data["id"], uri: get_actor_url(data["url"]), @@ -1534,7 +1536,8 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub 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 @@ defmodule Pleroma.Web.ActivityPub.UserView 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 @@ defmodule Pleroma.Web.ApiSpec.AccountOperation 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 @@ defmodule Pleroma.Web.ApiSpec.AccountOperation 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 @@ defmodule Pleroma.Web.MastodonAPI.AccountController do :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 @@ defmodule Pleroma.Web.MastodonAPI.AccountView 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 @@ defmodule Pleroma.Web.MastodonAPI.AccountView 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 @@ defmodule Pleroma.Web.MastodonAPI.AccountView do |> 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 @@ defmodule Pleroma.Web.MastodonAPI.AccountView do 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 @@ defmodule Pleroma.Web.PleromaAPI.AccountController do 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 @@ defmodule Pleroma.Web.MastodonAPI.UpdateCredentialsTest 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 @@ defmodule Pleroma.Web.PleromaAPI.AccountControllerTest 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 @@ defmodule Pleroma.Web.PleromaAPI.AccountControllerTest 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 @@ defmodule Pleroma.Web.PleromaAPI.AccountControllerTest 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 @@ defmodule Pleroma.Web.PleromaAPI.AccountControllerTest 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