From 4c0b059f24175dff7dd4d954530ac0c9229d12eb Mon Sep 17 00:00:00 2001 From: Jordan Bracco Date: Sun, 11 Dec 2022 05:57:14 +0000 Subject: feat(telegram): replace dets by couch for room settings Ref T58, T53 --- lib/lsg_telegram/room.ex | 64 +++++++++++++++++++++++++++++++++++++----------- 1 file changed, 50 insertions(+), 14 deletions(-) (limited to 'lib/lsg_telegram/room.ex') diff --git a/lib/lsg_telegram/room.ex b/lib/lsg_telegram/room.ex index f7e42c6..db3308e 100644 --- a/lib/lsg_telegram/room.ex +++ b/lib/lsg_telegram/room.ex @@ -3,36 +3,72 @@ defmodule LSG.TelegramRoom do @behaviour Telegram.ChatBot alias Telegram.Api - def dets() do - (LSG.data_path() <> "/telegram-rooms.dets") |> String.to_charlist() + @couch "bot-telegram-rooms" + + def rooms(), do: rooms(:with_docs) + + @spec rooms(:with_docs | :ids) :: [Map.t | integer( )] + def rooms(:with_docs) do + case Couch.get(@couch, :all_docs, include_docs: true) do + {:ok, %{"rows" => rows}} -> {:ok, for(%{"doc" => doc} <- rows, do: doc)} + error = {:error, _} -> error + end + end + + def rooms(:ids) do + case Couch.get(@couch, :all_docs) do + {:ok, %{"rows" => rows}} -> {:ok, for(%{"id" => id} <- rows, do: id)} + error = {:error, _} -> error + end + end + + def room(id, opts \\ []) do + Couch.get(@couch, id, opts) end + # TODO: Create couch def setup() do - {:ok, _} = :dets.open_file(dets(), []) :ok end def after_start() do - rooms = :dets.foldl(fn({id, _, _}, acc) -> [id | acc] end, [], dets()) - for id <- rooms, do: Telegram.Bot.ChatBot.Chat.Session.Supervisor.start_child(LSG.Telegram, id) + for id <- room_ids(), do: Telegram.Bot.ChatBot.Chat.Session.Supervisor.start_child(LSG.Telegram, Integer.parse(id) |> elem(0)) end @impl Telegram.ChatBot - def init(id) do + def init(id) when is_integer(id) and id < 0 do + Logger.metadata(transport: :telegram, id: id, telegram_room_id: id) + tg_room = case room(id) do + {:ok, tg_room = %{"network" => _net, "channel" => _chan}} -> tg_room + {:error, :not_found} -> + [net, chan] = String.split(chat["title"], "/", parts: 2) + {net, chan} = case IRC.Connection.get_network(net, chan) do + %IRC.Connection{} -> {net, chan} + _ -> {nil, nil} + end + {:ok, _id, _rev} = Couch.post(@couch, %{"_id" => id, "network" => net, "channel" => nil}) + {:ok, tg_room} = room(id) + tg_room + end token = Keyword.get(Application.get_env(:lsg, :telegram, []), :key) + %{"network" => net, "channel" => chan} = tg_room + Logger.info("Starting ChatBot for room #{id} \"#{chat["title"]}\" #{inspect tg_room}") {:ok, chat} = Api.request(token, "getChat", chat_id: id) - Logger.debug("Starting ChatBot for room #{id} \"#{chat["title"]}\"") - [net, chan] = String.split(chat["title"], "/", parts: 2) - case IRC.Connection.get_network(net, chan) do - %IRC.Connection{} -> + irc_plumbed = if net && chan do {:ok, _} = Registry.register(IRC.PubSub, "#{net}/#{chan}:messages", plugin: __MODULE__) {:ok, _} = Registry.register(IRC.PubSub, "#{net}/#{chan}:triggers", plugin: __MODULE__) {:ok, _} = Registry.register(IRC.PubSub, "#{net}/#{chan}:outputs", plugin: __MODULE__) - err -> - Logger.warn("Did not found telegram match for #{id} \"#{chat["title"]}\"") + true + else + Logger.warn("Did not found telegram match for #{id} \"#{chat["title"]}\"") + false end - :dets.insert(dets(), {id, net, chan}) - {:ok, %{id: id, net: net, chan: chan}} + {:ok, %{id: id, net: net, chan: chan, irc: irc_plumbed}} + end + + def init(id) do + Logger.error("telegram_room: bad id (not room id)", transport: :telegram, id: id, telegram_room_id: id) + :ignoree end def handle_update(%{"message" => %{"from" => %{"id" => user_id}, "text" => text}}, _token, state) do -- cgit v1.2.3