2020-04-06 01:20:44 -06:00
|
|
|
# Pleroma: A lightweight social networking server
|
2022-02-25 23:11:42 -07:00
|
|
|
# Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
|
2020-04-06 01:20:44 -06:00
|
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2020-06-24 00:42:36 -06:00
|
|
|
defmodule Pleroma.Web.Plugs.PlugHelper do
|
2020-04-06 01:20:44 -06:00
|
|
|
@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-06 01:20:44 -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-06 01:20:44 -06:00
|
|
|
|
2020-04-17 12:21:10 -06:00
|
|
|
@doc "Returns `true` if specified plug was called."
|
2020-04-06 01:20:44 -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-06 01:20:44 -06:00
|
|
|
end
|
|
|
|
|
2020-04-17 12:21:10 -06:00
|
|
|
@doc "Returns `true` if specified plug was explicitly marked as skipped."
|
2020-04-06 01:20:44 -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-06 01:20:44 -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-06 01:20:44 -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-06 01:20:44 -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-06 01:20:44 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
defp contained_in_private_list?(conn, private_variable, value) do
|
|
|
|
list = conn.private[private_variable] || []
|
|
|
|
value in list
|
|
|
|
end
|
|
|
|
end
|