2023-08-15 11:09:32 +00:00
|
|
|
defmodule BdfrBrowser.RenderUtils do
|
2023-10-04 09:04:29 +00:00
|
|
|
def selftext(txt) when is_binary(txt) do
|
2023-08-15 18:52:53 +00:00
|
|
|
Earmark.as_html!(txt)
|
|
|
|
end
|
|
|
|
|
2023-10-04 09:04:29 +00:00
|
|
|
def comment(cmt) when is_binary(cmt) do
|
2023-08-15 11:56:44 +00:00
|
|
|
Earmark.as_html!(cmt)
|
|
|
|
end
|
|
|
|
|
2023-10-04 09:04:29 +00:00
|
|
|
def message(msg) when is_binary(msg) do
|
2023-08-16 14:49:48 +00:00
|
|
|
img_replacement =
|
|
|
|
"<p class=\"text-center\"><img src=\"/chat_media/\\1\" class=\"img-fluid\" loading=\"lazy\" /></p>"
|
2023-08-15 11:09:32 +00:00
|
|
|
|
|
|
|
msg
|
2023-08-16 18:14:18 +00:00
|
|
|
|> maybe_insert_image(img_replacement)
|
2023-08-15 11:09:32 +00:00
|
|
|
|> String.replace(~r/https:\/\/i\.redd\.it\/(.+)/, img_replacement)
|
|
|
|
|> Earmark.as_html!()
|
|
|
|
end
|
2023-08-16 18:14:18 +00:00
|
|
|
|
2023-10-04 09:04:29 +00:00
|
|
|
def link_to_user(name) when is_binary(name) and name in ~w([deleted] DELETED), do: name
|
|
|
|
def link_to_user(name) when is_binary(name), do: "<a href=\"/user/#{name}\">#{name}</a>"
|
2023-08-29 18:33:38 +00:00
|
|
|
|
2023-10-18 11:29:55 +00:00
|
|
|
def format_date(date, :long) do
|
|
|
|
Calendar.strftime(date, "%d-%m-%Y, %H:%MZ")
|
|
|
|
end
|
|
|
|
|
|
|
|
def format_date(date, :short) do
|
|
|
|
Calendar.strftime(date, "%d-%m, %H:%MZ")
|
|
|
|
end
|
|
|
|
|
|
|
|
def post_type_icon(url) do
|
|
|
|
cond do
|
|
|
|
String.contains?(url, ["i.redd.it", "i.imgur.com", "i.postimg.cc", "twimg.com"]) ->
|
|
|
|
"<i class=\"bi bi-image\"></i>"
|
|
|
|
|
|
|
|
String.contains?(url, ["reddit.com/gallery", "imgur.com/a"]) ->
|
|
|
|
"<i class=\"bi bi-images\"></i>"
|
|
|
|
|
|
|
|
String.contains?(url, ["v.redd.it", "redgifs.com", "gfycat.com"]) ->
|
|
|
|
"<i class=\"bi bi-camera-reels\"></i>"
|
|
|
|
|
|
|
|
true ->
|
|
|
|
""
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-08-16 18:14:18 +00:00
|
|
|
# Helper
|
|
|
|
|
2023-10-04 09:04:29 +00:00
|
|
|
defp maybe_insert_image(<<"mxc://reddit.com/", filename::binary>> = msg, replacement) when is_binary(msg) do
|
2023-08-16 18:14:18 +00:00
|
|
|
chat_directory = Application.fetch_env!(:bdfr_browser, :chat_directory)
|
|
|
|
img_directory = Path.join([chat_directory, "images"])
|
|
|
|
imgs = Path.wildcard("#{img_directory}/#{filename}.*")
|
|
|
|
|
|
|
|
if Enum.empty?(imgs) do
|
|
|
|
msg
|
|
|
|
else
|
|
|
|
img = hd(imgs)
|
|
|
|
String.replace(replacement, "\\1", Path.basename(img))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-10-04 09:04:29 +00:00
|
|
|
defp maybe_insert_image(msg, _replacement) when is_binary(msg), do: msg
|
2023-08-15 11:09:32 +00:00
|
|
|
end
|