philomena/lib/philomena/images/query.ex

174 lines
5.7 KiB
Elixir
Raw Normal View History

2019-08-24 17:35:30 +02:00
defmodule Philomena.Images.Query do
2019-08-28 01:37:26 +02:00
import Philomena.Search.Parser
2019-08-28 03:20:12 +02:00
import Philomena.Search.String
alias Philomena.Repo
2019-08-28 01:37:26 +02:00
2019-08-28 01:37:44 +02:00
defparser("anonymous",
2019-08-26 15:57:04 +02:00
int:
~W(id width height comment_count score upvotes downvotes faves uploader_id faved_by_id tag_count),
2019-08-26 00:53:13 +02:00
float: ~W(aspect_ratio wilson_score),
date: ~W(created_at updated_at first_seen_at),
literal: ~W(faved_by orig_sha512_hash sha512_hash uploader source_url original_format),
2019-08-27 02:00:39 +02:00
ngram: ~W(description),
custom: ~W(gallery_id),
transforms: %{
"gallery_id" => fn _ctx, value ->
%{nested: %{path: :galleries, query: %{term: %{"galleries.id" => value}}}}
end
},
aliases: %{
"faved_by" => "favourited_by_users",
"faved_by_id" => "favourited_by_user_ids"
},
2019-08-28 01:37:26 +02:00
default: "namespaced_tags.name"
2019-08-28 01:37:44 +02:00
)
2019-08-28 01:37:26 +02:00
2019-08-28 01:37:44 +02:00
defparser("user",
2019-08-28 01:37:26 +02:00
int:
~W(id width height comment_count score upvotes downvotes faves uploader_id faved_by_id tag_count),
float: ~W(aspect_ratio wilson_score),
date: ~W(created_at updated_at first_seen_at),
literal: ~W(faved_by orig_sha512_hash sha512_hash uploader source_url original_format),
ngram: ~W(description),
custom: ~W(gallery_id my),
transforms: %{
"gallery_id" => fn _ctx, value ->
%{nested: %{path: :galleries, query: %{term: %{"galleries.id" => value}}}}
end,
"my" => fn
2019-08-28 01:37:44 +02:00
%{user: %{id: id}}, "faves" ->
%{term: %{favourited_by_user_ids: id}}
%{user: %{id: id}}, "upvotes" ->
%{term: %{upvoter_ids: id}}
%{user: %{id: id}}, "downvotes" ->
%{term: %{downvoter_ids: id}}
2019-08-28 03:20:12 +02:00
%{watch: true}, "watched" ->
raise ArgumentError, "Recursive watchlists are not allowed."
%{user: user} = ctx, "watched" ->
ctx = Map.merge(ctx, %{watch: true})
tag_include = %{terms: %{tag_ids: user.watched_tag_ids}}
{:ok, include_query} =
Philomena.Images.Query.user_parser(ctx, user.watched_images_query |> normalize())
{:ok, exclude_query} =
Philomena.Images.Query.user_parser(
ctx,
user.watched_images_exclude_query |> normalize()
)
should = [tag_include, include_query]
must_not = [exclude_query]
must_not =
if user.no_spoilered_in_watched do
user = user |> Repo.preload(:current_filter)
tag_exclude = %{terms: %{tag_ids: user.current_filter.spoilered_tag_ids}}
{:ok, spoiler_query} =
Philomena.Images.Query.user_parser(
ctx,
user.current_filter.spoilered_complex_str |> normalize()
)
[tag_exclude, spoiler_query | must_not]
else
must_not
end
%{bool: %{should: should, must_not: must_not}}
2019-08-28 01:37:26 +02:00
end
},
aliases: %{
"faved_by" => "favourited_by_users",
"faved_by_id" => "favourited_by_user_ids"
},
default: "namespaced_tags.name"
2019-08-28 01:37:44 +02:00
)
2019-08-28 01:37:26 +02:00
2019-08-28 01:37:44 +02:00
defparser("moderator",
2019-08-28 01:37:26 +02:00
int:
~W(id width height comment_count score upvotes downvotes faves uploader_id faved_by_id upvoted_by_id downvoted_by_id tag_count true_uploader_id hidden_by_id deleted_by_user-id),
float: ~W(aspect_ratio wilson_score),
date: ~W(created_at updated_at first_seen_at),
2019-08-28 01:37:44 +02:00
literal:
~W(faved_by orig_sha512_hash sha512_hash uploader source_url original_format fingerprint upvoted_by downvoted_by true_uploader hidden_by deleted_by_user),
2019-08-28 01:37:26 +02:00
ngram: ~W(description deletion_reason),
ip: ~W(ip),
bool: ~W(deleted),
custom: ~W(gallery_id my),
transforms: %{
"gallery_id" => fn _ctx, value ->
%{nested: %{path: :galleries, query: %{term: %{"galleries.id" => value}}}}
end,
"my" => fn
2019-08-28 01:37:44 +02:00
%{user: %{id: id}}, "faves" ->
%{term: %{favourited_by_user_ids: id}}
%{user: %{id: id}}, "upvotes" ->
%{term: %{upvoter_ids: id}}
%{user: %{id: id}}, "downvotes" ->
%{term: %{downvoter_ids: id}}
2019-08-28 03:20:12 +02:00
%{watch: true}, "watched" ->
raise ArgumentError, "Recursive watchlists are not allowed."
%{user: user} = ctx, "watched" ->
ctx = Map.merge(ctx, %{watch: true})
tag_include = %{terms: %{tag_ids: user.watched_tag_ids}}
{:ok, include_query} =
Philomena.Images.Query.moderator_parser(ctx, user.watched_images_query |> normalize())
{:ok, exclude_query} =
Philomena.Images.Query.moderator_parser(
ctx,
user.watched_images_exclude_query |> normalize()
)
should = [tag_include, include_query]
must_not = [exclude_query]
must_not =
if user.no_spoilered_in_watched do
user = user |> Repo.preload(:current_filter)
tag_exclude = %{terms: %{tag_ids: user.current_filter.spoilered_tag_ids}}
{:ok, spoiler_query} =
Philomena.Images.Query.moderator_parser(
ctx,
user.current_filter.spoilered_complex_str |> normalize()
)
[tag_exclude, spoiler_query | must_not]
else
must_not
end
%{bool: %{should: should, must_not: must_not}}
2019-08-28 01:37:26 +02:00
end
},
aliases: %{
2019-08-28 01:37:44 +02:00
"faved_by" => "favourited_by_users",
"upvoted_by" => "upvoters",
"downvoted_by" => "downvoters",
"faved_by_id" => "favourited_by_user_ids",
"upvoted_by_id" => "upvoter_ids",
2019-08-28 01:37:26 +02:00
"downvoted_by_id" => "downvoter_ids",
2019-08-28 01:37:44 +02:00
"hidden_by" => "hidden_by_users",
"hidden_by_id" => "hidden_by_user_ids",
"deleted" => "hidden_from_users"
2019-08-28 01:37:26 +02:00
},
default: "namespaced_tags.name"
2019-08-28 01:37:44 +02:00
)
2019-08-26 15:57:04 +02:00
end