philomena/lib/philomena_web/plugs/advert_plug.ex

31 lines
669 B
Elixir
Raw Normal View History

2019-11-29 07:26:05 +01:00
defmodule PhilomenaWeb.AdvertPlug do
alias Philomena.Adverts
alias Plug.Conn
def init([]), do: []
def call(conn, _opts) do
user = conn.assigns.current_user
image = conn.assigns[:image]
show_ads? = show_ads?(user)
conn
|> maybe_assign_ad(image, show_ads?)
end
defp maybe_assign_ad(conn, nil, true),
do: Conn.assign(conn, :advert, Adverts.random_live())
2020-01-11 05:20:19 +01:00
2019-11-29 07:26:05 +01:00
defp maybe_assign_ad(conn, image, true),
do: Conn.assign(conn, :advert, Adverts.random_live_for(image))
2020-01-11 05:20:19 +01:00
2019-11-29 07:26:05 +01:00
defp maybe_assign_ad(conn, _image, _false),
do: conn
defp show_ads?(%{hide_advertisements: false}),
do: true
2020-01-11 05:20:19 +01:00
2019-11-29 07:26:05 +01:00
defp show_ads?(_user),
do: true
2020-01-11 05:20:19 +01:00
end