Compare commits
2 commits
38df258ce5
...
e0107a3d81
Author | SHA1 | Date | |
---|---|---|---|
e0107a3d81 | |||
d68b03d133 |
6 changed files with 81 additions and 13 deletions
|
@ -42,7 +42,9 @@ defmodule BdfrBrowser.Comment do
|
|||
)
|
||||
end
|
||||
|
||||
def search(str) do
|
||||
def search(str), do: search(str, nil)
|
||||
|
||||
def search(str, subreddits) when is_nil(subreddits) do
|
||||
search_str = "%#{str}%"
|
||||
|
||||
from(c in __MODULE__,
|
||||
|
@ -63,4 +65,26 @@ 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
|
||||
|
|
|
@ -164,11 +164,12 @@ 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() |> Repo.all(),
|
||||
comments: search |> Comment.search() |> Repo.all()
|
||||
posts: search |> Post.search(subreddits) |> Repo.all(),
|
||||
comments: search |> Comment.search(subreddits) |> Repo.all()
|
||||
]
|
||||
|
||||
content = render_template("search", tpl_args)
|
||||
|
|
|
@ -120,7 +120,9 @@ defmodule BdfrBrowser.Post do
|
|||
)
|
||||
end
|
||||
|
||||
def search(str) do
|
||||
def search(str), do: search(str, nil)
|
||||
|
||||
def search(str, subreddits) when is_nil(subreddits) do
|
||||
search_str = "%#{str}%"
|
||||
|
||||
from(p in __MODULE__,
|
||||
|
@ -140,4 +142,25 @@ 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
|
||||
|
|
|
@ -1,13 +1,13 @@
|
|||
defmodule BdfrBrowser.RenderUtils do
|
||||
def selftext(txt) do
|
||||
def selftext(txt) when is_binary(txt) do
|
||||
Earmark.as_html!(txt)
|
||||
end
|
||||
|
||||
def comment(cmt) do
|
||||
def comment(cmt) when is_binary(cmt) do
|
||||
Earmark.as_html!(cmt)
|
||||
end
|
||||
|
||||
def message(msg) do
|
||||
def message(msg) when is_binary(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 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) and name in ~w([deleted] DELETED), do: name
|
||||
def link_to_user(name) when is_binary(name), do: "<a href=\"/user/#{name}\">#{name}</a>"
|
||||
|
||||
# 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)
|
||||
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), do: msg
|
||||
defp maybe_insert_image(msg, _replacement) when is_binary(msg), do: msg
|
||||
end
|
||||
|
|
|
@ -13,12 +13,32 @@
|
|||
|
||||
<h2>
|
||||
<%= if is_list(subreddit) do %>
|
||||
<%= Enum.join(subreddit, ", ") %>
|
||||
Multi-Reddit
|
||||
<% 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 %>
|
||||
|
|
|
@ -14,7 +14,7 @@
|
|||
|
||||
<h2>
|
||||
<%= if is_list(subreddit) do %>
|
||||
<%= Enum.join(subreddit, ", ") %>
|
||||
Multi-Reddit
|
||||
<% else %>
|
||||
<%= subreddit %>
|
||||
<% end %>
|
||||
|
|
Reference in a new issue