From ffc0bb5dab06874f2737a3def62835e45e0cd676 Mon Sep 17 00:00:00 2001 From: Guillermo Ramos Date: Tue, 4 Feb 2020 17:06:22 +0100 Subject: Parse exports --- lib/waev/export.ex | 83 +++++++++++++++++++++++++- lib/waev_web/controllers/exports_controller.ex | 11 +++- lib/waev_web/templates/exports/show.html.eex | 36 ++++------- 3 files changed, 102 insertions(+), 28 deletions(-) diff --git a/lib/waev/export.ex b/lib/waev/export.ex index 92a39c9..5140a89 100644 --- a/lib/waev/export.ex +++ b/lib/waev/export.ex @@ -1,12 +1,91 @@ defmodule Waev.Export do + defmodule Party do + defstruct name: nil, photo: nil + end + + defmodule Message do + defmodule Photo do + defstruct filename: nil, mime: nil, blob: nil + end + + defmodule File do + defstruct filename: nil + end + + defstruct party: nil, date: nil, text: nil, attachment: nil + end + + defstruct left: nil, right: nil, messages: [] + def path do Application.fetch_env!(:waev, __MODULE__)[:exports_path] end def list do case File.ls(path()) do - {:error, _reason} -> [] - {:ok, elems} -> Enum.filter(elems, &File.dir?/1) + {:error, _reason} -> + [] + + {:ok, elems} -> + Enum.filter(elems, &exists?/1) end end + + def get(e) do + case exists?(e) do + true -> + export = + File.stream!("#{path()}/#{e}/chat.txt") + |> Enum.reduce(%Waev.Export{}, fn line, export -> + line = String.trim(line) + + case Regex.run(~r/^(\d+\/\d+\/\d+ \d+:\d+) - ([^:]+): (.*)$/u, line) do + # Match: new message + [^line, datetime, name, text] -> + party = nil + + message = + case Regex.run(~r/^([^ ]+) \(archivo adjunto\)$/u, text) do + [^text, filename] -> + %Message{party: party, date: datetime, text: nil, attachment: filename} + + nil -> + %Message{party: party, date: datetime, text: text, attachment: nil} + end + + %{export | messages: [message | export.messages]} + + # Otherwise: text continuing from the previous one. We need to + # append it to the last message. + nil -> + case export.messages do + [first | rest] -> + text = + case first.text do + nil -> line + _ -> "#{first.text}\n#{line}" + end + + first = %{first | text: text} + %{export | messages: [first | rest]} + + # ... unless we are before the first message. Drop it. + _ -> + export + end + end + end) + + {:ok, export} + + false -> + :error + end + end + + defp exists?(e) do + e_path = "#{path()}/#{e}" + + File.dir?(e_path) && File.regular?("#{e_path}/chat.txt") + end end diff --git a/lib/waev_web/controllers/exports_controller.ex b/lib/waev_web/controllers/exports_controller.ex index a50896e..a3dde48 100644 --- a/lib/waev_web/controllers/exports_controller.ex +++ b/lib/waev_web/controllers/exports_controller.ex @@ -2,6 +2,15 @@ defmodule WaevWeb.ExportsController do use WaevWeb, :controller def show(conn, %{"id" => id}) do - render(conn, "show.html", id: id) + case Waev.Export.get(id) do + {:ok, export} -> + render(conn, "show.html", id: id, export: export) + + :error -> + conn + |> put_status(:not_found) + |> put_view(WaevWeb.ErrorView) + |> render("404.html") + end end end diff --git a/lib/waev_web/templates/exports/show.html.eex b/lib/waev_web/templates/exports/show.html.eex index 4351a17..4f0e3f5 100644 --- a/lib/waev_web/templates/exports/show.html.eex +++ b/lib/waev_web/templates/exports/show.html.eex @@ -3,33 +3,19 @@ ID: <%= @id %>
-

Left

-
    -
  • - Link 1 -
  • -
  • - Text 2 -
  • -
  • - Text 3 -
  • -
+

<%= @export.left %>

-

Right

-
    -
  • - Link 1 -
  • -
  • - Text 2 -
  • -
  • - Text 3 -
  • -
+

<%= @export.right %>

+ <%= for message <- @export.messages do %> +
+
+ <%= message.text %> +
+
+
+
+ <% end %>
- -- cgit v1.2.3