From 4cab6a9db052988b2075c69f9cb6e2423eb16bb6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?marcin=20miko=C5=82ajczak?= Date: Thu, 23 May 2024 16:54:57 +0200 Subject: [PATCH] Fix tests and regressions MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: marcin mikołajczak --- lib/pleroma/notification.ex | 16 +++++++++----- .../object_validators/common_fixes.ex | 2 ++ .../web/activity_pub/transmogrifier.ex | 3 ++- lib/pleroma/web/plugs/http_signature_plug.ex | 2 +- lib/pleroma/webhook.ex | 21 +++++++++++++------ .../web/activity_pub/transmogrifier_test.exs | 12 ----------- .../controllers/webhook_controller_test.exs | 4 +++- 7 files changed, 34 insertions(+), 26 deletions(-) diff --git a/lib/pleroma/notification.ex b/lib/pleroma/notification.ex index 0badfb9534..f50675fc4c 100644 --- a/lib/pleroma/notification.ex +++ b/lib/pleroma/notification.ex @@ -292,16 +292,22 @@ def set_read_up_to(%{id: user_id} = user, id) do |> Repo.transaction() Streamer.stream(["user", "user:notification"], marker) + + {:ok, %{marker: marker}} end @spec read_one(User.t(), String.t()) :: {:ok, Notification.t()} | {:error, Ecto.Changeset.t()} | nil def read_one(%User{} = user, notification_id) do - with {:ok, %Notification{} = notification} <- get(user, notification_id) do - Multi.new() - |> Multi.update(:update, changeset(notification, %{seen: true})) - |> Marker.multi_set_last_read_id(user, "notifications") - |> Repo.transaction() + with {:ok, %Notification{} = notification} <- get(user, notification_id), + {:ok, %{marker: marker}} <- + Multi.new() + |> Multi.update(:update, changeset(notification, %{seen: true})) + |> Marker.multi_set_last_read_id(user, "notifications") + |> Repo.transaction() do + Streamer.stream(["user", "user:notification"], marker) + + {:ok, %{marker: marker}} end end diff --git a/lib/pleroma/web/activity_pub/object_validators/common_fixes.ex b/lib/pleroma/web/activity_pub/object_validators/common_fixes.ex index 798868cbc0..3e21882468 100644 --- a/lib/pleroma/web/activity_pub/object_validators/common_fixes.ex +++ b/lib/pleroma/web/activity_pub/object_validators/common_fixes.ex @@ -188,6 +188,8 @@ defp get_language_from_content(%{"content" => content}) do defp get_language_from_content(_), do: nil + def maybe_add_content_map(%{"contentMap" => %{}} = object), do: object + def maybe_add_content_map(%{"language" => language, "content" => content} = object) when not_empty_string(language) do Map.put(object, "contentMap", Map.put(%{}, language, content)) diff --git a/lib/pleroma/web/activity_pub/transmogrifier.ex b/lib/pleroma/web/activity_pub/transmogrifier.ex index c5871d2fc9..54f929ab90 100644 --- a/lib/pleroma/web/activity_pub/transmogrifier.ex +++ b/lib/pleroma/web/activity_pub/transmogrifier.ex @@ -215,7 +215,7 @@ def fix_context(object) do def fix_attachments(%{"attachment" => attachment} = object) when is_list(attachment) do attachments = attachment - |> Enum.filter(fn data -> Map.has_key?(data, "url") end) + |> Enum.filter(fn data -> Map.has_key?(data, "url") or Map.has_key?(data, "href") end) |> Enum.map(fn data -> url = cond do @@ -342,6 +342,7 @@ def fix_tag(%{"tag" => %{} = tag} = object) do def fix_tag(object), do: object + # prefer content over contentMap def fix_content_map(%{"content" => content} = object) when not_empty_string(content), do: object # content map usually only has one language so this will do for now. diff --git a/lib/pleroma/web/plugs/http_signature_plug.ex b/lib/pleroma/web/plugs/http_signature_plug.ex index ed193225a4..f72e5fda9f 100644 --- a/lib/pleroma/web/plugs/http_signature_plug.ex +++ b/lib/pleroma/web/plugs/http_signature_plug.ex @@ -142,7 +142,7 @@ defp maybe_filter_requests(conn) do end defp rejected_domains do - Config.get([:instance, :rejected_instances]) + Config.get([:instance, :rejected_instances], []) |> Pleroma.Web.ActivityPub.MRF.instance_list_from_tuples() |> Pleroma.Web.ActivityPub.MRF.subdomains_regex() end diff --git a/lib/pleroma/webhook.ex b/lib/pleroma/webhook.ex index b9d9c5d296..67dd5d333c 100644 --- a/lib/pleroma/webhook.ex +++ b/lib/pleroma/webhook.ex @@ -31,35 +31,44 @@ def get_by_type(type) do |> Repo.all() end - def changeset(%__MODULE__{} = webhook, params) do + def changeset(%__MODULE__{} = webhook, params, opts \\ []) do webhook |> cast(params, [:url, :events, :enabled]) + |> maybe_update_internal(params, opts) |> validate_required([:url, :events]) |> unique_constraint(:url) |> strip_events() |> put_secret() end - def update_changeset(%__MODULE__{} = webhook, params \\ %{}) do + def update_changeset(%__MODULE__{} = webhook, params \\ %{}, opts \\ []) do webhook |> cast(params, [:url, :events, :enabled]) + |> maybe_update_internal(params, opts) |> unique_constraint(:url) |> strip_events() end - def create(params) do + defp maybe_update_internal(webhook, params, update_internal: true) do + webhook + |> cast(params, [:internal]) + end + + defp maybe_update_internal(webhook, _params, _opts), do: webhook + + def create(params, opts \\ []) do {:ok, webhook} = %__MODULE__{} - |> changeset(params) + |> changeset(params, opts) |> Repo.insert() webhook end - def update(%__MODULE__{} = webhook, params) do + def update(%__MODULE__{} = webhook, params, opts \\ []) do {:ok, webhook} = webhook - |> update_changeset(params) + |> update_changeset(params, opts) |> Repo.update() webhook diff --git a/test/pleroma/web/activity_pub/transmogrifier_test.exs b/test/pleroma/web/activity_pub/transmogrifier_test.exs index 241ed6033a..f7857c2284 100644 --- a/test/pleroma/web/activity_pub/transmogrifier_test.exs +++ b/test/pleroma/web/activity_pub/transmogrifier_test.exs @@ -349,18 +349,6 @@ test "custom emoji urls are URI encoded" do assert url == "http://localhost:4001/emoji/dino%20walking.gif" end - test "it adds contentMap if language is specified" do - user = insert(:user) - - {:ok, activity} = CommonAPI.post(user, %{status: "тест", language: "uk"}) - - {:ok, prepared} = Transmogrifier.prepare_outgoing(activity.data) - - assert prepared["object"]["contentMap"] == %{ - "uk" => "тест" - } - end - test "it prepares a quote post" do user = insert(:user) diff --git a/test/pleroma/web/admin_api/controllers/webhook_controller_test.exs b/test/pleroma/web/admin_api/controllers/webhook_controller_test.exs index 0cd00e7534..24d56346d8 100644 --- a/test/pleroma/web/admin_api/controllers/webhook_controller_test.exs +++ b/test/pleroma/web/admin_api/controllers/webhook_controller_test.exs @@ -67,7 +67,9 @@ test "edits a webhook", %{conn: conn} do test "can't edit an internal webhook", %{conn: conn} do %{id: id} = - Webhook.create(%{url: "https://example.com/webhook1", events: [], internal: true}) + Webhook.create(%{url: "https://example.com/webhook1", events: [], internal: true}, + update_internal: true + ) conn |> put_req_header("content-type", "application/json")