What I am trying to do: Plug a rate limiter plug for a list of actions with generated individual options for each.
What I have:
defmodule Pleroma.Web.MastodonAPI.TimelineController do
use Pleroma.Web, :controller
...
for timeline_action <- [:direct, :home, :public, :hashtag, :list] do
bucket_name = :"#{timeline_action}_timeline"
plug(
RateLimiter,
[name: :timeline, bucket_name: bucket_name] when action == timeline_action
)
end
...
end
However this fails to compile with
warning: variable "timeline_action" does not exist and is being expanded to "timeline_action()", please use parentheses to remove the ambiguity or change the variable name
and later
== Compilation error in file lib/pleroma/web/mastodon_api/controllers/timeline_controller.ex ==
** (CompileError) lib/pleroma/web/mastodon_api/controllers/timeline_controller.ex:24: cannot find or invoke local timeline_action/0 inside guard. Only macros can be invoked in a guard and they must be defined before their invocation. Called as: timeline_action()
As I understand the problem is that the plug
macro is evaluated before timeline_action
is an actual variable, but I am unsure how to sovle this.
Thanks in advance and apologies if the solution is obvious.