From 2df7e1c2f87f45b3a87ffa5d19019499e4605075 Mon Sep 17 00:00:00 2001 From: "byte[]" Date: Fri, 1 May 2020 22:33:40 -0400 Subject: [PATCH] fixes #69: livestream channel search --- .../controllers/channel_controller.ex | 20 +++++++++++++++++-- .../templates/channel/index.html.slime | 10 +++++----- 2 files changed, 23 insertions(+), 7 deletions(-) diff --git a/lib/philomena_web/controllers/channel_controller.ex b/lib/philomena_web/controllers/channel_controller.ex index 89ec850b..c99e49ae 100644 --- a/lib/philomena_web/controllers/channel_controller.ex +++ b/lib/philomena_web/controllers/channel_controller.ex @@ -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) diff --git a/lib/philomena_web/templates/channel/index.html.slime b/lib/philomena_web/templates/channel/index.html.slime index 75267f76..328e77e4 100644 --- a/lib/philomena_web/templates/channel/index.html.slime +++ b/lib/philomena_web/templates/channel/index.html.slime @@ -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/'s stream here? ' A: Send a private message to a site administrator