2019-08-28 18:34:37 +02:00
|
|
|
defmodule Philomena.Adverts do
|
|
|
|
@moduledoc """
|
|
|
|
The Adverts context.
|
|
|
|
"""
|
|
|
|
|
|
|
|
import Ecto.Query, warn: false
|
|
|
|
alias Philomena.Repo
|
|
|
|
|
|
|
|
alias Philomena.Adverts.Advert
|
2019-12-15 03:54:16 +01:00
|
|
|
alias Philomena.Adverts.Uploader
|
2019-08-28 18:34:37 +02:00
|
|
|
|
2019-11-29 07:26:05 +01:00
|
|
|
def random_live do
|
|
|
|
now = DateTime.utc_now()
|
2019-08-28 18:34:37 +02:00
|
|
|
|
2019-11-29 07:26:05 +01:00
|
|
|
Advert
|
|
|
|
|> where(live: true, restrictions: "none")
|
|
|
|
|> where([a], a.start_date < ^now and a.finish_date > ^now)
|
|
|
|
|> order_by(asc: fragment("random()"))
|
|
|
|
|> limit(1)
|
|
|
|
|> Repo.one()
|
|
|
|
end
|
2019-08-28 18:34:37 +02:00
|
|
|
|
2019-11-29 07:26:05 +01:00
|
|
|
def random_live_for(image) do
|
|
|
|
image = Repo.preload(image, :tags)
|
|
|
|
now = DateTime.utc_now()
|
|
|
|
|
|
|
|
Advert
|
|
|
|
|> where(live: true)
|
|
|
|
|> where([a], a.restrictions in ^restrictions(image))
|
|
|
|
|> where([a], a.start_date < ^now and a.finish_date > ^now)
|
|
|
|
|> order_by(asc: fragment("random()"))
|
|
|
|
|> limit(1)
|
|
|
|
|> Repo.one()
|
|
|
|
end
|
|
|
|
|
|
|
|
defp sfw?(image) do
|
|
|
|
image_tags = MapSet.new(image.tags |> Enum.map(& &1.name))
|
|
|
|
sfw_tags = MapSet.new(["safe", "suggestive"])
|
|
|
|
intersect = MapSet.intersection(image_tags, sfw_tags)
|
|
|
|
|
|
|
|
MapSet.size(intersect) > 0
|
|
|
|
end
|
|
|
|
|
|
|
|
defp nsfw?(image) do
|
|
|
|
image_tags = MapSet.new(image.tags |> Enum.map(& &1.name))
|
|
|
|
nsfw_tags = MapSet.new(["questionable", "explicit"])
|
|
|
|
intersect = MapSet.intersection(image_tags, nsfw_tags)
|
|
|
|
|
|
|
|
MapSet.size(intersect) > 0
|
|
|
|
end
|
|
|
|
|
|
|
|
defp restrictions(image) do
|
|
|
|
restrictions = ["none"]
|
|
|
|
restrictions = if nsfw?(image), do: ["nsfw" | restrictions], else: restrictions
|
|
|
|
restrictions = if sfw?(image), do: ["sfw" | restrictions], else: restrictions
|
|
|
|
restrictions
|
2019-08-28 18:34:37 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
@doc """
|
|
|
|
Gets a single advert.
|
|
|
|
|
|
|
|
Raises `Ecto.NoResultsError` if the Advert does not exist.
|
|
|
|
|
|
|
|
## Examples
|
|
|
|
|
|
|
|
iex> get_advert!(123)
|
|
|
|
%Advert{}
|
|
|
|
|
|
|
|
iex> get_advert!(456)
|
|
|
|
** (Ecto.NoResultsError)
|
|
|
|
|
|
|
|
"""
|
|
|
|
def get_advert!(id), do: Repo.get!(Advert, id)
|
|
|
|
|
|
|
|
@doc """
|
2020-09-06 07:30:53 +02:00
|
|
|
Creates an advert.
|
2019-08-28 18:34:37 +02:00
|
|
|
|
|
|
|
## Examples
|
|
|
|
|
|
|
|
iex> create_advert(%{field: value})
|
|
|
|
{:ok, %Advert{}}
|
|
|
|
|
|
|
|
iex> create_advert(%{field: bad_value})
|
|
|
|
{:error, %Ecto.Changeset{}}
|
|
|
|
|
|
|
|
"""
|
|
|
|
def create_advert(attrs \\ %{}) do
|
2020-09-06 07:30:53 +02:00
|
|
|
%Advert{}
|
|
|
|
|> Advert.save_changeset(attrs)
|
|
|
|
|> Uploader.analyze_upload(attrs)
|
|
|
|
|> Repo.insert()
|
|
|
|
|> case do
|
|
|
|
{:ok, advert} ->
|
|
|
|
Uploader.persist_upload(advert)
|
|
|
|
Uploader.unpersist_old_upload(advert)
|
|
|
|
|
|
|
|
{:ok, advert}
|
|
|
|
|
|
|
|
error ->
|
|
|
|
error
|
|
|
|
end
|
2019-08-28 18:34:37 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
@doc """
|
2020-09-06 07:30:53 +02:00
|
|
|
Updates an advert.
|
2019-08-28 18:34:37 +02:00
|
|
|
|
|
|
|
## Examples
|
|
|
|
|
|
|
|
iex> update_advert(advert, %{field: new_value})
|
|
|
|
{:ok, %Advert{}}
|
|
|
|
|
|
|
|
iex> update_advert(advert, %{field: bad_value})
|
|
|
|
{:error, %Ecto.Changeset{}}
|
|
|
|
|
|
|
|
"""
|
|
|
|
def update_advert(%Advert{} = advert, attrs) do
|
|
|
|
advert
|
2019-12-15 03:54:16 +01:00
|
|
|
|> Advert.save_changeset(attrs)
|
2019-08-28 18:34:37 +02:00
|
|
|
|> Repo.update()
|
|
|
|
end
|
|
|
|
|
2020-03-30 00:36:24 +02:00
|
|
|
def update_advert_image(%Advert{} = advert, attrs) do
|
2020-09-06 07:30:53 +02:00
|
|
|
advert
|
|
|
|
|> Advert.changeset(attrs)
|
|
|
|
|> Uploader.analyze_upload(attrs)
|
|
|
|
|> Repo.update()
|
|
|
|
|> case do
|
|
|
|
{:ok, advert} ->
|
|
|
|
Uploader.persist_upload(advert)
|
|
|
|
Uploader.unpersist_old_upload(advert)
|
|
|
|
|
|
|
|
{:ok, advert}
|
|
|
|
|
|
|
|
error ->
|
|
|
|
error
|
|
|
|
end
|
2020-03-30 00:36:24 +02:00
|
|
|
end
|
|
|
|
|
2019-08-28 18:34:37 +02:00
|
|
|
@doc """
|
2020-09-06 07:30:53 +02:00
|
|
|
Deletes an Advert.
|
2019-08-28 18:34:37 +02:00
|
|
|
|
|
|
|
## Examples
|
|
|
|
|
|
|
|
iex> delete_advert(advert)
|
|
|
|
{:ok, %Advert{}}
|
|
|
|
|
|
|
|
iex> delete_advert(advert)
|
|
|
|
{:error, %Ecto.Changeset{}}
|
|
|
|
|
|
|
|
"""
|
|
|
|
def delete_advert(%Advert{} = advert) do
|
|
|
|
Repo.delete(advert)
|
|
|
|
end
|
|
|
|
|
|
|
|
@doc """
|
|
|
|
Returns an `%Ecto.Changeset{}` for tracking advert changes.
|
|
|
|
|
|
|
|
## Examples
|
|
|
|
|
|
|
|
iex> change_advert(advert)
|
|
|
|
%Ecto.Changeset{source: %Advert{}}
|
|
|
|
|
|
|
|
"""
|
|
|
|
def change_advert(%Advert{} = advert) do
|
|
|
|
Advert.changeset(advert, %{})
|
|
|
|
end
|
|
|
|
end
|