Format announcements into html

This commit is contained in:
Tusooa Zhu 2022-03-08 23:00:51 -05:00
parent 0e0a1758f7
commit ebcda5265b
No known key found for this signature in database
GPG Key ID: 7B467EDE43A08224
6 changed files with 65 additions and 4 deletions

View File

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

View File

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

View File

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

View File

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

View File

@ -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(<a.+?https://pleroma.social)
assert announcement.data["content"] == raw
end
test "it formats mentions" do
user = insert(:user)
raw = "something on @#{user.nickname} ."
announcement = insert(:announcement, %{data: %{"content" => raw}})
assert announcement.rendered["content"] =~ ~r(<a.+?#{user.nickname})
assert announcement.data["content"] == raw
end
test "it formats tags" do
raw = "something on #mew ."
announcement = insert(:announcement, %{data: %{"content" => raw}})
assert announcement.rendered["content"] =~ ~r(<a.+?#mew)
assert announcement.data["content"] == raw
end
end
end

View File

@ -637,5 +637,6 @@ defmodule Pleroma.Factory do
data: Map.merge(%{"content" => "test announcement", "all_day" => false}, data)
}
|> Map.merge(params)
|> Pleroma.Announcement.add_rendered_properties()
end
end