mirror of
https://github.com/philomena-dev/philomena.git
synced 2024-11-27 13:47:58 +01:00
scoped random navigation
This commit is contained in:
parent
3668bac1e2
commit
4c98a6f4ec
4 changed files with 64 additions and 3 deletions
54
lib/philomena_web/controllers/image/random_controller.ex
Normal file
54
lib/philomena_web/controllers/image/random_controller.ex
Normal file
|
@ -0,0 +1,54 @@
|
||||||
|
defmodule PhilomenaWeb.Image.RandomController do
|
||||||
|
use PhilomenaWeb, :controller
|
||||||
|
|
||||||
|
alias Philomena.Images.Query
|
||||||
|
alias Philomena.Images.Image
|
||||||
|
alias Philomena.ImageSorter
|
||||||
|
|
||||||
|
def index(conn, params) do
|
||||||
|
user = conn.assigns.current_user
|
||||||
|
filter = conn.assigns.compiled_filter
|
||||||
|
|
||||||
|
scope = Philomena.ImageScope.scope(conn)
|
||||||
|
query = query(user, params)
|
||||||
|
random_id = random_image_id(query, filter)
|
||||||
|
|
||||||
|
if random_id do
|
||||||
|
redirect(conn, to: Routes.image_path(conn, :show, random_id, scope))
|
||||||
|
else
|
||||||
|
redirect(conn, external: conn.assigns.referrer)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
defp query(user, %{"q" => q}) do
|
||||||
|
{:ok, query} = Query.compile(user, q)
|
||||||
|
|
||||||
|
query
|
||||||
|
end
|
||||||
|
defp query(_user, _), do: %{match_all: %{}}
|
||||||
|
|
||||||
|
defp random_image_id(query, filter) do
|
||||||
|
sort = ImageSorter.parse_sort(%{"sf" => "random"})
|
||||||
|
|
||||||
|
Image.search_records(
|
||||||
|
%{
|
||||||
|
query: %{
|
||||||
|
bool: %{
|
||||||
|
must: List.flatten([sort.queries, query]),
|
||||||
|
must_not: [
|
||||||
|
filter,
|
||||||
|
%{term: %{hidden_from_users: true}}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
},
|
||||||
|
sort: sort.sorts
|
||||||
|
},
|
||||||
|
%{page_size: 1}
|
||||||
|
)
|
||||||
|
|> Enum.to_list()
|
||||||
|
|> unwrap()
|
||||||
|
end
|
||||||
|
|
||||||
|
defp unwrap([image]), do: image.id
|
||||||
|
defp unwrap([]), do: nil
|
||||||
|
end
|
|
@ -91,6 +91,7 @@ defmodule PhilomenaWeb.Router do
|
||||||
resources "/activity", ActivityController, only: [:index]
|
resources "/activity", ActivityController, only: [:index]
|
||||||
scope "/images", Image, as: :image do
|
scope "/images", Image, as: :image do
|
||||||
resources "/scrape", ScrapeController, only: [:create]
|
resources "/scrape", ScrapeController, only: [:create]
|
||||||
|
resources "/random", RandomController, only: [:index]
|
||||||
end
|
end
|
||||||
resources "/images", ImageController, only: [:index, :show, :new, :create] do
|
resources "/images", ImageController, only: [:index, :show, :new, :create] do
|
||||||
resources "/comments", Image.CommentController, only: [:index, :show, :create]
|
resources "/comments", Image.CommentController, only: [:index, :show, :create]
|
||||||
|
|
|
@ -7,7 +7,7 @@
|
||||||
i.fa.fa-chevron-up
|
i.fa.fa-chevron-up
|
||||||
a.js-next href=Routes.image_navigate_path(@conn, :index, @image, [rel: "next"] ++ scope(@conn)) title="Next Image (k)"
|
a.js-next href=Routes.image_navigate_path(@conn, :index, @image, [rel: "next"] ++ scope(@conn)) title="Next Image (k)"
|
||||||
i.fa.fa-chevron-right
|
i.fa.fa-chevron-right
|
||||||
a.js-rand href="/" title="Random (r)"
|
a.js-rand href=Routes.image_random_path(@conn, :index, scope(@conn)) title="Random (r)"
|
||||||
i.fa.fa-random
|
i.fa.fa-random
|
||||||
.stretched-mobile-links
|
.stretched-mobile-links
|
||||||
a.interaction--fave href="#" rel="nofollow" data-image-id=@image.id
|
a.interaction--fave href="#" rel="nofollow" data-image-id=@image.id
|
||||||
|
|
|
@ -1,6 +1,12 @@
|
||||||
.hide-mobile
|
.hide-mobile
|
||||||
a.header__link href="/images"
|
.dropdown.header__dropdown
|
||||||
| Images
|
a.header__link href="/images"
|
||||||
|
| Images
|
||||||
|
span data-click-preventdefault="true"
|
||||||
|
i.fa.fa-caret-down<
|
||||||
|
.dropdown__content
|
||||||
|
a.header__link href="/images/random"
|
||||||
|
| Random
|
||||||
.dropdown.header__dropdown
|
.dropdown.header__dropdown
|
||||||
a.header__link href="/activity"
|
a.header__link href="/activity"
|
||||||
| Activity
|
| Activity
|
||||||
|
|
Loading…
Reference in a new issue