diff --git a/lib/bdfr_browser/importer.ex b/lib/bdfr_browser/importer.ex index 0f29ef1..503c207 100644 --- a/lib/bdfr_browser/importer.ex +++ b/lib/bdfr_browser/importer.ex @@ -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 diff --git a/lib/bdfr_browser/render_utils.ex b/lib/bdfr_browser/render_utils.ex index bd0ff7b..3b314c4 100644 --- a/lib/bdfr_browser/render_utils.ex +++ b/lib/bdfr_browser/render_utils.ex @@ -12,7 +12,25 @@ defmodule BdfrBrowser.RenderUtils do "
" 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