mirror of
https://github.com/jakejarvis/sofa.git
synced 2026-07-14 18:15:56 -04:00
Replace tmdb-* URL routing with resolveTitle/resolvePerson server actions
- Add `resolveTitle` and `resolvePerson` server actions that import
from TMDB and return the internal DB id
- Remove `tmdb-{id}-{type}` URL pattern and server-side redirect logic
from TitleDetailPage and PersonDetailPage
- Rename `importTitle` → `getOrFetchTitleByTmdbId`; add `getOrFetchTitle`
combining fetch + children lookup
- Update HeroBanner, TitleCard, and CommandPalette to call resolve
actions client-side before pushing to router
- Batch availability offer inserts into a single transaction
This commit is contained in:
@@ -1,36 +1,137 @@
|
||||
This is a [Next.js](https://nextjs.org) project bootstrapped with [`create-next-app`](https://nextjs.org/docs/app/api-reference/cli/create-next-app).
|
||||
# 🛋️ Sofa
|
||||
|
||||
## Getting Started
|
||||
Sofa is a self-hosted movie and TV tracker for nerds. Track what you've watched, discover what's next, and plug your data into your existing home media stack.
|
||||
|
||||
First, run the development server:
|
||||
## What it does
|
||||
|
||||
- Track episode-level progress for TV series and pick shows back up from a dedicated "Continue Watching" view
|
||||
- Mark movies as watched to discover more like them
|
||||
- Rate titles, browse cast and crew, and get recommendations based on what you are already tracking
|
||||
- Search TMDB and explore trending movies and shows without leaving your own instance
|
||||
- Show streaming availability from TMDB's US provider data
|
||||
- Automatically log completed watches from Plex, Jellyfin, or Emby webhooks
|
||||
- Expose your watchlist as import lists for Sonarr and Radarr
|
||||
- Runs on SQLite with local image caching, built-in backups, and no external database requirement
|
||||
- Supports local accounts or OIDC SSO for private instances
|
||||
|
||||
> [!NOTE]
|
||||
> Sofa is extremely US-centric right now, in terms of streaming providers, content rating systems, etc. Contributions to address this are more than welcome!
|
||||
|
||||
## Quick start
|
||||
|
||||
A minimal [`docker-compose.yml`](./docker-compose.yml) is provided in this repo. For most setups, the shortest path is:
|
||||
|
||||
1. Copy the example environment file:
|
||||
|
||||
```bash
|
||||
npm run dev
|
||||
# or
|
||||
yarn dev
|
||||
# or
|
||||
pnpm dev
|
||||
# or
|
||||
bun dev
|
||||
cp .env.example .env
|
||||
```
|
||||
|
||||
Open [http://localhost:3000](http://localhost:3000) with your browser to see the result.
|
||||
2. Fill in the required values:
|
||||
|
||||
You can start editing the page by modifying `app/page.tsx`. The page auto-updates as you edit the file.
|
||||
```env
|
||||
TMDB_API_READ_ACCESS_TOKEN=your_tmdb_read_access_token
|
||||
BETTER_AUTH_SECRET=generate_a_long_random_secret
|
||||
BETTER_AUTH_URL=http://localhost:3000
|
||||
```
|
||||
|
||||
This project uses [`next/font`](https://nextjs.org/docs/app/building-your-application/optimizing/fonts) to automatically optimize and load [Geist](https://vercel.com/font), a new font family for Vercel.
|
||||
3. Start the container:
|
||||
|
||||
## Learn More
|
||||
```bash
|
||||
docker compose up -d
|
||||
```
|
||||
|
||||
To learn more about Next.js, take a look at the following resources:
|
||||
4. Open `http://localhost:3000` and create the first account. The first account becomes the admin automatically, and registration closes after that by default.
|
||||
|
||||
- [Next.js Documentation](https://nextjs.org/docs) - learn about Next.js features and API.
|
||||
- [Learn Next.js](https://nextjs.org/learn) - an interactive Next.js tutorial.
|
||||
> The included Compose file uses `ghcr.io/jakejarvis/sofa:edge`. If you prefer to pin releases, switch to a published version tag like `ghcr.io/jakejarvis/sofa:<version>` or use the matching `jakejarvis/sofa:<version>` image on Docker Hub. Multi-arch images are published for `linux/amd64` and `linux/arm64`.
|
||||
|
||||
You can check out [the Next.js GitHub repository](https://github.com/vercel/next.js) - your feedback and contributions are welcome!
|
||||
## Required setup
|
||||
|
||||
## Deploy on Vercel
|
||||
### TMDB
|
||||
|
||||
The easiest way to deploy your Next.js app is to use the [Vercel Platform](https://vercel.com/new?utm_medium=default-template&filter=next.js&utm_source=create-next-app&utm_campaign=create-next-app-readme) from the creators of Next.js.
|
||||
Sofa uses [TMDB](https://www.themoviedb.org/) for metadata, posters, cast, recommendations, and streaming availability. You need a TMDB API Read Access Token before the app can do anything useful.
|
||||
|
||||
Check out our [Next.js deployment documentation](https://nextjs.org/docs/app/building-your-application/deploying) for more details.
|
||||
Create one here:
|
||||
|
||||
- [TMDB signup](https://www.themoviedb.org/signup)
|
||||
- [API settings](https://www.themoviedb.org/settings/api)
|
||||
|
||||
> [!TIP]
|
||||
> We want the **API Read Access Token**, not the shorter API key.
|
||||
|
||||
### Auth secret
|
||||
|
||||
`BETTER_AUTH_SECRET` should be a long random string. To generate one:
|
||||
|
||||
```bash
|
||||
npx @better-auth/cli@latest secret
|
||||
# or
|
||||
openssl rand -base64 32
|
||||
```
|
||||
|
||||
### Public URL
|
||||
|
||||
Set `BETTER_AUTH_URL` to the real external URL of your instance. This especially matters for login flows and OIDC callbacks behind a reverse proxy (like nginx or Traefik).
|
||||
|
||||
## Configuration
|
||||
|
||||
| Variable | Required | Notes |
|
||||
| --- | --- | --- |
|
||||
| `TMDB_API_READ_ACCESS_TOKEN` | Yes | TMDB metadata and discovery |
|
||||
| `BETTER_AUTH_SECRET` | Yes | Session and auth secret |
|
||||
| `BETTER_AUTH_URL` | Yes | Public base URL of the app |
|
||||
| `DATA_DIR` | No | Root data directory. Defaults to `/data` in the container |
|
||||
| `IMAGE_CACHE_ENABLED` | No | Defaults to enabled. Set to `false` to use TMDB images directly instead of caching them locally |
|
||||
| `LOG_LEVEL` | No | `error`, `warn`, `info`, or `debug` |
|
||||
| `OIDC_CLIENT_ID` | No | Enable OIDC when set with the matching secret and issuer |
|
||||
| `OIDC_CLIENT_SECRET` | No | OIDC client secret |
|
||||
| `OIDC_ISSUER_URL` | No | OIDC issuer URL |
|
||||
| `OIDC_PROVIDER_NAME` | No | Login button label. Defaults to `SSO` |
|
||||
| `OIDC_AUTO_REGISTER` | No | Defaults to `true` |
|
||||
| `DISABLE_PASSWORD_LOGIN` | No | Set to `true` to hide email/password login when OIDC is configured |
|
||||
|
||||
See [`.env.example`](./.env.example) for the full list.
|
||||
|
||||
## Integrations
|
||||
|
||||
Sofa ships with two kinds of integrations (for now): incoming watch activity and outgoing import lists.
|
||||
|
||||
### Incoming watch activity
|
||||
|
||||
- Plex: logs completed watches through a webhook URL generated in Sofa. Requires an active [Plex Pass](https://www.plex.tv/plex-pass/) license.
|
||||
- Jellyfin: works through the Jellyfin Webhook plugin.
|
||||
- Emby: logs completed watches through webhooks. Requires Emby Server 4.7.9+ and an [Emby Premiere](https://emby.media/premiere.html) subscription.
|
||||
|
||||
These integrations are user-specific, so each user can connect their own media server account and watch history.
|
||||
|
||||
### Outgoing import lists
|
||||
|
||||
- Sonarr: expose your Sofa TV watchlist as a custom import list
|
||||
- Radarr: expose your Sofa movie watchlist as a custom import list
|
||||
|
||||
## Development
|
||||
|
||||
For local development:
|
||||
|
||||
```bash
|
||||
bun install
|
||||
cp .env.example .env
|
||||
bun run dev
|
||||
```
|
||||
|
||||
Useful commands:
|
||||
|
||||
- `bun run test`
|
||||
- `bun run lint`
|
||||
- `bun run check-types`
|
||||
- `bun run db:generate`
|
||||
- `bun run db:migrate`
|
||||
- `bun run db:seed`
|
||||
|
||||
## TMDB notice
|
||||
|
||||
This product uses the TMDB API but is not endorsed or certified by TMDB.
|
||||
|
||||
## License
|
||||
|
||||
[MIT](LICENSE)
|
||||
|
||||
@@ -2,7 +2,10 @@
|
||||
|
||||
import { IconPlus, IconStar } from "@tabler/icons-react";
|
||||
import Image from "next/image";
|
||||
import Link from "next/link";
|
||||
import { useRouter } from "next/navigation";
|
||||
import { useTransition } from "react";
|
||||
import { useProgress } from "@/components/navigation-progress";
|
||||
import { resolveTitle } from "@/lib/actions/titles";
|
||||
|
||||
interface HeroBannerProps {
|
||||
tmdbId: number;
|
||||
@@ -21,7 +24,18 @@ export function HeroBanner({
|
||||
backdropPath,
|
||||
voteAverage,
|
||||
}: HeroBannerProps) {
|
||||
const href = `/titles/tmdb-${tmdbId}-${type}`;
|
||||
const router = useRouter();
|
||||
const progress = useProgress();
|
||||
const [isPending, startTransition] = useTransition();
|
||||
|
||||
function handleNavigate() {
|
||||
if (isPending) return;
|
||||
progress.start();
|
||||
startTransition(async () => {
|
||||
const id = await resolveTitle(tmdbId, type);
|
||||
if (id) router.push(`/titles/${id}`);
|
||||
});
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="relative -mt-6 mr-[calc(-50vw+50%)] mb-4 ml-[calc(-50vw+50%)] animate-stagger-item overflow-hidden">
|
||||
@@ -67,21 +81,28 @@ export function HeroBanner({
|
||||
Trending today
|
||||
</span>
|
||||
</div>
|
||||
<Link href={href} className="group/title">
|
||||
<button
|
||||
type="button"
|
||||
className="group/title cursor-pointer text-left"
|
||||
onClick={handleNavigate}
|
||||
disabled={isPending}
|
||||
>
|
||||
<h2 className="text-balance font-display text-3xl tracking-tight transition-colors group-hover/title:text-primary sm:text-4xl">
|
||||
{title}
|
||||
</h2>
|
||||
</Link>
|
||||
</button>
|
||||
<p className="mt-2 line-clamp-2 max-w-2xl text-muted-foreground text-sm">
|
||||
{overview}
|
||||
</p>
|
||||
<Link
|
||||
href={href}
|
||||
className="mt-4 inline-flex h-9 items-center gap-2 rounded-lg bg-primary px-4 font-medium text-primary-foreground text-sm transition-shadow hover:shadow-md hover:shadow-primary/20"
|
||||
<button
|
||||
type="button"
|
||||
onClick={handleNavigate}
|
||||
disabled={isPending}
|
||||
className="mt-4 inline-flex h-9 cursor-pointer items-center gap-2 rounded-lg bg-primary px-4 font-medium text-primary-foreground text-sm transition-shadow hover:shadow-md hover:shadow-primary/20 disabled:opacity-70"
|
||||
>
|
||||
<IconPlus aria-hidden={true} className="size-4" />
|
||||
Add to Library
|
||||
</Link>
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@@ -4,25 +4,17 @@ import { notFound } from "next/navigation";
|
||||
import { getSession } from "@/lib/auth/session";
|
||||
import { db } from "@/lib/db/client";
|
||||
import { persons } from "@/lib/db/schema";
|
||||
import {
|
||||
getLocalFilmography,
|
||||
getOrFetchPerson,
|
||||
getOrFetchPersonByTmdbId,
|
||||
} from "@/lib/services/person";
|
||||
import { getLocalFilmography, getOrFetchPerson } from "@/lib/services/person";
|
||||
import { getUserStatusesByTitleIds } from "@/lib/services/tracking";
|
||||
import { FilmographyGrid } from "./_components/filmography-grid";
|
||||
import { PersonHero } from "./_components/person-hero";
|
||||
|
||||
const TMDB_PATTERN = /^tmdb-(\d+)$/;
|
||||
|
||||
export async function generateMetadata({
|
||||
params,
|
||||
}: {
|
||||
params: Promise<{ id: string }>;
|
||||
}): Promise<Metadata> {
|
||||
const { id } = await params;
|
||||
if (TMDB_PATTERN.test(id)) return { title: "Sofa" };
|
||||
|
||||
const person = db.select().from(persons).where(eq(persons.id, id)).get();
|
||||
if (!person) return { title: "Not Found — Sofa" };
|
||||
|
||||
@@ -38,13 +30,8 @@ export default async function PersonDetailPage({
|
||||
params: Promise<{ id: string }>;
|
||||
}) {
|
||||
const { id } = await params;
|
||||
await getSession();
|
||||
|
||||
const tmdbMatch = TMDB_PATTERN.exec(id);
|
||||
const person = tmdbMatch
|
||||
? await getOrFetchPersonByTmdbId(Number(tmdbMatch[1]))
|
||||
: await getOrFetchPerson(id);
|
||||
|
||||
const person = await getOrFetchPerson(id);
|
||||
if (!person) notFound();
|
||||
|
||||
const filmography = getLocalFilmography(person.id);
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { eq } from "drizzle-orm";
|
||||
import type { Metadata } from "next";
|
||||
import { notFound, redirect } from "next/navigation";
|
||||
import { notFound } from "next/navigation";
|
||||
import { Suspense } from "react";
|
||||
import {
|
||||
RecommendationsSkeleton,
|
||||
@@ -9,7 +9,7 @@ import {
|
||||
import { getSession } from "@/lib/auth/session";
|
||||
import { db } from "@/lib/db/client";
|
||||
import { titles } from "@/lib/db/schema";
|
||||
import { getTitleWithChildren, importTitle } from "@/lib/services/metadata";
|
||||
import { getOrFetchTitle } from "@/lib/services/metadata";
|
||||
import { getUserTitleInfo } from "@/lib/services/tracking";
|
||||
import { tmdbImageUrl } from "@/lib/tmdb/image";
|
||||
import { getTitleThemeStyle } from "@/lib/utils/title-theme";
|
||||
@@ -23,16 +23,12 @@ import { TitleProvider } from "./_components/title-provider";
|
||||
import { TitleRecommendations } from "./_components/title-recommendations";
|
||||
import { TitleSeasons } from "./_components/title-seasons";
|
||||
|
||||
const TMDB_ID_PATTERN = /^tmdb-(\d+)-(movie|tv)$/;
|
||||
|
||||
export async function generateMetadata({
|
||||
params,
|
||||
}: {
|
||||
params: Promise<{ id: string }>;
|
||||
}): Promise<Metadata> {
|
||||
const { id } = await params;
|
||||
if (TMDB_ID_PATTERN.test(id)) return { title: "Sofa" };
|
||||
|
||||
const title = db.select().from(titles).where(eq(titles.id, id)).get();
|
||||
if (!title) return { title: "Not Found — Sofa" };
|
||||
|
||||
@@ -56,21 +52,10 @@ export default async function TitleDetailPage({
|
||||
}) {
|
||||
const { id } = await params;
|
||||
|
||||
// TMDB ID resolution: tmdb-{id}-{type} → import + redirect
|
||||
const tmdbMatch = TMDB_ID_PATTERN.exec(id);
|
||||
if (tmdbMatch) {
|
||||
const title = await importTitle(
|
||||
Number(tmdbMatch[1]),
|
||||
tmdbMatch[2] as "movie" | "tv",
|
||||
);
|
||||
if (!title) notFound();
|
||||
redirect(`/titles/${title.id}`);
|
||||
}
|
||||
|
||||
// Fetch title + user info in parallel
|
||||
const session = await getSession();
|
||||
const [result, userInfo] = await Promise.all([
|
||||
getTitleWithChildren(id),
|
||||
getOrFetchTitle(id),
|
||||
session ? getUserTitleInfo(session.user.id, id) : null,
|
||||
]);
|
||||
if (!result) notFound();
|
||||
|
||||
@@ -36,6 +36,8 @@ import { Kbd } from "@/components/ui/kbd";
|
||||
import { Skeleton } from "@/components/ui/skeleton";
|
||||
import { useDebounce } from "@/hooks/use-debounce";
|
||||
import { useSearch } from "@/hooks/use-search";
|
||||
import { resolvePerson } from "@/lib/actions/people";
|
||||
import { resolveTitle } from "@/lib/actions/titles";
|
||||
import {
|
||||
commandPaletteOpenAtom,
|
||||
helpOpenAtom,
|
||||
@@ -118,9 +120,13 @@ export function CommandPalette() {
|
||||
setCommandPaletteOpen(false);
|
||||
progress.start();
|
||||
if (result.type === "person") {
|
||||
router.push(`/people/tmdb-${result.tmdbId}`);
|
||||
void resolvePerson(result.tmdbId).then((id) => {
|
||||
if (id) router.push(`/people/${id}`);
|
||||
});
|
||||
} else {
|
||||
router.push(`/titles/tmdb-${result.tmdbId}-${result.type}`);
|
||||
void resolveTitle(result.tmdbId, result.type).then((id) => {
|
||||
if (id) router.push(`/titles/${id}`);
|
||||
});
|
||||
}
|
||||
},
|
||||
[router, setCommandPaletteOpen, progress],
|
||||
|
||||
+47
-23
@@ -14,13 +14,16 @@ import {
|
||||
import { type MotionStyle, type MotionValue, motion } from "motion/react";
|
||||
import Image from "next/image";
|
||||
import Link from "next/link";
|
||||
import { useEffect, useState } from "react";
|
||||
import { useRouter } from "next/navigation";
|
||||
import { useEffect, useState, useTransition } from "react";
|
||||
import { useProgress } from "@/components/navigation-progress";
|
||||
import {
|
||||
Tooltip,
|
||||
TooltipContent,
|
||||
TooltipTrigger,
|
||||
} from "@/components/ui/tooltip";
|
||||
import { useTiltEffect } from "@/hooks/use-tilt-effect";
|
||||
import { resolveTitle } from "@/lib/actions/titles";
|
||||
import { quickAddToWatchlist } from "@/lib/actions/watchlist";
|
||||
|
||||
type TitleStatus = "watchlist" | "in_progress" | "completed";
|
||||
@@ -280,8 +283,30 @@ export function TitleCard({
|
||||
userStatus,
|
||||
episodeProgress,
|
||||
}: TitleCardProps) {
|
||||
const href = id ? `/titles/${id}` : `/titles/tmdb-${tmdbId}-${type}`;
|
||||
const tilt = useTiltEffect();
|
||||
const router = useRouter();
|
||||
const progress = useProgress();
|
||||
const [isPending, startTransition] = useTransition();
|
||||
|
||||
const cardContent = (
|
||||
<motion.div ref={tilt.ref} style={tilt.containerStyle} {...tilt.handlers}>
|
||||
<CardInner
|
||||
title={title}
|
||||
type={type}
|
||||
posterPath={posterPath}
|
||||
releaseDate={releaseDate}
|
||||
voteAverage={voteAverage}
|
||||
userStatus={userStatus}
|
||||
episodeProgress={episodeProgress}
|
||||
tiltStyles={{
|
||||
imageStyle: tilt.imageStyle,
|
||||
glareBackground: tilt.glareBackground,
|
||||
glareOpacity: tilt.glareOpacity,
|
||||
}}
|
||||
/>
|
||||
</motion.div>
|
||||
);
|
||||
|
||||
return (
|
||||
<div className="group relative">
|
||||
<QuickAddButton
|
||||
@@ -289,28 +314,27 @@ export function TitleCard({
|
||||
type={type as "movie" | "tv"}
|
||||
userStatus={userStatus}
|
||||
/>
|
||||
<Link href={href}>
|
||||
<motion.div
|
||||
ref={tilt.ref}
|
||||
style={tilt.containerStyle}
|
||||
{...tilt.handlers}
|
||||
{id ? (
|
||||
<Link href={`/titles/${id}`}>{cardContent}</Link>
|
||||
) : (
|
||||
<button
|
||||
type="button"
|
||||
disabled={isPending}
|
||||
className={`w-full text-left ${isPending ? "pointer-events-none opacity-70" : "cursor-pointer"}`}
|
||||
onClick={() => {
|
||||
progress.start();
|
||||
startTransition(async () => {
|
||||
const resolvedId = await resolveTitle(
|
||||
tmdbId,
|
||||
type as "movie" | "tv",
|
||||
);
|
||||
if (resolvedId) router.push(`/titles/${resolvedId}`);
|
||||
});
|
||||
}}
|
||||
>
|
||||
<CardInner
|
||||
title={title}
|
||||
type={type}
|
||||
posterPath={posterPath}
|
||||
releaseDate={releaseDate}
|
||||
voteAverage={voteAverage}
|
||||
userStatus={userStatus}
|
||||
episodeProgress={episodeProgress}
|
||||
tiltStyles={{
|
||||
imageStyle: tilt.imageStyle,
|
||||
glareBackground: tilt.glareBackground,
|
||||
glareOpacity: tilt.glareOpacity,
|
||||
}}
|
||||
/>
|
||||
</motion.div>
|
||||
</Link>
|
||||
{cardContent}
|
||||
</button>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,10 @@
|
||||
"use server";
|
||||
|
||||
import { requireSession } from "@/lib/auth/session";
|
||||
import { getOrFetchPersonByTmdbId } from "@/lib/services/person";
|
||||
|
||||
export async function resolvePerson(tmdbId: number): Promise<string | null> {
|
||||
await requireSession();
|
||||
const person = await getOrFetchPersonByTmdbId(tmdbId);
|
||||
return person?.id ?? null;
|
||||
}
|
||||
@@ -5,6 +5,7 @@ import { z } from "zod";
|
||||
import { requireSession } from "@/lib/auth/session";
|
||||
import { db } from "@/lib/db/client";
|
||||
import { episodes } from "@/lib/db/schema";
|
||||
import { getOrFetchTitleByTmdbId } from "@/lib/services/metadata";
|
||||
import {
|
||||
logEpisodeWatch,
|
||||
logEpisodeWatchBatch,
|
||||
@@ -22,6 +23,15 @@ async function getSessionUserId() {
|
||||
return session.user.id;
|
||||
}
|
||||
|
||||
export async function resolveTitle(
|
||||
tmdbId: number,
|
||||
type: "movie" | "tv",
|
||||
): Promise<string | null> {
|
||||
await requireSession();
|
||||
const title = await getOrFetchTitleByTmdbId(tmdbId, type);
|
||||
return title?.id ?? null;
|
||||
}
|
||||
|
||||
export async function updateTitleStatus(
|
||||
titleId: string,
|
||||
status: "in_progress" | null,
|
||||
|
||||
@@ -11,7 +11,7 @@ import {
|
||||
type HistoryBucket,
|
||||
type TimePeriod,
|
||||
} from "@/lib/services/discovery";
|
||||
import { importTitle } from "@/lib/services/metadata";
|
||||
import { getOrFetchTitleByTmdbId } from "@/lib/services/metadata";
|
||||
import {
|
||||
getEpisodeProgressByTmdbIds,
|
||||
getUserStatusesByTmdbIds,
|
||||
@@ -41,7 +41,7 @@ export async function quickAddToWatchlist(
|
||||
const session = await requireSession();
|
||||
const userId = session.user.id;
|
||||
|
||||
const title = await importTitle(tmdbId, type);
|
||||
const title = await getOrFetchTitleByTmdbId(tmdbId, type);
|
||||
if (!title) throw new Error("Failed to import title");
|
||||
|
||||
const existing = db
|
||||
|
||||
@@ -20,8 +20,26 @@ export async function refreshAvailability(titleId: string) {
|
||||
const now = new Date();
|
||||
const offerTypes = ["flatrate", "rent", "buy", "free", "ads"] as const;
|
||||
|
||||
// Collect all offer rows, then batch insert in a single transaction
|
||||
const allOfferRows: (typeof availabilityOffers.$inferInsert)[] = [];
|
||||
for (const offerType of offerTypes) {
|
||||
const providers = us[offerType];
|
||||
if (!providers) continue;
|
||||
for (const p of providers) {
|
||||
allOfferRows.push({
|
||||
titleId,
|
||||
region: "US",
|
||||
providerId: p.provider_id,
|
||||
providerName: p.provider_name,
|
||||
logoPath: p.logo_path,
|
||||
offerType,
|
||||
link: us.link ?? null,
|
||||
lastFetchedAt: now,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
db.transaction((tx) => {
|
||||
// Delete existing offers for this title+region
|
||||
tx.delete(availabilityOffers)
|
||||
.where(
|
||||
and(
|
||||
@@ -31,25 +49,11 @@ export async function refreshAvailability(titleId: string) {
|
||||
)
|
||||
.run();
|
||||
|
||||
for (const offerType of offerTypes) {
|
||||
const providers = us[offerType];
|
||||
if (!providers) continue;
|
||||
|
||||
for (const p of providers) {
|
||||
tx.insert(availabilityOffers)
|
||||
.values({
|
||||
titleId,
|
||||
region: "US",
|
||||
providerId: p.provider_id,
|
||||
providerName: p.provider_name,
|
||||
logoPath: p.logo_path,
|
||||
offerType,
|
||||
link: us.link ?? null,
|
||||
lastFetchedAt: now,
|
||||
})
|
||||
.onConflictDoNothing()
|
||||
.run();
|
||||
}
|
||||
if (allOfferRows.length > 0) {
|
||||
tx.insert(availabilityOffers)
|
||||
.values(allOfferRows)
|
||||
.onConflictDoNothing()
|
||||
.run();
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
+108
-136
@@ -1,4 +1,4 @@
|
||||
import { eq, inArray } from "drizzle-orm";
|
||||
import { eq, inArray, sql } from "drizzle-orm";
|
||||
import { db } from "@/lib/db/client";
|
||||
import { persons, titleCast, titles } from "@/lib/db/schema";
|
||||
import { createLogger } from "@/lib/logger";
|
||||
@@ -119,73 +119,59 @@ export async function refreshCredits(titleId: string) {
|
||||
];
|
||||
const personIds = batchUpsertPersons(allPeople);
|
||||
|
||||
// Batch insert titleCast rows
|
||||
db.transaction((tx) => {
|
||||
const now = new Date();
|
||||
for (let i = 0; i < castSlice.length; i++) {
|
||||
const c = castSlice[i];
|
||||
const personId = personIds.get(c.id);
|
||||
if (!personId) continue;
|
||||
tx.insert(titleCast)
|
||||
.values({
|
||||
titleId,
|
||||
personId,
|
||||
character: c.character,
|
||||
department: "Acting",
|
||||
job: null,
|
||||
displayOrder: i,
|
||||
episodeCount: null,
|
||||
lastFetchedAt: now,
|
||||
})
|
||||
.onConflictDoUpdate({
|
||||
target: [
|
||||
titleCast.titleId,
|
||||
titleCast.personId,
|
||||
titleCast.department,
|
||||
titleCast.character,
|
||||
],
|
||||
set: {
|
||||
job: null,
|
||||
displayOrder: i,
|
||||
episodeCount: null,
|
||||
lastFetchedAt: now,
|
||||
},
|
||||
})
|
||||
.run();
|
||||
}
|
||||
let crewOrder = 100;
|
||||
for (const c of notableCrew) {
|
||||
const personId = personIds.get(c.id);
|
||||
if (!personId) continue;
|
||||
tx.insert(titleCast)
|
||||
.values({
|
||||
titleId,
|
||||
personId,
|
||||
character: null,
|
||||
department: c.department,
|
||||
job: c.job,
|
||||
displayOrder: crewOrder,
|
||||
episodeCount: null,
|
||||
lastFetchedAt: now,
|
||||
})
|
||||
.onConflictDoUpdate({
|
||||
target: [
|
||||
titleCast.titleId,
|
||||
titleCast.personId,
|
||||
titleCast.department,
|
||||
titleCast.character,
|
||||
],
|
||||
set: {
|
||||
job: c.job,
|
||||
displayOrder: crewOrder,
|
||||
episodeCount: null,
|
||||
lastFetchedAt: now,
|
||||
},
|
||||
})
|
||||
.run();
|
||||
crewOrder++;
|
||||
}
|
||||
});
|
||||
// Collect all titleCast rows (cast + crew) and batch insert
|
||||
const now = new Date();
|
||||
const allCastRows: (typeof titleCast.$inferInsert)[] = [];
|
||||
for (let i = 0; i < castSlice.length; i++) {
|
||||
const c = castSlice[i];
|
||||
const personId = personIds.get(c.id);
|
||||
if (!personId) continue;
|
||||
allCastRows.push({
|
||||
titleId,
|
||||
personId,
|
||||
character: c.character,
|
||||
department: "Acting",
|
||||
job: null,
|
||||
displayOrder: i,
|
||||
episodeCount: null,
|
||||
lastFetchedAt: now,
|
||||
});
|
||||
}
|
||||
let crewOrder = 100;
|
||||
for (const c of notableCrew) {
|
||||
const personId = personIds.get(c.id);
|
||||
if (!personId) continue;
|
||||
allCastRows.push({
|
||||
titleId,
|
||||
personId,
|
||||
character: null,
|
||||
department: c.department,
|
||||
job: c.job,
|
||||
displayOrder: crewOrder,
|
||||
episodeCount: null,
|
||||
lastFetchedAt: now,
|
||||
});
|
||||
crewOrder++;
|
||||
}
|
||||
if (allCastRows.length > 0) {
|
||||
db.insert(titleCast)
|
||||
.values(allCastRows)
|
||||
.onConflictDoUpdate({
|
||||
target: [
|
||||
titleCast.titleId,
|
||||
titleCast.personId,
|
||||
titleCast.department,
|
||||
titleCast.character,
|
||||
],
|
||||
set: {
|
||||
job: sql`excluded.job`,
|
||||
displayOrder: sql`excluded.displayOrder`,
|
||||
episodeCount: sql`excluded.episodeCount`,
|
||||
lastFetchedAt: sql`excluded.lastFetchedAt`,
|
||||
},
|
||||
})
|
||||
.run();
|
||||
}
|
||||
} else {
|
||||
const credits = await getTvAggregateCredits(title.tmdbId);
|
||||
const castSlice = credits.cast.slice(0, 20);
|
||||
@@ -228,74 +214,60 @@ export async function refreshCredits(titleId: string) {
|
||||
];
|
||||
const personIds = batchUpsertPersons(allPeople);
|
||||
|
||||
// Batch insert titleCast rows
|
||||
db.transaction((tx) => {
|
||||
const now = new Date();
|
||||
for (let i = 0; i < castSlice.length; i++) {
|
||||
const c = castSlice[i];
|
||||
const personId = personIds.get(c.id);
|
||||
if (!personId) continue;
|
||||
const character = c.roles?.[0]?.character ?? null;
|
||||
tx.insert(titleCast)
|
||||
.values({
|
||||
titleId,
|
||||
personId,
|
||||
character,
|
||||
department: "Acting",
|
||||
job: null,
|
||||
displayOrder: i,
|
||||
episodeCount: c.total_episode_count,
|
||||
lastFetchedAt: now,
|
||||
})
|
||||
.onConflictDoUpdate({
|
||||
target: [
|
||||
titleCast.titleId,
|
||||
titleCast.personId,
|
||||
titleCast.department,
|
||||
titleCast.character,
|
||||
],
|
||||
set: {
|
||||
job: null,
|
||||
displayOrder: i,
|
||||
episodeCount: c.total_episode_count,
|
||||
lastFetchedAt: now,
|
||||
},
|
||||
})
|
||||
.run();
|
||||
}
|
||||
let crewOrder = 100;
|
||||
for (const c of notableCrew) {
|
||||
const personId = personIds.get(c.person.id);
|
||||
if (!personId) continue;
|
||||
tx.insert(titleCast)
|
||||
.values({
|
||||
titleId,
|
||||
personId,
|
||||
character: null,
|
||||
department: c.person.department,
|
||||
job: c.job,
|
||||
displayOrder: crewOrder,
|
||||
episodeCount: c.episodeCount,
|
||||
lastFetchedAt: now,
|
||||
})
|
||||
.onConflictDoUpdate({
|
||||
target: [
|
||||
titleCast.titleId,
|
||||
titleCast.personId,
|
||||
titleCast.department,
|
||||
titleCast.character,
|
||||
],
|
||||
set: {
|
||||
job: c.job,
|
||||
displayOrder: crewOrder,
|
||||
episodeCount: c.episodeCount,
|
||||
lastFetchedAt: now,
|
||||
},
|
||||
})
|
||||
.run();
|
||||
crewOrder++;
|
||||
}
|
||||
});
|
||||
// Collect all titleCast rows (cast + crew) and batch insert
|
||||
const now = new Date();
|
||||
const allCastRows: (typeof titleCast.$inferInsert)[] = [];
|
||||
for (let i = 0; i < castSlice.length; i++) {
|
||||
const c = castSlice[i];
|
||||
const personId = personIds.get(c.id);
|
||||
if (!personId) continue;
|
||||
const character = c.roles?.[0]?.character ?? null;
|
||||
allCastRows.push({
|
||||
titleId,
|
||||
personId,
|
||||
character,
|
||||
department: "Acting",
|
||||
job: null,
|
||||
displayOrder: i,
|
||||
episodeCount: c.total_episode_count,
|
||||
lastFetchedAt: now,
|
||||
});
|
||||
}
|
||||
let crewOrder = 100;
|
||||
for (const c of notableCrew) {
|
||||
const personId = personIds.get(c.person.id);
|
||||
if (!personId) continue;
|
||||
allCastRows.push({
|
||||
titleId,
|
||||
personId,
|
||||
character: null,
|
||||
department: c.person.department,
|
||||
job: c.job,
|
||||
displayOrder: crewOrder,
|
||||
episodeCount: c.episodeCount,
|
||||
lastFetchedAt: now,
|
||||
});
|
||||
crewOrder++;
|
||||
}
|
||||
if (allCastRows.length > 0) {
|
||||
db.insert(titleCast)
|
||||
.values(allCastRows)
|
||||
.onConflictDoUpdate({
|
||||
target: [
|
||||
titleCast.titleId,
|
||||
titleCast.personId,
|
||||
titleCast.department,
|
||||
titleCast.character,
|
||||
],
|
||||
set: {
|
||||
job: sql`excluded.job`,
|
||||
displayOrder: sql`excluded.displayOrder`,
|
||||
episodeCount: sql`excluded.episodeCount`,
|
||||
lastFetchedAt: sql`excluded.lastFetchedAt`,
|
||||
},
|
||||
})
|
||||
.run();
|
||||
}
|
||||
}
|
||||
|
||||
log.debug(`Credits refreshed for "${title.title}"`);
|
||||
|
||||
+60
-42
@@ -145,22 +145,13 @@ export async function cacheImagesForTitle(titleId: string) {
|
||||
const title = db.select().from(titles).where(eq(titles.id, titleId)).get();
|
||||
if (!title) return;
|
||||
|
||||
const tasks: Promise<unknown>[] = [];
|
||||
// Collect all candidate images, then check cache in parallel
|
||||
const candidates: { imgPath: string; category: ImageCategory }[] = [];
|
||||
if (title.posterPath)
|
||||
candidates.push({ imgPath: title.posterPath, category: "posters" });
|
||||
if (title.backdropPath)
|
||||
candidates.push({ imgPath: title.backdropPath, category: "backdrops" });
|
||||
|
||||
if (
|
||||
title.posterPath &&
|
||||
!(await isImageCached("posters", path.basename(title.posterPath)))
|
||||
) {
|
||||
tasks.push(downloadAndCacheImage(title.posterPath, "posters"));
|
||||
}
|
||||
if (
|
||||
title.backdropPath &&
|
||||
!(await isImageCached("backdrops", path.basename(title.backdropPath)))
|
||||
) {
|
||||
tasks.push(downloadAndCacheImage(title.backdropPath, "backdrops"));
|
||||
}
|
||||
|
||||
// Season posters
|
||||
if (title.type === "tv") {
|
||||
const allSeasons = db
|
||||
.select()
|
||||
@@ -168,15 +159,22 @@ export async function cacheImagesForTitle(titleId: string) {
|
||||
.where(eq(seasons.titleId, titleId))
|
||||
.all();
|
||||
for (const s of allSeasons) {
|
||||
if (
|
||||
s.posterPath &&
|
||||
!(await isImageCached("posters", path.basename(s.posterPath)))
|
||||
) {
|
||||
tasks.push(downloadAndCacheImage(s.posterPath, "posters"));
|
||||
}
|
||||
if (s.posterPath)
|
||||
candidates.push({ imgPath: s.posterPath, category: "posters" });
|
||||
}
|
||||
}
|
||||
|
||||
// Parallel cache checks instead of sequential awaits
|
||||
const checks = await Promise.all(
|
||||
candidates.map(async (c) => ({
|
||||
...c,
|
||||
cached: await isImageCached(c.category, path.basename(c.imgPath)),
|
||||
})),
|
||||
);
|
||||
const tasks = checks
|
||||
.filter((c) => !c.cached)
|
||||
.map((c) => downloadAndCacheImage(c.imgPath, c.category));
|
||||
|
||||
if (tasks.length > 0) {
|
||||
log.debug(`Caching ${tasks.length} images for title ${titleId}`);
|
||||
}
|
||||
@@ -200,15 +198,20 @@ export async function cacheEpisodeStills(titleId: string) {
|
||||
.where(inArray(episodes.seasonId, seasonIds))
|
||||
.all();
|
||||
|
||||
const tasks: Promise<unknown>[] = [];
|
||||
for (const ep of allEps) {
|
||||
if (
|
||||
ep.stillPath &&
|
||||
!(await isImageCached("stills", path.basename(ep.stillPath)))
|
||||
) {
|
||||
tasks.push(downloadAndCacheImage(ep.stillPath, "stills"));
|
||||
}
|
||||
}
|
||||
const epsWithStills = allEps.filter(
|
||||
(ep): ep is typeof ep & { stillPath: string } => ep.stillPath != null,
|
||||
);
|
||||
// Parallel cache checks instead of sequential awaits
|
||||
const checks = await Promise.all(
|
||||
epsWithStills.map(async (ep) => ({
|
||||
stillPath: ep.stillPath,
|
||||
cached: await isImageCached("stills", path.basename(ep.stillPath)),
|
||||
})),
|
||||
);
|
||||
const tasks = checks
|
||||
.filter((c) => !c.cached)
|
||||
.map((c) => downloadAndCacheImage(c.stillPath, "stills"));
|
||||
|
||||
await Promise.allSettled(tasks);
|
||||
}
|
||||
|
||||
@@ -219,17 +222,24 @@ export async function cacheProviderLogos(titleId: string) {
|
||||
.where(eq(availabilityOffers.titleId, titleId))
|
||||
.all();
|
||||
|
||||
const tasks: Promise<unknown>[] = [];
|
||||
const seen = new Set<string>();
|
||||
// Deduplicate and parallel cache checks
|
||||
const uniqueLogos = new Map<string, string>();
|
||||
for (const offer of offers) {
|
||||
if (offer.logoPath) {
|
||||
const basename = path.basename(offer.logoPath);
|
||||
if (!seen.has(basename) && !(await isImageCached("logos", basename))) {
|
||||
seen.add(basename);
|
||||
tasks.push(downloadAndCacheImage(offer.logoPath, "logos"));
|
||||
}
|
||||
if (!uniqueLogos.has(basename)) uniqueLogos.set(basename, offer.logoPath);
|
||||
}
|
||||
}
|
||||
const checks = await Promise.all(
|
||||
[...uniqueLogos.entries()].map(async ([basename, logoPath]) => ({
|
||||
logoPath,
|
||||
cached: await isImageCached("logos", basename),
|
||||
})),
|
||||
);
|
||||
const tasks = checks
|
||||
.filter((c) => !c.cached)
|
||||
.map((c) => downloadAndCacheImage(c.logoPath, "logos"));
|
||||
|
||||
await Promise.allSettled(tasks);
|
||||
}
|
||||
|
||||
@@ -241,17 +251,25 @@ export async function cacheProfilePhotos(titleId: string) {
|
||||
.where(eq(titleCast.titleId, titleId))
|
||||
.all();
|
||||
|
||||
const tasks: Promise<unknown>[] = [];
|
||||
const seen = new Set<string>();
|
||||
// Deduplicate and parallel cache checks
|
||||
const uniqueProfiles = new Map<string, string>();
|
||||
for (const row of castRows) {
|
||||
if (row.profilePath) {
|
||||
const basename = path.basename(row.profilePath);
|
||||
if (!seen.has(basename) && !(await isImageCached("profiles", basename))) {
|
||||
seen.add(basename);
|
||||
tasks.push(downloadAndCacheImage(row.profilePath, "profiles"));
|
||||
}
|
||||
if (!uniqueProfiles.has(basename))
|
||||
uniqueProfiles.set(basename, row.profilePath);
|
||||
}
|
||||
}
|
||||
const checks = await Promise.all(
|
||||
[...uniqueProfiles.entries()].map(async ([basename, profilePath]) => ({
|
||||
profilePath,
|
||||
cached: await isImageCached("profiles", basename),
|
||||
})),
|
||||
);
|
||||
const tasks = checks
|
||||
.filter((c) => !c.cached)
|
||||
.map((c) => downloadAndCacheImage(c.profilePath, "profiles"));
|
||||
|
||||
if (tasks.length > 0) {
|
||||
log.debug(`Caching ${tasks.length} profile photos for title ${titleId}`);
|
||||
}
|
||||
|
||||
+23
-18
@@ -88,27 +88,32 @@ export async function getSonarrList(
|
||||
)
|
||||
.all();
|
||||
|
||||
const result: { TvdbId: number; Title: string }[] = [];
|
||||
|
||||
for (const row of rows) {
|
||||
let { tvdbId } = row;
|
||||
|
||||
if (tvdbId == null) {
|
||||
try {
|
||||
const externalIds = await getTvExternalIds(row.tmdbId);
|
||||
tvdbId = externalIds.tvdb_id;
|
||||
// Resolve missing TVDB IDs in parallel instead of sequentially
|
||||
const needsResolution = rows.filter((r) => r.tvdbId == null);
|
||||
if (needsResolution.length > 0) {
|
||||
const resolved = await Promise.all(
|
||||
needsResolution.map(async (row) => {
|
||||
try {
|
||||
const externalIds = await getTvExternalIds(row.tmdbId);
|
||||
return { row, tvdbId: externalIds.tvdb_id };
|
||||
} catch (err) {
|
||||
log.warn(`Failed to resolve TVDB ID for TMDB ${row.tmdbId}:`, err);
|
||||
return { row, tvdbId: null };
|
||||
}
|
||||
}),
|
||||
);
|
||||
// Batch update resolved IDs in a single transaction
|
||||
db.transaction((tx) => {
|
||||
for (const { row, tvdbId } of resolved) {
|
||||
if (tvdbId != null) {
|
||||
db.update(titles).set({ tvdbId }).where(eq(titles.id, row.id)).run();
|
||||
row.tvdbId = tvdbId;
|
||||
tx.update(titles).set({ tvdbId }).where(eq(titles.id, row.id)).run();
|
||||
}
|
||||
} catch (err) {
|
||||
log.warn(`Failed to resolve TVDB ID for TMDB ${row.tmdbId}:`, err);
|
||||
}
|
||||
}
|
||||
|
||||
if (tvdbId != null) {
|
||||
result.push({ TvdbId: tvdbId, Title: row.title });
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
return result;
|
||||
return rows
|
||||
.filter((r): r is typeof r & { tvdbId: number } => r.tvdbId != null)
|
||||
.map((r) => ({ TvdbId: r.tvdbId, Title: r.title }));
|
||||
}
|
||||
|
||||
+68
-52
@@ -1,4 +1,4 @@
|
||||
import { eq, inArray } from "drizzle-orm";
|
||||
import { eq, inArray, sql } from "drizzle-orm";
|
||||
import { db } from "@/lib/db/client";
|
||||
import {
|
||||
availabilityOffers,
|
||||
@@ -48,7 +48,7 @@ const log = createLogger("metadata");
|
||||
* Insert a title row, or return the existing one if a concurrent insert won the race.
|
||||
* Catches SQLITE_CONSTRAINT_UNIQUE and falls back to a SELECT.
|
||||
*/
|
||||
function insertTitleOrGet(values: typeof titles.$inferInsert, tmdbId: number) {
|
||||
function upsertTitle(values: typeof titles.$inferInsert, tmdbId: number) {
|
||||
try {
|
||||
return db.insert(titles).values(values).returning().get();
|
||||
} catch (err: unknown) {
|
||||
@@ -66,19 +66,21 @@ function insertTitleOrGet(values: typeof titles.$inferInsert, tmdbId: number) {
|
||||
|
||||
function upsertGenres(titleId: string, tmdbGenres: TmdbGenre[]) {
|
||||
if (tmdbGenres.length === 0) return;
|
||||
for (const g of tmdbGenres) {
|
||||
db.insert(genres)
|
||||
.values({ id: g.id, name: g.name })
|
||||
.onConflictDoUpdate({ target: genres.id, set: { name: g.name } })
|
||||
.run();
|
||||
}
|
||||
db.delete(titleGenres).where(eq(titleGenres.titleId, titleId)).run();
|
||||
for (const g of tmdbGenres) {
|
||||
db.insert(titleGenres)
|
||||
.values({ titleId, genreId: g.id })
|
||||
.onConflictDoNothing()
|
||||
.run();
|
||||
}
|
||||
db.transaction((tx) => {
|
||||
for (const g of tmdbGenres) {
|
||||
tx.insert(genres)
|
||||
.values({ id: g.id, name: g.name })
|
||||
.onConflictDoUpdate({ target: genres.id, set: { name: g.name } })
|
||||
.run();
|
||||
}
|
||||
tx.delete(titleGenres).where(eq(titleGenres.titleId, titleId)).run();
|
||||
for (const g of tmdbGenres) {
|
||||
tx.insert(titleGenres)
|
||||
.values({ titleId, genreId: g.id })
|
||||
.onConflictDoNothing()
|
||||
.run();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/** @internal */
|
||||
@@ -99,12 +101,12 @@ export function extractTvContentRating(show: TmdbTvDetails): string | null {
|
||||
return us?.rating || null;
|
||||
}
|
||||
|
||||
type ImportResult = ReturnType<typeof _importTitle>;
|
||||
type ImportResult = ReturnType<typeof _getOrFetchTitleByTmdbId>;
|
||||
|
||||
/** In-flight import promises keyed by tmdbId — coalesces concurrent calls */
|
||||
const inflightImports = new Map<number, ImportResult>();
|
||||
|
||||
export function importTitle(
|
||||
export function getOrFetchTitleByTmdbId(
|
||||
tmdbId: number,
|
||||
type: "movie" | "tv",
|
||||
): ImportResult {
|
||||
@@ -114,14 +116,14 @@ export function importTitle(
|
||||
return inflight;
|
||||
}
|
||||
|
||||
const promise = _importTitle(tmdbId, type).finally(() => {
|
||||
const promise = _getOrFetchTitleByTmdbId(tmdbId, type).finally(() => {
|
||||
inflightImports.delete(tmdbId);
|
||||
}) as ImportResult;
|
||||
inflightImports.set(tmdbId, promise);
|
||||
return promise;
|
||||
}
|
||||
|
||||
async function _importTitle(tmdbId: number, type: "movie" | "tv") {
|
||||
async function _getOrFetchTitleByTmdbId(tmdbId: number, type: "movie" | "tv") {
|
||||
log.debug(`Importing ${type} TMDB ${tmdbId}`);
|
||||
|
||||
const existing = db
|
||||
@@ -191,7 +193,7 @@ async function _importTitle(tmdbId: number, type: "movie" | "tv") {
|
||||
|
||||
if (type === "movie") {
|
||||
const movie = await getMovieDetails(tmdbId);
|
||||
const row = insertTitleOrGet(
|
||||
const row = upsertTitle(
|
||||
{
|
||||
tmdbId: movie.id,
|
||||
type: "movie",
|
||||
@@ -237,7 +239,7 @@ async function _importTitle(tmdbId: number, type: "movie" | "tv") {
|
||||
}
|
||||
|
||||
const show = await getTvDetails(tmdbId);
|
||||
const row = insertTitleOrGet(
|
||||
const row = upsertTitle(
|
||||
{
|
||||
tmdbId: show.id,
|
||||
tvdbId: show.external_ids?.tvdb_id ?? null,
|
||||
@@ -401,28 +403,33 @@ export async function refreshTvChildren(
|
||||
.returning()
|
||||
.get();
|
||||
|
||||
for (const ep of seasonData.episodes) {
|
||||
db.insert(episodes)
|
||||
.values({
|
||||
seasonId: seasonRow.id,
|
||||
episodeNumber: ep.episode_number,
|
||||
name: ep.name,
|
||||
overview: ep.overview,
|
||||
stillPath: ep.still_path,
|
||||
airDate: ep.air_date,
|
||||
runtimeMinutes: ep.runtime,
|
||||
})
|
||||
.onConflictDoUpdate({
|
||||
target: [episodes.seasonId, episodes.episodeNumber],
|
||||
set: {
|
||||
name: ep.name,
|
||||
overview: ep.overview,
|
||||
stillPath: ep.still_path,
|
||||
airDate: ep.air_date,
|
||||
runtimeMinutes: ep.runtime,
|
||||
},
|
||||
})
|
||||
.run();
|
||||
// Batch all episode upserts in a single transaction per season
|
||||
if (seasonData.episodes.length > 0) {
|
||||
db.transaction((tx) => {
|
||||
for (const ep of seasonData.episodes) {
|
||||
tx.insert(episodes)
|
||||
.values({
|
||||
seasonId: seasonRow.id,
|
||||
episodeNumber: ep.episode_number,
|
||||
name: ep.name,
|
||||
overview: ep.overview,
|
||||
stillPath: ep.still_path,
|
||||
airDate: ep.air_date,
|
||||
runtimeMinutes: ep.runtime,
|
||||
})
|
||||
.onConflictDoUpdate({
|
||||
target: [episodes.seasonId, episodes.episodeNumber],
|
||||
set: {
|
||||
name: ep.name,
|
||||
overview: ep.overview,
|
||||
stillPath: ep.still_path,
|
||||
airDate: ep.air_date,
|
||||
runtimeMinutes: ep.runtime,
|
||||
},
|
||||
})
|
||||
.run();
|
||||
}
|
||||
});
|
||||
}
|
||||
} catch (err) {
|
||||
// Skip this season and continue with the rest — partial data is
|
||||
@@ -534,25 +541,34 @@ export async function refreshRecommendations(titleId: string) {
|
||||
for (const f of fallbacks) titleIdMap.set(f.tmdbId, f.id);
|
||||
}
|
||||
|
||||
// Upsert all recommendation rows
|
||||
for (const item of allItems) {
|
||||
const recTitleId = titleIdMap.get(item.result.id);
|
||||
if (!recTitleId) continue;
|
||||
tx.insert(titleRecommendations)
|
||||
.values({
|
||||
// Batch upsert all recommendation rows (N inserts → 1)
|
||||
const recRows = allItems
|
||||
.map((item) => {
|
||||
const recTitleId = titleIdMap.get(item.result.id);
|
||||
if (!recTitleId) return null;
|
||||
return {
|
||||
titleId,
|
||||
recommendedTitleId: recTitleId,
|
||||
source: item.source,
|
||||
rank: item.rank,
|
||||
lastFetchedAt: now,
|
||||
})
|
||||
};
|
||||
})
|
||||
.filter((r): r is NonNullable<typeof r> => r !== null);
|
||||
|
||||
if (recRows.length > 0) {
|
||||
tx.insert(titleRecommendations)
|
||||
.values(recRows)
|
||||
.onConflictDoUpdate({
|
||||
target: [
|
||||
titleRecommendations.titleId,
|
||||
titleRecommendations.recommendedTitleId,
|
||||
titleRecommendations.source,
|
||||
],
|
||||
set: { rank: item.rank, lastFetchedAt: now },
|
||||
set: {
|
||||
rank: sql`excluded.rank`,
|
||||
lastFetchedAt: sql`excluded.lastFetchedAt`,
|
||||
},
|
||||
})
|
||||
.run();
|
||||
}
|
||||
@@ -742,7 +758,7 @@ function readAvailability(
|
||||
}));
|
||||
}
|
||||
|
||||
export async function getTitleWithChildren(id: string): Promise<{
|
||||
export async function getOrFetchTitle(id: string): Promise<{
|
||||
title: ResolvedTitle;
|
||||
seasons: Season[];
|
||||
needsHydration: boolean;
|
||||
|
||||
+30
-56
@@ -77,16 +77,15 @@ export function logEpisodeWatch(
|
||||
.values({ userId, episodeId, watchedAt: now, source })
|
||||
.run();
|
||||
|
||||
// Find the title for this episode
|
||||
const ep = db.select().from(episodes).where(eq(episodes.id, episodeId)).get();
|
||||
if (!ep) return;
|
||||
const season = db
|
||||
.select()
|
||||
.from(seasons)
|
||||
.where(eq(seasons.id, ep.seasonId))
|
||||
// Find the title for this episode (single JOIN instead of 2 queries)
|
||||
const row = db
|
||||
.select({ titleId: seasons.titleId })
|
||||
.from(episodes)
|
||||
.innerJoin(seasons, eq(episodes.seasonId, seasons.id))
|
||||
.where(eq(episodes.id, episodeId))
|
||||
.get();
|
||||
if (!season) return;
|
||||
const { titleId } = season;
|
||||
if (!row) return;
|
||||
const { titleId } = row;
|
||||
|
||||
// Auto-set status to in_progress if not set
|
||||
const existing = db
|
||||
@@ -231,22 +230,14 @@ export function markAllEpisodesWatched(
|
||||
if (!title || title.type !== "tv") return;
|
||||
|
||||
const now = new Date();
|
||||
const allSeasons = db
|
||||
.select()
|
||||
.from(seasons)
|
||||
// Single JOIN instead of seasons → episodes chain (2 queries → 1)
|
||||
const allEps = db
|
||||
.select({ id: episodes.id })
|
||||
.from(episodes)
|
||||
.innerJoin(seasons, eq(episodes.seasonId, seasons.id))
|
||||
.where(eq(seasons.titleId, titleId))
|
||||
.all();
|
||||
|
||||
const seasonIds = allSeasons.map((s) => s.id);
|
||||
const allEps =
|
||||
seasonIds.length > 0
|
||||
? db
|
||||
.select()
|
||||
.from(episodes)
|
||||
.where(inArray(episodes.seasonId, seasonIds))
|
||||
.all()
|
||||
: [];
|
||||
|
||||
const epIds = allEps.map((ep) => ep.id);
|
||||
const existingWatches =
|
||||
epIds.length > 0
|
||||
@@ -279,19 +270,12 @@ export function markAllEpisodesWatched(
|
||||
}
|
||||
|
||||
function checkAllEpisodesWatched(userId: string, titleId: string) {
|
||||
const allSeasons = db
|
||||
.select()
|
||||
.from(seasons)
|
||||
.where(eq(seasons.titleId, titleId))
|
||||
.all();
|
||||
|
||||
if (allSeasons.length === 0) return;
|
||||
|
||||
const seasonIds = allSeasons.map((s) => s.id);
|
||||
// Single JOIN instead of seasons → episodes chain (2 queries → 1)
|
||||
const allEps = db
|
||||
.select()
|
||||
.select({ id: episodes.id })
|
||||
.from(episodes)
|
||||
.where(inArray(episodes.seasonId, seasonIds))
|
||||
.innerJoin(seasons, eq(episodes.seasonId, seasons.id))
|
||||
.where(eq(seasons.titleId, titleId))
|
||||
.all();
|
||||
|
||||
const totalEpisodes = allEps.length;
|
||||
@@ -327,14 +311,13 @@ export function unwatchEpisode(userId: string, episodeId: string) {
|
||||
.run();
|
||||
|
||||
// Find parent title and downgrade from completed to in_progress
|
||||
const ep = db.select().from(episodes).where(eq(episodes.id, episodeId)).get();
|
||||
if (!ep) return;
|
||||
const season = db
|
||||
.select()
|
||||
.from(seasons)
|
||||
.where(eq(seasons.id, ep.seasonId))
|
||||
const row = db
|
||||
.select({ titleId: seasons.titleId })
|
||||
.from(episodes)
|
||||
.innerJoin(seasons, eq(episodes.seasonId, seasons.id))
|
||||
.where(eq(episodes.id, episodeId))
|
||||
.get();
|
||||
if (!season) return;
|
||||
if (!row) return;
|
||||
|
||||
const existing = db
|
||||
.select()
|
||||
@@ -342,13 +325,13 @@ export function unwatchEpisode(userId: string, episodeId: string) {
|
||||
.where(
|
||||
and(
|
||||
eq(userTitleStatus.userId, userId),
|
||||
eq(userTitleStatus.titleId, season.titleId),
|
||||
eq(userTitleStatus.titleId, row.titleId),
|
||||
),
|
||||
)
|
||||
.get();
|
||||
|
||||
if (existing?.status === "completed") {
|
||||
setTitleStatus(userId, season.titleId, "in_progress");
|
||||
setTitleStatus(userId, row.titleId, "in_progress");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -545,23 +528,14 @@ export function getUserTitleInfo(userId: string, titleId: string) {
|
||||
)
|
||||
.get();
|
||||
|
||||
// Batch fetch all episode IDs for this title
|
||||
const titleSeasons = db
|
||||
.select()
|
||||
.from(seasons)
|
||||
// Single JOIN instead of seasons → episodes chain (2 queries → 1)
|
||||
const allEps = db
|
||||
.select({ id: episodes.id })
|
||||
.from(episodes)
|
||||
.innerJoin(seasons, eq(episodes.seasonId, seasons.id))
|
||||
.where(eq(seasons.titleId, titleId))
|
||||
.all();
|
||||
|
||||
const seasonIds = titleSeasons.map((s) => s.id);
|
||||
const allEps =
|
||||
seasonIds.length > 0
|
||||
? db
|
||||
.select()
|
||||
.from(episodes)
|
||||
.where(inArray(episodes.seasonId, seasonIds))
|
||||
.all()
|
||||
: [];
|
||||
|
||||
const epIds = allEps.map((ep) => ep.id);
|
||||
|
||||
// Batch fetch all watches for these episodes
|
||||
|
||||
@@ -10,7 +10,7 @@ import {
|
||||
} from "@/lib/db/schema";
|
||||
import { createLogger } from "@/lib/logger";
|
||||
import { findByExternalId, searchTv } from "@/lib/tmdb/client";
|
||||
import { importTitle } from "./metadata";
|
||||
import { getOrFetchTitleByTmdbId } from "./metadata";
|
||||
import { logEpisodeWatch, logMovieWatch } from "./tracking";
|
||||
|
||||
const log = createLogger("webhooks");
|
||||
@@ -330,7 +330,7 @@ export async function processWebhook(
|
||||
return { status: "error", message: "Could not resolve TMDB ID" };
|
||||
}
|
||||
|
||||
const title = await importTitle(tmdbId, "movie");
|
||||
const title = await getOrFetchTitleByTmdbId(tmdbId, "movie");
|
||||
if (!title) {
|
||||
logEvent(connectionId, event, "error", "Failed to import movie");
|
||||
return { status: "error", message: "Failed to import movie" };
|
||||
@@ -361,7 +361,7 @@ export async function processWebhook(
|
||||
return { status: "error", message: "Could not resolve episode" };
|
||||
}
|
||||
|
||||
const title = await importTitle(resolved.showTmdbId, "tv");
|
||||
const title = await getOrFetchTitleByTmdbId(resolved.showTmdbId, "tv");
|
||||
if (!title) {
|
||||
logEvent(connectionId, event, "error", "Failed to import TV show");
|
||||
return { status: "error", message: "Failed to import TV show" };
|
||||
|
||||
+3
-3
@@ -40,7 +40,7 @@ import {
|
||||
userTitleStatus,
|
||||
} from "@/lib/db/schema";
|
||||
import { createLogger } from "@/lib/logger";
|
||||
import { importTitle } from "@/lib/services/metadata";
|
||||
import { getOrFetchTitleByTmdbId } from "@/lib/services/metadata";
|
||||
import { setSetting } from "@/lib/services/settings";
|
||||
|
||||
const log = createLogger("seed");
|
||||
@@ -164,7 +164,7 @@ async function seedForUser(userId: string) {
|
||||
for (const movie of MOVIES) {
|
||||
try {
|
||||
log.info(` Importing movie: ${movie.name} (TMDB ${movie.tmdbId})`);
|
||||
const title = await importTitle(movie.tmdbId, "movie");
|
||||
const title = await getOrFetchTitleByTmdbId(movie.tmdbId, "movie");
|
||||
if (title) {
|
||||
movieTitles.push({
|
||||
id: title.id,
|
||||
@@ -184,7 +184,7 @@ async function seedForUser(userId: string) {
|
||||
for (const show of TV_SHOWS) {
|
||||
try {
|
||||
log.info(` Importing TV show: ${show.name} (TMDB ${show.tmdbId})`);
|
||||
const title = await importTitle(show.tmdbId, "tv");
|
||||
const title = await getOrFetchTitleByTmdbId(show.tmdbId, "tv");
|
||||
if (title) {
|
||||
tvTitles.push({ id: title.id, tmdbId: show.tmdbId, name: show.name });
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user