Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 47 additions & 3 deletions apps/server/src/orpc/procedures/discover.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
searchMulti,
searchPerson,
searchTv,
findByExternalId,
} from "@sofa/tmdb/client";
import { isTmdbConfigured } from "@sofa/tmdb/config";
import { tmdbImageUrl } from "@sofa/tmdb/image";
Expand All @@ -32,6 +33,40 @@ function requireTmdb() {
}
}


// ─── SEARCH BY IMDB ID ─────────────────────────────────────────────────
const IMDB_ID_REGEX = /^tt\d{7,10}$/i;

function isImdbIdSearch(query: string) {
return IMDB_ID_REGEX.test(query.trim());

}

async function searchByImdbId(query: string) {
const findResult = await findByExternalId(query.toLowerCase(), "imdb_id");

// result of all movies found
const movieResults = (findResult.movie_results ?? []).map((r) => ({
...r,
media_type: "movie" as const,
}));

// result of all shows found
const tvResults = (findResult.tv_results ?? []).map((r) => ({
...r,
media_type: "tv" as const,
}));

// join all the results
const results = [...movieResults, ...tvResults];
return {
page: 1,
total_pages: results.length > 0 ? 1 : 0,
total_results: results.length,
results,
};
}

// ─── Trending ─────────────────────────────────────────────────

export const trending = os.discover.trending.use(authed).handler(async ({ input, context }) => {
Expand Down Expand Up @@ -219,12 +254,21 @@ export const search = os.discover.search.use(authed).handler(async ({ input }) =
};
}

const raw =
type === "movie"
// Old Results without seach by imdb id

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P3: This leaves stale commented-out search logic in the procedure, which makes the active IMDb branching harder to read and easier to let drift from the real implementation. Since the new ternary below replaces it, removing this block would keep the search flow clearer.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At apps/server/src/orpc/procedures/discover.ts, line 257:

<comment>This leaves stale commented-out search logic in the procedure, which makes the active IMDb branching harder to read and easier to let drift from the real implementation. Since the new ternary below replaces it, removing this block would keep the search flow clearer.</comment>

<file context>
@@ -219,12 +254,21 @@ export const search = os.discover.search.use(authed).handler(async ({ input }) =
 
-  const raw =
-    type === "movie"
+  // Old Results without seach by imdb id
+  // const raw =
+  //   type === "movie"
</file context>

// const raw =
// type === "movie"
// ? await searchMovies(query, input.page)
// : type === "tv"
// ? await searchTv(query, input.page)
// : await searchMulti(query, input.page);

const raw = isImdbIdSearch(query)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2: Type-filtered search can now return the wrong media kind for IMDb ID queries: with input.type === "movie", a TV IMDb ID still comes back because the IMDb branch runs before the type branches and merges movie/TV results. Consider passing type into searchByImdbId or filtering its results to preserve the existing movie/TV search contract.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At apps/server/src/orpc/procedures/discover.ts, line 265:

<comment>Type-filtered search can now return the wrong media kind for IMDb ID queries: with `input.type === "movie"`, a TV IMDb ID still comes back because the IMDb branch runs before the `type` branches and merges movie/TV results. Consider passing `type` into `searchByImdbId` or filtering its results to preserve the existing movie/TV search contract.</comment>

<file context>
@@ -219,12 +254,21 @@ export const search = os.discover.search.use(authed).handler(async ({ input }) =
+  //       ? await searchTv(query, input.page)
+  //       : await searchMulti(query, input.page);
+
+  const raw = isImdbIdSearch(query)
+    ? await searchByImdbId(query)
+    : type === "movie"
</file context>

? await searchByImdbId(query)
: type === "movie"
? await searchMovies(query, input.page)
: type === "tv"
? await searchTv(query, input.page)
: await searchMulti(query, input.page);
: await searchMulti(query, input.page);

type SearchResult = {
id: number;
Expand Down