From 590a10af332650a8a1eb61db7db7457dc63f229a Mon Sep 17 00:00:00 2001 From: Mary Kate Date: Thu, 24 Dec 2020 16:40:08 -0600 Subject: Adds tests for breaking tag and mention links after text is converted from markdown to html --- test/pleroma/formatter_test.exs | 78 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) diff --git a/test/pleroma/formatter_test.exs b/test/pleroma/formatter_test.exs index 5781a3f01..f47331bc3 100644 --- a/test/pleroma/formatter_test.exs +++ b/test/pleroma/formatter_test.exs @@ -284,6 +284,34 @@ test "it correctly parses angry face D:< with mention" do assert {^expected_text, ^expected_mentions, []} = Formatter.linkify(text) end + + test "correctly parses mentions in html" do + text = "

@lain hello

" + lain = insert(:user, %{nickname: "lain"}) + + {text, mentions, []} = Formatter.linkify(text) + + assert length(mentions) == 1 + + expected_text = + ~s(

@lain hello

) + + assert expected_text == text + end + + test "correctly parses mentions on the last line of html" do + text = "

Hello

@lain

" + lain = insert(:user, %{nickname: "lain"}) + + {text, mentions, []} = Formatter.linkify(text) + + assert length(mentions) == 1 + + expected_text = + ~s(

Hello

@lain

) + + assert expected_text == text + end end describe ".parse_tags" do @@ -299,6 +327,56 @@ test "parses tags in the text" do assert {_text, [], ^expected_tags} = Formatter.linkify(text) end + + test "parses tags in html" do + text = "

This is a #test

" + + expected_tags = [ + {"#test", "test"}, + ] + + assert {_text, [], ^expected_tags} = Formatter.linkify(text) + end + + test "parses mulitple tags in html" do + text = "

#tag1 #tag2 #tag3 #tag4

" + + expected_tags = [ + {"#tag1", "tag1"}, + {"#tag2", "tag2"}, + {"#tag3", "tag3"}, + {"#tag4", "tag4"}, + ] + + assert {_text, [], ^expected_tags} = Formatter.linkify(text) + end + + test "parses tags on the last line of html" do + text = "

This is a

#test

" + + expected_tags = [ + {"#test", "test"}, + ] + + assert {_text, [], ^expected_tags} = Formatter.linkify(text) + end + + test "parses mulitple tags on mulitple lines in html" do + text = "

testing...

#tag1 #tag2 #tag3 #tag4

paragraph

#tag5 #tag6 #tag7 #tag8

" + + expected_tags = [ + {"#tag1", "tag1"}, + {"#tag2", "tag2"}, + {"#tag3", "tag3"}, + {"#tag4", "tag4"}, + {"#tag5", "tag5"}, + {"#tag6", "tag6"}, + {"#tag7", "tag7"}, + {"#tag8", "tag8"}, + ] + + assert {_text, [], ^expected_tags} = Formatter.linkify(text) + end end test "it escapes HTML in plain text" do -- cgit v1.2.3 From 790081540d27f1e70f88f719a987d77d54709f84 Mon Sep 17 00:00:00 2001 From: Hakaba Hitoyo Date: Sat, 15 Jan 2022 12:26:33 +0000 Subject: Update cheatsheet.md. Add `Pleroma.Web.ActivityPub.MRF.KeywordPolicy`. --- docs/configuration/cheatsheet.md | 1 + 1 file changed, 1 insertion(+) diff --git a/docs/configuration/cheatsheet.md b/docs/configuration/cheatsheet.md index 40e81cffb..5b2410c16 100644 --- a/docs/configuration/cheatsheet.md +++ b/docs/configuration/cheatsheet.md @@ -125,6 +125,7 @@ To add configuration to your config file, you can copy it from the base config. * `Pleroma.Web.ActivityPub.MRF.ActivityExpirationPolicy`: Sets a default expiration on all posts made by users of the local instance. Requires `Pleroma.Workers.PurgeExpiredActivity` to be enabled for processing the scheduled delections. * `Pleroma.Web.ActivityPub.MRF.ForceBotUnlistedPolicy`: Makes all bot posts to disappear from public timelines. * `Pleroma.Web.ActivityPub.MRF.FollowBotPolicy`: Automatically follows newly discovered users from the specified bot account. Local accounts, locked accounts, and users with "#nobot" in their bio are respected and excluded from being followed. + * `Pleroma.Web.ActivityPub.MRF.KeywordPolicy`: Rejects or removes from the federated timeline or replaces keywords. (See [`:mrf_keyword`](#mrf_keyword)). * `transparency`: Make the content of your Message Rewrite Facility settings public (via nodeinfo). * `transparency_exclusions`: Exclude specific instance names from MRF transparency. The use of the exclusions feature will be disclosed in nodeinfo as a boolean value. -- cgit v1.2.3 From d5eb44e8b39a3a896cf3457a2ca6f2055eebc982 Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Sat, 15 Jan 2022 12:27:16 -0600 Subject: Upgrade Linkify to v0.5.2 --- mix.exs | 2 +- mix.lock | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/mix.exs b/mix.exs index 62010d166..4387cb0aa 100644 --- a/mix.exs +++ b/mix.exs @@ -157,7 +157,7 @@ defp deps do {:floki, "~> 0.27"}, {:timex, "~> 3.6"}, {:ueberauth, "~> 0.4"}, - {:linkify, "~> 0.5.1"}, + {:linkify, "~> 0.5.2"}, {:http_signatures, "~> 0.1.1"}, {:telemetry, "~> 0.3"}, {:poolboy, "~> 1.5"}, diff --git a/mix.lock b/mix.lock index 05630a92e..817240538 100644 --- a/mix.lock +++ b/mix.lock @@ -69,7 +69,7 @@ "jose": {:hex, :jose, "1.11.1", "59da64010c69aad6cde2f5b9248b896b84472e99bd18f246085b7b9fe435dcdb", [:mix, :rebar3], [], "hexpm", "078f6c9fb3cd2f4cfafc972c814261a7d1e8d2b3685c0a76eb87e158efff1ac5"}, "jumper": {:hex, :jumper, "1.0.1", "3c00542ef1a83532b72269fab9f0f0c82bf23a35e27d278bfd9ed0865cecabff", [:mix], [], "hexpm", "318c59078ac220e966d27af3646026db9b5a5e6703cb2aa3e26bcfaba65b7433"}, "libring": {:hex, :libring, "1.4.0", "41246ba2f3fbc76b3971f6bce83119dfec1eee17e977a48d8a9cfaaf58c2a8d6", [:mix], [], "hexpm"}, - "linkify": {:hex, :linkify, "0.5.1", "6dc415cbc948b2f6ecec7cb226aab7ba9d3a1815bb501ae33e042334d707ecee", [:mix], [], "hexpm", "a3128c7e22fada4aa7214009501d8131e1fa3faf2f0a68b33dba379dc84ff944"}, + "linkify": {:hex, :linkify, "0.5.2", "fb66be139fdf1656ecb31f78a93592724d1b78d960a1b3598bd661013ea0e3c7", [:mix], [], "hexpm", "8d71ac690218d8952c90cbeb63cb8cc33738bb230d8a56d487d9447f2a5eab86"}, "majic": {:hex, :majic, "1.0.0", "37e50648db5f5c2ff0c9fb46454d034d11596c03683807b9fb3850676ffdaab3", [:make, :mix], [{:elixir_make, "~> 0.6.1", [hex: :elixir_make, repo: "hexpm", optional: false]}, {:mime, "~> 1.0", [hex: :mime, repo: "hexpm", optional: false]}, {:nimble_pool, "~> 0.2", [hex: :nimble_pool, repo: "hexpm", optional: false]}, {:plug, "~> 1.0", [hex: :plug, repo: "hexpm", optional: true]}], "hexpm", "7905858f76650d49695f14ea55cd9aaaee0c6654fa391671d4cf305c275a0a9e"}, "makeup": {:hex, :makeup, "1.0.5", "d5a830bc42c9800ce07dd97fa94669dfb93d3bf5fcf6ea7a0c67b2e0e4a7f26c", [:mix], [{:nimble_parsec, "~> 0.5 or ~> 1.0", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm", "cfa158c02d3f5c0c665d0af11512fed3fba0144cf1aadee0f2ce17747fba2ca9"}, "makeup_elixir": {:hex, :makeup_elixir, "0.14.1", "4f0e96847c63c17841d42c08107405a005a2680eb9c7ccadfd757bd31dabccfb", [:mix], [{:makeup, "~> 1.0", [hex: :makeup, repo: "hexpm", optional: false]}], "hexpm", "f2438b1a80eaec9ede832b5c41cd4f373b38fd7aa33e3b22d9db79e640cbde11"}, -- cgit v1.2.3 From 31148c1857dae28377f9b864a9ebe143d99beb96 Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Sat, 15 Jan 2022 12:31:36 -0600 Subject: FormatterTest: fix nesting in expected output --- test/pleroma/formatter_test.exs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/pleroma/formatter_test.exs b/test/pleroma/formatter_test.exs index 5b8babc83..045edf4b1 100644 --- a/test/pleroma/formatter_test.exs +++ b/test/pleroma/formatter_test.exs @@ -294,7 +294,7 @@ test "correctly parses mentions on the last line of html" do assert length(mentions) == 1 expected_text = - ~s(

Hello

@lain

) + ~s(

Hello

@lain

) assert expected_text == text end -- cgit v1.2.3 From cd8f1aac477935176ad5413bbe237b3791c7d9a4 Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Sat, 15 Jan 2022 12:36:33 -0600 Subject: CHANGELOG: hashtags markdown fix --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index d214fde8b..ca519a668 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -31,6 +31,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). - Handle Reject for already-accepted Follows properly - Display OpenGraph data on alternative notice routes. - Fix replies count for remote replies +- Fixed hashtags disappearing from the end of lines when Markdown is enabled - ChatAPI: Add link headers - Limited number of search results to 40 to prevent DoS attacks - ActivityPub: fixed federation of attachment dimensions -- cgit v1.2.3 From 39c5ebb1f634b691d06d607d91cbfc6b0741d541 Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Sat, 15 Jan 2022 12:55:12 -0600 Subject: mix format --- test/pleroma/formatter_test.exs | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/test/pleroma/formatter_test.exs b/test/pleroma/formatter_test.exs index 045edf4b1..6663fdbc6 100644 --- a/test/pleroma/formatter_test.exs +++ b/test/pleroma/formatter_test.exs @@ -318,7 +318,7 @@ test "parses tags in html" do text = "

This is a #test

" expected_tags = [ - {"#test", "test"}, + {"#test", "test"} ] assert {_text, [], ^expected_tags} = Formatter.linkify(text) @@ -331,7 +331,7 @@ test "parses mulitple tags in html" do {"#tag1", "tag1"}, {"#tag2", "tag2"}, {"#tag3", "tag3"}, - {"#tag4", "tag4"}, + {"#tag4", "tag4"} ] assert {_text, [], ^expected_tags} = Formatter.linkify(text) @@ -341,14 +341,15 @@ test "parses tags on the last line of html" do text = "

This is a

#test

" expected_tags = [ - {"#test", "test"}, + {"#test", "test"} ] assert {_text, [], ^expected_tags} = Formatter.linkify(text) end test "parses mulitple tags on mulitple lines in html" do - text = "

testing...

#tag1 #tag2 #tag3 #tag4

paragraph

#tag5 #tag6 #tag7 #tag8

" + text = + "

testing...

#tag1 #tag2 #tag3 #tag4

paragraph

#tag5 #tag6 #tag7 #tag8

" expected_tags = [ {"#tag1", "tag1"}, @@ -358,7 +359,7 @@ test "parses mulitple tags on mulitple lines in html" do {"#tag5", "tag5"}, {"#tag6", "tag6"}, {"#tag7", "tag7"}, - {"#tag8", "tag8"}, + {"#tag8", "tag8"} ] assert {_text, [], ^expected_tags} = Formatter.linkify(text) -- cgit v1.2.3 From ac434f83c39e8992fae453a230b860bb8e338e3a Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Sat, 15 Jan 2022 13:35:22 -0600 Subject: unit-testing-erratic: allow failure --- .gitlab-ci.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index bebd97efb..e5664da68 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -90,6 +90,7 @@ unit-testing: unit-testing-erratic: stage: test retry: 2 + allow_failure: true only: changes: - "**/*.ex" -- cgit v1.2.3 From b108b0565076d677a90ff38ec8926e1f3153b7f6 Mon Sep 17 00:00:00 2001 From: marcin mikołajczak Date: Tue, 18 Jan 2022 14:57:48 +0100 Subject: 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 +- lib/pleroma/web/activity_pub/views/user_view.ex | 8 ++++- .../web/api_spec/operations/account_operation.ex | 25 ++++++++++++++-- .../operations/pleroma_account_operation.ex | 29 ++++++++++++++++++ lib/pleroma/web/api_spec/schemas/account.ex | 5 +++- .../mastodon_api/controllers/account_controller.ex | 4 ++- lib/pleroma/web/mastodon_api/views/account_view.ex | 8 ++++- .../pleroma_api/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#" } ] } -- cgit v1.2.3 From 1dfb67f1e02fcd71f339c05901107926dad6fa88 Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Tue, 18 Jan 2022 11:02:23 -0600 Subject: Docs: PleromaAPI oauth_tokens endpoints --- docs/development/API/pleroma_api.md | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/docs/development/API/pleroma_api.md b/docs/development/API/pleroma_api.md index 2304291e5..0d15384b9 100644 --- a/docs/development/API/pleroma_api.md +++ b/docs/development/API/pleroma_api.md @@ -660,3 +660,38 @@ Emoji reactions work a lot like favourites do. They make it possible to react to "url": "https://example.com/media/backups/archive-foobar-20200910T161803-QUhx6VYDRQ2wfV0SdA2Pfj_2CLM_ATUlw-D5l5TJf4Q.zip" }] ``` + +## `GET /api/oauth_tokens` +### Retrieve a list of active sessions for the user +* Method: `GET` +* Authentication: required +* Params: none +* Response: JSON +* Example response: + +```json +[ + { + "app_name": "Pleroma FE", + "id": 9275, + "valid_until": "2121-11-24T15:51:08.234234" + }, + { + "app_name": "Patron", + "id": 8805, + "valid_until": "2121-10-26T18:09:59.857150" + }, + { + "app_name": "Soapbox FE", + "id": 9727, + "valid_until": "2121-12-25T16:52:39.692877" + } +] +``` + +## `DELETE /api/oauth_tokens/:id` +### Revoke a user session by its ID +* Method: `DELETE` +* Authentication: required +* Params: none +* Response: HTTP 200 on success, 500 on error -- cgit v1.2.3 From 397f67fef8658000be26fd3b9bd86f5968fcaf52 Mon Sep 17 00:00:00 2001 From: marcin mikołajczak Date: Tue, 18 Jan 2022 18:02:01 +0100 Subject: 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 +-- lib/pleroma/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 +++- .../web/pleroma_api/controllers/account_controller.ex | 12 ++++++------ test/pleroma/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: [], -- cgit v1.2.3 From dfb28085356aea63c2b0c755d7861946315c3931 Mon Sep 17 00:00:00 2001 From: marcin mikołajczak Date: Tue, 18 Jan 2022 19:29:21 +0100 Subject: 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 +- lib/pleroma/web/mastodon_api/views/account_view.ex | 26 ++++++++--- .../pleroma_api/controllers/account_controller.ex | 2 +- test/pleroma/user_test.exs | 48 +++++++++++++++++++ .../controllers/account_controller_test.exs | 54 ++++++++++++++++++++++ .../web/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 -- cgit v1.2.3 From c180f9276fe9c6d6e7c5d25165bf87606cd1c966 Mon Sep 17 00:00:00 2001 From: marcin mikołajczak Date: Wed, 19 Jan 2022 17:19:28 +0100 Subject: 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 -- cgit v1.2.3 From a02cfd7f5223da396538e264df580e7f33bee690 Mon Sep 17 00:00:00 2001 From: rinpatch Date: Wed, 19 Jan 2022 19:40:50 +0300 Subject: Add ForceMentionsInContentPolicy Even though latest PleromaFE supports displaying these properly, mobile apps still exist, so I think we should offer a workaround to those who want it. --- CHANGELOG.md | 1 + docs/configuration/cheatsheet.md | 1 + lib/pleroma/formatter.ex | 42 ++++++------ lib/pleroma/user.ex | 4 ++ .../activity_pub/mrf/force_mentions_in_content.ex | 80 ++++++++++++++++++++++ test/fixtures/soapbox_no_mentions_in_content.json | 79 +++++++++++++++++++++ .../mrf/force_mentions_in_content_test.exs | 34 +++++++++ 7 files changed, 221 insertions(+), 20 deletions(-) create mode 100644 lib/pleroma/web/activity_pub/mrf/force_mentions_in_content.ex create mode 100644 test/fixtures/soapbox_no_mentions_in_content.json create mode 100644 test/pleroma/web/activity_pub/mrf/force_mentions_in_content_test.exs diff --git a/CHANGELOG.md b/CHANGELOG.md index ca519a668..6aaadca42 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,6 +16,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). ### Added - `activeMonth` and `activeHalfyear` fields in NodeInfo usage.users object - Experimental support for Finch. Put `config :tesla, :adapter, {Tesla.Adapter.Finch, name: MyFinch}` in your secrets file to use it. Reverse Proxy will still use Hackney. +- `ForceMentionsInPostContent` MRF policy - AdminAPI: allow moderators to manage reports, users, invites, and custom emojis - AdminAPI: restrict moderators to access sensitive data: change user credentials, get password reset token, read private statuses and chats, etc - PleromaAPI: Add remote follow API endpoint at `POST /api/v1/pleroma/remote_interaction` diff --git a/docs/configuration/cheatsheet.md b/docs/configuration/cheatsheet.md index 5b2410c16..4dacdc68c 100644 --- a/docs/configuration/cheatsheet.md +++ b/docs/configuration/cheatsheet.md @@ -126,6 +126,7 @@ To add configuration to your config file, you can copy it from the base config. * `Pleroma.Web.ActivityPub.MRF.ForceBotUnlistedPolicy`: Makes all bot posts to disappear from public timelines. * `Pleroma.Web.ActivityPub.MRF.FollowBotPolicy`: Automatically follows newly discovered users from the specified bot account. Local accounts, locked accounts, and users with "#nobot" in their bio are respected and excluded from being followed. * `Pleroma.Web.ActivityPub.MRF.KeywordPolicy`: Rejects or removes from the federated timeline or replaces keywords. (See [`:mrf_keyword`](#mrf_keyword)). + * `Pleroma.Web.ActivityPub.MRF.ForceMentionsInContent`: Forces every mentioned user to be reflected in the post content. * `transparency`: Make the content of your Message Rewrite Facility settings public (via nodeinfo). * `transparency_exclusions`: Exclude specific instance names from MRF transparency. The use of the exclusions feature will be disclosed in nodeinfo as a boolean value. diff --git a/lib/pleroma/formatter.ex b/lib/pleroma/formatter.ex index ae37946ab..115835378 100644 --- a/lib/pleroma/formatter.ex +++ b/lib/pleroma/formatter.ex @@ -34,32 +34,34 @@ def escape_mention_handler("@" <> nickname = mention, buffer, _, _) do def mention_handler("@" <> nickname, buffer, opts, acc) do case User.get_cached_by_nickname(nickname) do - %User{id: id} = user -> - user_url = user.uri || user.ap_id - nickname_text = get_nickname_text(nickname, opts) - - link = - Phoenix.HTML.Tag.content_tag( - :span, - Phoenix.HTML.Tag.content_tag( - :a, - ["@", Phoenix.HTML.Tag.content_tag(:span, nickname_text)], - "data-user": id, - class: "u-url mention", - href: user_url, - rel: "ugc" - ), - class: "h-card" - ) - |> Phoenix.HTML.safe_to_string() - - {link, %{acc | mentions: MapSet.put(acc.mentions, {"@" <> nickname, user})}} + %User{} = user -> + {mention_from_user(user, opts), + %{acc | mentions: MapSet.put(acc.mentions, {"@" <> nickname, user})}} _ -> {buffer, acc} end end + def mention_from_user(%User{id: id} = user, opts \\ %{mentions_format: :full}) do + user_url = user.uri || user.ap_id + nickname_text = get_nickname_text(user.nickname, opts) + + Phoenix.HTML.Tag.content_tag( + :span, + Phoenix.HTML.Tag.content_tag( + :a, + ["@", Phoenix.HTML.Tag.content_tag(:span, nickname_text)], + "data-user": id, + class: "u-url mention", + href: user_url, + rel: "ugc" + ), + class: "h-card" + ) + |> Phoenix.HTML.safe_to_string() + end + def hashtag_handler("#" <> tag = tag_text, _buffer, _opts, acc) do tag = String.downcase(tag) url = "#{Pleroma.Web.Endpoint.url()}/tag/#{tag}" diff --git a/lib/pleroma/user.ex b/lib/pleroma/user.ex index 0a5dfccc9..bf5b6508b 100644 --- a/lib/pleroma/user.ex +++ b/lib/pleroma/user.ex @@ -1055,6 +1055,10 @@ def get_by_ap_id(ap_id) do Repo.get_by(User, ap_id: ap_id) end + def get_by_uri(uri) do + Repo.get_by(User, uri: uri) + end + def get_all_by_ap_id(ap_ids) do from(u in __MODULE__, where: u.ap_id in ^ap_ids diff --git a/lib/pleroma/web/activity_pub/mrf/force_mentions_in_content.ex b/lib/pleroma/web/activity_pub/mrf/force_mentions_in_content.ex new file mode 100644 index 000000000..522ae2f60 --- /dev/null +++ b/lib/pleroma/web/activity_pub/mrf/force_mentions_in_content.ex @@ -0,0 +1,80 @@ +# Pleroma: A lightweight social networking server +# Copyright © 2017-2022 Pleroma Authors +# SPDX-License-Identifier: AGPL-3.0-only + +defmodule Pleroma.Web.ActivityPub.MRF.ForceMentionsInContent do + alias Pleroma.Formatter + alias Pleroma.User + + @behaviour Pleroma.Web.ActivityPub.MRF.Policy + + defp do_extract({:a, attrs, _}, acc) do + if Enum.find(attrs, fn {name, value} -> + name == "class" && value in ["mention", "u-url mention", "mention u-url"] + end) do + href = Enum.find(attrs, fn {name, _} -> name == "href" end) |> elem(1) + acc ++ [href] + else + acc + end + end + + defp do_extract({_, _, children}, acc) do + do_extract(children, acc) + end + + defp do_extract(nodes, acc) when is_list(nodes) do + Enum.reduce(nodes, acc, fn node, acc -> do_extract(node, acc) end) + end + + defp do_extract(_, acc), do: acc + + defp extract_mention_uris_from_content(content) do + {:ok, tree} = :fast_html.decode(content, format: [:html_atoms]) + do_extract(tree, []) + end + + @impl true + def filter(%{"type" => "Create", "object" => %{"type" => "Note", "tag" => tag}} = object) do + # image-only posts from pleroma apparently reach this MRF without the content field + content = object["object"]["content"] || "" + + mention_users = + tag + |> Enum.filter(fn tag -> tag["type"] == "Mention" end) + |> Enum.map(& &1["href"]) + |> Enum.reject(&is_nil/1) + |> Enum.map(fn ap_id_or_uri -> + case User.get_or_fetch_by_ap_id(ap_id_or_uri) do + {:ok, user} -> {ap_id_or_uri, user} + _ -> {ap_id_or_uri, User.get_by_uri(ap_id_or_uri)} + end + end) + |> Enum.reject(fn {_, user} -> user == nil end) + |> Enum.into(%{}) + + explicitly_mentioned_uris = extract_mention_uris_from_content(content) + + added_mentions = + Enum.reduce(mention_users, "", fn {uri, user}, acc -> + unless uri in explicitly_mentioned_uris do + acc <> Formatter.mention_from_user(user) + else + acc + end + end) + + content = + if added_mentions != "", + do: added_mentions <> " " <> content, + else: content + + {:ok, put_in(object["object"]["content"], content)} + end + + @impl true + def filter(object), do: {:ok, object} + + @impl true + def describe, do: {:ok, %{}} +end diff --git a/test/fixtures/soapbox_no_mentions_in_content.json b/test/fixtures/soapbox_no_mentions_in_content.json new file mode 100644 index 000000000..03b4b8ee4 --- /dev/null +++ b/test/fixtures/soapbox_no_mentions_in_content.json @@ -0,0 +1,79 @@ +{ + "@context": [ + "https://www.w3.org/ns/activitystreams", + "https://gleasonator.com/schemas/litepub-0.1.jsonld", + { + "@language": "und" + } + ], + "actor": "https://gleasonator.com/users/alex", + "attachment": [ + { + "blurhash": "b15#-6_3~l%eDkNraAM#HYMf", + "height": 2147, + "mediaType": "image/png", + "name": "", + "type": "Document", + "url": "https://media.gleasonator.com/2df1c9cc26c35028db65848143971da0c5a6e04dbd423b194bb018b6c601db9b.png", + "width": 966 + }, + { + "blurhash": "b168EX~q~W-;DiM{VtIUD%Io", + "height": 2147, + "mediaType": "image/png", + "name": "", + "type": "Document", + "url": "https://media.gleasonator.com/22b42b4cddc1aecc7e2d3dc20bcdd66d5c4e44c2bef119de948e95a587e36b66.png", + "width": 966 + } + ], + "attributedTo": "https://gleasonator.com/users/alex", + "cc": [ + "https://www.w3.org/ns/activitystreams#Public" + ], + "content": "

Haha yeah, you can control who you reply to.

", + "context": "https://gleasonator.com/contexts/ba6c8bd9-ac4d-479e-9bd9-5bf570068ae7", + "conversation": "https://gleasonator.com/contexts/ba6c8bd9-ac4d-479e-9bd9-5bf570068ae7", + "id": "https://gleasonator.com/objects/02af65fe-04f8-46bc-9b1e-31dfe76eaffd", + "inReplyTo": "https://shitposter.club/objects/c686d811-4368-48e1-ba11-82c129f93165", + "published": "2022-01-19T03:37:35.976545Z", + "sensitive": false, + "source": "Haha yeah, you can control who you reply to.", + "summary": "", + "tag": [ + { + "href": "https://lain.com/users/lain", + "name": "@lain@lain.com", + "type": "Mention" + }, + { + "href": "https://shitposter.club/users/coolboymew", + "name": "@coolboymew@shitposter.club", + "type": "Mention" + }, + { + "href": "https://shitposter.club/users/dielan", + "name": "@dielan@shitposter.club", + "type": "Mention" + }, + { + "href": "https://tuusin.misono-ya.info/users/hakui", + "name": "@hakui@tuusin.misono-ya.info", + "type": "Mention" + }, + { + "href": "https://xyzzy.link/users/fence", + "name": "@fence@xyzzy.link", + "type": "Mention" + } + ], + "to": [ + "https://shitposter.club/users/dielan", + "https://gleasonator.com/users/alex/followers", + "https://shitposter.club/users/coolboymew", + "https://xyzzy.link/users/fence", + "https://tuusin.misono-ya.info/users/hakui", + "https://lain.com/users/lain" + ], + "type": "Note" +} diff --git a/test/pleroma/web/activity_pub/mrf/force_mentions_in_content_test.exs b/test/pleroma/web/activity_pub/mrf/force_mentions_in_content_test.exs new file mode 100644 index 000000000..090bdc35e --- /dev/null +++ b/test/pleroma/web/activity_pub/mrf/force_mentions_in_content_test.exs @@ -0,0 +1,34 @@ +# Pleroma: A lightweight social networking server +# Copyright © 2017-2021 Pleroma Authors +# SPDX-License-Identifier: AGPL-3.0-only + +defmodule Pleroma.Web.ActivityPub.MRF.ForceMentionsInContentTest do + alias Pleroma.Web.ActivityPub.MRF.ForceMentionsInContent + import Pleroma.Factory + use Pleroma.DataCase + + test "adds mentions to post content" do + users = %{ + "lain@lain.com" => "https://lain.com/users/lain", + "coolboymew@shitposter.club" => "https://shitposter.club/users/coolboymew", + "dielan@shitposter.club" => "https://shitposter.club/users/dielan", + "hakui@tuusin.misono-ya.info" => "https://tuusin.misono-ya.info/users/hakui", + "fence@xyzzy.link" => "https://xyzzy.link/users/fence" + } + + Enum.each(users, fn {nickname, ap_id} -> + insert(:user, ap_id: ap_id, nickname: nickname, local: false) + end) + + object = File.read!("test/fixtures/soapbox_no_mentions_in_content.json") |> Jason.decode!() + + activity = %{ + "type" => "Create", + "actor" => "https://gleasonator.com/users/alex", + "object" => object + } + + {:ok, %{"object" => %{"content" => filtered}}} = ForceMentionsInContent.filter(activity) + Enum.each(users, fn {nickname, _} -> assert filtered =~ nickname end) + end +end -- cgit v1.2.3 From 74cf0f0355fbfd21859e0ed374c263762279d89e Mon Sep 17 00:00:00 2001 From: marcin mikołajczak Date: Wed, 19 Jan 2022 19:51:30 +0100 Subject: 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 -- cgit v1.2.3 From 9faac1094f556681841a820717859155ad600de5 Mon Sep 17 00:00:00 2001 From: Tusooa Zhu Date: Wed, 19 Jan 2022 23:56:57 -0500 Subject: Add glitch-lily to clients.md --- docs/clients.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/docs/clients.md b/docs/clients.md index 5650ea236..31d2d27c3 100644 --- a/docs/clients.md +++ b/docs/clients.md @@ -116,3 +116,9 @@ Feel free to contact us to be added to this list! - Contact: [@r@freesoftwareextremist.com](https://freesoftwareextremist.com/users/r) - Features: Does not requires JavaScript - Features: MastoAPI + +### Glitch-lily +- Source Code: +- Contact: [@tusooa@kazv.moe](https://kazv.moe/users/tusooa) +- Features: MastoAPI +- Based on [glitch-soc](https://github.com/glitch-soc/mastodon) frontend -- cgit v1.2.3 From 88c21b928286782e174451fb6b039a34548ab75d Mon Sep 17 00:00:00 2001 From: marcin mikołajczak Date: Thu, 20 Jan 2022 11:59:15 +0100 Subject: Support private pinned posts from Mastodon 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 | 5 +- .../mastodon/collections/external_featured.json | 14 ++++ .../pleroma/web/activity_pub/activity_pub_test.exs | 77 ++++++++++++++++++++++ 3 files changed, 95 insertions(+), 1 deletion(-) create mode 100644 test/fixtures/mastodon/collections/external_featured.json diff --git a/lib/pleroma/web/activity_pub/activity_pub.ex b/lib/pleroma/web/activity_pub/activity_pub.ex index 756096952..9ca44c532 100644 --- a/lib/pleroma/web/activity_pub/activity_pub.ex +++ b/lib/pleroma/web/activity_pub/activity_pub.ex @@ -1664,7 +1664,10 @@ def pin_data_from_featured_collection(%{ "orderedItems" => objects }) when type in ["OrderedCollection", "Collection"] do - Map.new(objects, fn %{"id" => object_ap_id} -> {object_ap_id, NaiveDateTime.utc_now()} end) + Map.new(objects, fn + %{"id" => object_ap_id} -> {object_ap_id, NaiveDateTime.utc_now()} + object_ap_id when is_binary(object_ap_id) -> {object_ap_id, NaiveDateTime.utc_now()} + end) end def fetch_and_prepare_featured_from_ap_id(nil) do diff --git a/test/fixtures/mastodon/collections/external_featured.json b/test/fixtures/mastodon/collections/external_featured.json new file mode 100644 index 000000000..be5302cf8 --- /dev/null +++ b/test/fixtures/mastodon/collections/external_featured.json @@ -0,0 +1,14 @@ +{ + "@context": [ + "https://www.w3.org/ns/activitystreams", + "https://{{domain}}/schemas/litepub-0.1.jsonld", + { + "@language": "und" + } + ], + "id": "https://{{domain}}/users/{{nickname}}/collections/featured", + "orderedItems": [ + "https://{{domain}}/objects/{{object_id}}" + ], + "type": "OrderedCollection" +} diff --git a/test/pleroma/web/activity_pub/activity_pub_test.exs b/test/pleroma/web/activity_pub/activity_pub_test.exs index 574ef0d71..3d152b4d0 100644 --- a/test/pleroma/web/activity_pub/activity_pub_test.exs +++ b/test/pleroma/web/activity_pub/activity_pub_test.exs @@ -312,6 +312,83 @@ test "fetches user featured collection" do assert %{data: %{"id" => ^object_url}} = Object.get_by_ap_id(object_url) end + + test "fetches user featured collection without embedded object" do + ap_id = "https://example.com/users/lain" + + featured_url = "https://example.com/users/lain/collections/featured" + + user_data = + "test/fixtures/users_mock/user.json" + |> File.read!() + |> String.replace("{{nickname}}", "lain") + |> Jason.decode!() + |> Map.put("featured", featured_url) + |> Jason.encode!() + + object_id = Ecto.UUID.generate() + + featured_data = + "test/fixtures/mastodon/collections/external_featured.json" + |> File.read!() + |> String.replace("{{domain}}", "example.com") + |> String.replace("{{nickname}}", "lain") + |> String.replace("{{object_id}}", object_id) + + object_url = "https://example.com/objects/#{object_id}" + + object_data = + "test/fixtures/statuses/note.json" + |> File.read!() + |> String.replace("{{object_id}}", object_id) + |> String.replace("{{nickname}}", "lain") + + Tesla.Mock.mock(fn + %{ + method: :get, + url: ^ap_id + } -> + %Tesla.Env{ + status: 200, + body: user_data, + headers: [{"content-type", "application/activity+json"}] + } + + %{ + method: :get, + url: ^featured_url + } -> + %Tesla.Env{ + status: 200, + body: featured_data, + headers: [{"content-type", "application/activity+json"}] + } + end) + + Tesla.Mock.mock_global(fn + %{ + method: :get, + url: ^object_url + } -> + %Tesla.Env{ + status: 200, + body: object_data, + headers: [{"content-type", "application/activity+json"}] + } + end) + + {:ok, user} = ActivityPub.make_user_from_ap_id(ap_id) + Process.sleep(50) + + assert user.featured_address == featured_url + assert Map.has_key?(user.pinned_objects, object_url) + + in_db = Pleroma.User.get_by_ap_id(ap_id) + assert in_db.featured_address == featured_url + assert Map.has_key?(user.pinned_objects, object_url) + + assert %{data: %{"id" => ^object_url}} = Object.get_by_ap_id(object_url) + end end test "it fetches the appropriate tag-restricted posts" do -- cgit v1.2.3 From 00523bdf5e4c61ffb1fdd66a3345a26848a20ca9 Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Thu, 20 Jan 2022 16:52:43 -0600 Subject: Test pinned private statuses in AccountController --- .../controllers/account_controller_test.exs | 40 +++++++++++++++++----- 1 file changed, 31 insertions(+), 9 deletions(-) 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..9b07cd5c2 100644 --- a/test/pleroma/web/mastodon_api/controllers/account_controller_test.exs +++ b/test/pleroma/web/mastodon_api/controllers/account_controller_test.exs @@ -5,7 +5,9 @@ defmodule Pleroma.Web.MastodonAPI.AccountControllerTest do use Pleroma.Web.ConnCase + alias Pleroma.Object alias Pleroma.Repo + alias Pleroma.Tests.ObanHelpers alias Pleroma.User alias Pleroma.Web.ActivityPub.ActivityPub alias Pleroma.Web.ActivityPub.InternalFetchActor @@ -404,15 +406,6 @@ test "gets users statuses", %{conn: conn} do assert id_two == to_string(activity.id) end - test "unimplemented pinned statuses feature", %{conn: conn} do - note = insert(:note_activity) - user = User.get_cached_by_ap_id(note.data["actor"]) - - conn = get(conn, "/api/v1/accounts/#{user.id}/statuses?pinned=true") - - assert json_response_and_validate_schema(conn, 200) == [] - end - test "gets an users media, excludes reblogs", %{conn: conn} do note = insert(:note_activity) user = User.get_cached_by_ap_id(note.data["actor"]) @@ -1038,6 +1031,35 @@ test "returns pinned statuses", %{conn: conn, user: user, activity: %{id: activi end end + test "view pinned private statuses" do + user = insert(:user) + reader = insert(:user) + + # Create a private status and pin it + {:ok, %{id: activity_id} = activity} = + CommonAPI.post(user, %{status: "psst", visibility: "private"}) + + %{data: %{"id" => object_ap_id}} = Object.normalize(activity) + {:ok, _} = User.add_pinned_object_id(user, object_ap_id) + + %{conn: conn} = oauth_access(["read:statuses"], user: reader) + + # A non-follower can't see the pinned status + assert [] == + conn + |> get("/api/v1/accounts/#{user.id}/statuses?pinned=true") + |> json_response_and_validate_schema(200) + + # Follow the user, then the pinned status can be seen + CommonAPI.follow(reader, user) + ObanHelpers.perform_all() + + assert [%{"id" => ^activity_id, "pinned" => true}] = + conn + |> get("/api/v1/accounts/#{user.id}/statuses?pinned=true") + |> json_response_and_validate_schema(200) + end + test "blocking / unblocking a user" do %{conn: conn} = oauth_access(["follow"]) other_user = insert(:user) -- cgit v1.2.3 From 832828961921866a5f0ef9d13b421748f3262f7f Mon Sep 17 00:00:00 2001 From: Finn Behrens Date: Sat, 22 Jan 2022 10:00:43 +0100 Subject: Add autocompelete values suggested by Apple Link: https://developer.apple.com/documentation/security/password_autofill/enabling_password_autofill_on_an_html_input_element --- lib/pleroma/web/templates/o_auth/mfa/totp.html.eex | 2 +- lib/pleroma/web/templates/o_auth/o_auth/register.html.eex | 8 ++++---- lib/pleroma/web/templates/o_auth/o_auth/show.html.eex | 2 +- .../web/templates/twitter_api/remote_follow/follow_login.html.eex | 4 ++-- 4 files changed, 8 insertions(+), 8 deletions(-) diff --git a/lib/pleroma/web/templates/o_auth/mfa/totp.html.eex b/lib/pleroma/web/templates/o_auth/mfa/totp.html.eex index 29ea7c5fb..27600253c 100644 --- a/lib/pleroma/web/templates/o_auth/mfa/totp.html.eex +++ b/lib/pleroma/web/templates/o_auth/mfa/totp.html.eex @@ -10,7 +10,7 @@ <%= form_for @conn, Routes.mfa_verify_path(@conn, :verify), [as: "mfa"], fn f -> %>
<%= label f, :code, "Authentication code" %> - <%= text_input f, :code, [autocomplete: false, autocorrect: "off", autocapitalize: "off", autofocus: true, pattern: "[0-9]*", spellcheck: false] %> + <%= text_input f, :code, [autocomplete: "one-time-code", autocorrect: "off", autocapitalize: "off", autofocus: true, pattern: "[0-9]*", spellcheck: false] %> <%= hidden_input f, :mfa_token, value: @mfa_token %> <%= hidden_input f, :state, value: @state %> <%= hidden_input f, :redirect_uri, value: @redirect_uri %> diff --git a/lib/pleroma/web/templates/o_auth/o_auth/register.html.eex b/lib/pleroma/web/templates/o_auth/o_auth/register.html.eex index 99f900fb7..3ac428b2f 100644 --- a/lib/pleroma/web/templates/o_auth/o_auth/register.html.eex +++ b/lib/pleroma/web/templates/o_auth/o_auth/register.html.eex @@ -12,11 +12,11 @@
<%= label f, :nickname, "Nickname" %> - <%= text_input f, :nickname, value: @nickname %> + <%= text_input f, :nickname, value: @nickname, autocomplete: "username" %>
<%= label f, :email, "Email" %> - <%= text_input f, :email, value: @email %> + <%= text_input f, :email, value: @email, autocomplete: "email" %>
<%= submit "Proceed as new user", name: "op", value: "register" %> @@ -25,11 +25,11 @@
<%= label f, :name, "Name or email" %> - <%= text_input f, :name %> + <%= text_input f, :name, autocomplete: "username" %>
<%= label f, :password, "Password" %> - <%= password_input f, :password %> + <%= password_input f, :password, autocomplete: "password" %>
<%= submit "Proceed as existing user", name: "op", value: "connect" %> diff --git a/lib/pleroma/web/templates/o_auth/o_auth/show.html.eex b/lib/pleroma/web/templates/o_auth/o_auth/show.html.eex index 181a9519a..d63da6c1d 100644 --- a/lib/pleroma/web/templates/o_auth/o_auth/show.html.eex +++ b/lib/pleroma/web/templates/o_auth/o_auth/show.html.eex @@ -35,7 +35,7 @@

Choose carefully! You won't be able to change this later. You will be able to change your display name, though.

<%= label f, :nickname, "Pleroma Handle" %> - <%= text_input f, :nickname, placeholder: "lain" %> + <%= text_input f, :nickname, placeholder: "lain", autocomplete: "username" %>
<%= hidden_input f, :name, value: @params["name"] %> <%= hidden_input f, :password, value: @params["password"] %> diff --git a/lib/pleroma/web/templates/twitter_api/remote_follow/follow_login.html.eex b/lib/pleroma/web/templates/twitter_api/remote_follow/follow_login.html.eex index a8026fa9d..bc5fb28e3 100644 --- a/lib/pleroma/web/templates/twitter_api/remote_follow/follow_login.html.eex +++ b/lib/pleroma/web/templates/twitter_api/remote_follow/follow_login.html.eex @@ -5,9 +5,9 @@

<%= @followee.nickname %>

<%= form_for @conn, Routes.remote_follow_path(@conn, :do_follow), [as: "authorization"], fn f -> %> -<%= text_input f, :name, placeholder: "Username", required: true %> +<%= text_input f, :name, placeholder: "Username", required: true, autocomplete: "username" %>
-<%= password_input f, :password, placeholder: "Password", required: true %> +<%= password_input f, :password, placeholder: "Password", required: true, autocomplete: "password" %>
<%= hidden_input f, :id, value: @followee.id %> <%= submit "Authorize" %> -- cgit v1.2.3 From f5d4ef50b4d86c33af924c44cfcb2d72202476d9 Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Sat, 22 Jan 2022 12:13:09 -0600 Subject: FilterTest: tag erratic test --- test/pleroma/web/mastodon_api/controllers/filter_controller_test.exs | 1 + 1 file changed, 1 insertion(+) diff --git a/test/pleroma/web/mastodon_api/controllers/filter_controller_test.exs b/test/pleroma/web/mastodon_api/controllers/filter_controller_test.exs index 98ab9e717..4f71b40d1 100644 --- a/test/pleroma/web/mastodon_api/controllers/filter_controller_test.exs +++ b/test/pleroma/web/mastodon_api/controllers/filter_controller_test.exs @@ -49,6 +49,7 @@ test "a common filter", %{conn: conn, user: user} do assert filter.hide == true end + @tag :erratic test "a filter with expires_in", %{conn: conn, user: user} do in_seconds = 600 -- cgit v1.2.3 From 7a015b1fe2f177bcc4ea5941c454936d3792e935 Mon Sep 17 00:00:00 2001 From: NEETzsche Date: Sat, 22 Jan 2022 12:07:54 -0700 Subject: Make test less erratic by adding five second tolerance --- .../web/mastodon_api/controllers/filter_controller_test.exs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/test/pleroma/web/mastodon_api/controllers/filter_controller_test.exs b/test/pleroma/web/mastodon_api/controllers/filter_controller_test.exs index 4f71b40d1..5ed1f34b7 100644 --- a/test/pleroma/web/mastodon_api/controllers/filter_controller_test.exs +++ b/test/pleroma/web/mastodon_api/controllers/filter_controller_test.exs @@ -49,7 +49,6 @@ test "a common filter", %{conn: conn, user: user} do assert filter.hide == true end - @tag :erratic test "a filter with expires_in", %{conn: conn, user: user} do in_seconds = 600 @@ -65,12 +64,13 @@ test "a filter with expires_in", %{conn: conn, user: user} do assert response["irreversible"] == false - expires_at = + expected_expiration = NaiveDateTime.utc_now() |> NaiveDateTime.add(in_seconds) - |> Pleroma.Web.CommonAPI.Utils.to_masto_date() - assert response["expires_at"] == expires_at + {:ok, actual_expiration} = NaiveDateTime.from_iso8601(response["expires_at"]) + + assert abs(NaiveDateTime.diff(expected_expiration, actual_expiration)) <= 5 filter = Filter.get(response["id"], user) -- cgit v1.2.3 From 66e8c6f90fa0ca9ab01cc58c865344694548e4d6 Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Sat, 22 Jan 2022 13:21:55 -0600 Subject: 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 ++-- lib/pleroma/web/activity_pub/views/user_view.ex | 8 +++--- .../web/api_spec/operations/account_operation.ex | 16 +++++------ lib/pleroma/web/api_spec/schemas/account.ex | 6 ++-- .../mastodon_api/controllers/account_controller.ex | 4 +-- lib/pleroma/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 +++---- .../web/mastodon_api/update_credentials_test.exs | 12 ++++---- .../web/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) -- cgit v1.2.3 From 98ce239ebc99444f7f418040acaba2e6cc990c29 Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Sat, 22 Jan 2022 13:28:00 -0600 Subject: 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." } ] }, -- cgit v1.2.3 From 0266bc3c96c30cfee929c55babdca679ca17a479 Mon Sep 17 00:00:00 2001 From: marcin mikołajczak Date: Sun, 23 Jan 2022 08:42:18 +0100 Subject: 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 ++++- lib/pleroma/web/activity_pub/views/user_view.ex | 2 +- .../web/api_spec/operations/account_operation.ex | 6 +++--- lib/pleroma/web/api_spec/schemas/account.ex | 2 +- .../mastodon_api/controllers/account_controller.ex | 2 +- lib/pleroma/web/mastodon_api/views/account_view.ex | 20 ++++++++------------ .../pleroma_api/controllers/account_controller.ex | 2 +- lib/pleroma/web/router.ex | 2 +- .../29220116183110_add_birth_date_to_users.exs | 2 +- .../web/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 -- cgit v1.2.3 From e3d394eef64f96210b144c40d3055d0662da7109 Mon Sep 17 00:00:00 2001 From: marcin mikołajczak Date: Sun, 23 Jan 2022 09:41:21 +0100 Subject: 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 + test/pleroma/web/activity_pub/activity_pub_test.exs | 20 ++++++++++++++++++++ .../web/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 -- cgit v1.2.3 From 61bae9a407ae9480e46e3a33d0b7d1f4c940b02c Mon Sep 17 00:00:00 2001 From: marcin mikołajczak Date: Sun, 23 Jan 2022 09:54:24 +0100 Subject: 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 --- .../repo/migrations/20220116183110_add_birthday_to_users.exs | 12 ++++++++++++ .../migrations/29220116183110_add_birth_date_to_users.exs | 10 ---------- 2 files changed, 12 insertions(+), 10 deletions(-) create mode 100644 priv/repo/migrations/20220116183110_add_birthday_to_users.exs delete mode 100644 priv/repo/migrations/29220116183110_add_birth_date_to_users.exs diff --git a/priv/repo/migrations/20220116183110_add_birthday_to_users.exs b/priv/repo/migrations/20220116183110_add_birthday_to_users.exs new file mode 100644 index 000000000..0b22ecc69 --- /dev/null +++ b/priv/repo/migrations/20220116183110_add_birthday_to_users.exs @@ -0,0 +1,12 @@ +defmodule Pleroma.Repo.Migrations.AddBirthdayToUsers do + use Ecto.Migration + + def change do + alter table(:users) 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 diff --git a/priv/repo/migrations/29220116183110_add_birth_date_to_users.exs b/priv/repo/migrations/29220116183110_add_birth_date_to_users.exs deleted file mode 100644 index 57fedaee2..000000000 --- a/priv/repo/migrations/29220116183110_add_birth_date_to_users.exs +++ /dev/null @@ -1,10 +0,0 @@ -defmodule Pleroma.Repo.Migrations.AddBirthDateToUsers do - use Ecto.Migration - - def change do - alter table(:users) do - add_if_not_exists(:birthday, :date) - add_if_not_exists(:show_birthday, :boolean, default: false, null: false) - end - end -end -- cgit v1.2.3 From 1dba3bc4dcae9edf3df73b5855b010c9ba451fce Mon Sep 17 00:00:00 2001 From: marcin mikołajczak Date: Sun, 23 Jan 2022 12:29:01 +0100 Subject: Preserve order of mentioned users 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 | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/pleroma/user.ex b/lib/pleroma/user.ex index bf5b6508b..ef5a02249 100644 --- a/lib/pleroma/user.ex +++ b/lib/pleroma/user.ex @@ -2279,6 +2279,7 @@ def ensure_keys_present(%User{} = user) do def get_ap_ids_by_nicknames(nicknames) do from(u in User, where: u.nickname in ^nicknames, + order_by: fragment("array_position(?, ?)", ^nicknames, u.nickname), select: u.ap_id ) |> Repo.all() -- cgit v1.2.3 From 75c4fefb1c3a8e4ce21661cbd634c608365354ba Mon Sep 17 00:00:00 2001 From: marcin mikołajczak Date: Sun, 23 Jan 2022 12:50:34 +0100 Subject: Add a test MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: marcin mikołajczak --- test/pleroma/user_test.exs | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/test/pleroma/user_test.exs b/test/pleroma/user_test.exs index 0345a9290..a5a9021f6 100644 --- a/test/pleroma/user_test.exs +++ b/test/pleroma/user_test.exs @@ -2084,6 +2084,17 @@ test "it returns a list of AP ids for a given set of nicknames" do assert user.ap_id in ap_ids assert user_two.ap_id in ap_ids end + + test "it returns a list of AP ids in the same order" do + user = insert(:user) + user_two = insert(:user) + user_three = insert(:user) + + ap_ids = + User.get_ap_ids_by_nicknames([user.nickname, user_three.nickname, user_two.nickname]) + + assert [user.ap_id, user_three.ap_id, user_two.ap_id] == ap_ids + end end describe "sync followers count" do -- cgit v1.2.3 From 53de3a9d4425505da0a516ca3b5d42cf5bc30a97 Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Mon, 24 Jan 2022 18:54:43 +0000 Subject: Revert "Merge branch 'log-slow-queries' into 'develop'" This reverts merge request !3553 --- config/config.exs | 9 ------ lib/pleroma/telemetry/logger.ex | 70 ++--------------------------------------- 2 files changed, 2 insertions(+), 77 deletions(-) diff --git a/config/config.exs b/config/config.exs index 1385ce5de..8ac650f6a 100644 --- a/config/config.exs +++ b/config/config.exs @@ -149,8 +149,6 @@ ] # Configures Elixir's Logger -config :logger, truncate: 65536 - config :logger, :console, level: :debug, format: "\n$time $metadata[$level] $message\n", @@ -857,13 +855,6 @@ {Pleroma.Web.ActivityPub.MRF.MediaProxyWarmingPolicy, [max_running: 5, max_waiting: 5]} ] -config :pleroma, :telemetry, - slow_queries_logging: [ - enabled: false, - min_duration: 500_000, - exclude_sources: [nil, "oban_jobs"] - ] - # Import environment specific config. This must remain at the bottom # of this file so it overrides the configuration defined above. import_config "#{Mix.env()}.exs" diff --git a/lib/pleroma/telemetry/logger.ex b/lib/pleroma/telemetry/logger.ex index 35e245237..10165c1b2 100644 --- a/lib/pleroma/telemetry/logger.ex +++ b/lib/pleroma/telemetry/logger.ex @@ -12,16 +12,10 @@ defmodule Pleroma.Telemetry.Logger do [:pleroma, :connection_pool, :reclaim, :stop], [:pleroma, :connection_pool, :provision_failure], [:pleroma, :connection_pool, :client, :dead], - [:pleroma, :connection_pool, :client, :add], - [:pleroma, :repo, :query] + [:pleroma, :connection_pool, :client, :add] ] def attach do - :telemetry.attach_many( - "pleroma-logger", - @events, - &Pleroma.Telemetry.Logger.handle_event/4, - [] - ) + :telemetry.attach_many("pleroma-logger", @events, &handle_event/4, []) end # Passing anonymous functions instead of strings to logger is intentional, @@ -93,64 +87,4 @@ def handle_event( end def handle_event([:pleroma, :connection_pool, :client, :add], _, _, _), do: :ok - - def handle_event( - [:pleroma, :repo, :query] = _name, - %{query_time: query_time} = measurements, - %{source: source} = metadata, - config - ) do - logging_config = Pleroma.Config.get([:telemetry, :slow_queries_logging], []) - - if logging_config[:enabled] && - logging_config[:min_duration] && - query_time > logging_config[:min_duration] and - (is_nil(logging_config[:exclude_sources]) or - source not in logging_config[:exclude_sources]) do - log_slow_query(measurements, metadata, config) - else - :ok - end - end - - defp log_slow_query( - %{query_time: query_time} = _measurements, - %{source: _source, query: query, params: query_params, repo: repo} = _metadata, - _config - ) do - sql_explain = - with {:ok, %{rows: explain_result_rows}} <- - repo.query("EXPLAIN " <> query, query_params, log: false) do - Enum.map_join(explain_result_rows, "\n", & &1) - end - - {:current_stacktrace, stacktrace} = Process.info(self(), :current_stacktrace) - - pleroma_stacktrace = - Enum.filter(stacktrace, fn - {__MODULE__, _, _, _} -> - false - - {mod, _, _, _} -> - mod - |> to_string() - |> String.starts_with?("Elixir.Pleroma.") - end) - - Logger.warn(fn -> - """ - Slow query! - - Total time: #{round(query_time / 1_000)} ms - - #{query} - - #{inspect(query_params, limit: :infinity)} - - #{sql_explain} - - #{Exception.format_stacktrace(pleroma_stacktrace)} - """ - end) - end end -- cgit v1.2.3 From 1bbc701a3ababc4c6e856ad24f8826da0a0ba1a3 Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Mon, 24 Jan 2022 19:11:14 -0600 Subject: ForceMentionsInContent: use `to` instead of `tag` --- lib/pleroma/web/activity_pub/mrf/force_mentions_in_content.ex | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/lib/pleroma/web/activity_pub/mrf/force_mentions_in_content.ex b/lib/pleroma/web/activity_pub/mrf/force_mentions_in_content.ex index 522ae2f60..8d3885d7b 100644 --- a/lib/pleroma/web/activity_pub/mrf/force_mentions_in_content.ex +++ b/lib/pleroma/web/activity_pub/mrf/force_mentions_in_content.ex @@ -35,15 +35,12 @@ defp extract_mention_uris_from_content(content) do end @impl true - def filter(%{"type" => "Create", "object" => %{"type" => "Note", "tag" => tag}} = object) do + def filter(%{"type" => "Create", "object" => %{"type" => "Note", "to" => to}} = object) do # image-only posts from pleroma apparently reach this MRF without the content field content = object["object"]["content"] || "" mention_users = - tag - |> Enum.filter(fn tag -> tag["type"] == "Mention" end) - |> Enum.map(& &1["href"]) - |> Enum.reject(&is_nil/1) + to |> Enum.map(fn ap_id_or_uri -> case User.get_or_fetch_by_ap_id(ap_id_or_uri) do {:ok, user} -> {ap_id_or_uri, user} -- cgit v1.2.3 From d5644a52aa4f031c69a2938b333636660156c703 Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Mon, 24 Jan 2022 19:11:45 -0600 Subject: ForceMentionsInContent: wrap inline mentions with span tag --- lib/pleroma/web/activity_pub/mrf/force_mentions_in_content.ex | 2 +- priv/scrubbers/default.ex | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/pleroma/web/activity_pub/mrf/force_mentions_in_content.ex b/lib/pleroma/web/activity_pub/mrf/force_mentions_in_content.ex index 8d3885d7b..97ea67002 100644 --- a/lib/pleroma/web/activity_pub/mrf/force_mentions_in_content.ex +++ b/lib/pleroma/web/activity_pub/mrf/force_mentions_in_content.ex @@ -63,7 +63,7 @@ def filter(%{"type" => "Create", "object" => %{"type" => "Note", "to" => to}} = content = if added_mentions != "", - do: added_mentions <> " " <> content, + do: "#{added_mentions} " <> content, else: content {:ok, put_in(object["object"]["content"], content)} diff --git a/priv/scrubbers/default.ex b/priv/scrubbers/default.ex index 4694a92a5..f33a721a8 100644 --- a/priv/scrubbers/default.ex +++ b/priv/scrubbers/default.ex @@ -56,7 +56,7 @@ defmodule Pleroma.HTML.Scrubber.Default do Meta.allow_tag_with_these_attributes(:u, []) Meta.allow_tag_with_these_attributes(:ul, []) - Meta.allow_tag_with_this_attribute_values(:span, "class", ["h-card"]) + Meta.allow_tag_with_this_attribute_values(:span, "class", ["h-card", "recipients-inline"]) Meta.allow_tag_with_these_attributes(:span, []) Meta.allow_tag_with_this_attribute_values(:code, "class", ["inline"]) -- cgit v1.2.3 From c5a20c80c4ed8fdd4e7de18caf85efb56be1db6e Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Mon, 24 Jan 2022 19:44:44 -0600 Subject: ForceMentionsInContent: simplify finding users --- lib/pleroma/web/activity_pub/mrf/force_mentions_in_content.ex | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/lib/pleroma/web/activity_pub/mrf/force_mentions_in_content.ex b/lib/pleroma/web/activity_pub/mrf/force_mentions_in_content.ex index 97ea67002..a458bed31 100644 --- a/lib/pleroma/web/activity_pub/mrf/force_mentions_in_content.ex +++ b/lib/pleroma/web/activity_pub/mrf/force_mentions_in_content.ex @@ -41,13 +41,8 @@ def filter(%{"type" => "Create", "object" => %{"type" => "Note", "to" => to}} = mention_users = to - |> Enum.map(fn ap_id_or_uri -> - case User.get_or_fetch_by_ap_id(ap_id_or_uri) do - {:ok, user} -> {ap_id_or_uri, user} - _ -> {ap_id_or_uri, User.get_by_uri(ap_id_or_uri)} - end - end) - |> Enum.reject(fn {_, user} -> user == nil end) + |> Enum.map(& {&1, User.get_cached_by_ap_id(&1)}) + |> Enum.reject(fn {_, user} -> is_nil(user) end) |> Enum.into(%{}) explicitly_mentioned_uris = extract_mention_uris_from_content(content) -- cgit v1.2.3 From 267184b70e587a3e6301d3a01bd16fd465bc0f68 Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Mon, 24 Jan 2022 20:03:43 -0600 Subject: ForceMentionsInContentTest: return mentions in a not terrible format --- .../activity_pub/mrf/force_mentions_in_content.ex | 6 ++-- .../mrf/force_mentions_in_content_test.exs | 38 +++++++++++++++------- 2 files changed, 29 insertions(+), 15 deletions(-) diff --git a/lib/pleroma/web/activity_pub/mrf/force_mentions_in_content.ex b/lib/pleroma/web/activity_pub/mrf/force_mentions_in_content.ex index a458bed31..1d750d4c2 100644 --- a/lib/pleroma/web/activity_pub/mrf/force_mentions_in_content.ex +++ b/lib/pleroma/web/activity_pub/mrf/force_mentions_in_content.ex @@ -41,7 +41,7 @@ def filter(%{"type" => "Create", "object" => %{"type" => "Note", "to" => to}} = mention_users = to - |> Enum.map(& {&1, User.get_cached_by_ap_id(&1)}) + |> Enum.map(&{&1, User.get_cached_by_ap_id(&1)}) |> Enum.reject(fn {_, user} -> is_nil(user) end) |> Enum.into(%{}) @@ -50,7 +50,7 @@ def filter(%{"type" => "Create", "object" => %{"type" => "Note", "to" => to}} = added_mentions = Enum.reduce(mention_users, "", fn {uri, user}, acc -> unless uri in explicitly_mentioned_uris do - acc <> Formatter.mention_from_user(user) + acc <> Formatter.mention_from_user(user, %{mentions_format: :compact}) <> " " else acc end @@ -58,7 +58,7 @@ def filter(%{"type" => "Create", "object" => %{"type" => "Note", "to" => to}} = content = if added_mentions != "", - do: "#{added_mentions} " <> content, + do: "#{added_mentions}" <> content, else: content {:ok, put_in(object["object"]["content"], content)} diff --git a/test/pleroma/web/activity_pub/mrf/force_mentions_in_content_test.exs b/test/pleroma/web/activity_pub/mrf/force_mentions_in_content_test.exs index 090bdc35e..3c40887c4 100644 --- a/test/pleroma/web/activity_pub/mrf/force_mentions_in_content_test.exs +++ b/test/pleroma/web/activity_pub/mrf/force_mentions_in_content_test.exs @@ -8,17 +8,29 @@ defmodule Pleroma.Web.ActivityPub.MRF.ForceMentionsInContentTest do use Pleroma.DataCase test "adds mentions to post content" do - users = %{ - "lain@lain.com" => "https://lain.com/users/lain", - "coolboymew@shitposter.club" => "https://shitposter.club/users/coolboymew", - "dielan@shitposter.club" => "https://shitposter.club/users/dielan", - "hakui@tuusin.misono-ya.info" => "https://tuusin.misono-ya.info/users/hakui", - "fence@xyzzy.link" => "https://xyzzy.link/users/fence" - } - - Enum.each(users, fn {nickname, ap_id} -> - insert(:user, ap_id: ap_id, nickname: nickname, local: false) - end) + [lain, coolboymew, dielan, hakui, fence] = [ + insert(:user, ap_id: "https://lain.com/users/lain", nickname: "lain@lain.com", local: false), + insert(:user, + ap_id: "https://shitposter.club/users/coolboymew", + nickname: "coolboymew@shitposter.club", + local: false + ), + insert(:user, + ap_id: "https://shitposter.club/users/dielan", + nickname: "dielan@shitposter.club", + local: false + ), + insert(:user, + ap_id: "https://tuusin.misono-ya.info/users/hakui", + nickname: "hakui@tuusin.misono-ya.info", + local: false + ), + insert(:user, + ap_id: "https://xyzzy.link/users/fence", + nickname: "fence@xyzzy.link", + local: false + ) + ] object = File.read!("test/fixtures/soapbox_no_mentions_in_content.json") |> Jason.decode!() @@ -29,6 +41,8 @@ test "adds mentions to post content" do } {:ok, %{"object" => %{"content" => filtered}}} = ForceMentionsInContent.filter(activity) - Enum.each(users, fn {nickname, _} -> assert filtered =~ nickname end) + + assert filtered == + "@lain @coolboymew @dielan @hakui @fence

Haha yeah, you can control who you reply to.

" end end -- cgit v1.2.3 From 249fe88d12345c1c93f1812871cd70424b9e9a0d Mon Sep 17 00:00:00 2001 From: marcin mikołajczak Date: Tue, 25 Jan 2022 10:45:32 +0100 Subject: 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 --- .../20220125104429_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 -- cgit v1.2.3 From c1ae35ff2c4620cb6958d0c6910c79ed17f62db9 Mon Sep 17 00:00:00 2001 From: marcin mikołajczak Date: Tue, 25 Jan 2022 17:04:41 +0100 Subject: Fix show_birthday MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: marcin mikołajczak --- lib/pleroma/web/mastodon_api/views/account_view.ex | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/pleroma/web/mastodon_api/views/account_view.ex b/lib/pleroma/web/mastodon_api/views/account_view.ex index e73d03f06..073fd2fb9 100644 --- a/lib/pleroma/web/mastodon_api/views/account_view.ex +++ b/lib/pleroma/web/mastodon_api/views/account_view.ex @@ -298,7 +298,6 @@ 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 } } |> maybe_put_role(user, opts[:for]) -- cgit v1.2.3 From ab12a05a432be549b02e3fd84e4e30680a89bbf9 Mon Sep 17 00:00:00 2001 From: marcin mikołajczak Date: Tue, 25 Jan 2022 17:10:40 +0100 Subject: AccountView: Add test for show_birthday MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: marcin mikołajczak --- lib/pleroma/web/mastodon_api/views/account_view.ex | 2 +- .../web/mastodon_api/views/account_view_test.exs | 36 ++++++++++++++++++++-- 2 files changed, 35 insertions(+), 3 deletions(-) diff --git a/lib/pleroma/web/mastodon_api/views/account_view.ex b/lib/pleroma/web/mastodon_api/views/account_view.ex index 073fd2fb9..1d78ced19 100644 --- a/lib/pleroma/web/mastodon_api/views/account_view.ex +++ b/lib/pleroma/web/mastodon_api/views/account_view.ex @@ -297,7 +297,7 @@ 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 } } |> maybe_put_role(user, opts[:for]) 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 329813994..9fc56f7f0 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,6 @@ 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", - birthday: nil, favicon: nil, is_confirmed: true, tags: [], @@ -182,7 +181,6 @@ test "Represent a Service(bot) account" do ap_id: user.ap_id, also_known_as: [], background_image: nil, - birthday: nil, favicon: nil, is_confirmed: true, tags: [], @@ -496,6 +494,40 @@ test "shows email only to the account owner" do end end + describe "hiding birthday" do + test "doesn't show birthday if hidden" do + user = + insert(:user, %{ + birthday: "2001-02-12", + show_birthday: false + }) + + other_user = insert(:user) + + user = User.get_cached_by_ap_id(user.ap_id) + + assert AccountView.render( + "show.json", + %{user: user, for: other_user} + )[:birthday] == nil + end + + test "shows hidden birthday to the account owner" do + user = + insert(:user, %{ + birthday: "2001-02-12", + show_birthday: false + }) + + user = User.get_cached_by_ap_id(user.ap_id) + + assert AccountView.render( + "show.json", + %{user: user, for: user} + )[:birthday] == nil + end + end + describe "follow requests counter" do test "shows zero when no follow requests are pending" do user = insert(:user) -- cgit v1.2.3 From 65b4d2ce8450098ee2eaa2057968a4f47aa536c1 Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Tue, 25 Jan 2022 10:42:34 -0600 Subject: ForceMentionsInContent: fix order of mentions --- .../activity_pub/mrf/force_mentions_in_content.ex | 38 ++++++++++++++++--- .../mrf/force_mentions_in_content_test.exs | 44 +++++++++++++++++++++- 2 files changed, 75 insertions(+), 7 deletions(-) diff --git a/lib/pleroma/web/activity_pub/mrf/force_mentions_in_content.ex b/lib/pleroma/web/activity_pub/mrf/force_mentions_in_content.ex index 1d750d4c2..c9b022cf8 100644 --- a/lib/pleroma/web/activity_pub/mrf/force_mentions_in_content.ex +++ b/lib/pleroma/web/activity_pub/mrf/force_mentions_in_content.ex @@ -4,6 +4,7 @@ defmodule Pleroma.Web.ActivityPub.MRF.ForceMentionsInContent do alias Pleroma.Formatter + alias Pleroma.Object alias Pleroma.User @behaviour Pleroma.Web.ActivityPub.MRF.Policy @@ -34,21 +35,48 @@ defp extract_mention_uris_from_content(content) do do_extract(tree, []) end + defp get_replied_to_user(%{"inReplyTo" => in_reply_to}) do + case Object.normalize(in_reply_to, fetch: false) do + %Object{data: %{"actor" => actor}} -> User.get_cached_by_ap_id(actor) + _ -> nil + end + end + + defp get_replied_to_user(_object), do: nil + + # Ensure the replied-to user is sorted to the left + defp sort_replied_user([%User{id: user_id} | _] = users, %User{id: user_id}), do: users + + defp sort_replied_user(users, %User{id: user_id} = user) do + if Enum.find(users, fn u -> u.id == user_id end) do + users = Enum.reject(users, fn u -> u.id == user_id end) + [user | users] + else + users + end + end + + defp sort_replied_user(users, _), do: users + @impl true - def filter(%{"type" => "Create", "object" => %{"type" => "Note", "to" => to}} = object) do + def filter(%{"type" => "Create", "object" => %{"type" => "Note", "to" => to}} = object) + when is_list(to) do # image-only posts from pleroma apparently reach this MRF without the content field content = object["object"]["content"] || "" + # Get the replied-to user for sorting + replied_to_user = get_replied_to_user(object["object"]) + mention_users = to - |> Enum.map(&{&1, User.get_cached_by_ap_id(&1)}) - |> Enum.reject(fn {_, user} -> is_nil(user) end) - |> Enum.into(%{}) + |> Enum.map(&User.get_cached_by_ap_id/1) + |> Enum.reject(&is_nil/1) + |> sort_replied_user(replied_to_user) explicitly_mentioned_uris = extract_mention_uris_from_content(content) added_mentions = - Enum.reduce(mention_users, "", fn {uri, user}, acc -> + Enum.reduce(mention_users, "", fn %User{ap_id: uri} = user, acc -> unless uri in explicitly_mentioned_uris do acc <> Formatter.mention_from_user(user, %{mentions_format: :compact}) <> " " else diff --git a/test/pleroma/web/activity_pub/mrf/force_mentions_in_content_test.exs b/test/pleroma/web/activity_pub/mrf/force_mentions_in_content_test.exs index 3c40887c4..3bc07be94 100644 --- a/test/pleroma/web/activity_pub/mrf/force_mentions_in_content_test.exs +++ b/test/pleroma/web/activity_pub/mrf/force_mentions_in_content_test.exs @@ -3,9 +3,15 @@ # SPDX-License-Identifier: AGPL-3.0-only defmodule Pleroma.Web.ActivityPub.MRF.ForceMentionsInContentTest do + use Pleroma.DataCase + require Pleroma.Constants + + alias Pleroma.Constants + alias Pleroma.Object alias Pleroma.Web.ActivityPub.MRF.ForceMentionsInContent + alias Pleroma.Web.CommonAPI + import Pleroma.Factory - use Pleroma.DataCase test "adds mentions to post content" do [lain, coolboymew, dielan, hakui, fence] = [ @@ -43,6 +49,40 @@ test "adds mentions to post content" do {:ok, %{"object" => %{"content" => filtered}}} = ForceMentionsInContent.filter(activity) assert filtered == - "@lain @coolboymew @dielan @hakui @fence

Haha yeah, you can control who you reply to.

" + "@dielan @coolboymew @fence @hakui @lain

Haha yeah, you can control who you reply to.

" + end + + test "the replied-to user is sorted to the left" do + [mario, luigi, wario] = [ + insert(:user, nickname: "mario"), + insert(:user, nickname: "luigi"), + insert(:user, nickname: "wario") + ] + + {:ok, post1} = CommonAPI.post(mario, %{status: "Letsa go!"}) + + {:ok, post2} = + CommonAPI.post(luigi, %{status: "Oh yaah", in_reply_to_id: post1.id, to: [mario.ap_id]}) + + activity = %{ + "type" => "Create", + "actor" => wario.ap_id, + "object" => %{ + "type" => "Note", + "actor" => wario.ap_id, + "content" => "WHA-HA!", + "to" => [ + mario.ap_id, + luigi.ap_id, + Constants.as_public() + ], + "inReplyTo" => Object.normalize(post2).data["id"] + } + } + + {:ok, %{"object" => %{"content" => filtered}}} = ForceMentionsInContent.filter(activity) + + assert filtered == + "@luigi @mario WHA-HA!" end end -- cgit v1.2.3 From 0604b0dd091682727d26567e6166fb89db842a9c Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Tue, 25 Jan 2022 12:33:26 -0600 Subject: ForceMentionsInContent: don't mention self --- .../activity_pub/mrf/force_mentions_in_content.ex | 14 +++++++++++++ .../mrf/force_mentions_in_content_test.exs | 24 ++++++++++++++++++++++ 2 files changed, 38 insertions(+) diff --git a/lib/pleroma/web/activity_pub/mrf/force_mentions_in_content.ex b/lib/pleroma/web/activity_pub/mrf/force_mentions_in_content.ex index c9b022cf8..71c240727 100644 --- a/lib/pleroma/web/activity_pub/mrf/force_mentions_in_content.ex +++ b/lib/pleroma/web/activity_pub/mrf/force_mentions_in_content.ex @@ -3,6 +3,8 @@ # SPDX-License-Identifier: AGPL-3.0-only defmodule Pleroma.Web.ActivityPub.MRF.ForceMentionsInContent do + require Pleroma.Constants + alias Pleroma.Formatter alias Pleroma.Object alias Pleroma.User @@ -58,6 +60,17 @@ defp sort_replied_user(users, %User{id: user_id} = user) do defp sort_replied_user(users, _), do: users + # Drop constants and the actor's own AP ID + defp clean_recipients(recipients, object) do + Enum.reject(recipients, fn ap_id -> + ap_id in [ + object["object"]["actor"], + Pleroma.Constants.as_public(), + Pleroma.Web.ActivityPub.Utils.as_local_public() + ] + end) + end + @impl true def filter(%{"type" => "Create", "object" => %{"type" => "Note", "to" => to}} = object) when is_list(to) do @@ -69,6 +82,7 @@ def filter(%{"type" => "Create", "object" => %{"type" => "Note", "to" => to}} = mention_users = to + |> clean_recipients(object) |> Enum.map(&User.get_cached_by_ap_id/1) |> Enum.reject(&is_nil/1) |> sort_replied_user(replied_to_user) diff --git a/test/pleroma/web/activity_pub/mrf/force_mentions_in_content_test.exs b/test/pleroma/web/activity_pub/mrf/force_mentions_in_content_test.exs index 3bc07be94..589e8fdfb 100644 --- a/test/pleroma/web/activity_pub/mrf/force_mentions_in_content_test.exs +++ b/test/pleroma/web/activity_pub/mrf/force_mentions_in_content_test.exs @@ -85,4 +85,28 @@ test "the replied-to user is sorted to the left" do assert filtered == "@luigi @mario WHA-HA!" end + + test "don't mention self" do + mario = insert(:user, nickname: "mario") + + {:ok, post} = CommonAPI.post(mario, %{status: "Mama mia"}) + + activity = %{ + "type" => "Create", + "actor" => mario.ap_id, + "object" => %{ + "type" => "Note", + "actor" => mario.ap_id, + "content" => "I'ma tired...", + "to" => [ + mario.ap_id, + Constants.as_public() + ], + "inReplyTo" => Object.normalize(post).data["id"] + } + } + + {:ok, %{"object" => %{"content" => filtered}}} = ForceMentionsInContent.filter(activity) + assert filtered == "I'ma tired..." + end end -- cgit v1.2.3 From a4de79ced01d02cddeec525ee829792cd5dfefee Mon Sep 17 00:00:00 2001 From: bot <44222-bot@users.noreply.git.pleroma.social> Date: Wed, 26 Jan 2022 22:53:02 +0000 Subject: ForceBotUnlistedPolicy: fix to stop unlisting my posts >:( --- lib/pleroma/web/activity_pub/mrf/force_bot_unlisted_policy.ex | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/pleroma/web/activity_pub/mrf/force_bot_unlisted_policy.ex b/lib/pleroma/web/activity_pub/mrf/force_bot_unlisted_policy.ex index 11871375e..b10b27f06 100644 --- a/lib/pleroma/web/activity_pub/mrf/force_bot_unlisted_policy.ex +++ b/lib/pleroma/web/activity_pub/mrf/force_bot_unlisted_policy.ex @@ -10,7 +10,7 @@ defmodule Pleroma.Web.ActivityPub.MRF.ForceBotUnlistedPolicy do require Pleroma.Constants defp check_by_actor_type(user), do: user.actor_type in ["Application", "Service"] - defp check_by_nickname(user), do: Regex.match?(~r/bot@|ebooks@/i, user.nickname) + defp check_by_nickname(user), do: Regex.match?(~r/.bot@|ebooks@/i, user.nickname) defp check_if_bot(user), do: check_by_actor_type(user) or check_by_nickname(user) -- cgit v1.2.3 From 27cb3d62738f6ea174994f72688497db49cef754 Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Wed, 26 Jan 2022 21:24:26 -0600 Subject: ForceMentionsInContent: don't apply it to top-level posts --- .../activity_pub/mrf/force_mentions_in_content.ex | 9 ++++++-- .../mrf/force_mentions_in_content_test.exs | 25 ++++++++++++++++++++++ 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/lib/pleroma/web/activity_pub/mrf/force_mentions_in_content.ex b/lib/pleroma/web/activity_pub/mrf/force_mentions_in_content.ex index 71c240727..715771d9d 100644 --- a/lib/pleroma/web/activity_pub/mrf/force_mentions_in_content.ex +++ b/lib/pleroma/web/activity_pub/mrf/force_mentions_in_content.ex @@ -72,8 +72,13 @@ defp clean_recipients(recipients, object) do end @impl true - def filter(%{"type" => "Create", "object" => %{"type" => "Note", "to" => to}} = object) - when is_list(to) do + def filter( + %{ + "type" => "Create", + "object" => %{"type" => "Note", "to" => to, "inReplyTo" => in_reply_to} + } = object + ) + when is_list(to) and is_binary(in_reply_to) do # image-only posts from pleroma apparently reach this MRF without the content field content = object["object"]["content"] || "" diff --git a/test/pleroma/web/activity_pub/mrf/force_mentions_in_content_test.exs b/test/pleroma/web/activity_pub/mrf/force_mentions_in_content_test.exs index 589e8fdfb..6bcf75a92 100644 --- a/test/pleroma/web/activity_pub/mrf/force_mentions_in_content_test.exs +++ b/test/pleroma/web/activity_pub/mrf/force_mentions_in_content_test.exs @@ -109,4 +109,29 @@ test "don't mention self" do {:ok, %{"object" => %{"content" => filtered}}} = ForceMentionsInContent.filter(activity) assert filtered == "I'ma tired..." end + + test "don't mention in top-level posts" do + mario = insert(:user, nickname: "mario") + luigi = insert(:user, nickname: "luigi") + + {:ok, post} = CommonAPI.post(mario, %{status: "Letsa go"}) + + activity = %{ + "type" => "Create", + "actor" => mario.ap_id, + "object" => %{ + "type" => "Note", + "actor" => mario.ap_id, + "content" => "Mama mia!", + "to" => [ + luigi.ap_id, + Constants.as_public() + ], + "quoteUrl" => Object.normalize(post).data["id"] + } + } + + {:ok, %{"object" => %{"content" => filtered}}} = ForceMentionsInContent.filter(activity) + assert filtered == "Mama mia!" + end end -- cgit v1.2.3 From 3bf257171f546ef84993b08851c01cf064309017 Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Thu, 27 Jan 2022 14:15:06 -0600 Subject: ForceMentionsInContent: improve display of Markdown posts --- .../activity_pub/mrf/force_mentions_in_content.ex | 19 +++++++++++--- .../mrf/force_mentions_in_content_test.exs | 29 +++++++++++++++++++++- 2 files changed, 44 insertions(+), 4 deletions(-) diff --git a/lib/pleroma/web/activity_pub/mrf/force_mentions_in_content.ex b/lib/pleroma/web/activity_pub/mrf/force_mentions_in_content.ex index 715771d9d..255910b2f 100644 --- a/lib/pleroma/web/activity_pub/mrf/force_mentions_in_content.ex +++ b/lib/pleroma/web/activity_pub/mrf/force_mentions_in_content.ex @@ -103,10 +103,23 @@ def filter( end end) - content = + recipients_inline = if added_mentions != "", - do: "#{added_mentions}" <> content, - else: content + do: "#{added_mentions}", + else: "" + + content = + cond do + # For Markdown posts, insert the mentions inside the first

tag + recipients_inline != "" && String.starts_with?(content, "

") -> + "

" <> recipients_inline <> String.trim_leading(content, "

") + + recipients_inline != "" -> + recipients_inline <> content + + true -> + content + end {:ok, put_in(object["object"]["content"], content)} end diff --git a/test/pleroma/web/activity_pub/mrf/force_mentions_in_content_test.exs b/test/pleroma/web/activity_pub/mrf/force_mentions_in_content_test.exs index 6bcf75a92..669ec5251 100644 --- a/test/pleroma/web/activity_pub/mrf/force_mentions_in_content_test.exs +++ b/test/pleroma/web/activity_pub/mrf/force_mentions_in_content_test.exs @@ -49,7 +49,7 @@ test "adds mentions to post content" do {:ok, %{"object" => %{"content" => filtered}}} = ForceMentionsInContent.filter(activity) assert filtered == - "@dielan @coolboymew @fence @hakui @lain

Haha yeah, you can control who you reply to.

" + "

@dielan @coolboymew @fence @hakui @lain Haha yeah, you can control who you reply to.

" end test "the replied-to user is sorted to the left" do @@ -134,4 +134,31 @@ test "don't mention in top-level posts" do {:ok, %{"object" => %{"content" => filtered}}} = ForceMentionsInContent.filter(activity) assert filtered == "Mama mia!" end + + test "with markdown formatting" do + mario = insert(:user, nickname: "mario") + luigi = insert(:user, nickname: "luigi") + + {:ok, post} = CommonAPI.post(luigi, %{status: "Mama mia"}) + + activity = %{ + "type" => "Create", + "actor" => mario.ap_id, + "object" => %{ + "type" => "Note", + "actor" => mario.ap_id, + "content" => "

I'ma tired...

", + "to" => [ + luigi.ap_id, + Constants.as_public() + ], + "inReplyTo" => Object.normalize(post).data["id"] + } + } + + {:ok, %{"object" => %{"content" => filtered}}} = ForceMentionsInContent.filter(activity) + + assert filtered == + "

@luigi I'ma tired...

" + end end -- cgit v1.2.3 From f8f2a1775470ac181d3b2417157d18a52433b27b Mon Sep 17 00:00:00 2001 From: marcin mikołajczak Date: Tue, 1 Feb 2022 11:28:08 +0100 Subject: Birthdays: Fix outgoing federation of birth dates 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/views/user_view.ex | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/pleroma/web/activity_pub/views/user_view.ex b/lib/pleroma/web/activity_pub/views/user_view.ex index 5ed08d7f6..d20d4591a 100644 --- a/lib/pleroma/web/activity_pub/views/user_view.ex +++ b/lib/pleroma/web/activity_pub/views/user_view.ex @@ -93,8 +93,8 @@ def render("user.json", %{user: user}) do end birthday = - if user.show_birthday, - do: user.birthday, + if user.show_birthday && user.birthday, + do: Date.to_iso8601(user.birthday), else: nil %{ -- cgit v1.2.3 From 61dfeca1ccbff9d1fa1f3c1f709df9352573fffd Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Wed, 2 Feb 2022 10:38:30 -0600 Subject: Test that a Note from Roadhouse validates --- test/fixtures/roadhouse-create-activity.json | 109 +++++++++++++++++++++ .../article_note_page_validator_test.exs | 11 +++ 2 files changed, 120 insertions(+) create mode 100644 test/fixtures/roadhouse-create-activity.json diff --git a/test/fixtures/roadhouse-create-activity.json b/test/fixtures/roadhouse-create-activity.json new file mode 100644 index 000000000..c082f84d7 --- /dev/null +++ b/test/fixtures/roadhouse-create-activity.json @@ -0,0 +1,109 @@ +{ + "@context":[ + "https://www.w3.org/ns/activitystreams", + "https://w3id.org/security/v1", + { + "zot":"https://macgirvin.com/apschema#", + "toot":"http://joinmastodon.org/ns#", + "ostatus":"http://ostatus.org#", + "schema":"http://schema.org#", + "litepub":"http://litepub.social/ns#", + "sm":"http://smithereen.software/ns#", + "conversation":"ostatus:conversation", + "manuallyApprovesFollowers":"as:manuallyApprovesFollowers", + "oauthRegistrationEndpoint":"litepub:oauthRegistrationEndpoint", + "sensitive":"as:sensitive", + "movedTo":"as:movedTo", + "copiedTo":"as:copiedTo", + "alsoKnownAs":"as:alsoKnownAs", + "EmojiReact":"as:EmojiReact", + "commentPolicy":"zot:commentPolicy", + "topicalCollection":"zot:topicalCollection", + "eventRepeat":"zot:eventRepeat", + "emojiReaction":"zot:emojiReaction", + "expires":"zot:expires", + "directMessage":"zot:directMessage", + "Category":"zot:Category", + "replyTo":"zot:replyTo", + "PropertyValue":"schema:PropertyValue", + "value":"schema:value", + "discoverable":"toot:discoverable", + "wall":"sm:wall", + "capabilities":"litepub:capabilities", + "acceptsJoins":"litepub:acceptsJoins" + } + ], + "type":"Create", + "id":"https://macgirvin.com/activity/ce9ce740-e1cb-4a75-aa4e-9e923555870d", + "published":"2022-02-02T04:41:46Z", + "context":"https://gleasonator.com/objects/102eb097-a18b-4cd5-abfc-f952efcb70bb", + "conversation":"https://gleasonator.com/objects/102eb097-a18b-4cd5-abfc-f952efcb70bb", + "actor":"https://macgirvin.com/channel/mike", + "replyTo":"https://macgirvin.com/channel/mike", + "url":"https://macgirvin.com/activity/ce9ce740-e1cb-4a75-aa4e-9e923555870d", + "object":{ + "type":"Note", + "id":"https://macgirvin.com/item/ce9ce740-e1cb-4a75-aa4e-9e923555870d", + "published":"2022-02-02T04:41:46Z", + "attributedTo":"https://macgirvin.com/channel/mike", + "inReplyTo":"https://gleasonator.com/objects/102eb097-a18b-4cd5-abfc-f952efcb70bb", + "context":"https://gleasonator.com/objects/102eb097-a18b-4cd5-abfc-f952efcb70bb", + "conversation":"https://gleasonator.com/objects/102eb097-a18b-4cd5-abfc-f952efcb70bb", + "content":"The Accepts should get through now. Now to figure out why the comments are failing.", + "source":{ + "content":"The Accepts should get through now. Now to figure out why the comments are failing.", + "mediaType":"text/x-multicode" + }, + "replyTo":"https://macgirvin.com/channel/mike", + "url":"https://macgirvin.com/item/ce9ce740-e1cb-4a75-aa4e-9e923555870d", + "tag":[ + { + "type":"Mention", + "href":"https://gleasonator.com/users/macgirvin", + "name":"@macgirvin@gleasonator.com" + }, + { + "type":"Mention", + "href":"https://gleasonator.com/users/alex", + "name":"@alex@gleasonator.com" + } + ], + "to":[ + "https://www.w3.org/ns/activitystreams#Public", + "https://gleasonator.com/users/alex", + "https://gleasonator.com/users/macgirvin" + ], + "cc":[ + "https://macgirvin.com/followers/mike", + "https://gleasonator.com/users/macgirvin/followers" + ] + }, + "tag":[ + { + "type":"Mention", + "href":"https://gleasonator.com/users/macgirvin", + "name":"@macgirvin@gleasonator.com" + }, + { + "type":"Mention", + "href":"https://gleasonator.com/users/alex", + "name":"@alex@gleasonator.com" + } + ], + "to":[ + "https://www.w3.org/ns/activitystreams#Public", + "https://gleasonator.com/users/alex", + "https://gleasonator.com/users/macgirvin" + ], + "cc":[ + "https://macgirvin.com/followers/mike", + "https://gleasonator.com/users/macgirvin/followers" + ], + "signature":{ + "type":"RsaSignature2017", + "nonce":"544080164a412f0592f8257094a870a0177276f4a04cc4410974f5e8fa277a14", + "creator":"https://macgirvin.com/channel/mike", + "created":"2022-02-02T04:41:46Z", + "signatureValue":"BkHEdRkki/DGHctiI6BWyQdn0i9ip+7rFiqqlLgotgQzwA8wzcmyvFIRm8Z+3OqrbzkNyQLCmx4qHvkqWyNrR0eSSJPaBryYvTLpLBz5F5PSpHOI5x4kRGVaI2S1Po+pUMWgchyyQ8ylqdLVHe0FnqId7vFVo9uj4jWydU5wPGlbb5nvnRMQGGyPqzCJ69lopMgCRCUNgbOz6hYVU0Mhqgi9BDjs1crbRLoGexz87tu7FeGEmtyfz8/SBGrDy+X+U3ahykwqd7ggcptsYVu5BY7BfREOLNZE8BwapUCg+QSw9PJv7dLJytdh0kUrzpuAPNbugx7y662FALmHZlxcIC1IVwbPOwEiosDm6wXsOiIyTvnNFcvDre/B1corB2yt5wmO3Cu5jINvp4+aBTheGIKMxBvpJXywpWe6C0VBqfNZSreJtDwp9lHd6D1+L4V6hUCOqiCcqmLT/GI5TOR+EhSpJ02TOkuu+/8hyIfO6ec3uK98y8suyidckbfM60jrbvZh2s/kF9WlDsE9K+Jlyd9Xx3mI5jU+4MXo1MDA57zYpsXw0S+v++rvnOw8CkQr/opVdIRSCG+UB3VRinpgjwW1UQcdAI+fiVaAwqr45MgnIEaQdk13skJUfUl06D9IxKxhu7yNW1tR621w/HN+358qVQWWBoj+1ZhCG3Thjug=" + } +} diff --git a/test/pleroma/web/activity_pub/object_validators/article_note_page_validator_test.exs b/test/pleroma/web/activity_pub/object_validators/article_note_page_validator_test.exs index 720c17d8d..150b26bea 100644 --- a/test/pleroma/web/activity_pub/object_validators/article_note_page_validator_test.exs +++ b/test/pleroma/web/activity_pub/object_validators/article_note_page_validator_test.exs @@ -32,4 +32,15 @@ test "a basic note validates", %{note: note} do %{valid?: true} = ArticleNotePageValidator.cast_and_validate(note) end end + + test "a Note from Roadhouse validates" do + insert(:user, ap_id: "https://macgirvin.com/channel/mike") + + %{"object" => note} = + "test/fixtures/roadhouse-create-activity.json" + |> File.read!() + |> Jason.decode!() + + %{valid?: true} = ArticleNotePageValidator.cast_and_validate(note) + end end -- cgit v1.2.3 From 5a4e3aa715fdd588b7fefc79dc15535497513218 Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Wed, 2 Feb 2022 11:02:23 -0600 Subject: Test that a Create/Note from Roadhouse validates --- .../create_generic_validator_test.exs | 27 ++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 test/pleroma/web/activity_pub/object_validators/create_generic_validator_test.exs diff --git a/test/pleroma/web/activity_pub/object_validators/create_generic_validator_test.exs b/test/pleroma/web/activity_pub/object_validators/create_generic_validator_test.exs new file mode 100644 index 000000000..94c9f6800 --- /dev/null +++ b/test/pleroma/web/activity_pub/object_validators/create_generic_validator_test.exs @@ -0,0 +1,27 @@ +# Pleroma: A lightweight social networking server +# Copyright © 2017-2021 Pleroma Authors +# SPDX-License-Identifier: AGPL-3.0-only + +defmodule Pleroma.Web.ActivityPub.ObjectValidators.CreateGenericValidatorTest do + use Pleroma.DataCase, async: true + + alias Pleroma.Web.ActivityPub.ObjectValidator + alias Pleroma.Web.ActivityPub.ObjectValidators.CreateGenericValidator + + import Pleroma.Factory + + test "a Create/Note from Roadhouse validates" do + insert(:user, ap_id: "https://macgirvin.com/channel/mike") + + note_activity = + "test/fixtures/roadhouse-create-activity.json" + |> File.read!() + |> Jason.decode!() + + # Build metadata + {:ok, object_data} = ObjectValidator.cast_and_apply(note_activity["object"]) + meta = [object_data: ObjectValidator.stringify_keys(object_data)] + + %{valid?: true} = CreateGenericValidator.cast_and_validate(note_activity, meta) + end +end -- cgit v1.2.3 From 2d779763055ebfe02ffb1ffced5c10dbc0baa63b Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Wed, 2 Feb 2022 12:19:35 -0600 Subject: Add tests for mismatched context in replies --- .../tesla_mock/gleasonator-AG3RzWfwEKKrY63qj2.json | 35 ++++++++++++++++++ .../tesla_mock/macgirvin@gleasonator.com.json | 41 ++++++++++++++++++++++ .../create_generic_validator_test.exs | 32 +++++++++++++++++ .../web/activity_pub/transmogrifier_test.exs | 11 ++++++ test/support/http_request_mock.ex | 30 ++++++++++++++++ 5 files changed, 149 insertions(+) create mode 100644 test/fixtures/tesla_mock/gleasonator-AG3RzWfwEKKrY63qj2.json create mode 100644 test/fixtures/tesla_mock/macgirvin@gleasonator.com.json diff --git a/test/fixtures/tesla_mock/gleasonator-AG3RzWfwEKKrY63qj2.json b/test/fixtures/tesla_mock/gleasonator-AG3RzWfwEKKrY63qj2.json new file mode 100644 index 000000000..62d7bb9ae --- /dev/null +++ b/test/fixtures/tesla_mock/gleasonator-AG3RzWfwEKKrY63qj2.json @@ -0,0 +1,35 @@ +{ + "@context": [ + "https://www.w3.org/ns/activitystreams", + "https://gleasonator.com/schemas/litepub-0.1.jsonld", + { + "@language": "und" + } + ], + "actor": "https://gleasonator.com/users/macgirvin", + "attachment": [], + "attributedTo": "https://gleasonator.com/users/macgirvin", + "cc": [ + "https://gleasonator.com/users/macgirvin/followers" + ], + "content": "@alex Any idea why my posts are failing? I sent an Accept/Follow from macgirvin.com at 2022-02-02T04:06:01Z and it vanished into space. As do all my comments to you.

2022-02-02T04:06:01Z:LOG_INFO:d5c4aa7f6a:Queue.php:435:deliver: deliver: queue post returned 200 from https://gleasonator.com/users/macgirvin/inbox

It's OK if I'm blocked, but if that's the case, I shouldn't be able to send a follow to that address should I?", + "context": "https://gleasonator.com/contexts/b7f01f94-bc92-4d89-a085-0ffc211b0e42", + "conversation": "https://gleasonator.com/contexts/b7f01f94-bc92-4d89-a085-0ffc211b0e42", + "id": "https://gleasonator.com/objects/102eb097-a18b-4cd5-abfc-f952efcb70bb", + "published": "2022-02-02T04:14:10.965833Z", + "sensitive": false, + "source": "@alex Any idea why my posts are failing? I sent an Accept/Follow from macgirvin.com at 2022-02-02T04:06:01Z and it vanished into space. As do all my comments to you. \n\n2022-02-02T04:06:01Z:LOG_INFO:d5c4aa7f6a:Queue.php:435:deliver: deliver: queue post returned 200 from https://gleasonator.com/users/macgirvin/inbox\n\nIt's OK if I'm blocked, but if that's the case, I shouldn't be able to send a follow to that address should I?", + "summary": "", + "tag": [ + { + "href": "https://gleasonator.com/users/alex", + "name": "@alex", + "type": "Mention" + } + ], + "to": [ + "https://www.w3.org/ns/activitystreams#Public", + "https://gleasonator.com/users/alex" + ], + "type": "Note" +} diff --git a/test/fixtures/tesla_mock/macgirvin@gleasonator.com.json b/test/fixtures/tesla_mock/macgirvin@gleasonator.com.json new file mode 100644 index 000000000..9d7d47d40 --- /dev/null +++ b/test/fixtures/tesla_mock/macgirvin@gleasonator.com.json @@ -0,0 +1,41 @@ +{ + "@context": [ + "https://www.w3.org/ns/activitystreams", + "https://gleasonator.com/schemas/litepub-0.1.jsonld", + { + "@language": "und" + } + ], + "alsoKnownAs": [], + "attachment": [], + "capabilities": { + "acceptsChatMessages": true + }, + "discoverable": false, + "endpoints": { + "oauthAuthorizationEndpoint": "https://gleasonator.com/oauth/authorize", + "oauthRegistrationEndpoint": "https://gleasonator.com/api/v1/apps", + "oauthTokenEndpoint": "https://gleasonator.com/oauth/token", + "sharedInbox": "https://gleasonator.com/inbox", + "uploadMedia": "https://gleasonator.com/api/ap/upload_media" + }, + "featured": "https://gleasonator.com/users/macgirvin/collections/featured", + "followers": "https://gleasonator.com/users/macgirvin/followers", + "following": "https://gleasonator.com/users/macgirvin/following", + "id": "https://gleasonator.com/users/macgirvin", + "inbox": "https://gleasonator.com/users/macgirvin/inbox", + "manuallyApprovesFollowers": false, + "name": "macgirvin", + "outbox": "https://gleasonator.com/users/macgirvin/outbox", + "preferredUsername": "macgirvin", + "publicKey": { + "id": "https://gleasonator.com/users/macgirvin#main-key", + "owner": "https://gleasonator.com/users/macgirvin", + "publicKeyPem": "-----BEGIN PUBLIC KEY-----\nMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA0AUMgsQu87tLwoJjZfZu\nsj7NV0qt/THdK8aw4Ha2GZfNkwrep99AZ8gmCI+rr+N5vyETAARzG5/Qzr5bNTUx\nsc1fxFemhhi6sxzAv4qZ5AgvWQ4YPFWizSp5ZY1jpPHLOeF2IftMf8CwVI82PtD0\n7m7T6iUYA4vfvMp9LxVrzQA+CAtpsQxAejTGCt37yM9T2mEWqfmJQQHRIQ4brKBL\nI82sNbzk1cbTwCfH7vRNS/l1ND+vaUGGkDKtpRl56BLmt4picYL0avc+8oO7ebpc\n/zUoS8OOi+mpEzjv7TBrSirYEIGvIh3TKHWSPrpHpQTqj9xBQBy+AxXTWahQEO2M\ndQIDAQAB\n-----END PUBLIC KEY-----\n\n" + }, + "summary": "", + "tag": [], + "type": "Person", + "url": "https://gleasonator.com/users/macgirvin", + "vcard:bday": null +} diff --git a/test/pleroma/web/activity_pub/object_validators/create_generic_validator_test.exs b/test/pleroma/web/activity_pub/object_validators/create_generic_validator_test.exs index 94c9f6800..c3e6854e4 100644 --- a/test/pleroma/web/activity_pub/object_validators/create_generic_validator_test.exs +++ b/test/pleroma/web/activity_pub/object_validators/create_generic_validator_test.exs @@ -7,6 +7,7 @@ defmodule Pleroma.Web.ActivityPub.ObjectValidators.CreateGenericValidatorTest do alias Pleroma.Web.ActivityPub.ObjectValidator alias Pleroma.Web.ActivityPub.ObjectValidators.CreateGenericValidator + alias Pleroma.Web.ActivityPub.Utils import Pleroma.Factory @@ -24,4 +25,35 @@ test "a Create/Note from Roadhouse validates" do %{valid?: true} = CreateGenericValidator.cast_and_validate(note_activity, meta) end + + test "a Create/Note with mismatched context is invalid" do + user = insert(:user) + + note = %{ + "id" => Utils.generate_object_id(), + "type" => "Note", + "actor" => user.ap_id, + "to" => [user.follower_address], + "cc" => [], + "content" => "Hello world", + "context" => Utils.generate_context_id() + } + + note_activity = %{ + "id" => Utils.generate_activity_id(), + "type" => "Create", + "actor" => note["actor"], + "to" => note["to"], + "cc" => note["cc"], + "object" => note, + "published" => DateTime.utc_now() |> DateTime.to_iso8601(), + "context" => Utils.generate_context_id() + } + + # Build metadata + {:ok, object_data} = ObjectValidator.cast_and_apply(note_activity["object"]) + meta = [object_data: ObjectValidator.stringify_keys(object_data)] + + %{valid?: false} = CreateGenericValidator.cast_and_validate(note_activity, meta) + end end diff --git a/test/pleroma/web/activity_pub/transmogrifier_test.exs b/test/pleroma/web/activity_pub/transmogrifier_test.exs index 06daf6a9f..41a30be0b 100644 --- a/test/pleroma/web/activity_pub/transmogrifier_test.exs +++ b/test/pleroma/web/activity_pub/transmogrifier_test.exs @@ -107,6 +107,17 @@ test "it accepts Move activities" do assert activity.data["target"] == new_user.ap_id assert activity.data["type"] == "Move" end + + test "a reply with mismatched context is rejected" do + insert(:user, ap_id: "https://macgirvin.com/channel/mike") + + note_activity = + "test/fixtures/roadhouse-create-activity.json" + |> File.read!() + |> Jason.decode!() + + assert {:error, _} = Transmogrifier.handle_incoming(note_activity) + end end describe "prepare outgoing" do diff --git a/test/support/http_request_mock.ex b/test/support/http_request_mock.ex index 94900dc14..b99d70cda 100644 --- a/test/support/http_request_mock.ex +++ b/test/support/http_request_mock.ex @@ -1311,6 +1311,36 @@ def get("https://patch.cx/objects/a399c28e-c821-4820-bc3e-4afeb044c16f", _, _, _ }} end + def get("https://gleasonator.com/objects/102eb097-a18b-4cd5-abfc-f952efcb70bb", _, _, _) do + {:ok, + %Tesla.Env{ + status: 200, + body: File.read!("test/fixtures/tesla_mock/gleasonator-AG3RzWfwEKKrY63qj2.json"), + headers: activitypub_object_headers() + }} + end + + def get("https://gleasonator.com/users/macgirvin", _, _, _) do + {:ok, + %Tesla.Env{ + status: 200, + body: File.read!("test/fixtures/tesla_mock/macgirvin@gleasonator.com.json"), + headers: activitypub_object_headers() + }} + end + + def get("https://gleasonator.com/users/macgirvin/collections/featured", _, _, _) do + {:ok, + %Tesla.Env{ + status: 200, + body: + File.read!("test/fixtures/users_mock/masto_featured.json") + |> String.replace("{{domain}}", "gleasonator.com") + |> String.replace("{{nickname}}", "macgirvin"), + headers: activitypub_object_headers() + }} + end + def get(url, query, body, headers) do {:error, "Mock response not implemented for GET #{inspect(url)}, #{query}, #{inspect(body)}, #{inspect(headers)}"} -- cgit v1.2.3 From bd81af731eb6c391f647e66847d01afdf374c673 Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Wed, 2 Feb 2022 12:29:53 -0600 Subject: Tag erratic test --- test/pleroma/web/admin_api/controllers/report_controller_test.exs | 1 + 1 file changed, 1 insertion(+) diff --git a/test/pleroma/web/admin_api/controllers/report_controller_test.exs b/test/pleroma/web/admin_api/controllers/report_controller_test.exs index 99cc7bbd0..802a8c05f 100644 --- a/test/pleroma/web/admin_api/controllers/report_controller_test.exs +++ b/test/pleroma/web/admin_api/controllers/report_controller_test.exs @@ -355,6 +355,7 @@ test "it creates report note", %{admin_id: admin_id, report_id: report_id} do } = note end + @tag :erratic test "it returns reports with notes", %{conn: conn, admin: admin} do conn = get(conn, "/api/pleroma/admin/reports") -- cgit v1.2.3 From d1cc9e4eaf7d361d9033aa4d823a8b9682d184ea Mon Sep 17 00:00:00 2001 From: "Haelwenn (lanodan) Monnier" Date: Mon, 24 Jan 2022 19:03:10 +0100 Subject: Fix tests matching on "warn" Moving it to "warning" would break tests on 1.12.x --- test/pleroma/reverse_proxy_test.exs | 2 +- test/pleroma/web/common_api/utils_test.exs | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/test/pleroma/reverse_proxy_test.exs b/test/pleroma/reverse_proxy_test.exs index a4dd8e99a..49ddf251d 100644 --- a/test/pleroma/reverse_proxy_test.exs +++ b/test/pleroma/reverse_proxy_test.exs @@ -130,7 +130,7 @@ test "max_body_length returns error if streaming body more than that option", %{ assert capture_log(fn -> ReverseProxy.call(conn, "/stream-bytes/50", max_body_length: 30) end) =~ - "[warn] Elixir.Pleroma.ReverseProxy request to /stream-bytes/50 failed while reading/chunking: :body_too_large" + "Elixir.Pleroma.ReverseProxy request to /stream-bytes/50 failed while reading/chunking: :body_too_large" end end diff --git a/test/pleroma/web/common_api/utils_test.exs b/test/pleroma/web/common_api/utils_test.exs index fc01f820a..7063b2503 100644 --- a/test/pleroma/web/common_api/utils_test.exs +++ b/test/pleroma/web/common_api/utils_test.exs @@ -309,7 +309,7 @@ test "when date is a binary in wrong format" do assert capture_log(fn -> assert Utils.date_to_asctime(date) == expected - end) =~ "[warn] Date #{date} in wrong format, must be ISO 8601" + end) =~ "Date #{date} in wrong format, must be ISO 8601" end test "when date is a Unix timestamp" do @@ -319,7 +319,7 @@ test "when date is a Unix timestamp" do assert capture_log(fn -> assert Utils.date_to_asctime(date) == expected - end) =~ "[warn] Date #{date} in wrong format, must be ISO 8601" + end) =~ "Date #{date} in wrong format, must be ISO 8601" end test "when date is nil" do @@ -327,13 +327,13 @@ test "when date is nil" do assert capture_log(fn -> assert Utils.date_to_asctime(nil) == expected - end) =~ "[warn] Date in wrong format, must be ISO 8601" + end) =~ "Date in wrong format, must be ISO 8601" end test "when date is a random string" do assert capture_log(fn -> assert Utils.date_to_asctime("foo") == "" - end) =~ "[warn] Date foo in wrong format, must be ISO 8601" + end) =~ "Date foo in wrong format, must be ISO 8601" end end -- cgit v1.2.3 From 7c044a1841810d0b9ceef5dc9813c14d49699ee4 Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Wed, 2 Feb 2022 13:10:39 -0600 Subject: FilterControllerTest: tag erratic test --- test/pleroma/web/mastodon_api/controllers/filter_controller_test.exs | 1 + 1 file changed, 1 insertion(+) diff --git a/test/pleroma/web/mastodon_api/controllers/filter_controller_test.exs b/test/pleroma/web/mastodon_api/controllers/filter_controller_test.exs index 5ed1f34b7..d8fc2400b 100644 --- a/test/pleroma/web/mastodon_api/controllers/filter_controller_test.exs +++ b/test/pleroma/web/mastodon_api/controllers/filter_controller_test.exs @@ -177,6 +177,7 @@ test "common" do assert response["whole_word"] == true end + @tag :erratic test "with adding expires_at", %{conn: conn, user: user} do filter = insert(:filter, user: user) in_seconds = 600 -- cgit v1.2.3