Compare commits

..

No commits in common. "e0107a3d814583b3a521d254bbe09bfad1fc67ac" and "38df258ce573b14bf5d6d15983457d028631166a" have entirely different histories.

6 changed files with 13 additions and 81 deletions

View file

@ -42,9 +42,7 @@ defmodule BdfrBrowser.Comment do
)
end
def search(str), do: search(str, nil)
def search(str, subreddits) when is_nil(subreddits) do
def search(str) do
search_str = "%#{str}%"
from(c in __MODULE__,
@ -65,26 +63,4 @@ defmodule BdfrBrowser.Comment do
group_by: [c.id, p.id, s.name]
)
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

View file

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

View file

@ -120,9 +120,7 @@ defmodule BdfrBrowser.Post do
)
end
def search(str), do: search(str, nil)
def search(str, subreddits) when is_nil(subreddits) do
def search(str) do
search_str = "%#{str}%"
from(p in __MODULE__,
@ -142,25 +140,4 @@ defmodule BdfrBrowser.Post do
group_by: [p.id, s.name]
)
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

View file

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

View file

@ -13,32 +13,12 @@
<h2>
<%= if is_list(subreddit) do %>
Multi-Reddit
<%= Enum.join(subreddit, ", ") %>
<% else %>
<%= subreddit %>
<% end %>
</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="d-grid gap-2 col-12 mx-auto">
<%= for date <- dates do %>

View file

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