2020-04-06 07:20:44 +00:00
|
|
|
# Pleroma: A lightweight social networking server
|
2021-01-13 06:49:20 +00:00
|
|
|
# Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
|
2020-04-06 07:20:44 +00:00
|
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2020-06-24 06:42:36 +00:00
|
|
|
defmodule Pleroma.Web.Plugs.PlugHelper do
|
2020-04-06 07:20:44 +00:00
|
|
|
@moduledoc "Pleroma Plug helper"
|
|
|
|
|
2020-04-17 18:21:10 +00:00
|
|
|
@called_plugs_list_id :called_plugs
|
|
|
|
def called_plugs_list_id, do: @called_plugs_list_id
|
2020-04-06 07:20:44 +00:00
|
|
|
|
2020-04-17 18:21:10 +00:00
|
|
|
@skipped_plugs_list_id :skipped_plugs
|
|
|
|
def skipped_plugs_list_id, do: @skipped_plugs_list_id
|
2020-04-06 07:20:44 +00:00
|
|
|
|
2020-04-17 18:21:10 +00:00
|
|
|
@doc "Returns `true` if specified plug was called."
|
2020-04-06 07:20:44 +00:00
|
|
|
def plug_called?(conn, plug_module) do
|
2020-04-17 18:21:10 +00:00
|
|
|
contained_in_private_list?(conn, @called_plugs_list_id, plug_module)
|
2020-04-06 07:20:44 +00:00
|
|
|
end
|
|
|
|
|
2020-04-17 18:21:10 +00:00
|
|
|
@doc "Returns `true` if specified plug was explicitly marked as skipped."
|
2020-04-06 07:20:44 +00:00
|
|
|
def plug_skipped?(conn, plug_module) do
|
2020-04-17 18:21:10 +00:00
|
|
|
contained_in_private_list?(conn, @skipped_plugs_list_id, plug_module)
|
2020-04-06 07:20:44 +00:00
|
|
|
end
|
|
|
|
|
2020-04-17 18:21:10 +00:00
|
|
|
@doc "Returns `true` if specified plug was either called or explicitly marked as skipped."
|
2020-04-06 07:20:44 +00:00
|
|
|
def plug_called_or_skipped?(conn, plug_module) do
|
|
|
|
plug_called?(conn, plug_module) || plug_skipped?(conn, plug_module)
|
|
|
|
end
|
|
|
|
|
2020-04-17 18:21:10 +00:00
|
|
|
# Appends plug to known list (skipped, called). Intended to be used from within plug code only.
|
|
|
|
def append_to_private_list(conn, list_id, value) do
|
|
|
|
list = conn.private[list_id] || []
|
2020-04-06 07:20:44 +00:00
|
|
|
modified_list = Enum.uniq(list ++ [value])
|
2020-04-17 18:21:10 +00:00
|
|
|
Plug.Conn.put_private(conn, list_id, modified_list)
|
2020-04-06 07:20:44 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
defp contained_in_private_list?(conn, private_variable, value) do
|
|
|
|
list = conn.private[private_variable] || []
|
|
|
|
value in list
|
|
|
|
end
|
|
|
|
end
|