From 5bf2c4b01d883f13d8d98cd65721f5261bf72a82 Mon Sep 17 00:00:00 2001 From: Producer Matt <58014742+ProducerMatt@users.noreply.github.com> Date: Wed, 13 Mar 2024 19:46:31 -0500 Subject: [PATCH] rename functions according to elixir naming conventions --- lib/plugin.ex | 6 +++--- lib/plugin/test.ex | 6 +++--- lib/plugin/why.ex | 2 +- lib/service.ex | 3 ++- lib/service/discord.ex | 21 +++++++++++---------- lib/service/dummy.ex | 17 +++++++++++------ lib/stampede.ex | 12 ++++++------ test/stampede_stateless_test.exs | 6 +++--- 8 files changed, 40 insertions(+), 33 deletions(-) diff --git a/lib/plugin.ex b/lib/plugin.ex index 20e1a5e..dad9829 100644 --- a/lib/plugin.ex +++ b/lib/plugin.ex @@ -36,7 +36,7 @@ defmodule Plugin do """ @type! usage_tuples :: list(String.t() | {String.t(), String.t()}) @callback process_msg(SiteConfig.t(), Msg.t()) :: nil | Response.t() - @callback is_at_module(SiteConfig.t(), Msg.t()) :: boolean() | {:cleaned, text :: String.t()} + @callback at_module?(SiteConfig.t(), Msg.t()) :: boolean() | {:cleaned, text :: String.t()} @callback usage() :: usage_tuples() @callback description() :: String.t() @@ -45,7 +45,7 @@ defmodule Plugin do @behaviour unquote(__MODULE__) @impl Plugin - def is_at_module(cfg, msg) do + def at_module?(cfg, msg) do # Should we process the message? text = SiteConfig.fetch!(cfg, :prefix) @@ -58,7 +58,7 @@ defmodule Plugin do end end - defoverridable is_at_module: 2 + defoverridable at_module?: 2 end end diff --git a/lib/plugin/test.ex b/lib/plugin/test.ex index ff66bbe..8806b34 100644 --- a/lib/plugin/test.ex +++ b/lib/plugin/test.ex @@ -28,7 +28,7 @@ defmodule Plugin.Test do @spec! process_msg(any(), S.Msg.t()) :: nil | S.Response.t() @impl Plugin def process_msg(cfg, msg) do - case is_at_module(cfg, msg) do + case at_module?(cfg, msg) do {:cleaned, "ping"} -> S.Response.new( confidence: 10, @@ -83,7 +83,7 @@ defmodule Plugin.Test do end def lock_callback(cfg, msg, :b) do - case is_at_module(cfg, msg) do + case at_module?(cfg, msg) do {:cleaned, "b"} -> S.Response.new( confidence: 10, @@ -107,7 +107,7 @@ defmodule Plugin.Test do end def lock_callback(cfg, msg, :c) do - case is_at_module(cfg, msg) do + case at_module?(cfg, msg) do {:cleaned, "c"} -> S.Response.new( confidence: 10, diff --git a/lib/plugin/why.ex b/lib/plugin/why.ex index 0dbb365..dc3a434 100644 --- a/lib/plugin/why.ex +++ b/lib/plugin/why.ex @@ -41,7 +41,7 @@ defmodule Plugin.Why do at_module = at_module_regex() - case is_at_module(cfg, msg) do + case at_module?(cfg, msg) do false -> nil diff --git a/lib/service.ex b/lib/service.ex index 18c0e4f..963668b 100644 --- a/lib/service.ex +++ b/lib/service.ex @@ -4,6 +4,7 @@ defmodule Service do @callback site_config_schema() :: NimbleOptions.t() @callback into_msg(service_message :: term()) :: %Stampede.Msg{} + @callback dm?(service_message :: term()) :: boolean() @callback send_msg(destination :: term(), text :: binary(), opts :: keyword()) :: term() @callback log_plugin_error( cfg :: SiteConfig.t(), @@ -16,7 +17,7 @@ defmodule Service do {module :: Logger, message :: term(), _timestamp :: term(), _metadata :: term()}} ) :: :ok @callback reload_configs() :: :ok | {:error, any()} - @callback author_is_privileged(server_id :: any(), author_id :: any()) :: boolean() + @callback author_privileged?(server_id :: any(), author_id :: any()) :: boolean() @callback txt_format(blk :: TxtBlock.t(), type :: TxtBlock.type()) :: S.str_list() @callback format_plugin_fail( diff --git a/lib/service/discord.ex b/lib/service/discord.ex index d58cf6b..b1edf67 100644 --- a/lib/service/discord.ex +++ b/lib/service/discord.ex @@ -172,15 +172,16 @@ defmodule Service.Discord do end @impl Service - def author_is_privileged(server_id, author_id) do - GenServer.call(__MODULE__.Handler, {:author_is_privileged, server_id, author_id}) + def author_privileged?(server_id, author_id) do + GenServer.call(__MODULE__.Handler, {:author_privileged?, server_id, author_id}) end @impl Service def txt_format(blk, kind), do: TxtBlock.Md.format(blk, kind) - def is_dm(msg), do: msg.guild_id == nil + @impl Service + def dm?(msg), do: msg.guild_id == nil @spec! get_referenced_msg(Msg.t()) :: {:ok, Msg.t()} | {:error, any()} def get_referenced_msg(msg) do @@ -238,10 +239,10 @@ defmodule Service.Discord.Handler do vip_ids: _ :: vips() ) - @spec! is_vip_in_this_context(vips(), Discord.discord_guild_id(), Discord.discord_author_id()) :: + @spec! vip_in_this_context?(vips(), Discord.discord_guild_id(), Discord.discord_author_id()) :: boolean() - def is_vip_in_this_context(vips, server_id, author_id), - do: S.is_vip_in_this_context(vips, server_id, author_id) + def vip_in_this_context?(vips, server_id, author_id), + do: S.vip_in_this_context?(vips, server_id, author_id) def start_link(args) do GenServer.start_link(__MODULE__, args, name: __MODULE__) @@ -278,10 +279,10 @@ defmodule Service.Discord.Handler do {:reply, :ok, new_state} end - def handle_call({:author_is_privileged, server_id, author_id}, _from, state) do + def handle_call({:author_privileged?, server_id, author_id}, _from, state) do { :reply, - is_vip_in_this_context(state.vip_ids, server_id, author_id), + vip_in_this_context?(state.vip_ids, server_id, author_id), state } end @@ -304,8 +305,8 @@ defmodule Service.Discord.Handler do discord_msg.guild_id in state.guild_ids -> do_msg_create(discord_msg) - Discord.is_dm(discord_msg) -> - if is_vip_in_this_context(state.vip_ids, discord_msg.guild_id, discord_msg.author.id) do + Discord.dm?(discord_msg) -> + if vip_in_this_context?(state.vip_ids, discord_msg.guild_id, discord_msg.author.id) do do_msg_create(discord_msg) else Logger.warning(fn -> diff --git a/lib/service/dummy.ex b/lib/service/dummy.ex index b723ef8..85dcfb5 100644 --- a/lib/service/dummy.ex +++ b/lib/service/dummy.ex @@ -202,13 +202,13 @@ defmodule Service.Dummy do end @impl Service - def reload_configs() do - GenServer.call(__MODULE__, :reload_configs) - end + def dm?({_id, _server_id = {:dm, __MODULE__}, _channel, _user, _body, _ref}), + do: true + def dm?(_other), do: false @impl Service - def author_is_privileged(server_id, author_id) do - GenServer.call(__MODULE__, {:author_is_privileged, server_id, author_id}) + def author_privileged?(server_id, author_id) do + GenServer.call(__MODULE__, {:author_privileged?, server_id, author_id}) end @impl Service @@ -245,6 +245,11 @@ defmodule Service.Dummy do new_server(args) end + @impl Service + def reload_configs() do + GenServer.call(__MODULE__, :reload_configs) + end + # PLUMBING @spec! update_state() :: %__MODULE__{} @@ -348,7 +353,7 @@ defmodule Service.Dummy do {:reply, dump, state} end - def handle_call({:author_is_privileged, _server_id, author_id}, _from, state) do + def handle_call({:author_privileged?, _server_id, author_id}, _from, state) do case author_id do @system_user -> {:reply, true, state} diff --git a/lib/stampede.ex b/lib/stampede.ex index b17b6df..efe6708 100644 --- a/lib/stampede.ex +++ b/lib/stampede.ex @@ -33,19 +33,19 @@ defmodule Stampede do raise "intentional internal error: #{msg}" end - @spec! author_is_privileged( + @spec! author_privileged?( %{server_id: any()}, %{author_id: any()} ) :: boolean() - def author_is_privileged(cfg, msg) do - Service.apply_service_function(cfg, :author_is_privileged, [cfg.server_id, msg.author_id]) + def author_privileged?(cfg, msg) do + Service.apply_service_function(cfg, :author_privileged?, [cfg.server_id, msg.author_id]) end - @spec! is_vip_in_this_context(map(), server_id(), user_id()) :: boolean() - def is_vip_in_this_context(vips, nil, author_id), + @spec! vip_in_this_context?(map(), server_id(), user_id()) :: boolean() + def vip_in_this_context?(vips, nil, author_id), do: author_id in Map.values(vips) - def is_vip_in_this_context(vips, server_id, author_id) do + def vip_in_this_context?(vips, server_id, author_id) do Enum.any?(vips, fn {this_server, this_author} -> author_id == this_author and this_server == server_id end) diff --git a/test/stampede_stateless_test.exs b/test/stampede_stateless_test.exs index 7a151a6..65c6f29 100644 --- a/test/stampede_stateless_test.exs +++ b/test/stampede_stateless_test.exs @@ -129,20 +129,20 @@ defmodule StampedeStatelessTest do test "vip check" do vips = %{some_server: :admin} - assert S.is_vip_in_this_context( + assert S.vip_in_this_context?( vips, :some_server, :admin ) - assert S.is_vip_in_this_context( + assert S.vip_in_this_context?( vips, nil, :admin ) assert false == - S.is_vip_in_this_context( + S.vip_in_this_context?( vips, :some_server, :non_admin