Format announcements into html
This commit is contained in:
parent
0e0a1758f7
commit
ebcda5265b
6 changed files with 65 additions and 4 deletions
|
@ -18,13 +18,14 @@ defmodule Pleroma.Announcement do
|
||||||
field(:data, :map)
|
field(:data, :map)
|
||||||
field(:starts_at, :naive_datetime)
|
field(:starts_at, :naive_datetime)
|
||||||
field(:ends_at, :naive_datetime)
|
field(:ends_at, :naive_datetime)
|
||||||
|
field(:rendered, :map)
|
||||||
|
|
||||||
timestamps()
|
timestamps()
|
||||||
end
|
end
|
||||||
|
|
||||||
def change(struct, params \\ %{}) do
|
def change(struct, params \\ %{}) do
|
||||||
struct
|
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])
|
|> validate_required([:data])
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -42,6 +43,21 @@ defmodule Pleroma.Announcement do
|
||||||
|
|
||||||
params
|
params
|
||||||
|> Map.merge(%{data: merged_data})
|
|> 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
|
end
|
||||||
|
|
||||||
def add(params) do
|
def add(params) do
|
||||||
|
@ -100,9 +116,18 @@ defmodule Pleroma.Announcement do
|
||||||
%{}
|
%{}
|
||||||
end
|
end
|
||||||
|
|
||||||
|
admin_extra_params =
|
||||||
|
case Keyword.fetch(opts, :admin) do
|
||||||
|
{:ok, true} ->
|
||||||
|
%{pleroma: %{raw_content: announcement.data["content"]}}
|
||||||
|
|
||||||
|
_ ->
|
||||||
|
%{}
|
||||||
|
end
|
||||||
|
|
||||||
base = %{
|
base = %{
|
||||||
id: announcement.id,
|
id: announcement.id,
|
||||||
content: announcement.data["content"],
|
content: announcement.rendered["content"],
|
||||||
starts_at: announcement.starts_at,
|
starts_at: announcement.starts_at,
|
||||||
ends_at: announcement.ends_at,
|
ends_at: announcement.ends_at,
|
||||||
all_day: announcement.data["all_day"],
|
all_day: announcement.data["all_day"],
|
||||||
|
@ -117,6 +142,7 @@ defmodule Pleroma.Announcement do
|
||||||
|
|
||||||
base
|
base
|
||||||
|> Map.merge(extra_params)
|
|> Map.merge(extra_params)
|
||||||
|
|> Map.merge(admin_extra_params)
|
||||||
end
|
end
|
||||||
|
|
||||||
# "visible" means:
|
# "visible" means:
|
||||||
|
|
|
@ -10,6 +10,6 @@ defmodule Pleroma.Web.AdminAPI.AnnouncementView do
|
||||||
end
|
end
|
||||||
|
|
||||||
def render("show.json", %{announcement: announcement}) do
|
def render("show.json", %{announcement: announcement}) do
|
||||||
Pleroma.Announcement.render_json(announcement)
|
Pleroma.Announcement.render_json(announcement, admin: true)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -33,7 +33,13 @@ defmodule Pleroma.Web.ApiSpec.Schemas.Announcement do
|
||||||
statuses: %Schema{type: :array},
|
statuses: %Schema{type: :array},
|
||||||
tags: %Schema{type: :array},
|
tags: %Schema{type: :array},
|
||||||
emojis: %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
|
end
|
||||||
|
|
|
@ -7,6 +7,7 @@ defmodule Pleroma.Repo.Migrations.CreateAnnouncements do
|
||||||
add(:data, :map)
|
add(:data, :map)
|
||||||
add(:starts_at, :naive_datetime)
|
add(:starts_at, :naive_datetime)
|
||||||
add(:ends_at, :naive_datetime)
|
add(:ends_at, :naive_datetime)
|
||||||
|
add(:rendered, :map)
|
||||||
|
|
||||||
timestamps()
|
timestamps()
|
||||||
end
|
end
|
||||||
|
|
|
@ -68,4 +68,31 @@ defmodule Pleroma.AnnouncementTest do
|
||||||
assert [] = Announcement.list_all_visible_when(time)
|
assert [] = Announcement.list_all_visible_when(time)
|
||||||
end
|
end
|
||||||
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
|
end
|
||||||
|
|
|
@ -637,5 +637,6 @@ defmodule Pleroma.Factory do
|
||||||
data: Map.merge(%{"content" => "test announcement", "all_day" => false}, data)
|
data: Map.merge(%{"content" => "test announcement", "all_day" => false}, data)
|
||||||
}
|
}
|
||||||
|> Map.merge(params)
|
|> Map.merge(params)
|
||||||
|
|> Pleroma.Announcement.add_rendered_properties()
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue