Compare commits

...

2 commits

Author SHA1 Message Date
e0107a3d81
chore: Use when clauses to aid the JIT in RenderUtils
All checks were successful
Build / build (push) Successful in 2m42s
2023-10-04 11:04:29 +02:00
d68b03d133
feat: Search in certain subreddits only 2023-10-04 11:01:10 +02:00
6 changed files with 81 additions and 13 deletions

View file

@ -42,7 +42,9 @@ defmodule BdfrBrowser.Comment do
) )
end end
def search(str) do def search(str), do: search(str, nil)
def search(str, subreddits) when is_nil(subreddits) do
search_str = "%#{str}%" search_str = "%#{str}%"
from(c in __MODULE__, from(c in __MODULE__,
@ -63,4 +65,26 @@ defmodule BdfrBrowser.Comment do
group_by: [c.id, p.id, s.name] group_by: [c.id, p.id, s.name]
) )
end end
def search(str, subreddits) when is_list(subreddits) do
search_str = "%#{str}%"
from(c in __MODULE__,
join: p in assoc(c, :post),
join: s in assoc(p, :subreddit),
select: %{
author: c.author,
body: c.body,
children: [],
posted_at: c.posted_at,
subreddit: s.name,
post_id: p.id,
post_title: p.title,
post_date: fragment("to_char(?, 'YYYY-MM')", p.posted_at)
},
where: s.name in ^subreddits and ilike(c.body, ^search_str),
order_by: [desc: c.posted_at],
group_by: [c.id, p.id, s.name]
)
end
end end

View file

@ -164,11 +164,12 @@ defmodule BdfrBrowser.HTTP.Plug do
conn = Plug.Conn.fetch_query_params(conn) conn = Plug.Conn.fetch_query_params(conn)
params = conn.query_params params = conn.query_params
search = params["search"] search = params["search"]
subreddits = params["subreddit"]
tpl_args = [ tpl_args = [
search: search, search: search,
posts: search |> Post.search() |> Repo.all(), posts: search |> Post.search(subreddits) |> Repo.all(),
comments: search |> Comment.search() |> Repo.all() comments: search |> Comment.search(subreddits) |> Repo.all()
] ]
content = render_template("search", tpl_args) content = render_template("search", tpl_args)

View file

@ -120,7 +120,9 @@ defmodule BdfrBrowser.Post do
) )
end end
def search(str) do def search(str), do: search(str, nil)
def search(str, subreddits) when is_nil(subreddits) do
search_str = "%#{str}%" search_str = "%#{str}%"
from(p in __MODULE__, from(p in __MODULE__,
@ -140,4 +142,25 @@ defmodule BdfrBrowser.Post do
group_by: [p.id, s.name] group_by: [p.id, s.name]
) )
end end
def search(str, subreddits) when is_list(subreddits) do
search_str = "%#{str}%"
from(p in __MODULE__,
left_join: c in assoc(p, :comments),
join: s in assoc(p, :subreddit),
select: %{
id: p.id,
title: p.title,
author: p.author,
posted_at: p.posted_at,
num_comments: count(c.id),
subreddit: s.name,
date: fragment("to_char(?, 'YYYY-MM')", p.posted_at)
},
where: s.name in ^subreddits and (ilike(p.title, ^search_str) or ilike(p.selftext, ^search_str)),
order_by: [desc: p.posted_at],
group_by: [p.id, s.name]
)
end
end end

View file

@ -1,13 +1,13 @@
defmodule BdfrBrowser.RenderUtils do defmodule BdfrBrowser.RenderUtils do
def selftext(txt) do def selftext(txt) when is_binary(txt) do
Earmark.as_html!(txt) Earmark.as_html!(txt)
end end
def comment(cmt) do def comment(cmt) when is_binary(cmt) do
Earmark.as_html!(cmt) Earmark.as_html!(cmt)
end end
def message(msg) do def message(msg) when is_binary(msg) do
img_replacement = img_replacement =
"<p class=\"text-center\"><img src=\"/chat_media/\\1\" class=\"img-fluid\" loading=\"lazy\" /></p>" "<p class=\"text-center\"><img src=\"/chat_media/\\1\" class=\"img-fluid\" loading=\"lazy\" /></p>"
@ -17,12 +17,12 @@ defmodule BdfrBrowser.RenderUtils do
|> Earmark.as_html!() |> Earmark.as_html!()
end end
def link_to_user(name) when name in ~w([deleted] DELETED), do: name def link_to_user(name) when is_binary(name) and name in ~w([deleted] DELETED), do: name
def link_to_user(name), do: "<a href=\"/user/#{name}\">#{name}</a>" def link_to_user(name) when is_binary(name), do: "<a href=\"/user/#{name}\">#{name}</a>"
# Helper # Helper
defp maybe_insert_image(<<"mxc://reddit.com/", filename::binary>> = msg, replacement) do defp maybe_insert_image(<<"mxc://reddit.com/", filename::binary>> = msg, replacement) when is_binary(msg) do
chat_directory = Application.fetch_env!(:bdfr_browser, :chat_directory) chat_directory = Application.fetch_env!(:bdfr_browser, :chat_directory)
img_directory = Path.join([chat_directory, "images"]) img_directory = Path.join([chat_directory, "images"])
imgs = Path.wildcard("#{img_directory}/#{filename}.*") imgs = Path.wildcard("#{img_directory}/#{filename}.*")
@ -35,5 +35,5 @@ defmodule BdfrBrowser.RenderUtils do
end end
end end
defp maybe_insert_image(msg, _replacement), do: msg defp maybe_insert_image(msg, _replacement) when is_binary(msg), do: msg
end end

View file

@ -13,12 +13,32 @@
<h2> <h2>
<%= if is_list(subreddit) do %> <%= if is_list(subreddit) do %>
<%= Enum.join(subreddit, ", ") %> Multi-Reddit
<% else %> <% else %>
<%= subreddit %> <%= subreddit %>
<% end %> <% end %>
</h2> </h2>
<form method="get" action="/search" class="row">
<div class="col-8">
<input type="text" class="form-control" placeholder="Search term" name="search" />
</div>
<%= if is_list(subreddit) do %>
<%= for s <- subreddit do %>
<input type="hidden" name="subreddit[]" value="<%= s %>" />
<% end %>
<% else %>
<input type="hidden" name="subreddit[]" value="<%= subreddit %>" />
<% end %>
<div class="col-4 text-end">
<button type="submit" class="btn btn-primary">Search</button>
</div>
</form>
<br>
<div class="row text-center"> <div class="row text-center">
<div class="d-grid gap-2 col-12 mx-auto"> <div class="d-grid gap-2 col-12 mx-auto">
<%= for date <- dates do %> <%= for date <- dates do %>

View file

@ -14,7 +14,7 @@
<h2> <h2>
<%= if is_list(subreddit) do %> <%= if is_list(subreddit) do %>
<%= Enum.join(subreddit, ", ") %> Multi-Reddit
<% else %> <% else %>
<%= subreddit %> <%= subreddit %>
<% end %> <% end %>