fixes #69: livestream channel search

This commit is contained in:
byte[] 2020-05-01 22:33:40 -04:00
parent 7f8f229286
commit 2df7e1c2f8
2 changed files with 23 additions and 7 deletions

View file

@ -8,7 +8,7 @@ defmodule PhilomenaWeb.ChannelController do
plug :load_and_authorize_resource, model: Channel, only: [:show, :new, :create, :edit, :update]
def index(conn, _params) do
def index(conn, params) do
show_nsfw? = conn.cookies["chan_nsfw"] == "true"
channels =
@ -16,7 +16,9 @@ defmodule PhilomenaWeb.ChannelController do
|> maybe_show_nsfw(show_nsfw?)
|> where([c], not is_nil(c.last_fetched_at))
|> order_by(desc: :is_live, asc: :title)
|> preload(:associated_artist_tag)
|> join(:left, [c], _ in assoc(c, :associated_artist_tag))
|> preload([_c, t], associated_artist_tag: t)
|> maybe_search(params)
|> Repo.paginate(conn.assigns.scrivener)
subscriptions = Channels.subscriptions(channels, conn.assigns.current_user)
@ -72,6 +74,20 @@ defmodule PhilomenaWeb.ChannelController do
end
end
defp maybe_search(query, %{"cq" => cq}) when is_binary(cq) and cq != "" do
title_query = "#{cq}%"
tag_query = "%#{cq}%"
where(
query,
[c, t],
ilike(c.title, ^title_query) or ilike(c.short_name, ^title_query) or
ilike(t.name, ^tag_query)
)
end
defp maybe_search(query, _params), do: query
defp maybe_show_nsfw(query, true), do: query
defp maybe_show_nsfw(query, _falsy), do: where(query, [c], c.nsfw == false)

View file

@ -1,12 +1,12 @@
h1 Livestreams
- route = fn p -> Routes.channel_path(@conn, :index, p) end
- pagination = render PhilomenaWeb.PaginationView, "_pagination.html", page: @channels, route: route, conn: @conn
- pagination = render PhilomenaWeb.PaginationView, "_pagination.html", page: @channels, route: route, conn: @conn, params: [cq: @conn.params["cq"]]
/= form_tag channels_path, method: :get, class: 'hform', enforce_utf8: false do
= form_for :channels, Routes.channel_path(@conn, :index), [method: "get", class: "hform", enforce_utf8: false], fn f ->
.field
= text_field_tag :cq, params[:cq], class: 'input hform__text', placeholder: 'Search channels'
= submit_tag 'Search', class: 'hform__button button'
= text_input f, :cq, name: :cq, value: @conn.params["cq"], class: "input hform__text", placeholder: "Search channels", autocapitalize: "none"
= submit "Search", class: "hform__button button"
.block
.block__header
@ -26,7 +26,7 @@ br
h2 FAQ
p
strong> Q: Do you host streams?
| A: No, we cheat and just link to streams on Livestream/Picarto since that's where (almost) everyone is already. This is simply a nice way to track streaming artists.
| A: No, we cheat and just link to streams on Picarto since that's where (almost) everyone is already. This is simply a nice way to track streaming artists.
p
strong> Q: How do I get my stream/a friend's stream/<artist>'s stream here?
' A: Send a private message to a site administrator