fix a bunch of navigation weirdness

This commit is contained in:
byte[] 2019-12-26 17:43:32 -05:00
parent 625ed5a594
commit f443aa5386
9 changed files with 48 additions and 28 deletions

View file

@ -117,6 +117,7 @@ defmodule Philomena.Elasticsearch do
entries = Enum.map(results["hits"]["hits"], &String.to_integer(&1["_id"]))
Logger.debug("[Elasticsearch] Query took #{time}ms")
Logger.debug("[Elasticsearch] #{Jason.encode!(elastic_query)}")
%Scrivener.Page{
entries: entries,

View file

@ -8,15 +8,15 @@ defmodule Philomena.ImageNavigator do
# We get consecutive images by finding all images greater than or less than
# the current image, and grabbing the FIRST one
@range_comparison_for_order %{
asc: :gt,
desc: :lt
"asc" => :gt,
"desc" => :lt
}
# If we didn't reverse for prev, it would be the LAST image, which would
# make Elasticsearch choke on deep pagination
@order_for_dir %{
next: %{"asc" => :asc, "desc" => :desc},
prev: %{"asc" => :desc, "desc" => :asc}
next: %{"asc" => "asc", "desc" => "desc"},
prev: %{"asc" => "desc", "desc" => "asc"}
}
@range_map %{
@ -28,7 +28,7 @@ defmodule Philomena.ImageNavigator do
image_index =
Image
|> where(id: ^image.id)
|> preload(:gallery_interactions)
|> preload([:gallery_interactions, tags: :aliases])
|> Repo.one()
|> Map.merge(empty_fields())
|> ElasticsearchIndex.as_json()
@ -69,13 +69,13 @@ defmodule Philomena.ImageNavigator do
defp extract_filters(%{"galleries.position" => term} = sort, image, rel) do
# Extract gallery ID and current position
gid = term["nested_filter"]["term"]["galleries.id"]
gid = term.nested_filter.term."galleries.id"
pos = Enum.find(image[:galleries], & &1.id == gid).position
# Sort in the other direction if we are going backwards
sd = term["order"]
order = @order_for_dir[rel][sd]
term = %{term | "order" => order}
sd = term.order
order = @order_for_dir[rel][to_string(sd)]
term = %{term | order: order}
sort = %{sort | "galleries.position" => term}
filter = gallery_range_filter(@range_comparison_for_order[order], pos)
@ -164,8 +164,7 @@ defmodule Philomena.ImageNavigator do
upvoters: [],
downvoters: [],
favers: [],
hiders: [],
tags: []
hiders: []
}
end

View file

@ -14,7 +14,6 @@ defmodule Philomena.ImageSorter do
comment_count
tag_count
wilson_score
_score
)
def parse_sort(params) do
@ -27,7 +26,11 @@ defmodule Philomena.ImageSorter do
defp parse_sd(_params), do: "desc"
defp parse_sf(%{"sf" => sf}, sd) when sf in @allowed_fields do
%{queries: [], sorts: [%{sf => sd}]}
%{queries: [], sorts: [%{sf => sd}], constant_score: true}
end
defp parse_sf(%{"sf" => "_score"}, sd) do
%{queries: [], sorts: [%{"_score" => sd}], constant_score: false}
end
defp parse_sf(%{"sf" => "random"}, sd) do
@ -52,23 +55,24 @@ defmodule Philomena.ImageSorter do
sorts: [%{
"galleries.position" => %{
order: sd,
nested_path: :galleries,
nested_filter: %{
term: %{
"galleries.id": gallery
nested: %{
path: :galleries,
filter: %{
term: %{"galleries.id" => gallery}
}
}
}
}]
}],
constant_score: true
}
_ ->
%{queries: [%{match_none: %{}}], sorts: []}
%{queries: [%{match_none: %{}}], sorts: [], constant_score: true}
end
end
defp parse_sf(_params, sd) do
%{queries: [], sorts: [%{"created_at" => sd}]}
%{queries: [], sorts: [%{"created_at" => sd}], constant_score: true}
end
defp random_query(seed, sd) do
@ -76,11 +80,12 @@ defmodule Philomena.ImageSorter do
queries: [%{
function_score: %{
query: %{match_all: %{}},
random_score: %{seed: seed},
random_score: %{seed: seed, field: :id},
boost_mode: :replace
}
}],
sorts: [%{"_score" => sd}]
sorts: [%{"_score" => sd}],
constant_score: true
}
end
end

View file

@ -9,7 +9,7 @@ defmodule PhilomenaWeb.SearchController do
user = conn.assigns.current_user
sort = ImageSorter.parse_sort(params)
case ImageLoader.search_string(conn, params["q"], sorts: sort.sorts, queries: sort.queries) do
case ImageLoader.search_string(conn, params["q"], sorts: sort.sorts, queries: sort.queries, constant_score: sort.constant_score) do
{:ok, {images, tags}} ->
interactions =
Interactions.user_interactions(images, user)

View file

@ -18,14 +18,16 @@ defmodule PhilomenaWeb.ImageLoader do
end
def query(conn, body, options \\ []) do
sort_queries = Keyword.get(options, :queries, [])
sort_sorts = Keyword.get(options, :sorts, [%{created_at: :desc}])
pagination = Keyword.get(options, :pagination, conn.assigns.image_pagination)
queryable = Keyword.get(options, :queryable, Image |> preload(:tags))
sort_queries = Keyword.get(options, :queries, [])
sort_sorts = Keyword.get(options, :sorts, [%{created_at: :desc}])
pagination = Keyword.get(options, :pagination, conn.assigns.image_pagination)
queryable = Keyword.get(options, :queryable, Image |> preload(:tags))
constant_score = Keyword.get(options, :constant_score, true)
user = conn.assigns.current_user
filter = conn.assigns.compiled_filter
filters = create_filters(conn, user, filter)
body = maybe_constant_score(body, constant_score)
records =
Elasticsearch.search_records(
@ -93,6 +95,9 @@ defmodule PhilomenaWeb.ImageLoader do
defp maybe_custom_hide(filters, _user, _param),
do: filters
defp maybe_constant_score(body, false), do: body
defp maybe_constant_score(body, _), do: %{bool: %{filter: body}}
# TODO: the search parser should try to optimize queries
defp search_tag_name(%{term: %{"namespaced_tags.name" => tag_name}}), do: [tag_name]
defp search_tag_name(_other_query), do: []

View file

@ -8,7 +8,7 @@ elixir:
#sortable
.block#imagelist-container
section.block__header.flex
h1.block__header__title.hide-mobile
span.block__header__title.hide-mobile
' Viewing gallery
strong>
= @gallery.title
@ -16,6 +16,8 @@ elixir:
= pagination
.flex__right
= render PhilomenaWeb.ImageView, "_random_button.html", conn: @conn, params: scope
a href=Routes.gallery_report_path(@conn, :new, @gallery)
i.fa.fa-exclamation-triangle>
span.hide-mobile Report

View file

@ -0,0 +1,3 @@
a href=Routes.image_random_path(@conn, :index, @params) title="Random Image"
i.fa.fa-random>
span.hide-mobile.hide-limited-desktop Random Image

View file

@ -16,6 +16,7 @@ elixir:
= pagination
.flex__right
= random_button @conn, params
= hidden_toggle @conn, route, params
= deleted_toggle @conn, route, params
= quick_tag @conn

View file

@ -178,6 +178,10 @@ defmodule PhilomenaWeb.ImageView do
def hides_images?(conn), do: can?(conn, :hide, %Philomena.Images.Image{})
def random_button(conn, params) do
render PhilomenaWeb.ImageView, "_random_button.html", conn: conn, params: params
end
def hidden_toggle(%{assigns: %{current_user: nil}}, _route, _params), do: nil
def hidden_toggle(conn, route, params) do
render PhilomenaWeb.ImageView, "_hidden_toggle.html", route: route, params: params, conn: conn