apply filter evaluator

This commit is contained in:
byte[] 2019-12-01 12:11:00 -05:00
parent 9d6103d77c
commit 25319f1c76
15 changed files with 105 additions and 41 deletions

View file

@ -1,4 +1,5 @@
defmodule Philomena.Textile.Renderer do
# todo: belongs in PhilomenaWeb
alias Textile.Parser
alias Philomena.Images.Image
alias Philomena.Repo
@ -10,11 +11,11 @@ defmodule Philomena.Textile.Renderer do
image_transform: &Camo.Image.image_url/1
}
def render_one(post) do
hd(render_collection([post]))
def render_one(post, conn) do
hd(render_collection([post], conn))
end
def render_collection(posts) do
def render_collection(posts, conn) do
parsed =
posts
|> Enum.map(fn post ->
@ -41,7 +42,7 @@ defmodule Philomena.Textile.Renderer do
{:text, text} ->
text
|> replacement_entities()
|> replacement_images(images)
|> replacement_images(conn, images)
{_k, markup} ->
markup
@ -62,7 +63,7 @@ defmodule Philomena.Textile.Renderer do
|> String.replace("'", "’")
end
defp replacement_images(t, images) do
defp replacement_images(t, conn, images) do
t
|> String.replace(~r|>>(\d+)([pts])?|, fn match ->
# Stupid, but the method doesn't give us capture group information
@ -78,15 +79,15 @@ defmodule Philomena.Textile.Renderer do
match
[image, "p"] ->
Phoenix.View.render(PhilomenaWeb.ImageView, "_image_container.html", image: image, size: :medium)
Phoenix.View.render(PhilomenaWeb.ImageView, "_image_container.html", image: image, size: :medium, conn: conn)
|> safe_to_string()
[image, "t"] ->
Phoenix.View.render(PhilomenaWeb.ImageView, "_image_container.html", image: image, size: :small)
Phoenix.View.render(PhilomenaWeb.ImageView, "_image_container.html", image: image, size: :small, conn: conn)
|> safe_to_string()
[image, "s"] ->
Phoenix.View.render(PhilomenaWeb.ImageView, "_image_container.html", image: image, size: :thumb_small)
Phoenix.View.render(PhilomenaWeb.ImageView, "_image_container.html", image: image, size: :thumb_small, conn: conn)
|> safe_to_string()
[image] ->

View file

@ -29,7 +29,7 @@ defmodule PhilomenaWeb.CommentController do
rendered =
comments.entries
|> Renderer.render_collection()
|> Renderer.render_collection(conn)
comments =
%{comments | entries: Enum.zip(rendered, comments.entries)}

View file

@ -23,20 +23,23 @@ defmodule PhilomenaWeb.CommissionController do
item_descriptions =
commission.items
|> Enum.map(&%{body: &1.description})
|> Renderer.render_collection()
|> Renderer.render_collection(conn)
item_add_ons =
commission.items
|> Enum.map(&%{body: &1.add_ons})
|> Renderer.render_collection()
|> Renderer.render_collection(conn)
[information, contact, will_create, will_not_create] =
Renderer.render_collection([
%{body: commission.information},
%{body: commission.contact},
%{body: commission.will_create},
%{body: commission.will_not_create}
])
Renderer.render_collection(
[
%{body: commission.information},
%{body: commission.contact},
%{body: commission.will_create},
%{body: commission.will_not_create}
],
conn
)
rendered =
%{

View file

@ -35,7 +35,7 @@ defmodule PhilomenaWeb.ConversationController do
rendered =
messages.entries
|> Renderer.render_collection()
|> Renderer.render_collection(conn)
messages =
%{messages | entries: Enum.zip(messages.entries, rendered)}

View file

@ -22,7 +22,7 @@ defmodule PhilomenaWeb.DnpEntryController do
bodies =
dnp_entries
|> Enum.map(&%{body: &1.conditions || "-"})
|> Renderer.render_collection()
|> Renderer.render_collection(conn)
dnp_entries =
%{dnp_entries | entries: Enum.zip(bodies, dnp_entries.entries)}
@ -34,11 +34,14 @@ defmodule PhilomenaWeb.DnpEntryController do
dnp_entry = conn.assigns.dnp_entry
[conditions, reason, instructions] =
Renderer.render_collection([
%{body: dnp_entry.conditions || "-"},
%{body: dnp_entry.reason || "-"},
%{body: dnp_entry.instructions || "-"}
])
Renderer.render_collection(
[
%{body: dnp_entry.conditions || "-"},
%{body: dnp_entry.reason || "-"},
%{body: dnp_entry.instructions || "-"}
],
conn
)
render(conn, "show.html", dnp_entry: dnp_entry, conditions: conditions, reason: reason, instructions: instructions)
end

View file

@ -47,7 +47,7 @@ defmodule PhilomenaWeb.Image.CommentController do
rendered =
comments.entries
|> Renderer.render_collection()
|> Renderer.render_collection(conn)
comments =
%{comments | entries: Enum.zip(comments.entries, rendered)}
@ -56,7 +56,7 @@ defmodule PhilomenaWeb.Image.CommentController do
end
def show(conn, _params) do
rendered = Renderer.render_one(conn.assigns.comment)
rendered = Renderer.render_one(conn.assigns.comment, conn)
render(conn, "show.html", layout: false, image: conn.assigns.image, comment: conn.assigns.comment, body: rendered)
end

View file

@ -16,7 +16,7 @@ defmodule PhilomenaWeb.Image.DescriptionController do
{:ok, image} ->
Images.reindex_image(image)
body = Renderer.render_one(%{body: image.description})
body = Renderer.render_one(%{body: image.description}, conn)
conn
|> put_view(PhilomenaWeb.ImageView)

View file

@ -43,14 +43,14 @@ defmodule PhilomenaWeb.ImageController do
rendered =
comments.entries
|> Renderer.render_collection()
|> Renderer.render_collection(conn)
comments =
%{comments | entries: Enum.zip(comments.entries, rendered)}
description =
%{body: image.description}
|> Renderer.render_one()
|> Renderer.render_one(conn)
interactions =
Interactions.user_interactions([image], conn.assigns.current_user)

View file

@ -11,7 +11,7 @@ defmodule PhilomenaWeb.Post.PreviewController do
anonymous = params["anonymous"] == true
post = %Post{user: user, body: body, anonymous: anonymous}
rendered = Renderer.render_one(post)
rendered = Renderer.render_one(post, conn)
render(conn, "create.html", layout: false, post: post, body: rendered)
end

View file

@ -32,7 +32,7 @@ defmodule PhilomenaWeb.PostController do
rendered =
posts.entries
|> Renderer.render_collection()
|> Renderer.render_collection(conn)
posts =
%{posts | entries: Enum.zip(rendered, posts.entries)}

View file

@ -46,10 +46,10 @@ defmodule PhilomenaWeb.TagController do
Interactions.user_interactions(images, user)
body =
Renderer.render_one(%{body: tag.description || ""})
Renderer.render_one(%{body: tag.description || ""}, conn)
dnp_bodies =
Renderer.render_collection(Enum.map(tag.dnp_entries, &%{body: &1.conditions || ""}))
Renderer.render_collection(Enum.map(tag.dnp_entries, &%{body: &1.conditions || ""}), conn)
dnp_entries =
Enum.zip(dnp_bodies, tag.dnp_entries)

View file

@ -49,7 +49,7 @@ defmodule PhilomenaWeb.TopicController do
|> Repo.all()
rendered =
Renderer.render_collection(posts)
Renderer.render_collection(posts, conn)
posts =
Enum.zip(posts, rendered)

View file

@ -15,6 +15,7 @@ defmodule PhilomenaWeb.ImageFilterPlug do
tag_exclusion = %{terms: %{tag_ids: filter.hidden_tag_ids}}
{:ok, query_exclusion} = Query.compile(user, normalize(filter.hidden_complex_str))
{:ok, query_spoiler} = Query.compile(user, normalize(filter.spoilered_complex_str))
query = %{
bool: %{
@ -23,6 +24,8 @@ defmodule PhilomenaWeb.ImageFilterPlug do
}
conn
|> assign(:compiled_complex_filter, query_exclusion)
|> assign(:compiled_complex_spoiler, query_spoiler)
|> assign(:compiled_filter, query)
end
end

View file

@ -1,23 +1,25 @@
- link = assigns[:link] || "/images/#{@image.id}"
- cookies = if assigns[:conn], do: @conn.cookies, else: %{}
- link = assigns[:link] || Routes.image_path(@conn, :show, @image)
= image_container @image, @size, fn ->
= cond do
- @image.duplicate_id ->
.media-box__overlay
strong Marked Duplicate
- @image.destroyed_content ->
.media-box__overlay
strong Destroyed Content
- @image.hidden_from_users ->
.media-box__overlay
strong Deleted:
=< @image.deletion_reason
- true ->
.media-box__overlay.js-spoiler-info-overlay
a href=link
= case render_intent(cookies, @image, @size) do
= case render_intent(@conn, @image, @size) do
- {:hidpi, small_url, medium_url, hover_text} ->
picture
img src=small_url srcset="#{small_url} 1x, #{medium_url} 2x" alt=hover_text

View file

@ -7,19 +7,26 @@ defmodule PhilomenaWeb.ImageView do
def show_vote_counts?(_user), do: true
# this is a bit ridculous
def render_intent(_cookies, %{thumbnails_generated: false}, _size), do: :not_rendered
def render_intent(cookies, image, size) do
def render_intent(_conn, %{thumbnails_generated: false}, _size), do: :not_rendered
def render_intent(conn, image, size) do
uris = thumb_urls(image, false)
vid? = image.image_mime_type == "video/webm"
gif? = image.image_mime_type == "image/gif"
tags = Tag.display_order(image.tags) |> Enum.map_join(", ", & &1.name)
alt = "Size: #{image.image_width}x#{image.image_height} | Tagged: #{tags}"
hidpi? = cookies["hidpi"] == "true"
webm? = cookies["webm"] == "true"
hidpi? = conn.cookies["hidpi"] == "true"
webm? = conn.cookies["webm"] == "true"
use_gif? = vid? and not webm? and size in ~W(thumb thumb_small thumb_tiny)a
filtered? = filter_or_spoiler_hits?(conn, image)
cond do
filtered? and vid? ->
{:filtered_video, alt}
filtered? and not vid? ->
{:filtered_image, alt}
hidpi? and not (gif? or vid?) ->
{:hidpi, uris[size], uris[:medium], alt}
@ -131,4 +138,49 @@ defmodule PhilomenaWeb.ImageView do
defp thumb_format("svg"), do: "png"
defp thumb_format(format), do: format
defp image_filter_data(image) do
%{
id: image.id,
"namespaced_tags.name": String.split(image.tag_list_plus_alias_cache, ", "),
score: image.score,
faves: image.faves_count,
upvotes: image.upvotes_count,
downvotes: image.downvotes_count,
comment_count: image.comments_count,
created_at: image.created_at,
first_seen_at: image.first_seen_at,
source_url: image.source_url,
width: image.image_width,
height: image.image_height,
aspect_ratio: image.image_aspect_ratio,
sha512_hash: image.image_sha512_hash,
orig_sha512_hash: image.image_orig_sha512_hash
}
end
def filter_or_spoiler_hits?(conn, image) do
tag_filter_or_spoiler_hits?(conn, image) or complex_filter_or_spoiler_hits?(conn, image)
end
defp tag_filter_or_spoiler_hits?(conn, image) do
filter = conn.assigns.current_filter
filtered_tag_ids = MapSet.new(filter.spoilered_tag_ids ++ filter.hidden_tag_ids)
image_tag_ids = MapSet.new(image.tags, & &1.id)
MapSet.size(MapSet.intersection(filtered_tag_ids, image_tag_ids)) > 0
end
defp complex_filter_or_spoiler_hits?(conn, image) do
doc = image_filter_data(image)
complex_filter = conn.assigns.compiled_complex_filter
complex_spoiler = conn.assigns.compiled_complex_spoiler
query = %{
bool: %{
should: [complex_filter, complex_spoiler]
}
}
Search.Evaluator.hits?(doc, query)
end
end