diff options
author | href <href@random.sh> | 2022-12-11 01:47:26 +0000 |
---|---|---|
committer | Jordan Bracco <href@random.sh> | 2022-12-11 02:03:36 +0000 |
commit | d97e6d09f0db87989df42993499429883f1c224a (patch) | |
tree | 314132c1d68b271979c293318b6bf8e7aacbedce | |
parent | fix(user-track): ensure we only get an user per network even if it's over mul... (diff) |
feat(message): assign id (flake_id)
-rw-r--r-- | lib/irc/connection.ex | 10 | ||||
-rw-r--r-- | lib/irc/irc.ex | 6 | ||||
-rw-r--r-- | lib/irc/puppet_connection.ex | 48 | ||||
-rw-r--r-- | lib/lsg_irc/logger_plugin.ex | 15 | ||||
-rw-r--r-- | lib/lsg_irc/sms_plugin.ex | 3 | ||||
-rw-r--r-- | lib/lsg_telegram/telegram.ex | 1 | ||||
-rw-r--r-- | mix.exs | 3 | ||||
-rw-r--r-- | mix.lock | 3 |
8 files changed, 55 insertions, 34 deletions
diff --git a/lib/irc/connection.ex b/lib/irc/connection.ex index fcb6c5e..1e94003 100644 --- a/lib/irc/connection.ex +++ b/lib/irc/connection.ex @@ -294,7 +294,9 @@ defmodule IRC.Connection do if !Map.get(user.options, :puppet) do reply_fun = fn(text) -> irc_reply(state, {chan, sender}, text) end account = IRC.Account.lookup(sender) - message = %IRC.Message{at: NaiveDateTime.utc_now(), text: text, network: state.network, account: account, sender: sender, channel: chan, replyfun: reply_fun, trigger: extract_trigger(text)} + message = %IRC.Message{id: FlakeId.get(), transport: :irc, at: NaiveDateTime.utc_now(), text: text, network: state.network, + account: account, sender: sender, channel: chan, replyfun: reply_fun, + trigger: extract_trigger(text)} message = case IRC.UserTrack.messaged(message) do :ok -> message {:ok, message} -> message @@ -309,7 +311,8 @@ defmodule IRC.Connection do def handle_info({:received, text, sender}, state) do reply_fun = fn(text) -> irc_reply(state, {sender.nick, sender}, text) end account = IRC.Account.lookup(sender) - message = %IRC.Message{text: text, network: state.network, account: account, sender: sender, replyfun: reply_fun, trigger: extract_trigger(text)} + message = %IRC.Message{id: FlakeId.get(), transport: irc, text: text, network: state.network, at: NaiveDateTime.utc_now(), + account: account, sender: sender, replyfun: reply_fun, trigger: extract_trigger(text)} message = case IRC.UserTrack.messaged(message) do :ok -> message {:ok, message} -> message @@ -460,7 +463,8 @@ defmodule IRC.Connection do |> List.flatten() outputs = for line <- lines do ExIRC.Client.msg(client, :privmsg, target, line) - {:irc, :out, %IRC.Message{network: network, channel: target, text: line, sender: %ExIRC.SenderInfo{nick: state.conn.nick}, at: NaiveDateTime.utc_now(), meta: %{self: true}}} + {:irc, :out, %IRC.Message{id: FlakeId.get(), transport: :irc, network: network, + channel: target, text: line, sender: %ExIRC.SenderInfo{nick: state.conn.nick}, at: NaiveDateTime.utc_now(), meta: %{self: true}}} end for f <- outputs, do: dispatch(["irc:outputs", "#{network}/#{target}:outputs"], f) end diff --git a/lib/irc/irc.ex b/lib/irc/irc.ex index 78b0611..fbad6e1 100644 --- a/lib/irc/irc.ex +++ b/lib/irc/irc.ex @@ -1,8 +1,9 @@ defmodule IRC do - @derive {Poison.Encoder, except: [:replyfun]} defmodule Message do - defstruct [:text, + @derive {Poison.Encoder, except: [:replyfun]} + defstruct [:id, + :text, {:transport, :irc}, :network, :account, @@ -15,6 +16,7 @@ defmodule IRC do ] end defmodule Trigger do + @derive Poison.Encoder defstruct [:type, :trigger, :args] end diff --git a/lib/irc/puppet_connection.ex b/lib/irc/puppet_connection.ex index 4604b04..f12cbf7 100644 --- a/lib/irc/puppet_connection.ex +++ b/lib/irc/puppet_connection.ex @@ -76,14 +76,14 @@ defmodule IRC.PuppetConnection do end def handle_continue(:connect, state) do - ipv6 = if @env == :prod do - subnet = LSG.Subnet.assign(state.account_id) - IRC.Account.put_meta(IRC.Account.get(state.account_id), "subnet", subnet) - ip = Pfx.host(subnet, 1) - {:ok, ipv6} = :inet_parse.ipv6_address(to_charlist(ip)) - System.cmd("add-ip6", [ip]) - ipv6 - end + #ipv6 = if @env == :prod do + # subnet = LSG.Subnet.assign(state.account_id) + # IRC.Account.put_meta(IRC.Account.get(state.account_id), "subnet", subnet) + # ip = Pfx.host(subnet, 1) + # {:ok, ipv6} = :inet_parse.ipv6_address(to_charlist(ip)) + # System.cmd("add-ip6", [ip]) + # ipv6 + #end conn = IRC.Connection.lookup(state.connection_id) client_opts = [] @@ -102,21 +102,21 @@ defmodule IRC.PuppetConnection do {:nodelay, true} ] - {ip, opts} = case {ipv6, :inet_res.resolve(to_charlist(conn.host), :in, :aaaa)} do - {ipv6, {:ok, {:dns_rec, _dns_header, _query, rrs = [{:dns_rr, _, _, _, _, _, _, _, _, _} | _], _, _}}} -> - ip = rrs - |> Enum.map(fn({:dns_rr, _, :aaaa, :in, _, _, ipv6, _, _, _}) -> ipv6 end) - |> Enum.shuffle() - |> List.first() - - opts = [ - :inet6, - {:ifaddr, ipv6} - ] - {ip, opts} - _ -> - {to_charlist(conn.host), []} - end + #{ip, opts} = case {ipv6, :inet_res.resolve(to_charlist(conn.host), :in, :aaaa)} do + # {ipv6, {:ok, {:dns_rec, _dns_header, _query, rrs = [{:dns_rr, _, _, _, _, _, _, _, _, _} | _], _, _}}} -> + # ip = rrs + # |> Enum.map(fn({:dns_rr, _, :aaaa, :in, _, _, ipv6, _, _, _}) -> ipv6 end) + # |> Enum.shuffle() + # |> List.first() + + # opts = [ + # :inet6, + # {:ifaddr, ipv6} + # ] + # {ip, opts} + # _ -> + {ip, opts} = {to_charlist(conn.host), []} + #end conn_fun = if conn.tls, do: :connect_ssl!, else: :connect! apply(ExIRC.Client, conn_fun, [client, ip, conn.port, base_opts ++ opts]) @@ -152,7 +152,7 @@ defmodule IRC.PuppetConnection do reply_fun = fn(text) -> IRC.Connection.broadcast_message(state.network, channel, text) end - message = %IRC.Message{at: NaiveDateTime.utc_now(), text: text, network: state.network, account: account, sender: sender, channel: channel, replyfun: reply_fun, trigger: IRC.Connection.extract_trigger(text), meta: meta} + message = %IRC.Message{id: FlakeId.get(), at: NaiveDateTime.utc_now(), text: text, network: state.network, account: account, sender: sender, channel: channel, replyfun: reply_fun, trigger: IRC.Connection.extract_trigger(text), meta: meta} message = case IRC.UserTrack.messaged(message) do :ok -> message {:ok, message} -> message diff --git a/lib/lsg_irc/logger_plugin.ex b/lib/lsg_irc/logger_plugin.ex index 667f714..e5307bc 100644 --- a/lib/lsg_irc/logger_plugin.ex +++ b/lib/lsg_irc/logger_plugin.ex @@ -13,6 +13,7 @@ defmodule LSG.IRC.LoggerPlugin do regopts = [plugin: __MODULE__] {:ok, _} = Registry.register(IRC.PubSub, "triggers", regopts) {:ok, _} = Registry.register(IRC.PubSub, "messages", regopts) + {:ok, _} = Registry.register(IRC.PubSub, "messages:private", regopts) {:ok, nil} end @@ -49,12 +50,20 @@ defmodule LSG.IRC.LoggerPlugin do state end - def format_to_db(m = %IRC.Message{}) do - %IRC.Message{m | replyfun: nil} + def format_to_db(msg = %IRC.Message{id: id}) do + msg + |> Poison.encode!() + |> Map.drop("id") + + %{"_id" => id || FlakeId.get(), + "type" => "irc.message/v1", + "object" => msg} end def format_to_db(anything) do - anything + %{"_id" => FlakeId.get(), + "type" => "object", + "object" => anything} end end diff --git a/lib/lsg_irc/sms_plugin.ex b/lib/lsg_irc/sms_plugin.ex index b183f7d..be1611f 100644 --- a/lib/lsg_irc/sms_plugin.ex +++ b/lib/lsg_irc/sms_plugin.ex @@ -34,6 +34,7 @@ defmodule LSG.IRC.SmsPlugin do "!"<>message end message = %IRC.Message{ + id: FlakeId.get(), transport: :sms, network: "sms", channel: nil, @@ -43,7 +44,7 @@ defmodule LSG.IRC.SmsPlugin do replyfun: reply_fun, trigger: IRC.Connection.extract_trigger(trigger_text) } - IO.puts("converted sms to message: #{inspect message}") + Logger.debug("converted sms to message: #{inspect message}") IRC.Connection.publish(message, ["messages:sms"]) message end diff --git a/lib/lsg_telegram/telegram.ex b/lib/lsg_telegram/telegram.ex index 63940dc..748a456 100644 --- a/lib/lsg_telegram/telegram.ex +++ b/lib/lsg_telegram/telegram.ex @@ -196,6 +196,7 @@ defmodule LSG.Telegram do "!"<>text end message = %IRC.Message{ + id: FlakeId.get(), transport: :telegram, network: "telegram", channel: nil, @@ -4,7 +4,7 @@ defmodule LSG.Mixfile do def project do [ app: :lsg, - version: version("0.2.4"), + version: version("0.2.6"), elixir: "~> 1.4", elixirc_paths: elixirc_paths(Mix.env), compilers: [:phoenix, :gettext] ++ Mix.compilers, @@ -77,6 +77,7 @@ defmodule LSG.Mixfile do {:oauth2, "~> 2.0"}, {:powerdnsex, git: "https://git.random.sh/ircbot/powerdnsex.git", branch: "master"}, {:pfx, "~> 0.7.0"}, + {:flake_id, "~> 0.1.0"} ] end @@ -2,6 +2,7 @@ "abacus": {:hex, :abacus, "0.3.3", "f2f11e23073f5e16af36ac425cd9fa9a338695e2f8014684239fa14a9171d5f6", [:mix], [], "hexpm", "a41110183de16eda239f2187e7bb0c91c50658a8ae8254b85352287eb7034d88"}, "artificery": {:hex, :artificery, "0.4.3", "0bc4260f988dcb9dda4b23f9fc3c6c8b99a6220a331534fdf5bf2fd0d4333b02", [:mix], [], "hexpm", "12e95333a30e20884e937abdbefa3e7f5e05609c2ba8cf37b33f000b9ffc0504"}, "backoff": {:git, "https://github.com/ferd/backoff", "4b8c02d038de1055481b0193665944e11fec337e", [branch: "master"]}, + "base62": {:hex, :base62, "1.2.2", "85c6627eb609317b70f555294045895ffaaeb1758666ab9ef9ca38865b11e629", [:mix], [{:custom_base, "~> 0.2.1", [hex: :custom_base, repo: "hexpm", optional: false]}], "hexpm", "d41336bda8eaa5be197f1e4592400513ee60518e5b9f4dcf38f4b4dae6f377bb"}, "castore": {:hex, :castore, "0.1.11", "c0665858e0e1c3e8c27178e73dffea699a5b28eb72239a3b2642d208e8594914", [:mix], [], "hexpm", "91b009ba61973b532b84f7c09ce441cba7aa15cb8b006cf06c6f4bba18220081"}, "certifi": {:hex, :certifi, "2.6.1", "dbab8e5e155a0763eea978c913ca280a6b544bfa115633fa20249c3d396d9493", [:rebar3], [], "hexpm", "524c97b4991b3849dd5c17a631223896272c6b0af446778ba4675a1dff53bb7e"}, "combine": {:hex, :combine, "0.10.0", "eff8224eeb56498a2af13011d142c5e7997a80c8f5b97c499f84c841032e429f", [:mix], [], "hexpm", "1b1dbc1790073076580d0d1d64e42eae2366583e7aecd455d1215b0d16f2451b"}, @@ -9,6 +10,7 @@ "cowboy": {:hex, :cowboy, "2.9.0", "865dd8b6607e14cf03282e10e934023a1bd8be6f6bacf921a7e2a96d800cd452", [:make, :rebar3], [{:cowlib, "2.11.0", [hex: :cowlib, repo: "hexpm", optional: false]}, {:ranch, "1.8.0", [hex: :ranch, repo: "hexpm", optional: false]}], "hexpm", "2c729f934b4e1aa149aff882f57c6372c15399a20d54f65c8d67bef583021bde"}, "cowboy_telemetry": {:hex, :cowboy_telemetry, "0.4.0", "f239f68b588efa7707abce16a84d0d2acf3a0f50571f8bb7f56a15865aae820c", [:rebar3], [{:cowboy, "~> 2.7", [hex: :cowboy, repo: "hexpm", optional: false]}, {:telemetry, "~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "7d98bac1ee4565d31b62d59f8823dfd8356a169e7fcbb83831b8a5397404c9de"}, "cowlib": {:hex, :cowlib, "2.9.1", "61a6c7c50cf07fdd24b2f45b89500bb93b6686579b069a89f88cb211e1125c78", [:rebar3], [], "hexpm", "e4175dc240a70d996156160891e1c62238ede1729e45740bdd38064dad476170"}, + "custom_base": {:hex, :custom_base, "0.2.1", "4a832a42ea0552299d81652aa0b1f775d462175293e99dfbe4d7dbaab785a706", [:mix], [], "hexpm", "8df019facc5ec9603e94f7270f1ac73ddf339f56ade76a721eaa57c1493ba463"}, "date_time_parser": {:hex, :date_time_parser, "1.1.1", "cd7a04eb8f413a63cfb16892575d08a23651de1118c95278c13f84c105247901", [:mix], [{:nimble_parsec, "~> 1.0", [hex: :nimble_parsec, repo: "hexpm", optional: false]}, {:timex, ">= 3.2.1", [hex: :timex, repo: "hexpm", optional: false]}], "hexpm", "2ede6de7994c1589bcf118954999ed6ff5de97415b33827ea5b30804c7e512ef"}, "db_connection": {:hex, :db_connection, "2.4.0", "d04b1b73795dae60cead94189f1b8a51cc9e1f911c234cc23074017c43c031e5", [:mix], [{:connection, "~> 1.0", [hex: :connection, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "ad416c21ad9f61b3103d254a71b63696ecadb6a917b36f563921e0de00d7d7c8"}, "decimal": {:hex, :decimal, "2.0.0", "a78296e617b0f5dd4c6caf57c714431347912ffb1d0842e998e9792b5642d697", [:mix], [], "hexpm", "34666e9c55dea81013e77d9d87370fe6cb6291d1ef32f46a1600230b1d44f577"}, @@ -27,6 +29,7 @@ "extwitter": {:hex, :extwitter, "0.12.4", "8e69a55ca4c3ad1caa0fa4585ce33bbf4d636fd56210c961e36d109d0848c1d9", [:mix], [{:jason, "~> 1.1", [hex: :jason, repo: "hexpm", optional: false]}, {:oauther, "~> 1.1", [hex: :oauther, repo: "hexpm", optional: false]}], "hexpm", "1df46ffb49b196225afbf665a1a0f1a5fdd144b11bf1394509ea055f38c3343d"}, "file_size": {:hex, :file_size, "3.0.1", "ad447a69442a82fc701765a73992d7b1110136fa0d4a9d3190ea685d60034dcd", [:mix], [{:decimal, ">= 1.0.0 and < 3.0.0", [hex: :decimal, repo: "hexpm", optional: true]}, {:number, "~> 1.0", [hex: :number, repo: "hexpm", optional: false]}], "hexpm", "64dd665bc37920480c249785788265f5d42e98830d757c6a477b3246703b8e20"}, "file_system": {:hex, :file_system, "0.2.10", "fb082005a9cd1711c05b5248710f8826b02d7d1784e7c3451f9c1231d4fc162d", [:mix], [], "hexpm", "41195edbfb562a593726eda3b3e8b103a309b733ad25f3d642ba49696bf715dc"}, + "flake_id": {:hex, :flake_id, "0.1.0", "7716b086d2e405d09b647121a166498a0d93d1a623bead243e1f74216079ccb3", [:mix], [{:base62, "~> 1.2", [hex: :base62, repo: "hexpm", optional: false]}, {:ecto, ">= 2.0.0", [hex: :ecto, repo: "hexpm", optional: true]}], "hexpm", "31fc8090fde1acd267c07c36ea7365b8604055f897d3a53dd967658c691bd827"}, "floki": {:hex, :floki, "0.19.3", "652d1447767f783bd6cae1d882fd2145f25db28c6841ab87659225b468cff101", [:mix], [{:html_entities, "~> 0.4.0", [hex: :html_entities, repo: "hexpm", optional: false]}, {:mochiweb, "~> 2.15", [hex: :mochiweb, repo: "hexpm", optional: false]}], "hexpm", "1c8da482a0848c55a1d22af49ce6547790077adac2a04cf265e1f26583781adb"}, "gen_magic": {:git, "https://github.com/hrefhref/gen_magic", "48a12cca10305c8d357fe16b10fd7ead9b64a56a", [branch: "develop"]}, "gettext": {:hex, :gettext, "0.18.2", "7df3ea191bb56c0309c00a783334b288d08a879f53a7014341284635850a6e55", [:mix], [], "hexpm", "f9f537b13d4fdd30f3039d33cb80144c3aa1f8d9698e47d7bcbcc8df93b1f5c5"}, |