Skip to content

Commit

Permalink
Search created rooms
Browse files Browse the repository at this point in the history
  • Loading branch information
keianhzo committed Apr 2, 2024
1 parent 105eb0c commit 8d16470
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 2 deletions.
33 changes: 31 additions & 2 deletions lib/ret/media_search.ex
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ defmodule Ret.MediaSearch do
@max_collection_face_count 200_000

# see sketchfab bug about max_filesizes params broken
# @max_file_size_bytes 20 * 1024 * 1024
# @max_file_size_bytes 20 * 1024 * 1024
# @max_collection_file_size_bytes 100 * 1024 * 1024

def search(%Ret.MediaSearchQuery{
Expand Down Expand Up @@ -125,6 +125,16 @@ defmodule Ret.MediaSearch do
public_rooms_search(cursor, q)
end

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{
source: "sketchfab",
cursor: cursor,
Expand Down Expand Up @@ -225,7 +235,7 @@ defmodule Ret.MediaSearch do
downloadable: true,
count: @page_size,
max_face_count: @max_face_count,
# max_filesizes: "gltf:#{@max_file_size_bytes}",
# max_filesizes: "gltf:#{@max_file_size_bytes}",
processing_status: :succeeded,
cursor: cursor,
categories: filter,
Expand Down Expand Up @@ -547,6 +557,25 @@ defmodule Ret.MediaSearch do
{:commit, results}
end

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]
],
order_by: [desc: :inserted_at]

results =
ecto_query
|> Repo.paginate(%{page: page_number, page_size: @page_size})
|> result_for_page(page_number, :my_rooms, &hub_to_entry/1)

{:commit, results}
end

defp filter_by_hub_entry_mode(query, entry_mode) do
from fav in query,
join: hub in assoc(fav, :hub),
Expand Down
21 changes: 21 additions & 0 deletions lib/ret_web/controllers/api/v1/media_search_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down

0 comments on commit 8d16470

Please sign in to comment.