Birth dates: Add tests

Signed-off-by: marcin mikołajczak <git@mkljczk.pl>
This commit is contained in:
marcin mikołajczak 2022-01-18 19:29:21 +01:00
commit dfb2808535
9 changed files with 210 additions and 9 deletions

View file

@ -535,7 +535,8 @@ defmodule Pleroma.User do
:actor_type,
:accepts_chat_messages,
:disclose_client,
:birth_date
:birth_date,
:hide_birth_date
]
)
|> validate_min_age()
@ -2583,4 +2584,13 @@ defmodule Pleroma.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

View file

@ -234,12 +234,14 @@ defmodule Pleroma.User.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

View file

@ -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,

View file

@ -249,11 +249,6 @@ defmodule Pleroma.Web.MastodonAPI.AccountView 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 @@ defmodule Pleroma.Web.MastodonAPI.AccountView 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 @@ 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_birth_date(user, opts[:for])
end
defp username_from_nickname(string) when is_binary(string) do
@ -438,6 +435,23 @@ defmodule Pleroma.Web.MastodonAPI.AccountView do
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

View file

@ -146,7 +146,7 @@ defmodule Pleroma.Web.PleromaAPI.AccountController do
@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