diff options
Diffstat (limited to 'lib/web/controllers/irc_controller.ex')
-rw-r--r-- | lib/web/controllers/irc_controller.ex | 124 |
1 files changed, 82 insertions, 42 deletions
diff --git a/lib/web/controllers/irc_controller.ex b/lib/web/controllers/irc_controller.ex index e87382b..9de807b 100644 --- a/lib/web/controllers/irc_controller.ex +++ b/lib/web/controllers/irc_controller.ex @@ -1,31 +1,46 @@ defmodule NolaWeb.IrcController do use NolaWeb, :controller - plug NolaWeb.ContextPlug + plug(NolaWeb.ContextPlug) def index(conn, params) do network = Map.get(params, "network") channel = if c = Map.get(params, "chan"), do: NolaWeb.reformat_chan(c) - commands = for mod <- Enum.uniq([Nola.Plugins.Account] ++ Nola.Plugins.enabled()) do - if is_atom(mod) do - identifier = Module.split(mod) |> List.last |> Macro.underscore - {identifier, mod.irc_doc()} + + commands = + for mod <- Enum.uniq([Nola.Plugins.Account] ++ Nola.Plugins.enabled()) do + if is_atom(mod) do + identifier = Module.split(mod) |> List.last() |> Macro.underscore() + if Kernel.function_exported?(mod, :irc_doc, 0), do: {identifier, mod.irc_doc()} + end end - end - |> Enum.filter(& &1) - |> Enum.filter(fn({_, doc}) -> doc end) - members = cond do - network && channel -> Enum.map(Nola.UserTrack.channel(network, channel), fn(tuple) -> Nola.UserTrack.User.from_tuple(tuple) end) - true -> - Nola.Membership.of_account(conn.assigns.account) - end - render conn, "index.html", network: network, commands: commands, channel: channel, members: members + |> Enum.filter(& &1) + |> Enum.filter(fn {_, doc} -> doc end) + + members = + cond do + network && channel -> + Enum.map(Nola.UserTrack.channel(network, channel), fn tuple -> + Nola.UserTrack.User.from_tuple(tuple) + end) + + true -> + Nola.Membership.of_account(conn.assigns.account) + end + + render(conn, "index.html", + network: network, + commands: commands, + channel: channel, + members: members + ) end def txt(conn, %{"name" => name}) do if String.contains?(name, ".txt") do name = String.replace(name, ".txt", "") data = data() + if Map.has_key?(data, name) do lines = Enum.join(data[name], "\n") text(conn, lines) @@ -38,32 +53,54 @@ defmodule NolaWeb.IrcController do do_txt(conn, name) end end - def txt(conn, _), do: do_txt(conn, nil) + def txt(conn, _), do: do_txt(conn, nil) defp do_txt(conn, nil) do doc = Nola.Plugins.Txt.irc_doc() data = data() - main = Enum.filter(data, fn({trigger, _}) -> !String.contains?(trigger, ".") end) |> Enum.into(Map.new) - system = Enum.filter(data, fn({trigger, _}) -> String.contains?(trigger, ".") end) |> Enum.into(Map.new) - lines = Enum.reduce(main, 0, fn({_, lines}, acc) -> acc + Enum.count(lines) end) + + main = + Enum.filter(data, fn {trigger, _} -> !String.contains?(trigger, ".") end) + |> Enum.into(Map.new()) + + system = + Enum.filter(data, fn {trigger, _} -> String.contains?(trigger, ".") end) + |> Enum.into(Map.new()) + + lines = Enum.reduce(main, 0, fn {_, lines}, acc -> acc + Enum.count(lines) end) + conn |> assign(:title, "txt") - |> render("txts.html", data: main, doc: doc, files: Enum.count(main), lines: lines, system: system) + |> render("txts.html", + data: main, + doc: doc, + files: Enum.count(main), + lines: lines, + system: system + ) end defp do_txt(conn, txt) do data = data() - base_url = cond do - conn.assigns[:chan] -> "/#{conn.assigns.network}/#{NolaWeb.format_chan(conn.assigns.chan)}" - true -> "/-" - end + + base_url = + cond do + conn.assigns[:chan] -> + "/#{conn.assigns.network}/#{NolaWeb.format_chan(conn.assigns.chan)}" + + true -> + "/-" + end + if lines = Map.get(data, txt) do - lines = Enum.map(lines, fn(line) -> - line - |> String.split("\\\\") - |> Enum.intersperse(Phoenix.HTML.Tag.tag(:br)) - end) + lines = + Enum.map(lines, fn line -> + line + |> String.split("\\\\") + |> Enum.intersperse(Phoenix.HTML.Tag.tag(:br)) + end) + conn |> assign(:breadcrumbs, [{"txt", "#{base_url}/txt"}]) |> assign(:title, "#{txt}.txt") @@ -77,25 +114,28 @@ defmodule NolaWeb.IrcController do defp data() do dir = Application.get_env(:nola, :data_path) <> "/irc.txt/" + Path.wildcard(dir <> "/*.txt") - |> Enum.reduce(%{}, fn(path, m) -> + |> Enum.reduce(%{}, fn path, m -> path = String.split(path, "/") file = List.last(path) key = String.replace(file, ".txt", "") - data = dir <> file - |> File.read! - |> String.split("\n") - |> Enum.reject(fn(line) -> - cond do - line == "" -> true - !line -> true - true -> false - end - end) + + data = + (dir <> file) + |> File.read!() + |> String.split("\n") + |> Enum.reject(fn line -> + cond do + line == "" -> true + !line -> true + true -> false + end + end) + Map.put(m, key, data) end) - |> Enum.sort - |> Enum.into(Map.new) + |> Enum.sort() + |> Enum.into(Map.new()) end - end |