2020-04-16 15:58:57 -06:00
|
|
|
# Pleroma: A lightweight social networking server
|
|
|
|
# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
|
|
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
|
|
|
defmodule Pleroma.Plugs.PlugHelper do
|
|
|
|
@moduledoc "Pleroma Plug helper"
|
|
|
|
|
2020-04-17 12:21:10 -06:00
|
|
|
@called_plugs_list_id :called_plugs
|
|
|
|
def called_plugs_list_id, do: @called_plugs_list_id
|
2020-04-16 15:58:57 -06:00
|
|
|
|
2020-04-17 12:21:10 -06:00
|
|
|
@skipped_plugs_list_id :skipped_plugs
|
|
|
|
def skipped_plugs_list_id, do: @skipped_plugs_list_id
|
2020-04-16 15:58:57 -06:00
|
|
|
|
2020-04-17 12:21:10 -06:00
|
|
|
@doc "Returns `true` if specified plug was called."
|
2020-04-16 15:58:57 -06:00
|
|
|
def plug_called?(conn, plug_module) do
|
2020-04-17 12:21:10 -06:00
|
|
|
contained_in_private_list?(conn, @called_plugs_list_id, plug_module)
|
2020-04-16 15:58:57 -06:00
|
|
|
end
|
|
|
|
|
2020-04-17 12:21:10 -06:00
|
|
|
@doc "Returns `true` if specified plug was explicitly marked as skipped."
|
2020-04-16 15:58:57 -06:00
|
|
|
def plug_skipped?(conn, plug_module) do
|
2020-04-17 12:21:10 -06:00
|
|
|
contained_in_private_list?(conn, @skipped_plugs_list_id, plug_module)
|
2020-04-16 15:58:57 -06:00
|
|
|
end
|
|
|
|
|
2020-04-17 12:21:10 -06:00
|
|
|
@doc "Returns `true` if specified plug was either called or explicitly marked as skipped."
|
2020-04-16 15:58:57 -06:00
|
|
|
def plug_called_or_skipped?(conn, plug_module) do
|
|
|
|
plug_called?(conn, plug_module) || plug_skipped?(conn, plug_module)
|
|
|
|
end
|
|
|
|
|
2020-04-17 12:21:10 -06: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-16 15:58:57 -06:00
|
|
|
modified_list = Enum.uniq(list ++ [value])
|
2020-04-17 12:21:10 -06:00
|
|
|
Plug.Conn.put_private(conn, list_id, modified_list)
|
2020-04-16 15:58:57 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
defp contained_in_private_list?(conn, private_variable, value) do
|
|
|
|
list = conn.private[private_variable] || []
|
|
|
|
value in list
|
|
|
|
end
|
|
|
|
end
|