diff --git a/lib/slack.ex b/lib/slack.ex index ae80452..c035d56 100644 --- a/lib/slack.ex +++ b/lib/slack.ex @@ -19,8 +19,11 @@ defmodule Slack.Supervisor do def init(_arg) do registry_spec = supervisor(Registry, [[keys: :unique, name: Slack.BotRegistry]]) - bot_specs = bot_configs() |> Enum.map(fn (%{name: name, workspace: ws} = config) -> - supervisor(Slack.Bot.Supervisor, [config], id: {{ws, name}, Slack.Bot.Supervisor}) + + bot_specs = bot_configs() + |> Task.async_stream(&bot_config_to_spec/1) + |> Enum.map(fn ({:ok, {%{name: bot} = config, data}}) -> + supervisor(Slack.Bot.Supervisor, [config, data], id: {bot, Slack.Bot.Supervisor}) end) children = [registry_spec | bot_specs] @@ -38,14 +41,9 @@ defmodule Slack.Supervisor do Application.get_env(:slack, :use_console, false) end - # def start_bot(name) when is_binary(name) or is_atom(name) do - # config = bot_configs() |> Enum.find(fn (%{name: n}) -> n == name end) - # start_bot(config) - # end - - # TODO: use normal supervisor handler def start_bot(%{} = config) do - Slack.Bot.Supervisor.start_link(config) + {config, data} = bot_config_to_spec(config) + Slack.Bot.Supervisor.start_link(config, data) end def stop_bot(name) do @@ -57,4 +55,10 @@ defmodule Slack.Supervisor do |> Application.get_env(:bots, []) |> Enum.map(&struct(Slack.Bot.Config, &1)) end + + defp bot_config_to_spec(conf) do + bot_name = {conf.workspace, conf.name} + api_data = Slack.Bot.Supervisor.init_api_calls(conf.api_client, conf.token, bot_name) + {Map.put(conf, :name, bot_name), api_data} + end end diff --git a/lib/slack/bot/supervisor.ex b/lib/slack/bot/supervisor.ex index 39281d7..bdd15f4 100644 --- a/lib/slack/bot/supervisor.ex +++ b/lib/slack/bot/supervisor.ex @@ -3,15 +3,14 @@ defmodule Slack.Bot.Supervisor do import Slack.BotRegistry alias Slack.Bot - def start_link(%{name: name, workspace: ws} = config) do - Supervisor.start_link(__MODULE__, config, name: registry_key({ws, name}, __MODULE__)) + def start_link(%{name: bot} = config, data) do + Supervisor.start_link(__MODULE__, {config, data}, name: registry_key(bot, __MODULE__)) end # --- - def init(%Slack.Bot.Config{} = c) do - name = {c.workspace, c.name} - {uid, ws_url, channels} = init_api_calls(c.api_client, c.token, name) + def init({%Slack.Bot.Config{} = c, {uid, ws_url, channels}}) do + name = c.name Agent.start_link(fn -> channels end, name: registry_key(name, :channels)) children = [ @@ -24,7 +23,7 @@ defmodule Slack.Bot.Supervisor do supervise(children, strategy: :rest_for_one) end - defp init_api_calls(client, token, name) do + def init_api_calls(client, token, name) do %{ "self" => %{"id" => uid}, "url" => ws_url diff --git a/lib/slack/console/pub_sub.ex b/lib/slack/console/pub_sub.ex index 9700d47..4db8911 100644 --- a/lib/slack/console/pub_sub.ex +++ b/lib/slack/console/pub_sub.ex @@ -60,9 +60,10 @@ defmodule Slack.Console.PubSub do @impl true @spec handle_cast({:broadcast, String.t, String.t, map, pid}, {map, map}) :: {:noreply, {map, map}} def handle_cast({:broadcast, workspace, channel, unencoded_message, from_socket} = m, {channels, _} = state) do + ts = System.os_time(:microseconds) / 10_000_00 channel_key = {workspace, channel} uid = channels[channel_key][from_socket] || "console user" - message = unencoded_message |> Map.put("user", uid) |> Poison.encode! + message = unencoded_message |> Map.merge(%{"user" => uid, "ts" => "#{ts}"}) |> Poison.encode! text = unencoded_message["text"] Slack.Console.print(workspace, channel, uid, text) diff --git a/mix.exs b/mix.exs index 7f70857..61f9958 100644 --- a/mix.exs +++ b/mix.exs @@ -3,7 +3,7 @@ defmodule Slack.Mixfile do def project do [app: :slack, - version: "0.7.0", + version: "0.7.1", elixir: "~> 1.5", elixirc_paths: elixirc_paths(Mix.env), build_embedded: Mix.env == :prod,