feat: Import more chat images
This commit is contained in:
parent
ad0fa15832
commit
8871dfae11
2 changed files with 30 additions and 3 deletions
|
@ -327,13 +327,14 @@ defmodule BdfrBrowser.Importer do
|
|||
|
||||
defp import_message(message, chat) when not is_nil(chat) do
|
||||
id = :sha3_256 |> :crypto.hash([chat.id, message["timestamp"]]) |> Base.encode16(case: :lower)
|
||||
message_content = message["content"]["Message"]
|
||||
{:ok, posted_at, 0} = DateTime.from_iso8601(message["timestamp"])
|
||||
|
||||
{:ok, message} =
|
||||
{:ok, message_record} =
|
||||
%Message{
|
||||
id: id,
|
||||
author: message["author"],
|
||||
message: message["content"]["Message"],
|
||||
message: message_content,
|
||||
posted_at: posted_at,
|
||||
chat: chat
|
||||
}
|
||||
|
@ -342,6 +343,14 @@ defmodule BdfrBrowser.Importer do
|
|||
conflict_target: :id
|
||||
)
|
||||
|
||||
message
|
||||
message_record =
|
||||
if message_record.message == "Image" and String.starts_with?(message_content, "mxc://") do
|
||||
changeset = Ecto.Changeset.change(message_record, %{message: message_content})
|
||||
Repo.update(changeset)
|
||||
else
|
||||
message_record
|
||||
end
|
||||
|
||||
message_record
|
||||
end
|
||||
end
|
||||
|
|
|
@ -12,7 +12,25 @@ defmodule BdfrBrowser.RenderUtils do
|
|||
"<p class=\"text-center\"><img src=\"/chat_media/\\1\" class=\"img-fluid\" loading=\"lazy\" /></p>"
|
||||
|
||||
msg
|
||||
|> maybe_insert_image(img_replacement)
|
||||
|> String.replace(~r/https:\/\/i\.redd\.it\/(.+)/, img_replacement)
|
||||
|> Earmark.as_html!()
|
||||
end
|
||||
|
||||
# Helper
|
||||
|
||||
defp maybe_insert_image(<<"mxc://reddit.com/", filename::binary>> = msg, replacement) do
|
||||
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
|
||||
|
||||
defp maybe_insert_image(msg, _replacement), do: msg
|
||||
end
|
||||
|
|
Reference in a new issue