2019-08-18 12:17:05 -04:00
|
|
|
defmodule PhilomenaWeb.ImageController do
|
|
|
|
use PhilomenaWeb, :controller
|
|
|
|
|
2019-11-30 21:22:05 -05:00
|
|
|
alias PhilomenaWeb.ImageLoader
|
2019-12-19 15:11:44 -05:00
|
|
|
alias PhilomenaWeb.CommentLoader
|
2019-12-02 10:02:48 -05:00
|
|
|
alias PhilomenaWeb.NotificationCountPlug
|
2019-12-04 23:12:49 -05:00
|
|
|
alias Philomena.{Images, Images.Image, Comments.Comment, Galleries.Gallery, Galleries.Interaction, Textile.Renderer}
|
2019-12-20 20:08:18 -05:00
|
|
|
# alias Philomena.Servers.ImageProcessor
|
2019-12-05 14:23:26 -05:00
|
|
|
alias Philomena.UserStatistics
|
2019-11-16 21:20:33 -05:00
|
|
|
alias Philomena.Interactions
|
2019-11-16 23:59:24 -05:00
|
|
|
alias Philomena.Comments
|
2019-11-26 20:45:57 -05:00
|
|
|
alias Philomena.Tags
|
2019-10-04 20:51:56 -04:00
|
|
|
alias Philomena.Repo
|
2019-08-18 22:03:12 -04:00
|
|
|
import Ecto.Query
|
2019-08-18 12:17:05 -04:00
|
|
|
|
2019-12-07 15:13:32 -05:00
|
|
|
plug :load_image when action in [:show]
|
2019-08-28 21:14:54 -04:00
|
|
|
|
2019-11-25 23:51:17 -05:00
|
|
|
plug PhilomenaWeb.FilterBannedUsersPlug when action in [:new, :create]
|
|
|
|
plug PhilomenaWeb.UserAttributionPlug when action in [:create]
|
|
|
|
plug PhilomenaWeb.CaptchaPlug when action in [:create]
|
2019-11-28 18:19:47 -05:00
|
|
|
plug PhilomenaWeb.ScraperPlug, [params_name: "image", params_key: "image"] when action in [:create]
|
2019-11-29 01:26:05 -05:00
|
|
|
plug PhilomenaWeb.AdvertPlug when action in [:show]
|
2019-11-25 23:51:17 -05:00
|
|
|
|
2019-08-18 12:17:05 -04:00
|
|
|
def index(conn, _params) do
|
2019-12-07 15:16:17 -05:00
|
|
|
{:ok, {images, _tags}} = ImageLoader.search_string(conn, "created_at.lte:3 minutes ago")
|
2019-08-18 22:03:12 -04:00
|
|
|
|
2019-11-16 21:20:33 -05:00
|
|
|
interactions =
|
|
|
|
Interactions.user_interactions(images, conn.assigns.current_user)
|
|
|
|
|
2019-12-16 14:24:38 -05:00
|
|
|
render(conn, "index.html", title: "Images", layout_class: "layout--wide", images: images, interactions: interactions)
|
2019-08-18 12:17:05 -04:00
|
|
|
end
|
|
|
|
|
2019-10-03 19:58:54 -04:00
|
|
|
def show(conn, %{"id" => _id}) do
|
2019-11-16 21:20:33 -05:00
|
|
|
image = conn.assigns.image
|
2019-12-01 00:03:45 -05:00
|
|
|
user = conn.assigns.current_user
|
|
|
|
|
|
|
|
Images.clear_notification(image, user)
|
2019-11-16 21:20:33 -05:00
|
|
|
|
2019-12-02 09:55:48 -05:00
|
|
|
# Update the notification ticker in the header
|
|
|
|
conn = NotificationCountPlug.call(conn)
|
|
|
|
|
2019-12-19 15:11:44 -05:00
|
|
|
comments = CommentLoader.load_comments(conn, image)
|
|
|
|
|
|
|
|
rendered = Renderer.render_collection(comments.entries, conn)
|
2019-11-10 18:35:52 -05:00
|
|
|
|
|
|
|
comments =
|
2019-11-11 21:38:51 -05:00
|
|
|
%{comments | entries: Enum.zip(comments.entries, rendered)}
|
2019-11-10 18:35:52 -05:00
|
|
|
|
2019-11-11 17:57:29 -05:00
|
|
|
description =
|
2019-11-16 21:20:33 -05:00
|
|
|
%{body: image.description}
|
2019-12-01 12:11:00 -05:00
|
|
|
|> Renderer.render_one(conn)
|
2019-11-11 17:57:29 -05:00
|
|
|
|
2019-11-16 21:20:33 -05:00
|
|
|
interactions =
|
|
|
|
Interactions.user_interactions([image], conn.assigns.current_user)
|
|
|
|
|
2019-11-16 23:59:24 -05:00
|
|
|
comment_changeset =
|
|
|
|
%Comment{}
|
|
|
|
|> Comments.change_comment()
|
|
|
|
|
2019-11-24 21:16:22 -05:00
|
|
|
image_changeset =
|
|
|
|
image
|
|
|
|
|> Images.change_image()
|
|
|
|
|
2019-11-17 12:50:42 -05:00
|
|
|
watching =
|
|
|
|
Images.subscribed?(image, conn.assigns.current_user)
|
|
|
|
|
2019-12-04 23:12:49 -05:00
|
|
|
{user_galleries, image_galleries} = image_and_user_galleries(image, conn.assigns.current_user)
|
|
|
|
|
2019-12-07 23:53:18 -05:00
|
|
|
assigns = [
|
2019-11-16 23:59:24 -05:00
|
|
|
image: image,
|
|
|
|
comments: comments,
|
2019-11-24 21:16:22 -05:00
|
|
|
image_changeset: image_changeset,
|
2019-11-16 23:59:24 -05:00
|
|
|
comment_changeset: comment_changeset,
|
2019-12-04 23:12:49 -05:00
|
|
|
image_galleries: image_galleries,
|
|
|
|
user_galleries: user_galleries,
|
2019-11-16 23:59:24 -05:00
|
|
|
description: description,
|
2019-11-17 12:50:42 -05:00
|
|
|
interactions: interactions,
|
2019-11-17 17:51:14 -05:00
|
|
|
watching: watching,
|
2019-12-16 14:24:38 -05:00
|
|
|
layout_class: "layout--wide",
|
2019-12-20 09:44:05 -05:00
|
|
|
title: "##{image.id} - #{image.tag_list_cache}"
|
2019-12-07 23:53:18 -05:00
|
|
|
]
|
|
|
|
|
|
|
|
if image.hidden_from_users do
|
|
|
|
render(conn, "deleted.html", assigns)
|
|
|
|
else
|
|
|
|
render(conn, "show.html", assigns)
|
|
|
|
end
|
2019-08-18 12:17:05 -04:00
|
|
|
end
|
2019-11-25 23:51:17 -05:00
|
|
|
|
|
|
|
def new(conn, _params) do
|
|
|
|
changeset =
|
|
|
|
%Image{}
|
|
|
|
|> Images.change_image()
|
|
|
|
|
2019-12-16 14:24:38 -05:00
|
|
|
render(conn, "new.html", title: "New Image", changeset: changeset)
|
2019-11-25 23:51:17 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def create(conn, %{"image" => image_params}) do
|
|
|
|
attributes = conn.assigns.attributes
|
|
|
|
|
|
|
|
case Images.create_image(attributes, image_params) do
|
|
|
|
{:ok, %{image: image}} ->
|
2019-12-20 20:08:18 -05:00
|
|
|
spawn fn ->
|
|
|
|
Images.repair_image(image)
|
|
|
|
end
|
|
|
|
# ImageProcessor.cast(image.id)
|
2019-11-25 23:51:17 -05:00
|
|
|
Images.reindex_image(image)
|
2019-11-26 20:45:57 -05:00
|
|
|
Tags.reindex_tags(image.added_tags)
|
2019-12-05 14:23:26 -05:00
|
|
|
UserStatistics.inc_stat(conn.assigns.current_user, :uploads)
|
2019-11-25 23:51:17 -05:00
|
|
|
|
|
|
|
conn
|
|
|
|
|> put_flash(:info, "Image created successfully.")
|
|
|
|
|> redirect(to: Routes.image_path(conn, :show, image))
|
|
|
|
|
|
|
|
{:error, :image, changeset, _} ->
|
|
|
|
conn
|
|
|
|
|> render("new.html", changeset: changeset)
|
|
|
|
end
|
|
|
|
end
|
2019-12-04 23:12:49 -05:00
|
|
|
|
|
|
|
defp image_and_user_galleries(_image, nil), do: {[], []}
|
|
|
|
defp image_and_user_galleries(image, user) do
|
|
|
|
image_galleries =
|
|
|
|
Gallery
|
|
|
|
|> where(creator_id: ^user.id)
|
|
|
|
|> join(:inner, [g], gi in Interaction, on: g.id == gi.gallery_id and gi.image_id == ^image.id)
|
2019-12-21 17:57:29 -05:00
|
|
|
|> order_by(desc: :created_at)
|
2019-12-04 23:12:49 -05:00
|
|
|
|> Repo.all()
|
|
|
|
|
|
|
|
image_gallery_ids = Enum.map(image_galleries, & &1.id)
|
|
|
|
|
|
|
|
user_galleries =
|
|
|
|
Gallery
|
|
|
|
|> where(creator_id: ^user.id)
|
|
|
|
|> where([g], g.id not in ^image_gallery_ids)
|
2019-12-21 17:57:29 -05:00
|
|
|
|> order_by(desc: :created_at)
|
2019-12-04 23:12:49 -05:00
|
|
|
|> Repo.all()
|
|
|
|
|
|
|
|
{user_galleries, image_galleries}
|
|
|
|
end
|
2019-12-07 11:48:39 -05:00
|
|
|
|
|
|
|
defp load_image(conn, _opts) do
|
|
|
|
id = conn.params["id"]
|
|
|
|
|
2019-12-07 18:41:31 -05:00
|
|
|
{image, tag_changes, source_changes} =
|
2019-12-07 11:48:39 -05:00
|
|
|
Image
|
|
|
|
|> where(id: ^id)
|
2019-12-07 18:41:31 -05:00
|
|
|
|> join(:inner_lateral, [i], _ in fragment("SELECT COUNT(*) FROM tag_changes t WHERE t.image_id = ?", i.id))
|
|
|
|
|> join(:inner_lateral, [i, _], _ in fragment("SELECT COUNT(*) FROM source_changes s WHERE s.image_id = ?", i.id))
|
2019-12-22 22:13:36 -05:00
|
|
|
|> preload([:tags, :deleter, user: [awards: :badge]])
|
|
|
|
|> select([i, t, s], {i, t.count, s.count})
|
2019-12-07 11:48:39 -05:00
|
|
|
|> Repo.one()
|
2019-12-20 14:06:55 -05:00
|
|
|
|> case do
|
|
|
|
nil ->
|
|
|
|
{nil, nil, nil}
|
|
|
|
|
|
|
|
result ->
|
|
|
|
result
|
|
|
|
end
|
2019-12-07 11:48:39 -05:00
|
|
|
|
|
|
|
cond do
|
|
|
|
is_nil(image) ->
|
|
|
|
PhilomenaWeb.NotFoundPlug.call(conn)
|
|
|
|
|
2019-12-07 11:52:27 -05:00
|
|
|
not is_nil(image.duplicate_id) and not Canada.Can.can?(conn.assigns.current_user, :show, image) ->
|
2019-12-07 11:48:39 -05:00
|
|
|
conn
|
|
|
|
|> put_flash(:info, "The image you were looking for has been marked a duplicate of the image below")
|
|
|
|
|> redirect(to: Routes.image_path(conn, :show, image.duplicate_id))
|
2019-12-20 20:08:53 -05:00
|
|
|
|> Plug.Conn.halt()
|
2019-12-07 11:48:39 -05:00
|
|
|
|
|
|
|
true ->
|
2019-12-07 18:41:31 -05:00
|
|
|
conn
|
|
|
|
|> assign(:image, image)
|
|
|
|
|> assign(:tag_change_count, tag_changes)
|
|
|
|
|> assign(:source_change_count, source_changes)
|
2019-12-07 11:48:39 -05:00
|
|
|
end
|
|
|
|
end
|
2019-08-18 12:17:05 -04:00
|
|
|
end
|