philomena/lib/philomena/images.ex

168 lines
3.1 KiB
Elixir
Raw Normal View History

2019-08-18 18:17:05 +02:00
defmodule Philomena.Images do
@moduledoc """
The Images context.
"""
import Ecto.Query, warn: false
alias Philomena.Repo
alias Philomena.Images.Image
@doc """
Returns the list of images.
## Examples
iex> list_images()
[%Image{}, ...]
"""
def list_images do
2019-08-18 20:14:36 +02:00
Repo.all(Image |> where(hidden_from_users: false) |> order_by(desc: :created_at) |> limit(25))
2019-08-18 18:17:05 +02:00
end
@doc """
Gets a single image.
Raises `Ecto.NoResultsError` if the Image does not exist.
## Examples
iex> get_image!(123)
%Image{}
iex> get_image!(456)
** (Ecto.NoResultsError)
"""
2019-08-18 20:14:36 +02:00
def get_image!(id) do
Repo.one!(Image |> where(id: ^id) |> preload(:tags))
end
2019-08-18 18:17:05 +02:00
@doc """
Creates a image.
## Examples
iex> create_image(%{field: value})
{:ok, %Image{}}
iex> create_image(%{field: bad_value})
{:error, %Ecto.Changeset{}}
"""
def create_image(attrs \\ %{}) do
%Image{}
|> Image.changeset(attrs)
|> Repo.insert()
end
@doc """
Updates a image.
## Examples
iex> update_image(image, %{field: new_value})
{:ok, %Image{}}
iex> update_image(image, %{field: bad_value})
{:error, %Ecto.Changeset{}}
"""
def update_image(%Image{} = image, attrs) do
image
|> Image.changeset(attrs)
|> Repo.update()
end
@doc """
Deletes a Image.
## Examples
iex> delete_image(image)
{:ok, %Image{}}
iex> delete_image(image)
{:error, %Ecto.Changeset{}}
"""
def delete_image(%Image{} = image) do
Repo.delete(image)
end
@doc """
Returns an `%Ecto.Changeset{}` for tracking image changes.
## Examples
iex> change_image(image)
%Ecto.Changeset{source: %Image{}}
"""
def change_image(%Image{} = image) do
Image.changeset(image, %{})
end
2019-11-17 03:20:33 +01:00
def reindex_image(%Image{} = image) do
spawn fn ->
Image
|> preload(^indexing_preloads())
|> where(id: ^image.id)
|> Repo.one()
|> Image.index_document()
end
image
end
def indexing_preloads do
[:user, :favers, :downvoters, :upvoters, :hiders, :deleter, :gallery_interactions, tags: [:aliases, :aliased_tag]]
end
alias Philomena.Images.Subscription
2019-11-17 19:18:21 +01:00
def subscribed?(_image, nil), do: false
2019-11-17 18:50:42 +01:00
def subscribed?(image, user) do
Subscription
|> where(image_id: ^image.id, user_id: ^user.id)
|> Repo.exists?()
end
@doc """
Creates a subscription.
## Examples
iex> create_subscription(%{field: value})
{:ok, %Subscription{}}
iex> create_subscription(%{field: bad_value})
{:error, %Ecto.Changeset{}}
"""
2019-11-18 05:47:09 +01:00
def create_subscription(_image, nil), do: {:ok, nil}
2019-11-17 18:50:42 +01:00
def create_subscription(image, user) do
%Subscription{image_id: image.id, user_id: user.id}
|> Subscription.changeset(%{})
|> Repo.insert(on_conflict: :nothing)
end
@doc """
Deletes a Subscription.
## Examples
iex> delete_subscription(subscription)
{:ok, %Subscription{}}
iex> delete_subscription(subscription)
{:error, %Ecto.Changeset{}}
"""
2019-11-17 18:50:42 +01:00
def delete_subscription(image, user) do
%Subscription{image_id: image.id, user_id: user.id}
|> Repo.delete()
end
2019-08-18 18:17:05 +02:00
end