From 4b52640efc0f497d705a11f6096b64cef50d3a15 Mon Sep 17 00:00:00 2001 From: "Liam P. White" Date: Tue, 27 Aug 2019 21:20:12 -0400 Subject: [PATCH] flesh out my:watched --- lib/philomena/images/query.ex | 84 +++++++++++++++++++++++++++++++--- lib/philomena/search/string.ex | 9 ++++ 2 files changed, 87 insertions(+), 6 deletions(-) create mode 100644 lib/philomena/search/string.ex diff --git a/lib/philomena/images/query.ex b/lib/philomena/images/query.ex index e2a2cfc8..8523d7db 100644 --- a/lib/philomena/images/query.ex +++ b/lib/philomena/images/query.ex @@ -1,5 +1,7 @@ defmodule Philomena.Images.Query do import Philomena.Search.Parser + import Philomena.Search.String + alias Philomena.Repo defparser("anonymous", int: @@ -43,9 +45,44 @@ defmodule Philomena.Images.Query do %{user: %{id: id}}, "downvotes" -> %{term: %{downvoter_ids: id}} - %{user: _u}, "watched" -> - # todo - %{query: %{match_all: %{}}} + %{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}} end }, aliases: %{ @@ -80,9 +117,44 @@ defmodule Philomena.Images.Query do %{user: %{id: id}}, "downvotes" -> %{term: %{downvoter_ids: id}} - %{user: _u}, "watched" -> - # todo - %{query: %{match_all: %{}}} + %{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}} end }, aliases: %{ diff --git a/lib/philomena/search/string.ex b/lib/philomena/search/string.ex new file mode 100644 index 00000000..7d9dddee --- /dev/null +++ b/lib/philomena/search/string.ex @@ -0,0 +1,9 @@ +defmodule Philomena.Search.String do + def normalize(str) do + str + |> String.replace("\r", "") + |> String.split("\n") + |> Enum.map(fn s -> "(#{s})" end) + |> Enum.join(" || ") + end +end