From ebcda5265b9c82be26eae65b5ab39629a525c3fa Mon Sep 17 00:00:00 2001 From: Tusooa Zhu Date: Tue, 8 Mar 2022 23:00:51 -0500 Subject: [PATCH] Format announcements into html --- lib/pleroma/announcement.ex | 30 +++++++++++++++++-- .../web/admin_api/views/announcement_view.ex | 2 +- .../web/api_spec/schemas/announcement.ex | 8 ++++- .../20220308012601_create_announcements.exs | 1 + test/pleroma/announcement_test.exs | 27 +++++++++++++++++ test/support/factory.ex | 1 + 6 files changed, 65 insertions(+), 4 deletions(-) diff --git a/lib/pleroma/announcement.ex b/lib/pleroma/announcement.ex index 6c11eff7e..ad372629d 100644 --- a/lib/pleroma/announcement.ex +++ b/lib/pleroma/announcement.ex @@ -18,13 +18,14 @@ defmodule Pleroma.Announcement do field(:data, :map) field(:starts_at, :naive_datetime) field(:ends_at, :naive_datetime) + field(:rendered, :map) timestamps() end def change(struct, params \\ %{}) do struct - |> cast(validate_params(struct, params), [:data, :starts_at, :ends_at]) + |> cast(validate_params(struct, params), [:data, :starts_at, :ends_at, :rendered]) |> validate_required([:data]) end @@ -42,6 +43,21 @@ defmodule Pleroma.Announcement do params |> Map.merge(%{data: merged_data}) + |> add_rendered_properties() + end + + def add_rendered_properties(params) do + {content_html, _, _} = + Pleroma.Web.CommonAPI.Utils.format_input(params.data["content"], "text/plain", + mentions_format: :full + ) + + rendered = %{ + "content" => content_html + } + + params + |> Map.put(:rendered, rendered) end def add(params) do @@ -100,9 +116,18 @@ defmodule Pleroma.Announcement do %{} end + admin_extra_params = + case Keyword.fetch(opts, :admin) do + {:ok, true} -> + %{pleroma: %{raw_content: announcement.data["content"]}} + + _ -> + %{} + end + base = %{ id: announcement.id, - content: announcement.data["content"], + content: announcement.rendered["content"], starts_at: announcement.starts_at, ends_at: announcement.ends_at, all_day: announcement.data["all_day"], @@ -117,6 +142,7 @@ defmodule Pleroma.Announcement do base |> Map.merge(extra_params) + |> Map.merge(admin_extra_params) end # "visible" means: diff --git a/lib/pleroma/web/admin_api/views/announcement_view.ex b/lib/pleroma/web/admin_api/views/announcement_view.ex index 8fbdc338a..a35bd60cf 100644 --- a/lib/pleroma/web/admin_api/views/announcement_view.ex +++ b/lib/pleroma/web/admin_api/views/announcement_view.ex @@ -10,6 +10,6 @@ defmodule Pleroma.Web.AdminAPI.AnnouncementView do end def render("show.json", %{announcement: announcement}) do - Pleroma.Announcement.render_json(announcement) + Pleroma.Announcement.render_json(announcement, admin: true) end end diff --git a/lib/pleroma/web/api_spec/schemas/announcement.ex b/lib/pleroma/web/api_spec/schemas/announcement.ex index 094fd7c68..67d129ef6 100644 --- a/lib/pleroma/web/api_spec/schemas/announcement.ex +++ b/lib/pleroma/web/api_spec/schemas/announcement.ex @@ -33,7 +33,13 @@ defmodule Pleroma.Web.ApiSpec.Schemas.Announcement do statuses: %Schema{type: :array}, tags: %Schema{type: :array}, emojis: %Schema{type: :array}, - reactions: %Schema{type: :array} + reactions: %Schema{type: :array}, + pleroma: %Schema{ + type: :object, + properties: %{ + raw_content: %Schema{type: :string} + } + } } }) end diff --git a/priv/repo/migrations/20220308012601_create_announcements.exs b/priv/repo/migrations/20220308012601_create_announcements.exs index 9f273fda9..01c9ce041 100644 --- a/priv/repo/migrations/20220308012601_create_announcements.exs +++ b/priv/repo/migrations/20220308012601_create_announcements.exs @@ -7,6 +7,7 @@ defmodule Pleroma.Repo.Migrations.CreateAnnouncements do add(:data, :map) add(:starts_at, :naive_datetime) add(:ends_at, :naive_datetime) + add(:rendered, :map) timestamps() end diff --git a/test/pleroma/announcement_test.exs b/test/pleroma/announcement_test.exs index aa00f804b..a007c3718 100644 --- a/test/pleroma/announcement_test.exs +++ b/test/pleroma/announcement_test.exs @@ -68,4 +68,31 @@ defmodule Pleroma.AnnouncementTest do assert [] = Announcement.list_all_visible_when(time) end end + + describe "announcements formatting" do + test "it formats links" do + raw = "something on https://pleroma.social ." + announcement = insert(:announcement, %{data: %{"content" => raw}}) + + assert announcement.rendered["content"] =~ ~r( raw}}) + + assert announcement.rendered["content"] =~ ~r( raw}}) + + assert announcement.rendered["content"] =~ ~r( "test announcement", "all_day" => false}, data) } |> Map.merge(params) + |> Pleroma.Announcement.add_rendered_properties() end end