mirror of
https://github.com/philomena-dev/philomena.git
synced 2025-01-20 06:37:59 +01:00
apply filter evaluator
This commit is contained in:
parent
9d6103d77c
commit
25319f1c76
15 changed files with 105 additions and 41 deletions
|
@ -1,4 +1,5 @@
|
||||||
defmodule Philomena.Textile.Renderer do
|
defmodule Philomena.Textile.Renderer do
|
||||||
|
# todo: belongs in PhilomenaWeb
|
||||||
alias Textile.Parser
|
alias Textile.Parser
|
||||||
alias Philomena.Images.Image
|
alias Philomena.Images.Image
|
||||||
alias Philomena.Repo
|
alias Philomena.Repo
|
||||||
|
@ -10,11 +11,11 @@ defmodule Philomena.Textile.Renderer do
|
||||||
image_transform: &Camo.Image.image_url/1
|
image_transform: &Camo.Image.image_url/1
|
||||||
}
|
}
|
||||||
|
|
||||||
def render_one(post) do
|
def render_one(post, conn) do
|
||||||
hd(render_collection([post]))
|
hd(render_collection([post], conn))
|
||||||
end
|
end
|
||||||
|
|
||||||
def render_collection(posts) do
|
def render_collection(posts, conn) do
|
||||||
parsed =
|
parsed =
|
||||||
posts
|
posts
|
||||||
|> Enum.map(fn post ->
|
|> Enum.map(fn post ->
|
||||||
|
@ -41,7 +42,7 @@ defmodule Philomena.Textile.Renderer do
|
||||||
{:text, text} ->
|
{:text, text} ->
|
||||||
text
|
text
|
||||||
|> replacement_entities()
|
|> replacement_entities()
|
||||||
|> replacement_images(images)
|
|> replacement_images(conn, images)
|
||||||
|
|
||||||
{_k, markup} ->
|
{_k, markup} ->
|
||||||
markup
|
markup
|
||||||
|
@ -62,7 +63,7 @@ defmodule Philomena.Textile.Renderer do
|
||||||
|> String.replace("'", "’")
|
|> String.replace("'", "’")
|
||||||
end
|
end
|
||||||
|
|
||||||
defp replacement_images(t, images) do
|
defp replacement_images(t, conn, images) do
|
||||||
t
|
t
|
||||||
|> String.replace(~r|>>(\d+)([pts])?|, fn match ->
|
|> String.replace(~r|>>(\d+)([pts])?|, fn match ->
|
||||||
# Stupid, but the method doesn't give us capture group information
|
# Stupid, but the method doesn't give us capture group information
|
||||||
|
@ -78,15 +79,15 @@ defmodule Philomena.Textile.Renderer do
|
||||||
match
|
match
|
||||||
|
|
||||||
[image, "p"] ->
|
[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()
|
|> safe_to_string()
|
||||||
|
|
||||||
[image, "t"] ->
|
[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()
|
|> safe_to_string()
|
||||||
|
|
||||||
[image, "s"] ->
|
[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()
|
|> safe_to_string()
|
||||||
|
|
||||||
[image] ->
|
[image] ->
|
||||||
|
|
|
@ -29,7 +29,7 @@ defmodule PhilomenaWeb.CommentController do
|
||||||
|
|
||||||
rendered =
|
rendered =
|
||||||
comments.entries
|
comments.entries
|
||||||
|> Renderer.render_collection()
|
|> Renderer.render_collection(conn)
|
||||||
|
|
||||||
comments =
|
comments =
|
||||||
%{comments | entries: Enum.zip(rendered, comments.entries)}
|
%{comments | entries: Enum.zip(rendered, comments.entries)}
|
||||||
|
|
|
@ -23,20 +23,23 @@ defmodule PhilomenaWeb.CommissionController do
|
||||||
item_descriptions =
|
item_descriptions =
|
||||||
commission.items
|
commission.items
|
||||||
|> Enum.map(&%{body: &1.description})
|
|> Enum.map(&%{body: &1.description})
|
||||||
|> Renderer.render_collection()
|
|> Renderer.render_collection(conn)
|
||||||
|
|
||||||
item_add_ons =
|
item_add_ons =
|
||||||
commission.items
|
commission.items
|
||||||
|> Enum.map(&%{body: &1.add_ons})
|
|> Enum.map(&%{body: &1.add_ons})
|
||||||
|> Renderer.render_collection()
|
|> Renderer.render_collection(conn)
|
||||||
|
|
||||||
[information, contact, will_create, will_not_create] =
|
[information, contact, will_create, will_not_create] =
|
||||||
Renderer.render_collection([
|
Renderer.render_collection(
|
||||||
|
[
|
||||||
%{body: commission.information},
|
%{body: commission.information},
|
||||||
%{body: commission.contact},
|
%{body: commission.contact},
|
||||||
%{body: commission.will_create},
|
%{body: commission.will_create},
|
||||||
%{body: commission.will_not_create}
|
%{body: commission.will_not_create}
|
||||||
])
|
],
|
||||||
|
conn
|
||||||
|
)
|
||||||
|
|
||||||
rendered =
|
rendered =
|
||||||
%{
|
%{
|
||||||
|
|
|
@ -35,7 +35,7 @@ defmodule PhilomenaWeb.ConversationController do
|
||||||
|
|
||||||
rendered =
|
rendered =
|
||||||
messages.entries
|
messages.entries
|
||||||
|> Renderer.render_collection()
|
|> Renderer.render_collection(conn)
|
||||||
|
|
||||||
messages =
|
messages =
|
||||||
%{messages | entries: Enum.zip(messages.entries, rendered)}
|
%{messages | entries: Enum.zip(messages.entries, rendered)}
|
||||||
|
|
|
@ -22,7 +22,7 @@ defmodule PhilomenaWeb.DnpEntryController do
|
||||||
bodies =
|
bodies =
|
||||||
dnp_entries
|
dnp_entries
|
||||||
|> Enum.map(&%{body: &1.conditions || "-"})
|
|> Enum.map(&%{body: &1.conditions || "-"})
|
||||||
|> Renderer.render_collection()
|
|> Renderer.render_collection(conn)
|
||||||
|
|
||||||
dnp_entries =
|
dnp_entries =
|
||||||
%{dnp_entries | entries: Enum.zip(bodies, dnp_entries.entries)}
|
%{dnp_entries | entries: Enum.zip(bodies, dnp_entries.entries)}
|
||||||
|
@ -34,11 +34,14 @@ defmodule PhilomenaWeb.DnpEntryController do
|
||||||
dnp_entry = conn.assigns.dnp_entry
|
dnp_entry = conn.assigns.dnp_entry
|
||||||
|
|
||||||
[conditions, reason, instructions] =
|
[conditions, reason, instructions] =
|
||||||
Renderer.render_collection([
|
Renderer.render_collection(
|
||||||
|
[
|
||||||
%{body: dnp_entry.conditions || "-"},
|
%{body: dnp_entry.conditions || "-"},
|
||||||
%{body: dnp_entry.reason || "-"},
|
%{body: dnp_entry.reason || "-"},
|
||||||
%{body: dnp_entry.instructions || "-"}
|
%{body: dnp_entry.instructions || "-"}
|
||||||
])
|
],
|
||||||
|
conn
|
||||||
|
)
|
||||||
|
|
||||||
render(conn, "show.html", dnp_entry: dnp_entry, conditions: conditions, reason: reason, instructions: instructions)
|
render(conn, "show.html", dnp_entry: dnp_entry, conditions: conditions, reason: reason, instructions: instructions)
|
||||||
end
|
end
|
||||||
|
|
|
@ -47,7 +47,7 @@ defmodule PhilomenaWeb.Image.CommentController do
|
||||||
|
|
||||||
rendered =
|
rendered =
|
||||||
comments.entries
|
comments.entries
|
||||||
|> Renderer.render_collection()
|
|> Renderer.render_collection(conn)
|
||||||
|
|
||||||
comments =
|
comments =
|
||||||
%{comments | entries: Enum.zip(comments.entries, rendered)}
|
%{comments | entries: Enum.zip(comments.entries, rendered)}
|
||||||
|
@ -56,7 +56,7 @@ defmodule PhilomenaWeb.Image.CommentController do
|
||||||
end
|
end
|
||||||
|
|
||||||
def show(conn, _params) do
|
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)
|
render(conn, "show.html", layout: false, image: conn.assigns.image, comment: conn.assigns.comment, body: rendered)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -16,7 +16,7 @@ defmodule PhilomenaWeb.Image.DescriptionController do
|
||||||
{:ok, image} ->
|
{:ok, image} ->
|
||||||
Images.reindex_image(image)
|
Images.reindex_image(image)
|
||||||
|
|
||||||
body = Renderer.render_one(%{body: image.description})
|
body = Renderer.render_one(%{body: image.description}, conn)
|
||||||
|
|
||||||
conn
|
conn
|
||||||
|> put_view(PhilomenaWeb.ImageView)
|
|> put_view(PhilomenaWeb.ImageView)
|
||||||
|
|
|
@ -43,14 +43,14 @@ defmodule PhilomenaWeb.ImageController do
|
||||||
|
|
||||||
rendered =
|
rendered =
|
||||||
comments.entries
|
comments.entries
|
||||||
|> Renderer.render_collection()
|
|> Renderer.render_collection(conn)
|
||||||
|
|
||||||
comments =
|
comments =
|
||||||
%{comments | entries: Enum.zip(comments.entries, rendered)}
|
%{comments | entries: Enum.zip(comments.entries, rendered)}
|
||||||
|
|
||||||
description =
|
description =
|
||||||
%{body: image.description}
|
%{body: image.description}
|
||||||
|> Renderer.render_one()
|
|> Renderer.render_one(conn)
|
||||||
|
|
||||||
interactions =
|
interactions =
|
||||||
Interactions.user_interactions([image], conn.assigns.current_user)
|
Interactions.user_interactions([image], conn.assigns.current_user)
|
||||||
|
|
|
@ -11,7 +11,7 @@ defmodule PhilomenaWeb.Post.PreviewController do
|
||||||
anonymous = params["anonymous"] == true
|
anonymous = params["anonymous"] == true
|
||||||
|
|
||||||
post = %Post{user: user, body: body, anonymous: anonymous}
|
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)
|
render(conn, "create.html", layout: false, post: post, body: rendered)
|
||||||
end
|
end
|
||||||
|
|
|
@ -32,7 +32,7 @@ defmodule PhilomenaWeb.PostController do
|
||||||
|
|
||||||
rendered =
|
rendered =
|
||||||
posts.entries
|
posts.entries
|
||||||
|> Renderer.render_collection()
|
|> Renderer.render_collection(conn)
|
||||||
|
|
||||||
posts =
|
posts =
|
||||||
%{posts | entries: Enum.zip(rendered, posts.entries)}
|
%{posts | entries: Enum.zip(rendered, posts.entries)}
|
||||||
|
|
|
@ -46,10 +46,10 @@ defmodule PhilomenaWeb.TagController do
|
||||||
Interactions.user_interactions(images, user)
|
Interactions.user_interactions(images, user)
|
||||||
|
|
||||||
body =
|
body =
|
||||||
Renderer.render_one(%{body: tag.description || ""})
|
Renderer.render_one(%{body: tag.description || ""}, conn)
|
||||||
|
|
||||||
dnp_bodies =
|
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 =
|
dnp_entries =
|
||||||
Enum.zip(dnp_bodies, tag.dnp_entries)
|
Enum.zip(dnp_bodies, tag.dnp_entries)
|
||||||
|
|
|
@ -49,7 +49,7 @@ defmodule PhilomenaWeb.TopicController do
|
||||||
|> Repo.all()
|
|> Repo.all()
|
||||||
|
|
||||||
rendered =
|
rendered =
|
||||||
Renderer.render_collection(posts)
|
Renderer.render_collection(posts, conn)
|
||||||
|
|
||||||
posts =
|
posts =
|
||||||
Enum.zip(posts, rendered)
|
Enum.zip(posts, rendered)
|
||||||
|
|
|
@ -15,6 +15,7 @@ defmodule PhilomenaWeb.ImageFilterPlug do
|
||||||
|
|
||||||
tag_exclusion = %{terms: %{tag_ids: filter.hidden_tag_ids}}
|
tag_exclusion = %{terms: %{tag_ids: filter.hidden_tag_ids}}
|
||||||
{:ok, query_exclusion} = Query.compile(user, normalize(filter.hidden_complex_str))
|
{:ok, query_exclusion} = Query.compile(user, normalize(filter.hidden_complex_str))
|
||||||
|
{:ok, query_spoiler} = Query.compile(user, normalize(filter.spoilered_complex_str))
|
||||||
|
|
||||||
query = %{
|
query = %{
|
||||||
bool: %{
|
bool: %{
|
||||||
|
@ -23,6 +24,8 @@ defmodule PhilomenaWeb.ImageFilterPlug do
|
||||||
}
|
}
|
||||||
|
|
||||||
conn
|
conn
|
||||||
|
|> assign(:compiled_complex_filter, query_exclusion)
|
||||||
|
|> assign(:compiled_complex_spoiler, query_spoiler)
|
||||||
|> assign(:compiled_filter, query)
|
|> assign(:compiled_filter, query)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,23 +1,25 @@
|
||||||
- link = assigns[:link] || "/images/#{@image.id}"
|
- link = assigns[:link] || Routes.image_path(@conn, :show, @image)
|
||||||
- cookies = if assigns[:conn], do: @conn.cookies, else: %{}
|
|
||||||
|
|
||||||
= image_container @image, @size, fn ->
|
= image_container @image, @size, fn ->
|
||||||
= cond do
|
= cond do
|
||||||
- @image.duplicate_id ->
|
- @image.duplicate_id ->
|
||||||
.media-box__overlay
|
.media-box__overlay
|
||||||
strong Marked Duplicate
|
strong Marked Duplicate
|
||||||
|
|
||||||
- @image.destroyed_content ->
|
- @image.destroyed_content ->
|
||||||
.media-box__overlay
|
.media-box__overlay
|
||||||
strong Destroyed Content
|
strong Destroyed Content
|
||||||
|
|
||||||
- @image.hidden_from_users ->
|
- @image.hidden_from_users ->
|
||||||
.media-box__overlay
|
.media-box__overlay
|
||||||
strong Deleted:
|
strong Deleted:
|
||||||
=< @image.deletion_reason
|
=< @image.deletion_reason
|
||||||
|
|
||||||
- true ->
|
- true ->
|
||||||
|
|
||||||
.media-box__overlay.js-spoiler-info-overlay
|
.media-box__overlay.js-spoiler-info-overlay
|
||||||
a href=link
|
a href=link
|
||||||
= case render_intent(cookies, @image, @size) do
|
= case render_intent(@conn, @image, @size) do
|
||||||
- {:hidpi, small_url, medium_url, hover_text} ->
|
- {:hidpi, small_url, medium_url, hover_text} ->
|
||||||
picture
|
picture
|
||||||
img src=small_url srcset="#{small_url} 1x, #{medium_url} 2x" alt=hover_text
|
img src=small_url srcset="#{small_url} 1x, #{medium_url} 2x" alt=hover_text
|
||||||
|
|
|
@ -7,19 +7,26 @@ defmodule PhilomenaWeb.ImageView do
|
||||||
def show_vote_counts?(_user), do: true
|
def show_vote_counts?(_user), do: true
|
||||||
|
|
||||||
# this is a bit ridculous
|
# this is a bit ridculous
|
||||||
def render_intent(_cookies, %{thumbnails_generated: false}, _size), do: :not_rendered
|
def render_intent(_conn, %{thumbnails_generated: false}, _size), do: :not_rendered
|
||||||
def render_intent(cookies, image, size) do
|
def render_intent(conn, image, size) do
|
||||||
uris = thumb_urls(image, false)
|
uris = thumb_urls(image, false)
|
||||||
vid? = image.image_mime_type == "video/webm"
|
vid? = image.image_mime_type == "video/webm"
|
||||||
gif? = image.image_mime_type == "image/gif"
|
gif? = image.image_mime_type == "image/gif"
|
||||||
tags = Tag.display_order(image.tags) |> Enum.map_join(", ", & &1.name)
|
tags = Tag.display_order(image.tags) |> Enum.map_join(", ", & &1.name)
|
||||||
alt = "Size: #{image.image_width}x#{image.image_height} | Tagged: #{tags}"
|
alt = "Size: #{image.image_width}x#{image.image_height} | Tagged: #{tags}"
|
||||||
|
|
||||||
hidpi? = cookies["hidpi"] == "true"
|
hidpi? = conn.cookies["hidpi"] == "true"
|
||||||
webm? = cookies["webm"] == "true"
|
webm? = conn.cookies["webm"] == "true"
|
||||||
use_gif? = vid? and not webm? and size in ~W(thumb thumb_small thumb_tiny)a
|
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
|
cond do
|
||||||
|
filtered? and vid? ->
|
||||||
|
{:filtered_video, alt}
|
||||||
|
|
||||||
|
filtered? and not vid? ->
|
||||||
|
{:filtered_image, alt}
|
||||||
|
|
||||||
hidpi? and not (gif? or vid?) ->
|
hidpi? and not (gif? or vid?) ->
|
||||||
{:hidpi, uris[size], uris[:medium], alt}
|
{:hidpi, uris[size], uris[:medium], alt}
|
||||||
|
|
||||||
|
@ -131,4 +138,49 @@ defmodule PhilomenaWeb.ImageView do
|
||||||
|
|
||||||
defp thumb_format("svg"), do: "png"
|
defp thumb_format("svg"), do: "png"
|
||||||
defp thumb_format(format), do: format
|
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
|
end
|
||||||
|
|
Loading…
Reference in a new issue