philomena/lib/philomena_web/views/post_view.ex

37 lines
779 B
Elixir
Raw Normal View History

2019-10-06 23:31:48 +02:00
defmodule PhilomenaWeb.PostView do
2019-11-27 02:45:57 +01:00
alias Philomena.Attribution
2019-12-26 06:39:57 +01:00
alias FastTextile.Parser
2019-11-27 02:45:57 +01:00
2019-10-06 23:31:48 +02:00
use PhilomenaWeb, :view
2019-11-27 02:45:57 +01:00
def textile_safe_author(object) do
author_name = author_name(object)
2019-12-26 06:39:57 +01:00
at_author_name = "@" <> author_name
2019-11-27 02:45:57 +01:00
2019-12-26 06:39:57 +01:00
Parser.parse(%{image_transform: & &1}, at_author_name)
|> Parser.flatten()
2019-11-27 02:45:57 +01:00
|> case do
2019-12-26 06:39:57 +01:00
^at_author_name ->
2019-11-27 02:45:57 +01:00
author_name
_ ->
# Cover *all* possibilities.
literal =
author_name
|> String.replace("==]", "==]==][==")
"[==#{literal}==]"
end
end
defp author_name(object) do
2019-11-27 05:52:49 +01:00
cond do
Attribution.anonymous?(object) || !object.user ->
PhilomenaWeb.UserAttributionView.anonymous_name(object)
true ->
object.user.name
2019-11-27 02:45:57 +01:00
end
end
2019-10-06 23:31:48 +02:00
end