Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 10 additions & 7 deletions lib/philomena/activities.ex
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,11 @@ defmodule Philomena.Activities do

defp watched_definition(%Actor{} = actor, scope) do
with {:ok, {definition, _tags}} <-
ImageSearch.search_string(actor, scope, "my:watched",
ImageSearch.search_string(
actor,
scope,
ImageSearch.default_sort(),
"my:watched",
pagination: %{scope.pagination | page_number: 1}
) do
{:ok, definition}
Expand All @@ -62,8 +66,8 @@ defmodule Philomena.Activities do
ImageSearch.query(
actor,
scope,
ImageSearch.default_sort(),
%{range: %{first_seen_at: %{gt: "now-3d"}}},
sorts: &%{query: &1, sorts: [%{wilson_score: :desc}, %{first_seen_at: :desc}]},
pagination: %{page_number: :rand.uniform(6), page_size: 4}
)

Expand All @@ -78,11 +82,10 @@ defmodule Philomena.Activities do

case watched_definition(actor, scope) do
{:ok, watched_definition} ->
{:ok,
{images_definition, top_scoring_definition, comments_definition, watched_definition}}
{images_definition, top_scoring_definition, comments_definition, watched_definition}

_error ->
{:ok, {images_definition, top_scoring_definition, comments_definition, nil}}
{images_definition, top_scoring_definition, comments_definition, nil}
end
end

Expand Down Expand Up @@ -154,8 +157,8 @@ defmodule Philomena.Activities do
show_nsfw_channels?
)
when is_boolean(show_nsfw_channels?) do
with :ok <- authorize(actor, :show, FrontPage),
{:ok, definitions} <- search_definitions(actor, scope, filter) do
with :ok <- authorize(actor, :show, FrontPage) do
definitions = search_definitions(actor, scope, filter)
{:ok, assemble_front_page(actor, scope, definitions, show_nsfw_channels?)}
end
end
Expand Down
17 changes: 9 additions & 8 deletions lib/philomena/galleries.ex
Original file line number Diff line number Diff line change
Expand Up @@ -194,13 +194,13 @@ defmodule Philomena.Galleries do
{:ok, nil}
end

defp image_sort_direction(%{order_position_asc: true}), do: "asc"
defp image_sort_direction(_gallery), do: "desc"
defp image_sort_direction(%{order_position_asc: true}), do: :asc
defp image_sort_direction(_gallery), do: :desc

defp put_query(list, name, actor, scope, query, pagination) do
defp put_query(list, name, {actor, scope, sort, query}, pagination) do
if pagination.page_number > 0 do
{:ok, {definition, _tags}} =
ImageSearch.search_string(actor, scope, query, pagination: pagination)
ImageSearch.search_string(actor, scope, sort, query, pagination: pagination)

Keyword.put(list, name, {definition, preload(Image, [:sources, tags: :aliases])})
else
Expand All @@ -210,7 +210,8 @@ defmodule Philomena.Galleries do

defp reorder_window(%Actor{} = actor, %Scope{} = scope, %Gallery{} = gallery) do
query = "gallery_id:#{gallery.id}"
scope = %{scope | sf: query, sd: image_sort_direction(gallery)}
sort = ImageSearch.gallery_sort(gallery.id, image_sort_direction(gallery))
params = {actor, scope, sort, query}

limit = scope.pagination.page_size
offset = (scope.pagination.page_number - 1) * limit
Expand All @@ -219,9 +220,9 @@ defmodule Philomena.Galleries do
# with an empty page is inserted if no search was performed.

[]
|> put_query(:images, actor, scope, query, scope.pagination)
|> put_query(:leading, actor, scope, query, %{page_number: offset - 1, page_size: 1})
|> put_query(:trailing, actor, scope, query, %{page_number: offset + limit, page_size: 1})
|> put_query(:images, params, scope.pagination)
|> put_query(:leading, params, %{page_number: offset - 1, page_size: 1})
|> put_query(:trailing, params, %{page_number: offset + limit, page_size: 1})
|> Search.msearch_records_with_hits()
|> Map.put_new(:leading, %Scrivener.Page{})
end
Expand Down
25 changes: 16 additions & 9 deletions lib/philomena/images.ex
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,7 @@ defmodule Philomena.Images do
)
end

defp custom_ordering?(%{sf: sf}) when sf not in [nil, "id", "first_seen_at"], do: true
defp custom_ordering?(_scope), do: false
defp custom_ordering?(%{sf: sf}), do: sf not in [:id, {:field, :first_seen_at}]

defp maybe_jump_to_last_page(
%Actor{
Expand Down Expand Up @@ -802,8 +801,9 @@ defmodule Philomena.Images do
{:ok, %{images: Scrivener.Page.t(), tags: [Tag.t()]}} | {:error, String.t()}
def query_images(%Actor{} = actor, scope, opts \\ []) do
with :ok <- authorize(actor, :index, Image),
sort = ImageSearch.scope_sort(scope),
{:ok, {definition, tags}} <-
ImageSearch.search_string(actor, scope, scope.q) do
ImageSearch.search_string(actor, scope, sort, scope.q) do
preload = Keyword.get(opts, :preload, [:sources, tags: :aliases])
hits = Keyword.get(opts, :hits, custom_ordering?(scope))

Expand Down Expand Up @@ -851,7 +851,8 @@ defmodule Philomena.Images do
{:ok, Scrivener.Page.t()} | {:error, :unauthorized | String.t()}
def list_watched_images(%Actor{} = actor, scope) do
with :ok <- authorize(actor, :index_watched, Image),
{:ok, {definition, _tags}} <- ImageSearch.search_string(actor, scope, "my:watched") do
sort = ImageSearch.scope_sort(scope),
{:ok, {definition, _tags}} <- ImageSearch.search_string(actor, scope, sort, "my:watched") do
{:ok, ImageSearch.execute(definition)}
end
end
Expand Down Expand Up @@ -994,7 +995,7 @@ defmodule Philomena.Images do
@doc group: "Browsing and discovery"
@doc """
Returns the 1-based page number on which the image `image_id`
names appears when all images are listed by descending id, on behalf of
names appears when all images are listed by the default sort, on behalf of
`actor`.

Loading and authorization follow `find_consecutive_image/3`.
Expand All @@ -1012,7 +1013,13 @@ defmodule Philomena.Images do
pagination = %{scope.pagination | page_number: 1}

{definition, _tags} =
ImageSearch.query(actor, scope, %{range: %{id: %{gt: image.id}}}, pagination: pagination)
ImageSearch.query(
actor,
scope,
ImageSearch.default_sort(),
%{range: %{id: %{gt: image.id}}},
pagination: pagination
)

images = ImageSearch.execute(definition, preload: [])

Expand Down Expand Up @@ -1079,8 +1086,8 @@ defmodule Philomena.Images do
ImageSearch.query(
actor,
scope,
ImageSearch.relevance_sort(),
query,
sorts: &%{query: &1, sorts: [%{_score: :desc}]},
pagination: %{scope.pagination | page_number: 1}
)

Expand Down Expand Up @@ -1111,9 +1118,9 @@ defmodule Philomena.Images do
ImageSearch.search_string(
actor,
scope,
ImageSearch.random_sort(),
scope.q || "*",
pagination: %{page_size: 1},
sorts: &ImageSearch.parse_sort(%{"sf" => "random"}, &1)
pagination: %{page_size: 1}
) do
definition
|> ImageSearch.execute(preload: [])
Expand Down
Loading
Loading