From c972f00cdc3cb2652b0ae26107150ae84d8eaca2 Mon Sep 17 00:00:00 2001 From: SomewhatDamaged Date: Mon, 27 Jan 2020 10:04:30 +1100 Subject: [PATCH] Featured image hiding (#24) * Featured image hiding This makes sure the Featured Image on the front page obeys user hiding. * Optimized Took the `hidden=1` check out of the `WHERE` clause. * proper anti join Co-authored-by: liamwhite --- .../controllers/activity_controller.ex | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/lib/philomena_web/controllers/activity_controller.ex b/lib/philomena_web/controllers/activity_controller.ex index be591013..359b5b07 100644 --- a/lib/philomena_web/controllers/activity_controller.ex +++ b/lib/philomena_web/controllers/activity_controller.ex @@ -71,6 +71,7 @@ defmodule PhilomenaWeb.ActivityController do featured_image = Image |> join(:inner, [i], f in ImageFeature, on: [image_id: i.id]) + |> filter_hidden(user, conn.params["hidden"]) |> order_by([i, f], desc: f.created_at) |> limit(1) |> preload([:tags]) @@ -116,4 +117,17 @@ defmodule PhilomenaWeb.ActivityController do layout_class: "layout--wide" ) end + + def filter_hidden(featured_image, nil, _hidden) do + featured_image + end + + def filter_hidden(featured_image, _user, "1") do + featured_image + end + + def filter_hidden(featured_image, user, _hidden) do + featured_image + |> where([i], fragment("NOT EXISTS(SELECT 1 FROM image_hides WHERE image_id = ? AND user_id = ?)", i.id, ^user.id)) + end end