mirror of
https://github.com/jakejarvis/sofa.git
synced 2026-08-29 03:55:38 -04:00
- 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
96 lines
2.4 KiB
TypeScript
96 lines
2.4 KiB
TypeScript
"use server";
|
|
|
|
import { eq } from "drizzle-orm";
|
|
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,
|
|
logMovieWatch,
|
|
markAllEpisodesWatched,
|
|
rateTitleStars,
|
|
removeTitleStatus,
|
|
setTitleStatus,
|
|
unwatchEpisode,
|
|
unwatchSeason,
|
|
} from "@/lib/services/tracking";
|
|
|
|
async function getSessionUserId() {
|
|
const session = await requireSession();
|
|
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,
|
|
) {
|
|
const userId = await getSessionUserId();
|
|
if (status === null) {
|
|
removeTitleStatus(userId, titleId);
|
|
} else {
|
|
setTitleStatus(userId, titleId, status);
|
|
}
|
|
}
|
|
|
|
export async function markAllWatchedAction(titleId: string) {
|
|
const userId = await getSessionUserId();
|
|
markAllEpisodesWatched(userId, titleId);
|
|
}
|
|
|
|
const ratingSchema = z.number().int().min(0).max(5);
|
|
|
|
export async function updateTitleRating(titleId: string, ratingStars: number) {
|
|
const userId = await getSessionUserId();
|
|
rateTitleStars(userId, titleId, ratingSchema.parse(ratingStars));
|
|
}
|
|
|
|
export async function watchMovie(titleId: string) {
|
|
const userId = await getSessionUserId();
|
|
logMovieWatch(userId, titleId);
|
|
}
|
|
|
|
export async function watchEpisode(episodeId: string) {
|
|
const userId = await getSessionUserId();
|
|
logEpisodeWatch(userId, episodeId);
|
|
}
|
|
|
|
export async function unwatchEpisodeAction(episodeId: string) {
|
|
const userId = await getSessionUserId();
|
|
unwatchEpisode(userId, episodeId);
|
|
}
|
|
|
|
export async function watchSeason(seasonId: string) {
|
|
const userId = await getSessionUserId();
|
|
const seasonEps = db
|
|
.select()
|
|
.from(episodes)
|
|
.where(eq(episodes.seasonId, seasonId))
|
|
.all();
|
|
logEpisodeWatchBatch(
|
|
userId,
|
|
seasonEps.map((ep) => ep.id),
|
|
);
|
|
}
|
|
|
|
export async function unwatchSeasonAction(seasonId: string) {
|
|
const userId = await getSessionUserId();
|
|
unwatchSeason(userId, seasonId);
|
|
}
|
|
|
|
export async function batchWatchEpisodes(episodeIds: string[]) {
|
|
const userId = await getSessionUserId();
|
|
logEpisodeWatchBatch(userId, episodeIds);
|
|
}
|