From c934e79e5852e05f714b2d542cc2678e287c49b8 Mon Sep 17 00:00:00 2001 From: Jordan Bracco Date: Wed, 25 Jun 2025 19:22:59 +0200 Subject: format. --- lib/nola/account.ex | 58 ++++++++++++++++++++++++++++++++++------------------- 1 file changed, 37 insertions(+), 21 deletions(-) (limited to 'lib/nola/account.ex') diff --git a/lib/nola/account.ex b/lib/nola/account.ex index 70e9e40..d850a82 100644 --- a/lib/nola/account.ex +++ b/lib/nola/account.ex @@ -44,7 +44,7 @@ defmodule Nola.Account do end def start_link() do - GenServer.start_link(__MODULE__, [], [name: __MODULE__]) + GenServer.start_link(__MODULE__, [], name: __MODULE__) end def init(_) do @@ -63,6 +63,7 @@ defmodule Nola.Account do def get_by_name(name) do spec = [{{:_, :"$1", :_}, [{:==, :"$1", {:const, name}}], [:"$_"]}] + case :dets.select(file("db"), spec) do [account] -> from_tuple(account) _ -> nil @@ -71,7 +72,7 @@ defmodule Nola.Account do def get_meta(%__MODULE__{id: id}, key, default \\ nil) do case :dets.lookup(file("meta"), {id, key}) do - [{_, value}] -> (value || default) + [{_, value}] -> value || default _ -> default end end @@ -86,8 +87,12 @@ defmodule Nola.Account do @doc "Find an account given a specific meta `key` and `value`." @spec find_meta_account(String.t(), String.t()) :: t() | nil def find_meta_account(key, value) do - #spec = [{{{:"$1", :"$2"}, :"$3"}, [:andalso, {:==, :"$2", {:const, key}}, {:==, :"$3", {:const, value}}], [:"$1"]}] - spec = [{{{:"$1", :"$2"}, :"$3"}, [{:andalso, {:==, :"$2", {:const, key}}, {:==, {:const, value}, :"$3"}}], [:"$1"]}] + # spec = [{{{:"$1", :"$2"}, :"$3"}, [:andalso, {:==, :"$2", {:const, key}}, {:==, :"$3", {:const, value}}], [:"$1"]}] + spec = [ + {{{:"$1", :"$2"}, :"$3"}, + [{:andalso, {:==, :"$2", {:const, key}}, {:==, {:const, value}, :"$3"}}], [:"$1"]} + ] + case :dets.select(file("meta"), spec) do [id] -> get(id) _ -> nil @@ -100,7 +105,7 @@ defmodule Nola.Account do end def put_user_meta(account = %__MODULE__{}, key, value) do - put_meta(account, "u:"<>key, value) + put_meta(account, "u:" <> key, value) end def put_meta(%__MODULE__{id: id}, key, value) do @@ -112,15 +117,15 @@ defmodule Nola.Account do end def all_accounts() do - :dets.traverse(file("db"), fn(obj) -> {:continue, from_tuple(obj)} end) + :dets.traverse(file("db"), fn obj -> {:continue, from_tuple(obj)} end) end def all_predicates() do - :dets.traverse(file("predicates"), fn(obj) -> {:continue, obj} end) + :dets.traverse(file("predicates"), fn obj -> {:continue, obj} end) end def all_meta() do - :dets.traverse(file("meta"), fn(obj) -> {:continue, obj} end) + :dets.traverse(file("meta"), fn obj -> {:continue, obj} end) end def merge_account(old_id, new_id) do @@ -130,16 +135,19 @@ defmodule Nola.Account do for pred <- predicates, do: :ok = :dets.insert(file("predicates"), {pred, new_id}) spec = [{{{:"$1", :"$2"}, :"$3"}, [{:==, :"$1", {:const, old_id}}], [{{:"$2", :"$3"}}]}] metas = :dets.select(file("meta"), spec) - for {k,v} <- metas do + + for {k, v} <- metas do :dets.delete(file("meta"), {{old_id, k}}) :ok = :dets.insert(file("meta"), {{new_id, k}, v}) end + :dets.delete(file("db"), old_id) Nola.Membership.merge_account(old_id, new_id) Nola.UserTrack.merge_account(old_id, new_id) Nola.Irc.Connection.dispatch("account", {:account_change, old_id, new_id}) Nola.Irc.Connection.dispatch("conn", {:account_change, old_id, new_id}) end + :ok end @@ -150,16 +158,15 @@ defmodule Nola.Account do @doc "Always find an account by nickname, even if offline. Uses predicates and then account name." def find_always_by_nick(network, chan, nick) do - with \ - nil <- find_by_nick(network, nick), + with nil <- find_by_nick(network, nick), nil <- do_lookup(%User{network: network, nick: nick}, false), - nil <- get_by_name(nick) - do + nil <- get_by_name(nick) do nil else %__MODULE__{} = account -> memberships = Nola.Membership.of_account(account) - if Enum.any?(memberships, fn({net, ch}) -> (net == network) or (chan && chan == ch) end) do + + if Enum.any?(memberships, fn {net, ch} -> net == network or (chan && chan == ch) end) do account else nil @@ -173,9 +180,14 @@ defmodule Nola.Account do def lookup(something, make_default \\ true) do account = do_lookup(something, make_default) + if account && Map.get(something, :nick) do - Nola.Irc.Connection.dispatch("account", {:account_auth, Map.get(something, :nick), account.id}) + Nola.Irc.Connection.dispatch( + "account", + {:account_auth, Map.get(something, :nick), account.id} + ) end + account end @@ -198,7 +210,8 @@ defmodule Nola.Account do end end - defp do_lookup(message = %Nola.Message{account: account_id}, make_default) when is_binary(account_id) do + defp do_lookup(message = %Nola.Message{account: account_id}, make_default) + when is_binary(account_id) do get(account_id) end @@ -206,8 +219,12 @@ defmodule Nola.Account do if user = Nola.UserTrack.find_by_nick(sender) do lookup(user, make_default) else - #FIXME this will never work with continued lookup by other methods as Who isn't compatible - lookup_by_nick(sender, :dets.lookup(file("predicates"), {sender.network,{:nick, sender.nick}}), make_default) + # FIXME this will never work with continued lookup by other methods as Who isn't compatible + lookup_by_nick( + sender, + :dets.lookup(file("predicates"), {sender.network, {:nick, sender.nick}}), + make_default + ) end end @@ -220,7 +237,7 @@ defmodule Nola.Account do end defp do_lookup(user = %User{network: server, nick: nick}, make_default) do - lookup_by_nick(user, :dets.lookup(file("predicates"), {server,{:nick, nick}}), make_default) + lookup_by_nick(user, :dets.lookup(file("predicates"), {server, {:nick, nick}}), make_default) end defp do_lookup(nil, _) do @@ -232,7 +249,7 @@ defmodule Nola.Account do end defp lookup_by_nick(user, _, make_default) do - #authenticate_by_host(user) + # authenticate_by_host(user) if make_default, do: new_account(user), else: nil end @@ -259,5 +276,4 @@ defmodule Nola.Account do spec = [{{:"$1", :"$2"}, [{:==, :"$2", {:const, account.id}}], [:"$1"]}] :dets.select(file("predicates"), spec) end - end -- cgit v1.2.3