feat: Import more chat images

This commit is contained in:
Daniel Kempkens 2023-08-16 20:14:18 +02:00
parent ad0fa15832
commit 8871dfae11
Signed by: daniel
SSH key fingerprint: SHA256:Ks/MyhQYcPRQiwMKLAKquWCdCPe3JXlb1WttgnAoSeM
2 changed files with 30 additions and 3 deletions

View file

@ -327,13 +327,14 @@ defmodule BdfrBrowser.Importer do
defp import_message(message, chat) when not is_nil(chat) 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) 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, posted_at, 0} = DateTime.from_iso8601(message["timestamp"])
{:ok, message} = {:ok, message_record} =
%Message{ %Message{
id: id, id: id,
author: message["author"], author: message["author"],
message: message["content"]["Message"], message: message_content,
posted_at: posted_at, posted_at: posted_at,
chat: chat chat: chat
} }
@ -342,6 +343,14 @@ defmodule BdfrBrowser.Importer do
conflict_target: :id 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
end end

View file

@ -12,7 +12,25 @@ defmodule BdfrBrowser.RenderUtils do
"<p class=\"text-center\"><img src=\"/chat_media/\\1\" class=\"img-fluid\" loading=\"lazy\" /></p>" "<p class=\"text-center\"><img src=\"/chat_media/\\1\" class=\"img-fluid\" loading=\"lazy\" /></p>"
msg msg
|> maybe_insert_image(img_replacement)
|> String.replace(~r/https:\/\/i\.redd\.it\/(.+)/, img_replacement) |> String.replace(~r/https:\/\/i\.redd\.it\/(.+)/, img_replacement)
|> Earmark.as_html!() |> Earmark.as_html!()
end 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 end