Skip to content

Commit

Permalink
Featured image hiding (#24)
Browse files Browse the repository at this point in the history
* 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 <liamwhite@users.noreply.github.com>
  • Loading branch information
SomewhatDamaged and liamwhite committed Jan 26, 2020
1 parent f558e76 commit c972f00
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions lib/philomena_web/controllers/activity_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand Down Expand Up @@ -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

0 comments on commit c972f00

Please sign in to comment.