aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--lib/waev/export.ex83
-rw-r--r--lib/waev_web/controllers/exports_controller.ex11
-rw-r--r--lib/waev_web/templates/exports/show.html.eex36
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 %>
<section class="phx-hero">
<section class="row">
<article class="column">
- <h2>Left</h2>
- <ul>
- <li>
- <a href="#">Link 1</a>
- </li>
- <li>
- Text 2
- </li>
- <li>
- Text 3
- </li>
- </ul>
+ <h2><%= @export.left %></h2>
</article>
<article class="column">
- <h2>Right</h2>
- <ul>
- <li>
- <a href="#">Link 1</a>
- </li>
- <li>
- Text 2
- </li>
- <li>
- Text 3
- </li>
- </ul>
+ <h2><%= @export.right %></h2>
</article>
</section>
+ <%= for message <- @export.messages do %>
+ <section class="row">
+ <article class="column">
+ <%= message.text %>
+ </article>
+ <article class="column">
+ </article>
+ </section>
+ <% end %>
</section>
-