From 502c6e2e884e9cb701b2569e0d9bdb5f34d21664 Mon Sep 17 00:00:00 2001 From: Manuel Martin Date: Tue, 2 Apr 2024 11:16:41 +0200 Subject: [PATCH] Search created rooms --- lib/ret/media_search.ex | 14 +++++++++---- .../api/v1/media_search_controller.ex | 21 +++++++++++++++++++ 2 files changed, 31 insertions(+), 4 deletions(-) diff --git a/lib/ret/media_search.ex b/lib/ret/media_search.ex index 77413aadd..03a4f615c 100644 --- a/lib/ret/media_search.ex +++ b/lib/ret/media_search.ex @@ -125,8 +125,14 @@ defmodule Ret.MediaSearch do public_rooms_search(cursor, q) end - def search(%Ret.MediaSearchQuery{source: "rooms", filter: "created", cursor: cursor, q: q}) do - created_rooms_search(cursor, q) + def search(%Ret.MediaSearchQuery{ + source: "rooms", + filter: "created", + cursor: cursor, + user: account_id, + q: q + }) do + created_rooms_search(cursor, account_id, q) end def search(%Ret.MediaSearchQuery{ @@ -551,11 +557,11 @@ defmodule Ret.MediaSearch do {:commit, results} end - defp created_rooms_search(cursor, _query) do + defp created_rooms_search(cursor, account_id, _query) do page_number = (cursor || "1") |> Integer.parse() |> elem(0) - ecto_query = from h in Hub, + where: h.created_by_account_id == ^account_id, preload: [ scene: [:screenshot_owned_file], scene_listing: [:scene, :screenshot_owned_file] diff --git a/lib/ret_web/controllers/api/v1/media_search_controller.ex b/lib/ret_web/controllers/api/v1/media_search_controller.ex index b4885e3f3..f21ca1460 100644 --- a/lib/ret_web/controllers/api/v1/media_search_controller.ex +++ b/lib/ret_web/controllers/api/v1/media_search_controller.ex @@ -2,6 +2,27 @@ defmodule RetWeb.Api.V1.MediaSearchController do use RetWeb, :controller use Retry + def index(conn, %{"source" => source, "filter" => "created", "user" => user} = params) + when source in ["rooms"] do + account = conn |> Guardian.Plug.current_resource() + + if account && account.account_id == String.to_integer(user) do + {:commit, results} = + %Ret.MediaSearchQuery{ + source: "rooms", + cursor: params["cursor"] || "1", + user: account.account_id, + filter: "created", + q: params["q"] + } + |> Ret.MediaSearch.search() + + conn |> render("index.json", results: results) + else + conn |> send_resp(401, "You can only search created rooms for your own account.") + end + end + def index(conn, %{"source" => "rooms"} = params) do {:commit, results} = %Ret.MediaSearchQuery{