mirror of
https://github.com/jakejarvis/sofa.git
synced 2026-08-29 03:55:38 -04:00
Remove unnecessary await/async from sync bun:sqlite db calls
drizzle-orm/bun-sqlite is fully synchronous — all queries return values directly, not promises. Remove await from all db calls, drop async from functions that no longer need it, simplify Promise.all patterns that wrapped sync operations, and fix setSetting() which was missing .run() (previously masked by await triggering execution via thenable). Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -28,7 +28,7 @@ export async function saveWebhookConnection(
|
||||
throw new Error("Media server username is required");
|
||||
}
|
||||
|
||||
const existing = await db
|
||||
const existing = db
|
||||
.select()
|
||||
.from(webhookConnections)
|
||||
.where(
|
||||
@@ -40,7 +40,7 @@ export async function saveWebhookConnection(
|
||||
.get();
|
||||
|
||||
if (existing) {
|
||||
const connection = await db
|
||||
const connection = db
|
||||
.update(webhookConnections)
|
||||
.set({
|
||||
mediaServerUsername: mediaServerUsername.trim(),
|
||||
@@ -59,7 +59,7 @@ export async function saveWebhookConnection(
|
||||
const token = crypto.randomBytes(32).toString("hex");
|
||||
const now = new Date();
|
||||
|
||||
const connection = await db
|
||||
const connection = db
|
||||
.insert(webhookConnections)
|
||||
.values({
|
||||
userId: session.user.id,
|
||||
@@ -86,8 +86,7 @@ export async function deleteWebhookConnection(provider: "plex" | "jellyfin") {
|
||||
throw new Error("Invalid provider");
|
||||
}
|
||||
|
||||
await db
|
||||
.delete(webhookConnections)
|
||||
db.delete(webhookConnections)
|
||||
.where(
|
||||
and(
|
||||
eq(webhookConnections.userId, session.user.id),
|
||||
@@ -106,7 +105,7 @@ export async function regenerateWebhookToken(provider: "plex" | "jellyfin") {
|
||||
|
||||
const newToken = crypto.randomBytes(32).toString("hex");
|
||||
|
||||
const connection = await db
|
||||
const connection = db
|
||||
.update(webhookConnections)
|
||||
.set({ token: newToken })
|
||||
.where(
|
||||
@@ -136,5 +135,5 @@ export async function toggleRegistration(open: boolean) {
|
||||
throw new Error("Forbidden");
|
||||
}
|
||||
|
||||
await setSetting("registrationOpen", String(open));
|
||||
setSetting("registrationOpen", String(open));
|
||||
}
|
||||
|
||||
@@ -17,47 +17,41 @@ export default async function SettingsPage() {
|
||||
|
||||
const isAdmin = session.user.role === "admin";
|
||||
|
||||
const [connections, registrationOpen] = await Promise.all([
|
||||
(async () => {
|
||||
const rows = await db
|
||||
const connections = db
|
||||
.select()
|
||||
.from(webhookConnections)
|
||||
.where(eq(webhookConnections.userId, session.user.id))
|
||||
.all()
|
||||
.map((conn) => {
|
||||
const events = db
|
||||
.select()
|
||||
.from(webhookConnections)
|
||||
.where(eq(webhookConnections.userId, session.user.id))
|
||||
.from(webhookEventLog)
|
||||
.where(eq(webhookEventLog.connectionId, conn.id))
|
||||
.orderBy(desc(webhookEventLog.receivedAt))
|
||||
.limit(10)
|
||||
.all();
|
||||
|
||||
return Promise.all(
|
||||
rows.map(async (conn) => {
|
||||
const events = await db
|
||||
.select()
|
||||
.from(webhookEventLog)
|
||||
.where(eq(webhookEventLog.connectionId, conn.id))
|
||||
.orderBy(desc(webhookEventLog.receivedAt))
|
||||
.limit(10)
|
||||
.all();
|
||||
return {
|
||||
id: conn.id,
|
||||
provider: conn.provider,
|
||||
token: conn.token,
|
||||
mediaServerUsername: conn.mediaServerUsername,
|
||||
enabled: conn.enabled,
|
||||
lastEventAt: conn.lastEventAt?.toISOString() ?? null,
|
||||
recentEvents: events.map((e) => ({
|
||||
id: e.id,
|
||||
eventType: e.eventType,
|
||||
mediaType: e.mediaType,
|
||||
mediaTitle: e.mediaTitle,
|
||||
status: e.status,
|
||||
receivedAt: e.receivedAt.toISOString(),
|
||||
})),
|
||||
};
|
||||
});
|
||||
|
||||
return {
|
||||
id: conn.id,
|
||||
provider: conn.provider,
|
||||
token: conn.token,
|
||||
mediaServerUsername: conn.mediaServerUsername,
|
||||
enabled: conn.enabled,
|
||||
lastEventAt: conn.lastEventAt?.toISOString() ?? null,
|
||||
recentEvents: events.map((e) => ({
|
||||
id: e.id,
|
||||
eventType: e.eventType,
|
||||
mediaType: e.mediaType,
|
||||
mediaTitle: e.mediaTitle,
|
||||
status: e.status,
|
||||
receivedAt: e.receivedAt.toISOString(),
|
||||
})),
|
||||
};
|
||||
}),
|
||||
);
|
||||
})(),
|
||||
isAdmin
|
||||
? getSetting("registrationOpen").then((v) => v === "true")
|
||||
: Promise.resolve(false),
|
||||
]);
|
||||
const registrationOpen = isAdmin
|
||||
? getSetting("registrationOpen") === "true"
|
||||
: false;
|
||||
|
||||
const repoUrl = "https://github.com/jakejarvis/sofa";
|
||||
|
||||
|
||||
@@ -28,58 +28,58 @@ export async function updateTitleStatus(
|
||||
) {
|
||||
const userId = await getSessionUserId();
|
||||
if (status === null) {
|
||||
await removeTitleStatus(userId, titleId);
|
||||
removeTitleStatus(userId, titleId);
|
||||
} else {
|
||||
await setTitleStatus(userId, titleId, status);
|
||||
setTitleStatus(userId, titleId, status);
|
||||
}
|
||||
}
|
||||
|
||||
export async function markAllWatchedAction(titleId: string) {
|
||||
const userId = await getSessionUserId();
|
||||
await markAllEpisodesWatched(userId, titleId);
|
||||
markAllEpisodesWatched(userId, titleId);
|
||||
}
|
||||
|
||||
export async function updateTitleRating(titleId: string, ratingStars: number) {
|
||||
const userId = await getSessionUserId();
|
||||
if (ratingStars < 0 || ratingStars > 5) throw new Error("Invalid rating");
|
||||
await rateTitleStars(userId, titleId, ratingStars);
|
||||
rateTitleStars(userId, titleId, ratingStars);
|
||||
}
|
||||
|
||||
export async function watchMovie(titleId: string) {
|
||||
const userId = await getSessionUserId();
|
||||
await logMovieWatch(userId, titleId);
|
||||
logMovieWatch(userId, titleId);
|
||||
}
|
||||
|
||||
export async function watchEpisode(episodeId: string) {
|
||||
const userId = await getSessionUserId();
|
||||
await logEpisodeWatch(userId, episodeId);
|
||||
logEpisodeWatch(userId, episodeId);
|
||||
}
|
||||
|
||||
export async function unwatchEpisodeAction(episodeId: string) {
|
||||
const userId = await getSessionUserId();
|
||||
await unwatchEpisode(userId, episodeId);
|
||||
unwatchEpisode(userId, episodeId);
|
||||
}
|
||||
|
||||
export async function watchSeason(seasonId: string) {
|
||||
const userId = await getSessionUserId();
|
||||
const seasonEps = await db
|
||||
const seasonEps = db
|
||||
.select()
|
||||
.from(episodes)
|
||||
.where(eq(episodes.seasonId, seasonId))
|
||||
.all();
|
||||
for (const ep of seasonEps) {
|
||||
await logEpisodeWatch(userId, ep.id);
|
||||
logEpisodeWatch(userId, ep.id);
|
||||
}
|
||||
}
|
||||
|
||||
export async function unwatchSeasonAction(seasonId: string) {
|
||||
const userId = await getSessionUserId();
|
||||
await unwatchSeason(userId, seasonId);
|
||||
unwatchSeason(userId, seasonId);
|
||||
}
|
||||
|
||||
export async function batchWatchEpisodes(episodeIds: string[]) {
|
||||
const userId = await getSessionUserId();
|
||||
for (const id of episodeIds) {
|
||||
await logEpisodeWatch(userId, id);
|
||||
logEpisodeWatch(userId, id);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -29,7 +29,7 @@ export async function generateMetadata({
|
||||
const { id } = await params;
|
||||
if (TMDB_ID_PATTERN.test(id)) return { title: "Sofa" };
|
||||
|
||||
const title = await db.select().from(titles).where(eq(titles.id, id)).get();
|
||||
const title = db.select().from(titles).where(eq(titles.id, id)).get();
|
||||
if (!title) return { title: "Not Found — Sofa" };
|
||||
|
||||
const year = (title.releaseDate ?? title.firstAirDate)?.slice(0, 4);
|
||||
|
||||
Reference in New Issue
Block a user