2019-08-18 18:17:05 +02:00
|
|
|
defmodule PhilomenaWeb.ImageController do
|
|
|
|
use PhilomenaWeb, :controller
|
|
|
|
|
2019-12-01 03:22:05 +01:00
|
|
|
alias PhilomenaWeb.ImageLoader
|
2019-12-19 21:11:44 +01:00
|
|
|
alias PhilomenaWeb.CommentLoader
|
2019-12-02 16:02:48 +01:00
|
|
|
alias PhilomenaWeb.NotificationCountPlug
|
2020-05-08 04:19:08 +02:00
|
|
|
alias PhilomenaWeb.TextileRenderer
|
2020-01-11 05:20:19 +01:00
|
|
|
|
|
|
|
alias Philomena.{
|
|
|
|
Images,
|
|
|
|
Images.Image,
|
|
|
|
Comments.Comment,
|
2020-05-08 04:19:08 +02:00
|
|
|
Galleries.Gallery
|
2020-01-11 05:20:19 +01:00
|
|
|
}
|
|
|
|
|
2020-08-23 21:47:42 +02:00
|
|
|
alias Philomena.Elasticsearch
|
2019-11-17 03:20:33 +01:00
|
|
|
alias Philomena.Interactions
|
2019-11-17 05:59:24 +01:00
|
|
|
alias Philomena.Comments
|
2019-10-05 02:51:56 +02:00
|
|
|
alias Philomena.Repo
|
2019-08-19 04:03:12 +02:00
|
|
|
import Ecto.Query
|
2019-08-18 18:17:05 +02:00
|
|
|
|
2020-07-21 16:50:33 +02:00
|
|
|
plug PhilomenaWeb.LimitPlug,
|
|
|
|
[time: 10, error: "You may only upload images once every 10 seconds."]
|
|
|
|
when action in [:create]
|
|
|
|
|
2019-12-07 21:13:32 +01:00
|
|
|
plug :load_image when action in [:show]
|
2019-08-29 03:14:54 +02:00
|
|
|
|
2019-11-26 05:51:17 +01:00
|
|
|
plug PhilomenaWeb.FilterBannedUsersPlug when action in [:new, :create]
|
|
|
|
plug PhilomenaWeb.UserAttributionPlug when action in [:create]
|
2020-09-12 19:43:16 +02:00
|
|
|
plug PhilomenaWeb.CaptchaPlug when action in [:new, :show, :create]
|
|
|
|
plug PhilomenaWeb.CheckCaptchaPlug when action in [:create]
|
2020-01-11 05:20:19 +01:00
|
|
|
|
|
|
|
plug PhilomenaWeb.ScraperPlug,
|
|
|
|
[params_name: "image", params_key: "image"] when action in [:create]
|
|
|
|
|
2019-11-29 07:26:05 +01:00
|
|
|
plug PhilomenaWeb.AdvertPlug when action in [:show]
|
2019-11-26 05:51:17 +01:00
|
|
|
|
2019-08-18 18:17:05 +02:00
|
|
|
def index(conn, _params) do
|
2019-12-07 21:16:17 +01:00
|
|
|
{:ok, {images, _tags}} = ImageLoader.search_string(conn, "created_at.lte:3 minutes ago")
|
2019-08-19 04:03:12 +02:00
|
|
|
|
2020-12-03 20:23:32 +01:00
|
|
|
images = Elasticsearch.search_records(images, preload(Image, tags: :aliases))
|
2020-08-23 21:47:42 +02:00
|
|
|
|
2020-01-11 05:20:19 +01:00
|
|
|
interactions = Interactions.user_interactions(images, conn.assigns.current_user)
|
2019-11-17 03:20:33 +01:00
|
|
|
|
2020-01-11 05:20:19 +01:00
|
|
|
render(conn, "index.html",
|
|
|
|
title: "Images",
|
|
|
|
layout_class: "layout--wide",
|
|
|
|
images: images,
|
|
|
|
interactions: interactions
|
|
|
|
)
|
2019-08-18 18:17:05 +02:00
|
|
|
end
|
|
|
|
|
2019-10-04 01:58:54 +02:00
|
|
|
def show(conn, %{"id" => _id}) do
|
2019-11-17 03:20:33 +01:00
|
|
|
image = conn.assigns.image
|
2019-12-01 06:03:45 +01:00
|
|
|
user = conn.assigns.current_user
|
|
|
|
|
|
|
|
Images.clear_notification(image, user)
|
2019-11-17 03:20:33 +01:00
|
|
|
|
2019-12-02 15:55:48 +01:00
|
|
|
# Update the notification ticker in the header
|
|
|
|
conn = NotificationCountPlug.call(conn)
|
|
|
|
|
2019-12-19 21:11:44 +01:00
|
|
|
comments = CommentLoader.load_comments(conn, image)
|
|
|
|
|
2020-05-08 04:19:08 +02:00
|
|
|
rendered = TextileRenderer.render_collection(comments.entries, conn)
|
2019-11-11 00:35:52 +01:00
|
|
|
|
2020-01-11 05:20:19 +01:00
|
|
|
comments = %{comments | entries: Enum.zip(comments.entries, rendered)}
|
2019-11-11 00:35:52 +01:00
|
|
|
|
2019-11-11 23:57:29 +01:00
|
|
|
description =
|
2019-11-17 03:20:33 +01:00
|
|
|
%{body: image.description}
|
2020-05-08 04:19:08 +02:00
|
|
|
|> TextileRenderer.render_one(conn)
|
2019-11-11 23:57:29 +01:00
|
|
|
|
2020-01-11 05:20:19 +01:00
|
|
|
interactions = Interactions.user_interactions([image], conn.assigns.current_user)
|
2019-11-17 03:20:33 +01:00
|
|
|
|
2019-11-17 05:59:24 +01:00
|
|
|
comment_changeset =
|
|
|
|
%Comment{}
|
|
|
|
|> Comments.change_comment()
|
|
|
|
|
2019-11-25 03:16:22 +01:00
|
|
|
image_changeset =
|
|
|
|
image
|
|
|
|
|> Images.change_image()
|
|
|
|
|
2020-01-11 05:20:19 +01:00
|
|
|
watching = Images.subscribed?(image, conn.assigns.current_user)
|
2019-11-17 18:50:42 +01:00
|
|
|
|
2020-05-01 20:09:57 +02:00
|
|
|
user_galleries = user_galleries(image, conn.assigns.current_user)
|
2019-12-05 05:12:49 +01:00
|
|
|
|
2019-12-08 05:53:18 +01:00
|
|
|
assigns = [
|
2019-11-17 05:59:24 +01:00
|
|
|
image: image,
|
|
|
|
comments: comments,
|
2019-11-25 03:16:22 +01:00
|
|
|
image_changeset: image_changeset,
|
2019-11-17 05:59:24 +01:00
|
|
|
comment_changeset: comment_changeset,
|
2019-12-05 05:12:49 +01:00
|
|
|
user_galleries: user_galleries,
|
2019-11-17 05:59:24 +01:00
|
|
|
description: description,
|
2019-11-17 18:50:42 +01:00
|
|
|
interactions: interactions,
|
2019-11-17 23:51:14 +01:00
|
|
|
watching: watching,
|
2019-12-16 20:24:38 +01:00
|
|
|
layout_class: "layout--wide",
|
2019-12-20 15:44:05 +01:00
|
|
|
title: "##{image.id} - #{image.tag_list_cache}"
|
2019-12-08 05:53:18 +01:00
|
|
|
]
|
|
|
|
|
|
|
|
if image.hidden_from_users do
|
|
|
|
render(conn, "deleted.html", assigns)
|
|
|
|
else
|
|
|
|
render(conn, "show.html", assigns)
|
|
|
|
end
|
2019-08-18 18:17:05 +02:00
|
|
|
end
|
2019-11-26 05:51:17 +01:00
|
|
|
|
|
|
|
def new(conn, _params) do
|
|
|
|
changeset =
|
|
|
|
%Image{}
|
|
|
|
|> Images.change_image()
|
|
|
|
|
2019-12-16 20:24:38 +01:00
|
|
|
render(conn, "new.html", title: "New Image", changeset: changeset)
|
2019-11-26 05:51:17 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def create(conn, %{"image" => image_params}) do
|
|
|
|
attributes = conn.assigns.attributes
|
|
|
|
|
|
|
|
case Images.create_image(attributes, image_params) do
|
|
|
|
{:ok, %{image: image}} ->
|
2020-06-12 18:56:11 +02:00
|
|
|
PhilomenaWeb.Endpoint.broadcast!(
|
|
|
|
"firehose",
|
|
|
|
"image:create",
|
|
|
|
PhilomenaWeb.Api.Json.ImageView.render("show.json", %{image: image, interactions: []})
|
|
|
|
)
|
|
|
|
|
2019-11-26 05:51:17 +01: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-05 05:12:49 +01:00
|
|
|
|
2020-05-01 20:09:57 +02:00
|
|
|
defp user_galleries(_image, nil), do: []
|
|
|
|
|
|
|
|
defp user_galleries(image, user) do
|
|
|
|
Gallery
|
|
|
|
|> where(creator_id: ^user.id)
|
|
|
|
|> join(
|
|
|
|
:inner_lateral,
|
|
|
|
[g],
|
|
|
|
_ in fragment(
|
|
|
|
"SELECT EXISTS(SELECT 1 FROM gallery_interactions gi WHERE gi.image_id = ? AND gi.gallery_id = ?)",
|
|
|
|
^image.id,
|
|
|
|
g.id
|
2020-01-11 05:20:19 +01:00
|
|
|
)
|
2020-05-01 20:09:57 +02:00
|
|
|
)
|
|
|
|
|> select([g, e], {g, e.exists})
|
|
|
|
|> order_by(desc: :updated_at)
|
|
|
|
|> Repo.all()
|
2019-12-05 05:12:49 +01:00
|
|
|
end
|
2019-12-07 17:48:39 +01:00
|
|
|
|
|
|
|
defp load_image(conn, _opts) do
|
|
|
|
id = conn.params["id"]
|
|
|
|
|
2019-12-08 00:41:31 +01:00
|
|
|
{image, tag_changes, source_changes} =
|
2019-12-07 17:48:39 +01:00
|
|
|
Image
|
|
|
|
|> where(id: ^id)
|
2020-01-11 05:20:19 +01: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)
|
|
|
|
)
|
2020-12-03 20:23:32 +01:00
|
|
|
|> preload([:deleter, user: [awards: :badge], tags: :aliases])
|
2019-12-23 04:13:36 +01:00
|
|
|
|> select([i, t, s], {i, t.count, s.count})
|
2019-12-07 17:48:39 +01:00
|
|
|
|> Repo.one()
|
2019-12-20 20:06:55 +01:00
|
|
|
|> case do
|
|
|
|
nil ->
|
|
|
|
{nil, nil, nil}
|
|
|
|
|
|
|
|
result ->
|
|
|
|
result
|
|
|
|
end
|
2019-12-07 17:48:39 +01:00
|
|
|
|
|
|
|
cond do
|
|
|
|
is_nil(image) ->
|
|
|
|
PhilomenaWeb.NotFoundPlug.call(conn)
|
|
|
|
|
2020-01-11 05:20:19 +01:00
|
|
|
not is_nil(image.duplicate_id) and
|
|
|
|
not Canada.Can.can?(conn.assigns.current_user, :show, image) ->
|
2019-12-07 17:48:39 +01:00
|
|
|
conn
|
2020-01-11 05:20:19 +01:00
|
|
|
|> put_flash(
|
|
|
|
:info,
|
|
|
|
"The image you were looking for has been marked a duplicate of the image below"
|
|
|
|
)
|
2019-12-07 17:48:39 +01:00
|
|
|
|> redirect(to: Routes.image_path(conn, :show, image.duplicate_id))
|
2020-05-04 09:18:36 +02:00
|
|
|
|> halt()
|
2019-12-07 17:48:39 +01:00
|
|
|
|
|
|
|
true ->
|
2019-12-08 00:41:31 +01:00
|
|
|
conn
|
|
|
|
|> assign(:image, image)
|
|
|
|
|> assign(:tag_change_count, tag_changes)
|
|
|
|
|> assign(:source_change_count, source_changes)
|
2019-12-07 17:48:39 +01:00
|
|
|
end
|
|
|
|
end
|
2019-08-18 18:17:05 +02:00
|
|
|
end
|