From 3cd51526d925b7b56cc0b20d01d4d31f2f842364 Mon Sep 17 00:00:00 2001 From: "byte[]" Date: Fri, 20 Dec 2019 18:02:03 -0500 Subject: [PATCH] add related endpoint and controller --- .../controllers/image/related_controller.ex | 61 +++++++++++++++++++ lib/philomena_web/router.ex | 1 + .../templates/image/related/index.html.slime | 11 ++++ lib/philomena_web/views/image/related_view.ex | 3 + .../controllers/controller_callbacks.ex | 1 + 5 files changed, 77 insertions(+) create mode 100644 lib/philomena_web/controllers/image/related_controller.ex create mode 100644 lib/philomena_web/templates/image/related/index.html.slime create mode 100644 lib/philomena_web/views/image/related_view.ex diff --git a/lib/philomena_web/controllers/image/related_controller.ex b/lib/philomena_web/controllers/image/related_controller.ex new file mode 100644 index 00000000..21b42237 --- /dev/null +++ b/lib/philomena_web/controllers/image/related_controller.ex @@ -0,0 +1,61 @@ +defmodule PhilomenaWeb.Image.RelatedController do + use PhilomenaWeb, :controller + + alias PhilomenaWeb.ImageLoader + alias Philomena.Interactions + alias Philomena.Images.Image + + plug PhilomenaWeb.CanaryMapPlug, index: :show + plug :load_and_authorize_resource, model: Image, id_name: "image_id", persisted: true, preload: [:tags, :faves] + + def index(conn, _params) do + image = conn.assigns.image + user = conn.assigns.current_user + + tags_to_match = + image.tags + |> Enum.reject(& &1.category == "rating") + |> Enum.sort_by(& &1.images_count) + |> Enum.take(10) + |> Enum.map(& &1.id) + + low_count_tags = + tags_to_match + |> Enum.take(5) + |> Enum.map(&%{term: %{tag_ids: &1}}) + + high_count_tags = + tags_to_match + |> Enum.take(-5) + |> Enum.map(&%{term: %{tag_ids: &1}}) + + favs_to_match = + image.faves + |> Enum.take(11) + |> Enum.map(&%{term: %{favourited_by_user_ids: &1.user_id}}) + + query = + %{ + bool: %{ + must: [ + %{bool: %{should: low_count_tags, boost: 2}}, + %{bool: %{should: high_count_tags, boost: 3, minimum_should_match: "5%"}}, + %{bool: %{should: favs_to_match, boost: 0.2, minimum_should_match: "5%"}} + ], + must_not: %{term: %{id: image.id}} + } + } + + {images, _tags} = + ImageLoader.query( + conn, + query, + sorts: [%{_score: :desc}], + pagination: %{conn.assigns.image_pagination | page_number: 1} + ) + + interactions = Interactions.user_interactions(images, user) + + render(conn, "index.html", title: "##{image.id} - Related Images", layout_class: "wide", images: images, interactions: interactions) + end +end diff --git a/lib/philomena_web/router.ex b/lib/philomena_web/router.ex index 2987ace4..799079e6 100644 --- a/lib/philomena_web/router.ex +++ b/lib/philomena_web/router.ex @@ -275,6 +275,7 @@ defmodule PhilomenaWeb.Router do resources "/random", RandomController, only: [:index] end resources "/images", ImageController, only: [:index, :show, :new, :create] do + resources "/related", Image.RelatedController, only: [:index] resources "/comments", Image.CommentController, only: [:index, :show, :create] do resources "/reports", Image.Comment.ReportController, only: [:new, :create] resources "/history", Image.Comment.HistoryController, only: [:index] diff --git a/lib/philomena_web/templates/image/related/index.html.slime b/lib/philomena_web/templates/image/related/index.html.slime new file mode 100644 index 00000000..1feabfa1 --- /dev/null +++ b/lib/philomena_web/templates/image/related/index.html.slime @@ -0,0 +1,11 @@ +.block#imagelist-container + .block__header + .block__header__title.hide-mobile + ' Viewing related images for + strong> + | # + = @image.id + + .block__content.js-resizable-media-container + = for image <- @images do + = render PhilomenaWeb.ImageView, "_image_box.html", image: image, link: Routes.image_path(@conn, :show, image), size: :thumb, conn: @conn diff --git a/lib/philomena_web/views/image/related_view.ex b/lib/philomena_web/views/image/related_view.ex new file mode 100644 index 00000000..c0d95288 --- /dev/null +++ b/lib/philomena_web/views/image/related_view.ex @@ -0,0 +1,3 @@ +defmodule PhilomenaWeb.Image.RelatedView do + use PhilomenaWeb, :view +end diff --git a/lib/pow_lockout/phoenix/controllers/controller_callbacks.ex b/lib/pow_lockout/phoenix/controllers/controller_callbacks.ex index 91936a0c..a410245a 100644 --- a/lib/pow_lockout/phoenix/controllers/controller_callbacks.ex +++ b/lib/pow_lockout/phoenix/controllers/controller_callbacks.ex @@ -81,6 +81,7 @@ defmodule PowLockout.Phoenix.ControllerCallbacks do |> Conn.assign(:changeset, Plug.change_user(conn, conn.params["user"])) |> Controller.put_flash(:error, messages(conn).invalid_credentials(conn)) |> Controller.render("new.html") + |> Conn.halt() end @doc """