From ccc3ac241f5b7c88b36efe60a4f9e5d791d2d49a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?marcin=20miko=C5=82ajczak?= Date: Sat, 6 Apr 2024 10:54:59 +0200 Subject: [PATCH] Add hint to rules MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: marcin mikołajczak --- docs/development/API/admin_api.md | 5 +++- lib/pleroma/rule.ex | 3 ++- lib/pleroma/web/admin_api/views/rule_view.ex | 3 ++- .../operations/admin/report_operation.ex | 3 ++- .../operations/admin/rule_operation.ex | 9 +++++--- .../api_spec/operations/instance_operation.ex | 3 ++- .../web/mastodon_api/views/instance_view.ex | 3 ++- .../20240406000000_add_hint_to_rules.exs | 13 +++++++++++ .../controllers/instance_controller_test.exs | 23 ++++++++++++++----- 9 files changed, 50 insertions(+), 15 deletions(-) create mode 100644 priv/repo/migrations/20240406000000_add_hint_to_rules.exs diff --git a/docs/development/API/admin_api.md b/docs/development/API/admin_api.md index 7bbed16d6..5b373b8e1 100644 --- a/docs/development/API/admin_api.md +++ b/docs/development/API/admin_api.md @@ -1764,7 +1764,8 @@ Note that this differs from the Mastodon API variant: Mastodon API only returns { "id": "1", "priority": 1, - "text": "There are no rules" + "text": "There are no rules", + "hint": null } ] ``` @@ -1775,6 +1776,7 @@ Note that this differs from the Mastodon API variant: Mastodon API only returns - Params: - `text`: string, required, rule content + - `hint`: string, optional, rule description - `priority`: integer, optional, rule ordering priority - Response: JSON, a single rule @@ -1785,6 +1787,7 @@ Note that this differs from the Mastodon API variant: Mastodon API only returns - Params: - `text`: string, optional, rule content + - `hint`: string, optional, rule description - `priority`: integer, optional, rule ordering priority - Response: JSON, a single rule diff --git a/lib/pleroma/rule.ex b/lib/pleroma/rule.ex index c8e3470c7..3ba413214 100644 --- a/lib/pleroma/rule.ex +++ b/lib/pleroma/rule.ex @@ -14,13 +14,14 @@ defmodule Pleroma.Rule do schema "rules" do field(:priority, :integer, default: 0) field(:text, :string) + field(:hint, :string) timestamps() end def changeset(%Rule{} = rule, params \\ %{}) do rule - |> cast(params, [:priority, :text]) + |> cast(params, [:priority, :text, :hint]) |> validate_required([:text]) end diff --git a/lib/pleroma/web/admin_api/views/rule_view.ex b/lib/pleroma/web/admin_api/views/rule_view.ex index abfdd593f..606443f05 100644 --- a/lib/pleroma/web/admin_api/views/rule_view.ex +++ b/lib/pleroma/web/admin_api/views/rule_view.ex @@ -15,7 +15,8 @@ def render("show.json", %{rule: rule} = _opts) do %{ id: to_string(rule.id), priority: rule.priority, - text: rule.text + text: rule.text, + hint: rule.hint } end end diff --git a/lib/pleroma/web/api_spec/operations/admin/report_operation.ex b/lib/pleroma/web/api_spec/operations/admin/report_operation.ex index 971231f4d..25a604beb 100644 --- a/lib/pleroma/web/api_spec/operations/admin/report_operation.ex +++ b/lib/pleroma/web/api_spec/operations/admin/report_operation.ex @@ -182,7 +182,8 @@ defp report do type: :object, properties: %{ id: %Schema{type: :string}, - text: %Schema{type: :string} + text: %Schema{type: :string}, + hint: %Schema{type: :string, nullable: true} } } } diff --git a/lib/pleroma/web/api_spec/operations/admin/rule_operation.ex b/lib/pleroma/web/api_spec/operations/admin/rule_operation.ex index 2360880e4..c3a3ecc7c 100644 --- a/lib/pleroma/web/api_spec/operations/admin/rule_operation.ex +++ b/lib/pleroma/web/api_spec/operations/admin/rule_operation.ex @@ -84,7 +84,8 @@ defp create_request do required: [:text], properties: %{ priority: %Schema{type: :integer}, - text: %Schema{type: :string} + text: %Schema{type: :string}, + hint: %Schema{type: :string} } } end @@ -94,7 +95,8 @@ defp update_request do type: :object, properties: %{ priority: %Schema{type: :integer}, - text: %Schema{type: :string} + text: %Schema{type: :string}, + hint: %Schema{type: :string} } } end @@ -105,7 +107,8 @@ defp rule do properties: %{ id: %Schema{type: :string}, priority: %Schema{type: :integer}, - text: %Schema{type: :string} + text: %Schema{type: :string}, + hint: %Schema{type: :string, nullable: true} } } end diff --git a/lib/pleroma/web/api_spec/operations/instance_operation.ex b/lib/pleroma/web/api_spec/operations/instance_operation.ex index faf79c2ea..4cd457ae7 100644 --- a/lib/pleroma/web/api_spec/operations/instance_operation.ex +++ b/lib/pleroma/web/api_spec/operations/instance_operation.ex @@ -364,7 +364,8 @@ defp array_of_rules do type: :object, properties: %{ id: %Schema{type: :string}, - text: %Schema{type: :string} + text: %Schema{type: :string}, + hint: %Schema{type: :string} } } } diff --git a/lib/pleroma/web/mastodon_api/views/instance_view.ex b/lib/pleroma/web/mastodon_api/views/instance_view.ex index 6a3d445c5..d00f1bd9e 100644 --- a/lib/pleroma/web/mastodon_api/views/instance_view.ex +++ b/lib/pleroma/web/mastodon_api/views/instance_view.ex @@ -85,7 +85,8 @@ def render("rules.json", _) do def render("rule.json", %{rule: rule}) do %{ id: to_string(rule.id), - text: rule.text + text: rule.text, + hint: rule.hint || "" } end diff --git a/priv/repo/migrations/20240406000000_add_hint_to_rules.exs b/priv/repo/migrations/20240406000000_add_hint_to_rules.exs new file mode 100644 index 000000000..273290560 --- /dev/null +++ b/priv/repo/migrations/20240406000000_add_hint_to_rules.exs @@ -0,0 +1,13 @@ +# Pleroma: A lightweight social networking server +# Copyright © 2017-2024 Pleroma Authors +# SPDX-License-Identifier: AGPL-3.0-only + +defmodule Pleroma.Repo.Migrations.AddHintToRules do + use Ecto.Migration + + def change do + alter table(:rules) do + add_if_not_exists(:hint, :text) + end + end +end diff --git a/test/pleroma/web/mastodon_api/controllers/instance_controller_test.exs b/test/pleroma/web/mastodon_api/controllers/instance_controller_test.exs index bfc672ff7..373a84303 100644 --- a/test/pleroma/web/mastodon_api/controllers/instance_controller_test.exs +++ b/test/pleroma/web/mastodon_api/controllers/instance_controller_test.exs @@ -129,16 +129,27 @@ test "get instance information v2", %{conn: conn} do end test "get instance rules", %{conn: conn} do - Rule.create(%{text: "Example rule"}) - Rule.create(%{text: "Second rule"}) - Rule.create(%{text: "Third rule"}) + Rule.create(%{text: "Example rule", hint: "Rule description", priority: 1}) + Rule.create(%{text: "Third rule", priority: 2}) + Rule.create(%{text: "Second rule", priority: 1}) conn = get(conn, "/api/v1/instance") assert result = json_response_and_validate_schema(conn, 200) - rules = result["rules"] - - assert length(rules) == 3 + assert [ + %{ + "text" => "Example rule", + "hint" => "Rule description" + }, + %{ + "text" => "Second rule", + "hint" => "" + }, + %{ + "text" => "Third rule", + "hint" => "" + } + ] = result["rules"] end end