Skip to content

Commit

Permalink
rename functions according to elixir naming conventions
Browse files Browse the repository at this point in the history
  • Loading branch information
ProducerMatt committed Mar 14, 2024
1 parent c0b583c commit 5bf2c4b
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 33 deletions.
6 changes: 3 additions & 3 deletions lib/plugin.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand All @@ -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)
Expand All @@ -58,7 +58,7 @@ defmodule Plugin do
end
end

defoverridable is_at_module: 2
defoverridable at_module?: 2
end
end

Expand Down
6 changes: 3 additions & 3 deletions lib/plugin/test.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion lib/plugin/why.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
3 changes: 2 additions & 1 deletion lib/service.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand All @@ -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(
Expand Down
21 changes: 11 additions & 10 deletions lib/service/discord.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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__)
Expand Down Expand Up @@ -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
Expand All @@ -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 ->
Expand Down
17 changes: 11 additions & 6 deletions lib/service/dummy.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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__{}
Expand Down Expand Up @@ -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}
Expand Down
12 changes: 6 additions & 6 deletions lib/stampede.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions test/stampede_stateless_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 5bf2c4b

Please sign in to comment.