Add request approval system and audiobook path template

Implements admin approval workflow for user requests with global and per-user auto-approve controls. Adds new request statuses ('awaiting_approval', 'denied'), related API endpoints, and UI for pending approvals. Introduces configurable audiobook organization path template with validation and preview in settings, updates database schema and migrations for new fields.
This commit is contained in:
kikootwo
2026-01-16 13:47:36 -05:00
parent 428d9a12e0
commit 3a9ae4a439
59 changed files with 4043 additions and 256 deletions
+1 -1
View File
@@ -175,7 +175,7 @@ function AdminJobsPageContent() {
<div className="min-h-screen bg-gray-50 dark:bg-gray-900">
<div className="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8 py-8">
{/* Header */}
<div className="mb-8 flex items-center justify-between">
<div className="sticky top-0 z-10 mb-8 flex items-center justify-between bg-gray-50 dark:bg-gray-900 py-4 -mx-4 px-4 sm:-mx-6 sm:px-6 lg:-mx-8 lg:px-8 border-b border-gray-200 dark:border-gray-800">
<div>
<h1 className="text-3xl font-bold text-gray-900 dark:text-gray-100">
Scheduled Jobs
+1 -1
View File
@@ -142,7 +142,7 @@ export default function AdminLogsPage() {
<div className="min-h-screen bg-gray-50 dark:bg-gray-900">
<div className="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8 py-8">
{/* Header */}
<div className="mb-8 flex items-center justify-between">
<div className="sticky top-0 z-10 mb-8 flex items-center justify-between bg-gray-50 dark:bg-gray-900 py-4 -mx-4 px-4 sm:-mx-6 sm:px-6 lg:-mx-8 lg:px-8 border-b border-gray-200 dark:border-gray-800">
<div>
<h1 className="text-3xl font-bold text-gray-900 dark:text-gray-100">
System Logs
+289 -4
View File
@@ -5,13 +5,285 @@
'use client';
import useSWR from 'swr';
import useSWR, { mutate } from 'swr';
import Link from 'next/link';
import { authenticatedFetcher } from '@/lib/utils/api';
import { authenticatedFetcher, fetchJSON } from '@/lib/utils/api';
import { MetricCard } from './components/MetricCard';
import { ActiveDownloadsTable } from './components/ActiveDownloadsTable';
import { RecentRequestsTable } from './components/RecentRequestsTable';
import { ToastProvider } from '@/components/ui/Toast';
import { ToastProvider, useToast } from '@/components/ui/Toast';
import { formatDistanceToNow } from 'date-fns';
import { useState } from 'react';
interface PendingApprovalRequest {
id: string;
createdAt: string;
audiobook: {
title: string;
author: string;
coverArtUrl: string | null;
};
user: {
id: string;
plexUsername: string;
avatarUrl: string | null;
};
}
function PendingApprovalSection({ requests }: { requests: PendingApprovalRequest[] }) {
const toast = useToast();
const [loadingStates, setLoadingStates] = useState<Record<string, boolean>>({});
const handleApproveRequest = async (requestId: string) => {
setLoadingStates((prev) => ({ ...prev, [requestId]: true }));
try {
await fetchJSON(`/api/admin/requests/${requestId}/approve`, {
method: 'POST',
body: JSON.stringify({ action: 'approve' }),
});
toast.success('Request approved');
// Mutate both pending requests and recent requests caches
await mutate('/api/admin/requests/pending-approval');
await mutate('/api/admin/requests/recent');
await mutate('/api/admin/metrics');
} catch (error) {
console.error('[Admin] Failed to approve request:', error);
toast.error(
`Failed to approve request: ${error instanceof Error ? error.message : 'Unknown error'}`
);
} finally {
setLoadingStates((prev) => ({ ...prev, [requestId]: false }));
}
};
const handleDenyRequest = async (requestId: string) => {
setLoadingStates((prev) => ({ ...prev, [requestId]: true }));
try {
await fetchJSON(`/api/admin/requests/${requestId}/approve`, {
method: 'POST',
body: JSON.stringify({ action: 'deny' }),
});
toast.success('Request denied');
// Mutate pending requests cache
await mutate('/api/admin/requests/pending-approval');
await mutate('/api/admin/metrics');
} catch (error) {
console.error('[Admin] Failed to deny request:', error);
toast.error(
`Failed to deny request: ${error instanceof Error ? error.message : 'Unknown error'}`
);
} finally {
setLoadingStates((prev) => ({ ...prev, [requestId]: false }));
}
};
return (
<div className="mb-8">
{/* Section Header */}
<div className="flex items-center gap-3 mb-4">
<div className="flex items-center gap-2">
<svg
className="w-6 h-6 text-amber-600 dark:text-amber-400"
fill="none"
stroke="currentColor"
viewBox="0 0 24 24"
>
<path
strokeLinecap="round"
strokeLinejoin="round"
strokeWidth={2}
d="M12 9v2m0 4h.01m-6.938 4h13.856c1.54 0 2.502-1.667 1.732-3L13.732 4c-.77-1.333-2.694-1.333-3.464 0L3.34 16c-.77 1.333.192 3 1.732 3z"
/>
</svg>
<h2 className="text-xl font-bold text-gray-900 dark:text-gray-100">
Requests Awaiting Approval
</h2>
</div>
<span className="inline-flex items-center px-2.5 py-0.5 rounded-full text-xs font-medium bg-amber-100 text-amber-800 dark:bg-amber-900 dark:text-amber-200">
{requests.length}
</span>
</div>
{/* Requests Grid */}
<div className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-4">
{requests.map((request) => {
const isLoading = loadingStates[request.id] || false;
return (
<div
key={request.id}
className="bg-white dark:bg-gray-800 border-2 border-amber-200 dark:border-amber-800 rounded-lg shadow-sm hover:shadow-md transition-shadow overflow-hidden"
>
{/* Card Content */}
<div className="p-4">
<div className="flex gap-3">
{/* Cover Image */}
<div className="flex-shrink-0">
{request.audiobook.coverArtUrl ? (
<img
src={request.audiobook.coverArtUrl}
alt={request.audiobook.title}
className="w-16 h-16 rounded object-cover"
/>
) : (
<div className="w-16 h-16 rounded bg-gray-200 dark:bg-gray-700 flex items-center justify-center">
<svg
className="w-8 h-8 text-gray-400 dark:text-gray-600"
fill="currentColor"
viewBox="0 0 20 20"
>
<path d="M9 4.804A7.968 7.968 0 005.5 4c-1.255 0-2.443.29-3.5.804v10A7.969 7.969 0 015.5 14c1.669 0 3.218.51 4.5 1.385A7.962 7.962 0 0114.5 14c1.255 0 2.443.29 3.5.804v-10A7.968 7.968 0 0014.5 4c-1.255 0-2.443.29-3.5.804V12a1 1 0 11-2 0V4.804z" />
</svg>
</div>
)}
</div>
{/* Book Info */}
<div className="flex-1 min-w-0">
<h3 className="text-sm font-bold text-gray-900 dark:text-gray-100 truncate">
{request.audiobook.title}
</h3>
<p className="text-sm text-gray-600 dark:text-gray-400 truncate">
{request.audiobook.author}
</p>
{/* User Info */}
<div className="flex items-center gap-2 mt-2">
{request.user.avatarUrl ? (
<img
src={request.user.avatarUrl}
alt={request.user.plexUsername}
className="w-5 h-5 rounded-full"
/>
) : (
<div className="w-5 h-5 rounded-full bg-gray-300 dark:bg-gray-600 flex items-center justify-center">
<svg
className="w-3 h-3 text-gray-600 dark:text-gray-400"
fill="currentColor"
viewBox="0 0 20 20"
>
<path
fillRule="evenodd"
d="M10 9a3 3 0 100-6 3 3 0 000 6zm-7 9a7 7 0 1114 0H3z"
clipRule="evenodd"
/>
</svg>
</div>
)}
<span className="text-xs text-gray-600 dark:text-gray-400">
{request.user.plexUsername}
</span>
</div>
{/* Timestamp */}
<p className="text-xs text-gray-500 dark:text-gray-500 mt-1">
{formatDistanceToNow(new Date(request.createdAt), { addSuffix: true })}
</p>
</div>
</div>
</div>
{/* Action Buttons */}
<div className="border-t border-amber-200 dark:border-amber-800 bg-gray-50 dark:bg-gray-900/50 px-4 py-3 flex gap-2">
<button
onClick={() => handleApproveRequest(request.id)}
disabled={isLoading}
className="flex-1 inline-flex items-center justify-center gap-2 px-3 py-2 bg-green-600 hover:bg-green-700 disabled:bg-green-400 disabled:cursor-not-allowed text-white text-sm font-medium rounded-lg transition-colors"
>
{isLoading ? (
<svg
className="animate-spin h-4 w-4"
fill="none"
viewBox="0 0 24 24"
>
<circle
className="opacity-25"
cx="12"
cy="12"
r="10"
stroke="currentColor"
strokeWidth="4"
/>
<path
className="opacity-75"
fill="currentColor"
d="M4 12a8 8 0 018-8V0C5.373 0 0 5.373 0 12h4zm2 5.291A7.962 7.962 0 014 12H0c0 3.042 1.135 5.824 3 7.938l3-2.647z"
/>
</svg>
) : (
<svg
className="w-4 h-4"
fill="none"
stroke="currentColor"
viewBox="0 0 24 24"
>
<path
strokeLinecap="round"
strokeLinejoin="round"
strokeWidth={2}
d="M5 13l4 4L19 7"
/>
</svg>
)}
<span>Approve</span>
</button>
<button
onClick={() => handleDenyRequest(request.id)}
disabled={isLoading}
className="flex-1 inline-flex items-center justify-center gap-2 px-3 py-2 bg-red-600 hover:bg-red-700 disabled:bg-red-400 disabled:cursor-not-allowed text-white text-sm font-medium rounded-lg transition-colors"
>
{isLoading ? (
<svg
className="animate-spin h-4 w-4"
fill="none"
viewBox="0 0 24 24"
>
<circle
className="opacity-25"
cx="12"
cy="12"
r="10"
stroke="currentColor"
strokeWidth="4"
/>
<path
className="opacity-75"
fill="currentColor"
d="M4 12a8 8 0 018-8V0C5.373 0 0 5.373 0 12h4zm2 5.291A7.962 7.962 0 014 12H0c0 3.042 1.135 5.824 3 7.938l3-2.647z"
/>
</svg>
) : (
<svg
className="w-4 h-4"
fill="none"
stroke="currentColor"
viewBox="0 0 24 24"
>
<path
strokeLinecap="round"
strokeLinejoin="round"
strokeWidth={2}
d="M6 18L18 6M6 6l12 12"
/>
</svg>
)}
<span>Deny</span>
</button>
</div>
</div>
);
})}
</div>
</div>
);
}
function AdminDashboardContent() {
// Fetch data with auto-refresh every 10 seconds
@@ -39,6 +311,14 @@ function AdminDashboardContent() {
}
);
const { data: pendingApprovalData } = useSWR(
'/api/admin/requests/pending-approval',
authenticatedFetcher,
{
refreshInterval: 10000,
}
);
const { data: settingsData } = useSWR(
'/api/admin/settings',
authenticatedFetcher,
@@ -74,7 +354,7 @@ function AdminDashboardContent() {
<div className="min-h-screen bg-gray-50 dark:bg-gray-900">
<div className="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8 py-8">
{/* Header */}
<div className="mb-8 flex items-center justify-between">
<div className="sticky top-0 z-10 mb-8 flex items-center justify-between bg-gray-50 dark:bg-gray-900 py-4 -mx-4 px-4 sm:-mx-6 sm:px-6 lg:-mx-8 lg:px-8 border-b border-gray-200 dark:border-gray-800">
<div>
<h1 className="text-3xl font-bold text-gray-900 dark:text-gray-100">
Admin Dashboard
@@ -197,6 +477,11 @@ function AdminDashboardContent() {
/>
</div>
{/* Requests Awaiting Approval */}
{pendingApprovalData?.requests && pendingApprovalData.requests.length > 0 && (
<PendingApprovalSection requests={pendingApprovalData.requests} />
)}
{/* Active Downloads */}
<div className="mb-8">
<h2 className="text-xl font-bold text-gray-900 dark:text-gray-100 mb-4">
+6
View File
@@ -95,6 +95,7 @@ export interface DownloadClientSettings {
export interface PathsSettings {
downloadDir: string;
mediaDir: string;
audiobookPathTemplate?: string;
metadataTaggingEnabled: boolean;
chapterMergingEnabled: boolean;
}
@@ -187,6 +188,11 @@ export interface TestResult {
success: boolean;
message: string;
responseTime?: number;
templateValidation?: {
isValid: boolean;
error?: string;
previewPaths?: string[];
};
}
/**
+17 -15
View File
@@ -168,22 +168,24 @@ export default function AdminSettings() {
<div className="min-h-screen bg-gray-50 dark:bg-gray-900">
<div className="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8 py-8">
{/* Header */}
<div className="mb-8">
<div className="flex items-center gap-4 mb-4">
<Link
href="/admin"
className="text-gray-600 dark:text-gray-400 hover:text-gray-900 dark:hover:text-gray-100"
>
<svg className="w-6 h-6" fill="currentColor" viewBox="0 0 20 20">
<path
fillRule="evenodd"
d="M9.707 14.707a1 1 0 01-1.414 0l-4-4a1 1 0 010-1.414l4-4a1 1 0 011.414 1.414L7.414 9H15a1 1 0 110 2H7.414l2.293 2.293a1 1 0 010 1.414z"
clipRule="evenodd"
/>
</svg>
</Link>
<h1 className="text-3xl font-bold text-gray-900 dark:text-gray-100">Settings</h1>
<div className="sticky top-0 z-10 mb-8 flex items-center justify-between bg-gray-50 dark:bg-gray-900 py-4 -mx-4 px-4 sm:-mx-6 sm:px-6 lg:-mx-8 lg:px-8 border-b border-gray-200 dark:border-gray-800">
<div>
<h1 className="text-3xl font-bold text-gray-900 dark:text-gray-100">
Settings
</h1>
<p className="text-gray-600 dark:text-gray-400 mt-2">
Configure system integrations and preferences
</p>
</div>
<Link
href="/admin"
className="inline-flex items-center gap-2 px-4 py-2 bg-gray-200 dark:bg-gray-700 hover:bg-gray-300 dark:hover:bg-gray-600 text-gray-900 dark:text-gray-100 rounded-lg transition-colors"
>
<svg className="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M10 19l-7-7m0 0l7-7m-7 7h18" />
</svg>
<span>Back to Dashboard</span>
</Link>
</div>
{/* Tab Navigation */}
@@ -5,11 +5,12 @@
'use client';
import React from 'react';
import React, { useState, useEffect } from 'react';
import { Button } from '@/components/ui/Button';
import { Input } from '@/components/ui/Input';
import { usePathsSettings } from './usePathsSettings';
import type { PathsSettings } from '../../lib/types';
import { validateTemplate, generateMockPreviews } from '@/lib/utils/path-template.util';
interface PathsTabProps {
paths: PathsSettings;
@@ -24,6 +25,31 @@ export function PathsTab({ paths, onChange, onValidationChange }: PathsTabProps)
onValidationChange,
});
// Live preview state (client-side validation)
const [livePreview, setLivePreview] = useState<{
isValid: boolean;
error?: string;
previewPaths?: string[];
} | null>(null);
// Update live preview whenever template changes
useEffect(() => {
const template = paths.audiobookPathTemplate || '{author}/{title} {asin}';
const validation = validateTemplate(template);
if (validation.valid) {
setLivePreview({
isValid: true,
previewPaths: generateMockPreviews(template),
});
} else {
setLivePreview({
isValid: false,
error: validation.error,
});
}
}, [paths.audiobookPathTemplate]);
return (
<div className="space-y-6 max-w-2xl">
<div>
@@ -69,6 +95,78 @@ export function PathsTab({ paths, onChange, onValidationChange }: PathsTabProps)
</p>
</div>
{/* Audiobook Organization Template */}
<div>
<label className="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-2">
Audiobook Organization Template
</label>
<Input
type="text"
value={paths.audiobookPathTemplate || '{author}/{title} {asin}'}
onChange={(e) => updatePath('audiobookPathTemplate', e.target.value)}
placeholder="{author}/{title} {asin}"
className="font-mono"
/>
<p className="text-sm text-gray-500 dark:text-gray-400 mt-1">
Customize how audiobooks are organized within the media directory
</p>
{/* Variable Reference Panel */}
<div className="mt-4 bg-blue-50 dark:bg-blue-900/20 border border-blue-200 dark:border-blue-800 rounded-lg p-4">
<h3 className="text-sm font-semibold text-blue-900 dark:text-blue-100 mb-3">
Available Variables
</h3>
<div className="grid grid-cols-1 sm:grid-cols-2 gap-2 text-sm">
<div>
<code className="text-blue-700 dark:text-blue-300 font-mono">{'{author}'}</code>
<span className="text-gray-600 dark:text-gray-400 ml-2">- Book author</span>
</div>
<div>
<code className="text-blue-700 dark:text-blue-300 font-mono">{'{title}'}</code>
<span className="text-gray-600 dark:text-gray-400 ml-2">- Book title</span>
</div>
<div>
<code className="text-blue-700 dark:text-blue-300 font-mono">{'{narrator}'}</code>
<span className="text-gray-600 dark:text-gray-400 ml-2">- Narrator name</span>
</div>
<div>
<code className="text-blue-700 dark:text-blue-300 font-mono">{'{year}'}</code>
<span className="text-gray-600 dark:text-gray-400 ml-2">- Release year</span>
</div>
<div>
<code className="text-blue-700 dark:text-blue-300 font-mono">{'{asin}'}</code>
<span className="text-gray-600 dark:text-gray-400 ml-2">- Audible ASIN</span>
</div>
</div>
</div>
{/* Live Preview - Client-side validation */}
{livePreview && !livePreview.isValid && (
<div className="mt-3 p-3 rounded-lg text-sm flex items-start gap-2 bg-red-50 dark:bg-red-900/20 border border-red-200 dark:border-red-800 text-red-800 dark:text-red-200">
<span className="flex-shrink-0 mt-0.5"></span>
<div className="flex-1">
<span>{livePreview.error || 'Invalid template format'}</span>
</div>
</div>
)}
{/* Live Preview Examples - Show while editing */}
{livePreview && livePreview.isValid && livePreview.previewPaths && (
<div className="mt-3 bg-gray-50 dark:bg-gray-800 border border-gray-200 dark:border-gray-700 rounded-lg p-4">
<h4 className="text-sm font-semibold text-gray-900 dark:text-gray-100 mb-2">
Preview Examples
</h4>
<div className="space-y-1.5 text-sm font-mono text-gray-700 dark:text-gray-300">
{livePreview.previewPaths.map((preview, index) => (
<div key={index} className="text-xs">
{paths.mediaDir || '/media/audiobooks'}/{preview}
</div>
))}
</div>
</div>
)}
</div>
{/* Metadata Tagging Toggle */}
<div className="bg-gray-50 dark:bg-gray-800 rounded-lg p-4 border border-gray-200 dark:border-gray-700">
<div className="flex items-start gap-4">
@@ -27,7 +27,7 @@ export function usePathsSettings({ paths, onChange, onValidationChange }: UsePat
};
/**
* Test if paths are valid and writable
* Test if paths are valid and writable, including template validation
*/
const testPaths = async () => {
setTesting(true);
@@ -40,6 +40,7 @@ export function usePathsSettings({ paths, onChange, onValidationChange }: UsePat
body: JSON.stringify({
downloadDir: paths.downloadDir,
mediaDir: paths.mediaDir,
audiobookPathTemplate: paths.audiobookPathTemplate,
}),
});
@@ -48,7 +49,8 @@ export function usePathsSettings({ paths, onChange, onValidationChange }: UsePat
if (data.success) {
const result: TestResult = {
success: true,
message: 'All paths are valid and writable'
message: 'All paths are valid and writable',
templateValidation: data.template
};
setTestResult(result);
onValidationChange(true);
@@ -56,10 +58,13 @@ export function usePathsSettings({ paths, onChange, onValidationChange }: UsePat
} else {
const result: TestResult = {
success: false,
message: data.error || 'Path validation failed'
message: data.error || 'Path validation failed',
templateValidation: data.template
};
setTestResult(result);
onValidationChange(false);
// Only mark as valid if paths are valid AND template is valid (if provided)
const isValid = false;
onValidationChange(isValid);
return result;
}
} catch (error) {
+130 -2
View File
@@ -5,7 +5,7 @@
'use client';
import { useState } from 'react';
import { useState, useEffect } from 'react';
import useSWR from 'swr';
import Link from 'next/link';
import { authenticatedFetcher, fetchJSON } from '@/lib/utils/api';
@@ -24,6 +24,7 @@ interface User {
createdAt: string;
updatedAt: string;
lastLoginAt: string | null;
autoApproveRequests: boolean | null;
_count: {
requests: number;
};
@@ -43,6 +44,10 @@ function AdminUsersPageContent() {
'/api/admin/users/pending',
authenticatedFetcher
);
const { data: globalAutoApproveData, error: globalAutoApproveError, mutate: mutateGlobalAutoApprove } = useSWR(
'/api/admin/settings/auto-approve',
authenticatedFetcher
);
const [editDialog, setEditDialog] = useState<{
isOpen: boolean;
user: User | null;
@@ -60,11 +65,77 @@ function AdminUsersPageContent() {
user: User | null;
}>({ isOpen: false, user: null });
const [deleting, setDeleting] = useState(false);
const [globalAutoApprove, setGlobalAutoApprove] = useState<boolean>(false);
const toast = useToast();
const isLoading = !data && !error;
const pendingUsers: PendingUser[] = pendingData?.users || [];
// Sync global auto-approve state (default to true if not set)
useEffect(() => {
if (globalAutoApproveData?.autoApproveRequests !== undefined) {
setGlobalAutoApprove(globalAutoApproveData.autoApproveRequests);
} else if (globalAutoApproveData !== undefined && globalAutoApproveData.autoApproveRequests === undefined) {
// API returned but no value - default to true
setGlobalAutoApprove(true);
}
}, [globalAutoApproveData]);
const handleGlobalAutoApproveToggle = async (newValue: boolean) => {
// Optimistic update
setGlobalAutoApprove(newValue);
try {
await fetchJSON('/api/admin/settings/auto-approve', {
method: 'PATCH',
body: JSON.stringify({ autoApproveRequests: newValue }),
});
toast.success(`Global auto-approve ${newValue ? 'enabled' : 'disabled'}`);
mutateGlobalAutoApprove();
mutate(); // Refresh users list to show updated state
} catch (err) {
// Revert on error
setGlobalAutoApprove(!newValue);
const errorMsg = err instanceof Error ? err.message : 'Failed to update auto-approve setting';
toast.error(errorMsg);
console.error(err);
}
};
const handleUserAutoApproveToggle = async (user: User, newValue: boolean) => {
console.log('[AutoApprove] Toggle clicked:', { userId: user.id, username: user.plexUsername, newValue });
// Optimistic update
const previousUsers = data?.users || [];
const optimisticUsers = previousUsers.map((u: User) =>
u.id === user.id ? { ...u, autoApproveRequests: newValue } : u
);
console.log('[AutoApprove] Applying optimistic update');
mutate({ users: optimisticUsers }, false);
try {
console.log('[AutoApprove] Sending API request...');
const response = await fetchJSON(`/api/admin/users/${user.id}`, {
method: 'PUT',
body: JSON.stringify({
role: user.role,
autoApproveRequests: newValue
}),
});
console.log('[AutoApprove] API response received:', response);
toast.success(`Auto-approve ${newValue ? 'enabled' : 'disabled'} for ${user.plexUsername}`);
console.log('[AutoApprove] Triggering cache revalidation...');
mutate(); // Refresh users list
} catch (err) {
// Revert on error
console.error('[AutoApprove] Error occurred, reverting:', err);
mutate({ users: previousUsers }, false);
const errorMsg = err instanceof Error ? err.message : 'Failed to update user auto-approve setting';
toast.error(errorMsg);
console.error(err);
}
};
const showEditDialog = (user: User) => {
setEditRole(user.role);
setEditDialog({ isOpen: true, user });
@@ -207,7 +278,7 @@ function AdminUsersPageContent() {
<div className="min-h-screen bg-gray-50 dark:bg-gray-900">
<div className="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8 py-8">
{/* Header */}
<div className="mb-8 flex items-center justify-between">
<div className="sticky top-0 z-10 mb-8 flex items-center justify-between bg-gray-50 dark:bg-gray-900 py-4 -mx-4 px-4 sm:-mx-6 sm:px-6 lg:-mx-8 lg:px-8 border-b border-gray-200 dark:border-gray-800">
<div>
<h1 className="text-3xl font-bold text-gray-900 dark:text-gray-100">
User Management
@@ -227,6 +298,32 @@ function AdminUsersPageContent() {
</Link>
</div>
{/* Global Auto-Approve Toggle */}
<div className="mb-8 bg-blue-50 dark:bg-blue-900/20 border border-blue-200 dark:border-blue-800 rounded-lg p-6">
<div className="flex items-start gap-4">
<button
onClick={() => handleGlobalAutoApproveToggle(!globalAutoApprove)}
className="relative inline-flex h-6 w-11 items-center rounded-full transition-colors focus:outline-none focus:ring-2 focus:ring-blue-500 focus:ring-offset-2 dark:focus:ring-offset-gray-800 mt-0.5"
style={{ backgroundColor: globalAutoApprove ? '#3b82f6' : '#d1d5db' }}
>
<span
className={`inline-block h-4 w-4 transform rounded-full bg-white transition-transform ${globalAutoApprove ? 'translate-x-6' : 'translate-x-1'}`}
/>
</button>
<div className="flex-1">
<label
onClick={() => handleGlobalAutoApproveToggle(!globalAutoApprove)}
className="block text-base font-semibold text-gray-900 dark:text-gray-100 cursor-pointer"
>
Auto-Approve All Requests
</label>
<p className="text-sm text-gray-600 dark:text-gray-400 mt-1">
When enabled, all user requests are automatically processed. When disabled, you can set per-user approval settings below.
</p>
</div>
</div>
</div>
{/* Pending Users Section */}
{pendingUsers.length > 0 && (
<div className="mb-8">
@@ -305,6 +402,9 @@ function AdminUsersPageContent() {
<th className="px-6 py-3 text-left text-xs font-medium text-gray-500 dark:text-gray-400 uppercase tracking-wider">
Role
</th>
<th className="px-6 py-3 text-left text-xs font-medium text-gray-500 dark:text-gray-400 uppercase tracking-wider">
Auto-Approve
</th>
<th className="px-6 py-3 text-left text-xs font-medium text-gray-500 dark:text-gray-400 uppercase tracking-wider">
Requests
</th>
@@ -370,6 +470,33 @@ function AdminUsersPageContent() {
)}
</div>
</td>
<td className="px-6 py-4 whitespace-nowrap">
<div className="flex items-center">
{user.role === 'admin' ? (
<span className="inline-flex items-center gap-1 px-2 py-1 text-xs font-semibold rounded-full bg-purple-100 text-purple-800 dark:bg-purple-900/30 dark:text-purple-400">
<svg className="w-3 h-3" fill="none" stroke="currentColor" viewBox="0 0 24 24">
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M12 15v2m-6 4h12a2 2 0 002-2v-6a2 2 0 00-2-2H6a2 2 0 00-2 2v6a2 2 0 002 2zm10-10V7a4 4 0 00-8 0v4h8z" />
</svg>
Always On
</span>
) : globalAutoApprove ? (
<span className="text-xs text-gray-500 dark:text-gray-400">
Global Setting
</span>
) : (
<button
onClick={() => handleUserAutoApproveToggle(user, !(user.autoApproveRequests ?? false))}
className="relative inline-flex h-5 w-10 items-center rounded-full transition-colors focus:outline-none focus:ring-2 focus:ring-blue-500 focus:ring-offset-2 dark:focus:ring-offset-gray-800"
style={{ backgroundColor: (user.autoApproveRequests ?? false) ? '#3b82f6' : '#d1d5db' }}
title={`Toggle auto-approve for ${user.plexUsername}`}
>
<span
className={`inline-block h-3 w-3 transform rounded-full bg-white transition-transform ${(user.autoApproveRequests ?? false) ? 'translate-x-6' : 'translate-x-1'}`}
/>
</button>
)}
</div>
</td>
<td className="px-6 py-4 whitespace-nowrap text-sm text-gray-500 dark:text-gray-400">
{user._count.requests}
</td>
@@ -460,6 +587,7 @@ function AdminUsersPageContent() {
<li> <strong>User:</strong> Can request audiobooks, view own requests, and search the catalog</li>
<li> <strong>Admin:</strong> Full system access including settings, user management, and all requests</li>
<li> <strong>Setup Admin:</strong> The initial admin account created during setup - this account is protected and cannot be changed or deleted</li>
<li> <strong>Auto-Approve:</strong> When the global setting is enabled, all requests are automatically processed. When disabled, you can control auto-approval per user. Admin requests are always auto-approved.</li>
<li> <strong>OIDC Users:</strong> Role management is handled by the identity provider - use admin role mapping in OIDC settings. Cannot be deleted as access is managed externally.</li>
<li> <strong>Plex Users:</strong> Can have their roles changed, but cannot be deleted as access is managed by Plex.</li>
<li> <strong>Local Users:</strong> Can be freely assigned user or admin roles (except setup admin). Can be deleted (their requests are preserved for historical records).</li>
@@ -0,0 +1,169 @@
/**
* Component: Admin Request Approval API
* Documentation: documentation/admin-features/request-approval.md
*/
import { NextRequest, NextResponse } from 'next/server';
import { requireAuth, requireAdmin, AuthenticatedRequest } from '@/lib/middleware/auth';
import { prisma } from '@/lib/db';
import { getJobQueueService } from '@/lib/services/job-queue.service';
import { RMABLogger } from '@/lib/utils/logger';
import { z } from 'zod';
const logger = RMABLogger.create('API.Admin.Requests.Approve');
const ApprovalActionSchema = z.object({
action: z.enum(['approve', 'deny']),
});
/**
* POST /api/admin/requests/[id]/approve
* Approve or deny a request in 'awaiting_approval' status
*/
export async function POST(
request: NextRequest,
{ params }: { params: Promise<{ id: string }> }
) {
return requireAuth(request, async (req: AuthenticatedRequest) => {
return requireAdmin(req, async () => {
try {
if (!req.user) {
return NextResponse.json(
{ error: 'Unauthorized', message: 'User not authenticated' },
{ status: 401 }
);
}
const { id } = await params;
const body = await request.json();
// Validate action
const { action } = ApprovalActionSchema.parse(body);
// Fetch the request
const existingRequest = await prisma.request.findUnique({
where: { id },
include: {
audiobook: true,
user: {
select: {
id: true,
plexUsername: true,
},
},
},
});
if (!existingRequest) {
return NextResponse.json(
{ error: 'NotFound', message: 'Request not found' },
{ status: 404 }
);
}
// Validate request is in 'awaiting_approval' status
if (existingRequest.status !== 'awaiting_approval') {
return NextResponse.json(
{
error: 'InvalidStatus',
message: `Request is not awaiting approval (current status: ${existingRequest.status})`,
currentStatus: existingRequest.status,
},
{ status: 400 }
);
}
// Update request based on action
if (action === 'approve') {
// Approve: Change status to 'pending' and trigger search job
const updatedRequest = await prisma.request.update({
where: { id },
data: { status: 'pending' },
include: {
audiobook: true,
user: {
select: {
id: true,
plexUsername: true,
},
},
},
});
// Trigger search job
const jobQueue = getJobQueueService();
await jobQueue.addSearchJob(updatedRequest.id, {
id: updatedRequest.audiobook.id,
title: updatedRequest.audiobook.title,
author: updatedRequest.audiobook.author,
asin: updatedRequest.audiobook.audibleAsin || undefined,
});
logger.info(`Request ${id} approved by admin ${req.user.sub}`, {
requestId: id,
userId: updatedRequest.userId,
audiobookTitle: updatedRequest.audiobook.title,
adminId: req.user.sub,
});
return NextResponse.json({
success: true,
message: 'Request approved and search job triggered',
request: updatedRequest,
});
} else {
// Deny: Change status to 'denied'
const updatedRequest = await prisma.request.update({
where: { id },
data: { status: 'denied' },
include: {
audiobook: true,
user: {
select: {
id: true,
plexUsername: true,
},
},
},
});
logger.info(`Request ${id} denied by admin ${req.user.sub}`, {
requestId: id,
userId: updatedRequest.userId,
audiobookTitle: updatedRequest.audiobook.title,
adminId: req.user.sub,
});
return NextResponse.json({
success: true,
message: 'Request denied',
request: updatedRequest,
});
}
} catch (error) {
logger.error('Failed to process approval action', {
error: error instanceof Error ? error.message : String(error)
});
if (error instanceof z.ZodError) {
return NextResponse.json(
{
error: 'ValidationError',
message: 'Invalid action. Must be "approve" or "deny"',
details: error.errors,
},
{ status: 400 }
);
}
return NextResponse.json(
{
error: 'ApprovalError',
message: 'Failed to process approval action',
},
{ status: 500 }
);
}
});
});
}
@@ -0,0 +1,58 @@
/**
* Component: Admin Pending Approval Requests API
* Documentation: documentation/admin-features/request-approval.md
*/
import { NextRequest, NextResponse } from 'next/server';
import { requireAuth, requireAdmin, AuthenticatedRequest } from '@/lib/middleware/auth';
import { prisma } from '@/lib/db';
import { RMABLogger } from '@/lib/utils/logger';
const logger = RMABLogger.create('API.Admin.Requests.PendingApproval');
/**
* GET /api/admin/requests/pending-approval
* Get all requests with status 'awaiting_approval'
*/
export async function GET(request: NextRequest) {
return requireAuth(request, async (req: AuthenticatedRequest) => {
return requireAdmin(req, async () => {
try {
const requests = await prisma.request.findMany({
where: {
status: 'awaiting_approval',
deletedAt: null,
},
include: {
audiobook: true,
user: {
select: {
id: true,
plexUsername: true,
avatarUrl: true,
},
},
},
orderBy: { createdAt: 'desc' },
});
return NextResponse.json({
success: true,
requests,
count: requests.length,
});
} catch (error) {
logger.error('Failed to fetch pending approval requests', {
error: error instanceof Error ? error.message : String(error)
});
return NextResponse.json(
{
error: 'FetchError',
message: 'Failed to fetch pending approval requests',
},
{ status: 500 }
);
}
});
});
}
@@ -0,0 +1,89 @@
/**
* Component: Admin Auto-Approve Settings API
* Documentation: documentation/settings-pages.md
*/
import { NextRequest, NextResponse } from 'next/server';
import { requireAuth, requireAdmin, AuthenticatedRequest } from '@/lib/middleware/auth';
import { prisma } from '@/lib/db';
import { RMABLogger } from '@/lib/utils/logger';
const logger = RMABLogger.create('API.Admin.Settings.AutoApprove');
/**
* GET /api/admin/settings/auto-approve
* Get current global auto-approve setting
*/
export async function GET(request: NextRequest) {
return requireAuth(request, async (req: AuthenticatedRequest) => {
return requireAdmin(req, async () => {
try {
const config = await prisma.configuration.findUnique({
where: { key: 'auto_approve_requests' },
});
// Default to true if not configured (backward compatibility)
const autoApproveRequests = config === null ? true : config.value === 'true';
return NextResponse.json({ autoApproveRequests });
} catch (error) {
logger.error('Failed to fetch auto-approve setting', {
error: error instanceof Error ? error.message : String(error)
});
return NextResponse.json(
{ error: 'Failed to fetch auto-approve setting' },
{ status: 500 }
);
}
});
});
}
/**
* PATCH /api/admin/settings/auto-approve
* Update global auto-approve setting
*/
export async function PATCH(request: NextRequest) {
return requireAuth(request, async (req: AuthenticatedRequest) => {
return requireAdmin(req, async () => {
try {
const body = await request.json();
const { autoApproveRequests } = body;
// Validate input
if (typeof autoApproveRequests !== 'boolean') {
return NextResponse.json(
{ error: 'Invalid input. autoApproveRequests must be a boolean' },
{ status: 400 }
);
}
// Update configuration
await prisma.configuration.upsert({
where: { key: 'auto_approve_requests' },
create: {
key: 'auto_approve_requests',
value: autoApproveRequests.toString(),
},
update: {
value: autoApproveRequests.toString(),
},
});
logger.info(`Auto-approve setting updated to: ${autoApproveRequests}`, {
userId: req.user?.sub,
});
return NextResponse.json({ autoApproveRequests });
} catch (error) {
logger.error('Failed to update auto-approve setting', {
error: error instanceof Error ? error.message : String(error)
});
return NextResponse.json(
{ error: 'Failed to update auto-approve setting' },
{ status: 500 }
);
}
});
});
}
+15 -1
View File
@@ -14,7 +14,7 @@ export async function PUT(request: NextRequest) {
return requireAuth(request, async (req: AuthenticatedRequest) => {
return requireAdmin(req, async () => {
try {
const { downloadDir, mediaDir, metadataTaggingEnabled, chapterMergingEnabled } = await request.json();
const { downloadDir, mediaDir, audiobookPathTemplate, metadataTaggingEnabled, chapterMergingEnabled } = await request.json();
if (!downloadDir || !mediaDir) {
return NextResponse.json(
@@ -44,6 +44,20 @@ export async function PUT(request: NextRequest) {
create: { key: 'media_dir', value: mediaDir },
});
// Update audiobook path template
if (audiobookPathTemplate !== undefined) {
await prisma.configuration.upsert({
where: { key: 'audiobook_path_template' },
update: { value: audiobookPathTemplate },
create: {
key: 'audiobook_path_template',
value: audiobookPathTemplate,
category: 'automation',
description: 'Template for organizing audiobook files in media directory',
},
});
}
// Update metadata tagging setting
await prisma.configuration.upsert({
where: { key: 'metadata_tagging_enabled' },
+1
View File
@@ -86,6 +86,7 @@ export async function GET(request: NextRequest) {
paths: {
downloadDir: configMap.get('download_dir') || '/downloads',
mediaDir: configMap.get('media_dir') || '/media/audiobooks',
audiobookPathTemplate: configMap.get('audiobook_path_template') || '{author}/{title} {asin}',
metadataTaggingEnabled: configMap.get('metadata_tagging_enabled') === 'true',
chapterMergingEnabled: configMap.get('chapter_merging_enabled') === 'true',
},
+34 -7
View File
@@ -19,7 +19,7 @@ export async function PUT(
try {
const { id } = await params;
const body = await request.json();
const { role } = body;
const { role, autoApproveRequests } = body;
// Validate role
if (!role || (role !== 'user' && role !== 'admin')) {
@@ -29,6 +29,14 @@ export async function PUT(
);
}
// Validate autoApproveRequests (optional)
if (autoApproveRequests !== undefined && autoApproveRequests !== null && typeof autoApproveRequests !== 'boolean') {
return NextResponse.json(
{ error: 'Invalid autoApproveRequests. Must be a boolean or null' },
{ status: 400 }
);
}
// Prevent user from demoting themselves
if (req.user && id === req.user.sub) {
return NextResponse.json(
@@ -45,6 +53,7 @@ export async function PUT(
authProvider: true,
plexUsername: true,
deletedAt: true,
role: true, // Need current role to detect role changes
},
});
@@ -63,30 +72,48 @@ export async function PUT(
);
}
// Prevent changing setup admin role
if (targetUser.isSetupAdmin && role !== 'admin') {
// Detect if role is being changed
const isRoleChange = targetUser.role !== role;
// Prevent changing setup admin role (only if role is actually being changed)
if (targetUser.isSetupAdmin && isRoleChange && role !== 'admin') {
return NextResponse.json(
{ error: 'Cannot change the setup admin role. This account must always remain an admin.' },
{ status: 403 }
);
}
// Prevent changing OIDC user roles (managed by identity provider)
if (targetUser.authProvider === 'oidc') {
// Prevent changing OIDC user roles (only if role is actually being changed)
if (targetUser.authProvider === 'oidc' && isRoleChange) {
return NextResponse.json(
{ error: 'Cannot change OIDC user roles. Use admin role mapping in OIDC settings instead.' },
{ status: 403 }
);
}
// Update user role
// Validate that admins cannot have autoApproveRequests set to false
if (role === 'admin' && autoApproveRequests === false) {
return NextResponse.json(
{ error: 'Admins must always auto-approve requests. Cannot set autoApproveRequests to false for admin users.' },
{ status: 400 }
);
}
// Prepare update data
const updateData: { role: string; autoApproveRequests?: boolean | null } = { role };
if (autoApproveRequests !== undefined) {
updateData.autoApproveRequests = autoApproveRequests;
}
// Update user role and autoApproveRequests
const updatedUser = await prisma.user.update({
where: { id },
data: { role },
data: updateData,
select: {
id: true,
plexUsername: true,
role: true,
autoApproveRequests: true,
},
});
+1
View File
@@ -30,6 +30,7 @@ export async function GET(request: NextRequest) {
createdAt: true,
updatedAt: true,
lastLoginAt: true,
autoApproveRequests: true,
_count: {
select: {
requests: true,
@@ -10,6 +10,7 @@ import { requireAuth, AuthenticatedRequest } from '@/lib/middleware/auth';
import { prisma } from '@/lib/db';
import { getJobQueueService } from '@/lib/services/job-queue.service';
import { findPlexMatch } from '@/lib/utils/audiobook-matcher';
import { getAudibleService } from '@/lib/integrations/audible.service';
import { z } from 'zod';
import { RMABLogger } from '@/lib/utils/logger';
@@ -112,6 +113,27 @@ export async function POST(request: NextRequest) {
);
}
// Fetch full details from Audnexus to get releaseDate and year
let year: number | undefined;
try {
const audibleService = getAudibleService();
const audnexusData = await audibleService.getAudiobookDetails(audiobook.asin);
if (audnexusData?.releaseDate) {
try {
const releaseYear = new Date(audnexusData.releaseDate).getFullYear();
if (!isNaN(releaseYear)) {
year = releaseYear;
logger.debug(`Extracted year ${year} from Audnexus releaseDate: ${audnexusData.releaseDate}`);
}
} catch (error) {
logger.warn(`Failed to parse Audnexus releaseDate "${audnexusData.releaseDate}": ${error instanceof Error ? error.message : 'Unknown error'}`);
}
}
} catch (error) {
logger.warn(`Failed to fetch Audnexus data for ASIN ${audiobook.asin}: ${error instanceof Error ? error.message : 'Unknown error'}`);
}
// Try to find existing audiobook record by ASIN
let audiobookRecord = await prisma.audiobook.findFirst({
where: { audibleAsin: audiobook.asin },
@@ -127,9 +149,18 @@ export async function POST(request: NextRequest) {
narrator: audiobook.narrator,
description: audiobook.description,
coverArtUrl: audiobook.coverArtUrl,
year,
status: 'requested',
},
});
logger.debug(`Created audiobook ${audiobookRecord.id} with year: ${year || 'none'}`);
} else if (year) {
// Always update year if we have it from Audnexus (even if audiobook already has one)
audiobookRecord = await prisma.audiobook.update({
where: { id: audiobookRecord.id },
data: { year },
});
logger.debug(`Updated audiobook ${audiobookRecord.id} with year ${year}`);
}
// Check if user already has an active (non-deleted) request for this audiobook
+32 -1
View File
@@ -6,6 +6,7 @@
import { NextRequest, NextResponse } from 'next/server';
import { requireAuth, AuthenticatedRequest } from '@/lib/middleware/auth';
import { prisma } from '@/lib/db';
import { getAudibleService } from '@/lib/integrations/audible.service';
import { RMABLogger } from '@/lib/utils/logger';
const logger = RMABLogger.create('API.BookDateSwipe');
@@ -62,12 +63,33 @@ async function handler(req: AuthenticatedRequest) {
// If swiped right and not marked as known, create request
if (action === 'right' && !markedAsKnown && recommendation.audnexusAsin) {
try {
// Fetch full details from Audnexus to get releaseDate and year
let year: number | undefined;
try {
const audibleService = getAudibleService();
const audnexusData = await audibleService.getAudiobookDetails(recommendation.audnexusAsin);
if (audnexusData?.releaseDate) {
try {
const releaseYear = new Date(audnexusData.releaseDate).getFullYear();
if (!isNaN(releaseYear)) {
year = releaseYear;
logger.debug(`Extracted year ${year} from Audnexus releaseDate: ${audnexusData.releaseDate}`);
}
} catch (error) {
logger.warn(`Failed to parse Audnexus releaseDate "${audnexusData.releaseDate}": ${error instanceof Error ? error.message : 'Unknown error'}`);
}
}
} catch (error) {
logger.warn(`Failed to fetch Audnexus data for ASIN ${recommendation.audnexusAsin}: ${error instanceof Error ? error.message : 'Unknown error'}`);
}
// Check if book already exists in audiobooks table
let audiobook = await prisma.audiobook.findFirst({
where: { audibleAsin: recommendation.audnexusAsin },
});
// If not, create it
// If not, create it with year
if (!audiobook) {
audiobook = await prisma.audiobook.create({
data: {
@@ -77,9 +99,18 @@ async function handler(req: AuthenticatedRequest) {
narrator: recommendation.narrator,
description: recommendation.description,
coverArtUrl: recommendation.coverUrl,
year,
status: 'requested',
},
});
logger.debug(`Created audiobook ${audiobook.id} with year: ${year || 'none'}`);
} else if (year) {
// Always update year if we have it from Audnexus (even if audiobook already has one)
audiobook = await prisma.audiobook.update({
where: { id: audiobook.id },
data: { year },
});
logger.debug(`Updated audiobook ${audiobook.id} with year ${year}`);
}
// Create request (if not already exists)
+18 -52
View File
@@ -9,53 +9,13 @@ import { NextRequest, NextResponse } from 'next/server';
import { requireAuth, requireAdmin, AuthenticatedRequest } from '@/lib/middleware/auth';
import { prisma } from '@/lib/db';
import { downloadEbook } from '@/lib/services/ebook-scraper';
import { buildAudiobookPath } from '@/lib/utils/file-organizer';
import fs from 'fs/promises';
import path from 'path';
import { RMABLogger } from '@/lib/utils/logger';
const logger = RMABLogger.create('API.FetchEbook');
/**
* Sanitize path component (same logic as file-organizer)
*/
function sanitizePath(name: string): string {
return (
name
.replace(/[<>:"/\\|?*]/g, '')
.trim()
.replace(/^\.+/, '')
.replace(/\.+$/, '')
.replace(/\s+/g, ' ')
.slice(0, 200)
);
}
/**
* Build target path (same logic as file-organizer)
*/
function buildTargetPath(
baseDir: string,
author: string,
title: string,
year?: number | null,
asin?: string | null
): string {
const authorClean = sanitizePath(author);
const titleClean = sanitizePath(title);
let folderName = titleClean;
if (year) {
folderName = `${folderName} (${year})`;
}
if (asin) {
folderName = `${folderName} ${asin}`;
}
return path.join(baseDir, authorClean, folderName);
}
export async function POST(
request: NextRequest,
{ params }: { params: Promise<{ id: string }> }
@@ -103,37 +63,43 @@ export async function POST(
const audiobook = requestRecord.audiobook;
// Get configuration
const [mediaDirConfig, formatConfig, baseUrlConfig, flaresolverrConfig] = await Promise.all([
const [mediaDirConfig, templateConfig, formatConfig, baseUrlConfig, flaresolverrConfig] = await Promise.all([
prisma.configuration.findUnique({ where: { key: 'media_dir' } }),
prisma.configuration.findUnique({ where: { key: 'audiobook_path_template' } }),
prisma.configuration.findUnique({ where: { key: 'ebook_sidecar_preferred_format' } }),
prisma.configuration.findUnique({ where: { key: 'ebook_sidecar_base_url' } }),
prisma.configuration.findUnique({ where: { key: 'ebook_sidecar_flaresolverr_url' } }),
]);
const mediaDir = mediaDirConfig?.value || '/media/audiobooks';
const template = templateConfig?.value || '{author}/{title} {asin}';
const preferredFormat = formatConfig?.value || 'epub';
const baseUrl = baseUrlConfig?.value || 'https://annas-archive.li';
const flaresolverrUrl = flaresolverrConfig?.value || undefined;
// Get year from AudibleCache if available
// Fetch year from audible cache if ASIN is available
let year: number | undefined;
if (audiobook.audibleAsin) {
const audibleCacheData = await prisma.audibleCache.findUnique({
const audibleCache = await prisma.audibleCache.findUnique({
where: { asin: audiobook.audibleAsin },
select: { releaseDate: true },
});
if (audibleCacheData?.releaseDate) {
year = new Date(audibleCacheData.releaseDate).getFullYear();
if (audibleCache?.releaseDate) {
year = new Date(audibleCache.releaseDate).getFullYear();
}
}
// Build target path
const targetPath = buildTargetPath(
// Build target path using centralized function
const targetPath = buildAudiobookPath(
mediaDir,
audiobook.author,
audiobook.title,
year,
audiobook.audibleAsin
template,
{
author: audiobook.author,
title: audiobook.title,
narrator: audiobook.narrator || undefined,
asin: audiobook.audibleAsin || undefined,
year,
}
);
logger.debug('Fetch e-book request', {
+86 -3
View File
@@ -8,6 +8,7 @@ import { requireAuth, AuthenticatedRequest } from '@/lib/middleware/auth';
import { prisma } from '@/lib/db';
import { getJobQueueService } from '@/lib/services/job-queue.service';
import { findPlexMatch } from '@/lib/utils/audiobook-matcher';
import { getAudibleService } from '@/lib/integrations/audible.service';
import { z } from 'zod';
import { RMABLogger } from '@/lib/utils/logger';
@@ -96,6 +97,27 @@ export async function POST(request: NextRequest) {
);
}
// Fetch full details from Audnexus to get releaseDate and year
let year: number | undefined;
try {
const audibleService = getAudibleService();
const audnexusData = await audibleService.getAudiobookDetails(audiobook.asin);
if (audnexusData?.releaseDate) {
try {
const releaseYear = new Date(audnexusData.releaseDate).getFullYear();
if (!isNaN(releaseYear)) {
year = releaseYear;
logger.debug(`Extracted year ${year} from Audnexus releaseDate: ${audnexusData.releaseDate}`);
}
} catch (error) {
logger.warn(`Failed to parse Audnexus releaseDate "${audnexusData.releaseDate}": ${error instanceof Error ? error.message : 'Unknown error'}`);
}
}
} catch (error) {
logger.warn(`Failed to fetch Audnexus data for ASIN ${audiobook.asin}: ${error instanceof Error ? error.message : 'Unknown error'}`);
}
// Try to find existing audiobook record by ASIN
let audiobookRecord = await prisma.audiobook.findFirst({
where: { audibleAsin: audiobook.asin },
@@ -111,9 +133,18 @@ export async function POST(request: NextRequest) {
narrator: audiobook.narrator,
description: audiobook.description,
coverArtUrl: audiobook.coverArtUrl,
year,
status: 'requested',
},
});
logger.debug(`Created audiobook ${audiobookRecord.id} with year: ${year || 'none'}`);
} else if (year) {
// Always update year if we have it from Audnexus (even if audiobook already has one)
audiobookRecord = await prisma.audiobook.update({
where: { id: audiobookRecord.id },
data: { year },
});
logger.debug(`Updated audiobook ${audiobookRecord.id} with year ${year}`);
}
// Check if user already has an active (non-deleted) request for this audiobook
@@ -150,12 +181,64 @@ export async function POST(request: NextRequest) {
// Check if we should skip auto-search (for interactive search)
const skipAutoSearch = req.nextUrl.searchParams.get('skipAutoSearch') === 'true';
// Check if request needs approval
let needsApproval = false;
let shouldTriggerSearch = !skipAutoSearch;
// Fetch user with autoApproveRequests setting
const user = await prisma.user.findUnique({
where: { id: req.user.id },
select: {
role: true,
autoApproveRequests: true,
},
});
if (!user) {
return NextResponse.json(
{ error: 'UserNotFound', message: 'User not found' },
{ status: 404 }
);
}
// Determine if approval is needed
if (user.role === 'admin') {
// Admins always auto-approve
needsApproval = false;
} else {
// Check user's personal setting first
if (user.autoApproveRequests === true) {
needsApproval = false;
} else if (user.autoApproveRequests === false) {
needsApproval = true;
} else {
// User setting is null, check global setting
const globalConfig = await prisma.configuration.findUnique({
where: { key: 'auto_approve_requests' },
});
// Default to true if not configured (backward compatibility)
const globalAutoApprove = globalConfig === null ? true : globalConfig.value === 'true';
needsApproval = !globalAutoApprove;
}
}
// Determine initial status
let initialStatus: string;
if (needsApproval) {
initialStatus = 'awaiting_approval';
shouldTriggerSearch = false; // Don't trigger search if awaiting approval
} else if (skipAutoSearch) {
initialStatus = 'awaiting_search';
} else {
initialStatus = 'pending';
}
// Create request with appropriate status
const newRequest = await prisma.request.create({
data: {
userId: req.user.id,
audiobookId: audiobookRecord.id,
status: skipAutoSearch ? 'awaiting_search' : 'pending',
status: initialStatus,
progress: 0,
},
include: {
@@ -169,8 +252,8 @@ export async function POST(request: NextRequest) {
},
});
// Trigger search job only if not skipped
if (!skipAutoSearch) {
// Trigger search job only if not skipped and not awaiting approval
if (shouldTriggerSearch) {
const jobQueue = getJobQueueService();
await jobQueue.addSearchJob(newRequest.id, {
id: audiobookRecord.id,
+38 -5
View File
@@ -7,6 +7,7 @@ import { NextRequest, NextResponse } from 'next/server';
import fs from 'fs/promises';
import path from 'path';
import { RMABLogger } from '@/lib/utils/logger';
import { validateTemplate, generateMockPreviews } from '@/lib/utils/path-template.util';
const logger = RMABLogger.create('API.Setup.TestPaths');
@@ -45,7 +46,7 @@ async function testPath(dirPath: string): Promise<boolean> {
export async function POST(request: NextRequest) {
try {
const { downloadDir, mediaDir } = await request.json();
const { downloadDir, mediaDir, audiobookPathTemplate } = await request.json();
if (!downloadDir || !mediaDir) {
return NextResponse.json(
@@ -58,6 +59,26 @@ export async function POST(request: NextRequest) {
const downloadDirValid = await testPath(downloadDir);
const mediaDirValid = await testPath(mediaDir);
// Validate template if provided
let templateValidation: {
isValid: boolean;
error?: string;
previewPaths?: string[];
} | undefined;
if (audiobookPathTemplate) {
const validation = validateTemplate(audiobookPathTemplate);
templateValidation = {
isValid: validation.valid,
error: validation.error,
};
// Generate previews only if template is valid
if (validation.valid) {
templateValidation.previewPaths = generateMockPreviews(audiobookPathTemplate);
}
}
const success = downloadDirValid && mediaDirValid;
if (!success) {
@@ -71,16 +92,28 @@ export async function POST(request: NextRequest) {
return NextResponse.json({
success: false,
downloadDirValid,
mediaDirValid,
downloadDir: {
valid: downloadDirValid,
error: downloadDirValid ? undefined : 'Download directory path is invalid or parent mount is not writable',
},
mediaDir: {
valid: mediaDirValid,
error: mediaDirValid ? undefined : 'Media directory path is invalid or parent mount is not writable',
},
template: templateValidation,
error: errors.join('. '),
});
}
return NextResponse.json({
success: true,
downloadDirValid,
mediaDirValid,
downloadDir: {
valid: downloadDirValid,
},
mediaDir: {
valid: mediaDirValid,
},
template: templateValidation,
message: 'Directories are ready and writable (created if needed)',
});
} catch (error) {
+4 -1
View File
@@ -6,6 +6,7 @@
import type { Metadata } from "next";
import { Geist, Geist_Mono } from "next/font/google";
import { AuthProvider } from "@/contexts/AuthContext";
import { PreferencesProvider } from "@/contexts/PreferencesContext";
import "./globals.css";
const geistSans = Geist({
@@ -50,7 +51,9 @@ export default function RootLayout({
className={`${geistSans.variable} ${geistMono.variable} antialiased bg-gray-50 dark:bg-gray-900 text-gray-900 dark:text-gray-100`}
>
<AuthProvider>
{children}
<PreferencesProvider>
{children}
</PreferencesProvider>
</AuthProvider>
</body>
</html>
+11
View File
@@ -11,10 +11,13 @@ import { AudiobookGrid } from '@/components/audiobooks/AudiobookGrid';
import { useAudiobooks } from '@/lib/hooks/useAudiobooks';
import { ProtectedRoute } from '@/components/auth/ProtectedRoute';
import { StickyPagination } from '@/components/ui/StickyPagination';
import { CardSizeControls } from '@/components/ui/CardSizeControls';
import { usePreferences } from '@/contexts/PreferencesContext';
export default function HomePage() {
const [popularPage, setPopularPage] = useState(1);
const [newReleasesPage, setNewReleasesPage] = useState(1);
const { cardSize, setCardSize } = usePreferences();
// Refs for auto-scrolling to section tops
const popularSectionRef = useRef<HTMLElement>(null);
@@ -62,6 +65,9 @@ export default function HomePage() {
<h2 className="text-xl sm:text-2xl md:text-3xl font-bold text-gray-900 dark:text-gray-100">
Popular Audiobooks
</h2>
<div className="ml-auto">
<CardSizeControls size={cardSize} onSizeChange={setCardSize} />
</div>
</div>
</div>
</div>
@@ -82,6 +88,7 @@ export default function HomePage() {
audiobooks={popular}
isLoading={loadingPopular}
emptyMessage="No popular audiobooks available"
cardSize={cardSize}
/>
)}
</div>
@@ -97,6 +104,9 @@ export default function HomePage() {
<h2 className="text-xl sm:text-2xl md:text-3xl font-bold text-gray-900 dark:text-gray-100">
New Releases
</h2>
<div className="ml-auto">
<CardSizeControls size={cardSize} onSizeChange={setCardSize} />
</div>
</div>
</div>
</div>
@@ -117,6 +127,7 @@ export default function HomePage() {
audiobooks={newReleases}
isLoading={loadingNewReleases}
emptyMessage="No new releases available"
cardSize={cardSize}
/>
)}
</div>
+22 -5
View File
@@ -10,11 +10,14 @@ import { Header } from '@/components/layout/Header';
import { AudiobookGrid } from '@/components/audiobooks/AudiobookGrid';
import { useSearch } from '@/lib/hooks/useAudiobooks';
import { ProtectedRoute } from '@/components/auth/ProtectedRoute';
import { CardSizeControls } from '@/components/ui/CardSizeControls';
import { usePreferences } from '@/contexts/PreferencesContext';
export default function SearchPage() {
const [query, setQuery] = useState('');
const [debouncedQuery, setDebouncedQuery] = useState('');
const [page, setPage] = useState(1);
const { cardSize, setCardSize } = usePreferences();
// Debounce search query
useEffect(() => {
@@ -101,18 +104,32 @@ export default function SearchPage() {
{/* Results */}
{debouncedQuery ? (
<div className="space-y-6">
{/* Results Count */}
{!isLoading && totalResults > 0 && (
<div className="text-center text-gray-600 dark:text-gray-400">
Found {totalResults.toLocaleString()} result{totalResults !== 1 ? 's' : ''} for "{debouncedQuery}"
{/* Sticky Results Header with Card Size Controls */}
<div className="sticky top-14 sm:top-16 z-30 mb-4 sm:mb-6">
<div className="bg-white/90 dark:bg-gray-800/90 backdrop-blur-md rounded-2xl px-4 sm:px-6 py-3 border border-gray-200/50 dark:border-gray-700/50 shadow-sm">
<div className="flex items-center gap-3">
<div className="w-1 h-6 bg-gradient-to-b from-blue-500 to-purple-500 rounded-full" />
<h2 className="text-xl sm:text-2xl font-bold text-gray-900 dark:text-gray-100">
Search Results
</h2>
{!isLoading && totalResults > 0 && (
<span className="text-sm text-gray-600 dark:text-gray-400">
({totalResults.toLocaleString()} result{totalResults !== 1 ? 's' : ''})
</span>
)}
<div className="ml-auto">
<CardSizeControls size={cardSize} onSizeChange={setCardSize} />
</div>
</div>
</div>
)}
</div>
{/* Results Grid */}
<AudiobookGrid
audiobooks={results}
isLoading={!!(isLoading && page === 1)}
emptyMessage={`No results found for "${debouncedQuery}"`}
cardSize={cardSize}
/>
{/* Load More */}
+12 -3
View File
@@ -173,12 +173,21 @@ export function AudiobookCard({
}
// Check if book is requested and in progress (non-re-requestable statuses)
const inProgressStatuses = ['pending', 'awaiting_search', 'searching', 'downloading', 'processing', 'downloaded', 'awaiting_import'];
const inProgressStatuses = ['pending', 'awaiting_search', 'searching', 'downloading', 'processing', 'downloaded', 'awaiting_import', 'awaiting_approval', 'denied'];
if (audiobook.isRequested && audiobook.requestStatus && inProgressStatuses.includes(audiobook.requestStatus)) {
// Special text for 'downloaded' status (waiting for Plex scan)
// Determine button text based on status
let buttonText;
let buttonClass = 'w-full cursor-not-allowed opacity-75';
if (audiobook.requestStatus === 'downloaded') {
buttonText = 'Processing...';
} else if (audiobook.requestStatus === 'awaiting_approval') {
buttonText = audiobook.requestedByUsername
? `Pending Approval (${audiobook.requestedByUsername})`
: 'Pending Approval';
} else if (audiobook.requestStatus === 'denied') {
buttonText = 'Request Denied';
buttonClass = 'w-full cursor-not-allowed opacity-75 bg-red-100 dark:bg-red-900/30 text-red-700 dark:text-red-300 hover:bg-red-100 dark:hover:bg-red-900/30';
} else {
buttonText = audiobook.requestedByUsername
? `Requested by ${audiobook.requestedByUsername}`
@@ -191,7 +200,7 @@ export function AudiobookCard({
disabled={true}
variant="primary"
size="md"
className="w-full cursor-not-allowed opacity-75"
className={buttonClass}
>
{buttonText}
</Button>
@@ -411,16 +411,27 @@ export function AudiobookDetailsModal({
'processing',
'downloaded',
'awaiting_import',
'awaiting_approval',
'denied',
];
if (
isRequested &&
requestStatus &&
inProgressStatuses.includes(requestStatus)
) {
// Special text for 'downloaded' status (waiting for Plex scan)
// Determine button text and styling based on status
let buttonText;
let buttonClass = 'w-full cursor-not-allowed opacity-75';
if (requestStatus === 'downloaded') {
buttonText = 'Processing...';
} else if (requestStatus === 'awaiting_approval') {
buttonText = requestedByUsername
? `Pending Approval (${requestedByUsername})`
: 'Pending Approval';
} else if (requestStatus === 'denied') {
buttonText = 'Request Denied';
buttonClass = 'w-full cursor-not-allowed opacity-75 bg-red-100 dark:bg-red-900/30 text-red-700 dark:text-red-300 hover:bg-red-100 dark:hover:bg-red-900/30';
} else {
buttonText = requestedByUsername
? `Requested by ${requestedByUsername}`
@@ -434,7 +445,7 @@ export function AudiobookDetailsModal({
disabled={true}
variant="primary"
size="lg"
className="w-full cursor-not-allowed opacity-75"
className={buttonClass}
>
{buttonText}
</Button>
+24 -2
View File
@@ -14,6 +14,25 @@ interface AudiobookGridProps {
isLoading?: boolean;
emptyMessage?: string;
onRequestSuccess?: () => void;
cardSize?: number; // 1-9, default 5
}
// Helper function to get grid classes based on card size
// IMPORTANT: Classes must be explicit strings (not template literals) for Tailwind purging
function getGridClasses(size: number): string {
const sizeMap: Record<number, string> = {
1: 'grid-cols-4 md:grid-cols-6 lg:grid-cols-8 xl:grid-cols-10', // Smallest
2: 'grid-cols-3 md:grid-cols-5 lg:grid-cols-7 xl:grid-cols-9',
3: 'grid-cols-3 md:grid-cols-4 lg:grid-cols-6 xl:grid-cols-8',
4: 'grid-cols-2 md:grid-cols-4 lg:grid-cols-5 xl:grid-cols-7',
5: 'grid-cols-2 md:grid-cols-3 lg:grid-cols-4 xl:grid-cols-5', // Default
6: 'grid-cols-2 md:grid-cols-3 lg:grid-cols-4',
7: 'grid-cols-2 md:grid-cols-3',
8: 'grid-cols-2',
9: 'grid-cols-1', // Largest
};
return sizeMap[size] || sizeMap[5];
}
export function AudiobookGrid({
@@ -21,10 +40,13 @@ export function AudiobookGrid({
isLoading = false,
emptyMessage = 'No audiobooks found',
onRequestSuccess,
cardSize = 5,
}: AudiobookGridProps) {
const gridClasses = getGridClasses(cardSize);
if (isLoading) {
return (
<div className="grid grid-cols-2 md:grid-cols-3 lg:grid-cols-4 xl:grid-cols-5 gap-3 sm:gap-4 md:gap-6">
<div className={`grid ${gridClasses} gap-3 sm:gap-4 md:gap-6`}>
{Array.from({ length: 8 }).map((_, i) => (
<SkeletonCard key={i} />
))}
@@ -54,7 +76,7 @@ export function AudiobookGrid({
}
return (
<div className="grid grid-cols-2 md:grid-cols-3 lg:grid-cols-4 xl:grid-cols-5 gap-3 sm:gap-4 md:gap-6">
<div className={`grid ${gridClasses} gap-3 sm:gap-4 md:gap-6`}>
{audiobooks.map((audiobook) => (
<AudiobookCard
key={audiobook.asin}
+8
View File
@@ -64,6 +64,14 @@ export function StatusBadge({ status, progress, className }: StatusBadgeProps) {
label: 'Cancelled',
color: 'bg-gray-100 text-gray-800 dark:bg-gray-700 dark:text-gray-300',
},
awaiting_approval: {
label: 'Pending Approval',
color: 'bg-yellow-100 text-yellow-800 dark:bg-yellow-900 dark:text-yellow-200',
},
denied: {
label: 'Request Denied',
color: 'bg-red-100 text-red-800 dark:bg-red-900 dark:text-red-200',
},
};
const config = statusConfig[status] || {
+132
View File
@@ -0,0 +1,132 @@
/**
* Component: Card Size Controls
* Documentation: UI controls for adjusting audiobook card size (zoom level)
*/
'use client';
import React from 'react';
interface CardSizeControlsProps {
size: number; // 1-9
onSizeChange: (size: number) => void;
}
// Column count mapping for each size at each breakpoint
const columnMap = {
base: { 1: 4, 2: 3, 3: 3, 4: 2, 5: 2, 6: 2, 7: 2, 8: 2, 9: 1 },
md: { 1: 6, 2: 5, 3: 4, 4: 4, 5: 3, 6: 3, 7: 3, 8: 2, 9: 1 },
lg: { 1: 8, 2: 7, 3: 6, 4: 5, 5: 4, 6: 4, 7: 3, 8: 2, 9: 1 },
xl: { 1: 10, 2: 9, 3: 8, 4: 7, 5: 5, 6: 4, 7: 3, 8: 2, 9: 1 },
};
// Get current breakpoint based on window width
function getCurrentBreakpoint(): 'base' | 'md' | 'lg' | 'xl' {
if (typeof window === 'undefined') return 'base';
const width = window.innerWidth;
if (width >= 1280) return 'xl';
if (width >= 1024) return 'lg';
if (width >= 768) return 'md';
return 'base';
}
// Get column count for a size at current breakpoint
function getColumnCount(size: number, breakpoint: 'base' | 'md' | 'lg' | 'xl'): number {
return columnMap[breakpoint][size as keyof typeof columnMap.base];
}
// Find next size that produces a visible column change
function findNextVisibleSize(currentSize: number, direction: 'in' | 'out'): number {
const breakpoint = getCurrentBreakpoint();
const currentCols = getColumnCount(currentSize, breakpoint);
if (direction === 'in') {
// Zoom in: increase size (fewer columns, bigger cards)
for (let size = currentSize + 1; size <= 9; size++) {
const cols = getColumnCount(size, breakpoint);
if (cols < currentCols) {
return size;
}
}
return 9; // Max boundary
} else {
// Zoom out: decrease size (more columns, smaller cards)
for (let size = currentSize - 1; size >= 1; size--) {
const cols = getColumnCount(size, breakpoint);
if (cols > currentCols) {
return size;
}
}
return 1; // Min boundary
}
}
export function CardSizeControls({ size, onSizeChange }: CardSizeControlsProps) {
const handleZoomOut = () => {
const nextSize = findNextVisibleSize(size, 'out');
if (nextSize !== size) {
onSizeChange(nextSize);
}
};
const handleZoomIn = () => {
const nextSize = findNextVisibleSize(size, 'in');
if (nextSize !== size) {
onSizeChange(nextSize);
}
};
// Check if zoom buttons should be disabled
const canZoomOut = findNextVisibleSize(size, 'out') !== size;
const canZoomIn = findNextVisibleSize(size, 'in') !== size;
return (
<div className="flex items-center gap-1">
{/* Zoom Out Button */}
<button
onClick={handleZoomOut}
disabled={!canZoomOut}
aria-label="Zoom out"
className="p-1.5 text-gray-600 dark:text-gray-400 hover:bg-white/20 dark:hover:bg-gray-700/50 rounded-md transition-colors disabled:opacity-40 disabled:cursor-not-allowed disabled:hover:bg-transparent"
>
<svg
className="w-5 h-5"
fill="none"
stroke="currentColor"
viewBox="0 0 24 24"
xmlns="http://www.w3.org/2000/svg"
>
<path
strokeLinecap="round"
strokeLinejoin="round"
strokeWidth={2}
d="M15 12H9m12 0a9 9 0 11-18 0 9 9 0 0118 0z"
/>
</svg>
</button>
{/* Zoom In Button */}
<button
onClick={handleZoomIn}
disabled={!canZoomIn}
aria-label="Zoom in"
className="p-1.5 text-gray-600 dark:text-gray-400 hover:bg-white/20 dark:hover:bg-gray-700/50 rounded-md transition-colors disabled:opacity-40 disabled:cursor-not-allowed disabled:hover:bg-transparent"
>
<svg
className="w-5 h-5"
fill="none"
stroke="currentColor"
viewBox="0 0 24 24"
xmlns="http://www.w3.org/2000/svg"
>
<path
strokeLinecap="round"
strokeLinejoin="round"
strokeWidth={2}
d="M12 9v3m0 0v3m0-3h3m-3 0H9m12 0a9 9 0 11-18 0 9 9 0 0118 0z"
/>
</svg>
</button>
</div>
);
}
+108
View File
@@ -0,0 +1,108 @@
/**
* Component: User Preferences Context Provider
* Documentation: Manages user preferences (card size, etc.) with localStorage persistence
*/
'use client';
import React, { createContext, useContext, useState, useEffect, ReactNode } from 'react';
interface Preferences {
cardSize: number; // 1-9, default 5
}
interface PreferencesContextType {
cardSize: number;
setCardSize: (size: number) => void;
}
const PreferencesContext = createContext<PreferencesContextType | undefined>(undefined);
const DEFAULT_PREFERENCES: Preferences = {
cardSize: 5,
};
const STORAGE_KEY = 'preferences';
export function PreferencesProvider({ children }: { children: ReactNode }) {
const [cardSize, setCardSizeState] = useState<number>(DEFAULT_PREFERENCES.cardSize);
// Load preferences from localStorage on mount
useEffect(() => {
if (typeof window === 'undefined') return;
try {
const stored = localStorage.getItem(STORAGE_KEY);
if (stored) {
const preferences: Preferences = JSON.parse(stored);
// Validate cardSize is within range 1-9
if (preferences.cardSize >= 1 && preferences.cardSize <= 9) {
setCardSizeState(preferences.cardSize);
} else {
// Invalid size, reset to default
setCardSizeState(DEFAULT_PREFERENCES.cardSize);
}
}
} catch (error) {
console.error('Failed to load preferences from localStorage:', error);
setCardSizeState(DEFAULT_PREFERENCES.cardSize);
}
}, []);
// Update card size in state and localStorage
const setCardSize = (size: number) => {
if (typeof window === 'undefined') return;
// Validate size is within range 1-9
const validSize = Math.max(1, Math.min(9, size));
setCardSizeState(validSize);
try {
const stored = localStorage.getItem(STORAGE_KEY);
const preferences: Preferences = stored ? JSON.parse(stored) : { ...DEFAULT_PREFERENCES };
preferences.cardSize = validSize;
localStorage.setItem(STORAGE_KEY, JSON.stringify(preferences));
} catch (error) {
console.error('Failed to save preferences to localStorage:', error);
}
};
// Listen for storage changes in other tabs (cross-tab sync)
useEffect(() => {
if (typeof window === 'undefined') return;
const handleStorageChange = (e: StorageEvent) => {
if (e.key === STORAGE_KEY && e.newValue) {
try {
const preferences: Preferences = JSON.parse(e.newValue);
// Validate cardSize is within range 1-9
if (preferences.cardSize >= 1 && preferences.cardSize <= 9) {
setCardSizeState(preferences.cardSize);
}
} catch (error) {
console.error('Failed to parse preferences from storage event:', error);
}
}
};
window.addEventListener('storage', handleStorageChange);
return () => {
window.removeEventListener('storage', handleStorageChange);
};
}, []);
return (
<PreferencesContext.Provider value={{ cardSize, setCardSize }}>
{children}
</PreferencesContext.Provider>
);
}
export function usePreferences() {
const context = useContext(PreferencesContext);
if (context === undefined) {
throw new Error('usePreferences must be used within a PreferencesProvider');
}
return context;
}
+40 -1
View File
@@ -44,10 +44,47 @@ export async function processOrganizeFiles(payload: OrganizeFilesPayload): Promi
logger.info(`Organizing: ${audiobook.title} by ${audiobook.author}`);
// Fetch year from multiple sources (priority order)
let year = audiobook.year || undefined;
logger.info(`Initial year from audiobook record: ${year || 'null'}`);
if (!year && audiobook.audibleAsin) {
logger.info(`No year in audiobook record, attempting to fetch from AudibleCache for ASIN: ${audiobook.audibleAsin}`);
// Try AudibleCache (for popular/new releases)
const audibleCache = await prisma.audibleCache.findUnique({
where: { asin: audiobook.audibleAsin },
select: { releaseDate: true },
});
if (audibleCache?.releaseDate) {
logger.info(`Found AudibleCache entry with releaseDate: ${audibleCache.releaseDate}`);
year = new Date(audibleCache.releaseDate).getFullYear();
logger.info(`Extracted year ${year} from AudibleCache releaseDate`);
// Update audiobook record with year for future use
await prisma.audiobook.update({
where: { id: audiobookId },
data: { year },
});
logger.info(`Updated audiobook record with year ${year}`);
} else {
logger.info(`No year found in AudibleCache for ASIN ${audiobook.audibleAsin}`);
}
}
logger.info(`Final year value for path organization: ${year || 'null (year will be omitted from path)'}`)
// Get file organizer (reads media_dir from database config)
const organizer = await getFileOrganizer();
// Organize files (pass logger to file organizer)
// Read path template from configuration
const templateConfig = await prisma.configuration.findUnique({
where: { key: 'audiobook_path_template' },
});
const template = templateConfig?.value || '{author}/{title} {asin}';
// Organize files (pass template and logger to file organizer)
const result = await organizer.organize(
downloadPath,
{
@@ -56,7 +93,9 @@ export async function processOrganizeFiles(payload: OrganizeFilesPayload): Promi
narrator: audiobook.narrator || undefined,
coverArtUrl: audiobook.coverArtUrl || undefined,
asin: audiobook.audibleAsin || undefined,
year,
},
template,
jobId ? { jobId, context: 'FileOrganizer' } : undefined
);
+31
View File
@@ -152,3 +152,34 @@ export async function triggerABSItemMatch(itemId: string, asin?: string) {
logger.error(`Failed to trigger match for item ${itemId}`, { error: error instanceof Error ? error.message : String(error) });
}
}
/**
* Delete a library item from Audiobookshelf
* Note: This only removes the item from Audiobookshelf's database, not the actual files
*
* @param itemId - The Audiobookshelf item ID to delete
*/
export async function deleteABSItem(itemId: string): Promise<void> {
const configService = getConfigService();
const serverUrl = await configService.get('audiobookshelf.server_url');
const apiToken = await configService.get('audiobookshelf.api_token');
if (!serverUrl || !apiToken) {
throw new Error('Audiobookshelf not configured');
}
const url = `${serverUrl.replace(/\/$/, '')}/api/items/${itemId}`;
const response = await fetch(url, {
method: 'DELETE',
headers: {
'Authorization': `Bearer ${apiToken}`,
},
});
if (!response.ok) {
throw new Error(`ABS API error: ${response.status} ${response.statusText}`);
}
logger.info(`Deleted library item ${itemId} from Audiobookshelf`);
}
+39 -57
View File
@@ -9,6 +9,7 @@ import { prisma } from '../db';
import * as fs from 'fs/promises';
import * as path from 'path';
import { RMABLogger } from '../utils/logger';
import { buildAudiobookPath } from '../utils/file-organizer';
const logger = RMABLogger.create('RequestDelete');
@@ -52,6 +53,7 @@ export async function deleteRequest(
id: true,
title: true,
author: true,
narrator: true,
audibleAsin: true,
plexGuid: true,
absItemId: true,
@@ -190,42 +192,34 @@ export async function deleteRequest(
const { getConfigService } = await import('./config.service');
const configService = getConfigService();
const mediaDir = (await configService.get('media_dir')) || '/media/audiobooks';
const template = (await configService.get('audiobook_path_template')) || '{author}/{title} {asin}';
// Sanitize author and title for path (same logic as file-organizer.ts)
const sanitizedAuthor = sanitizePath(request.audiobook.author);
const sanitizedTitle = sanitizePath(request.audiobook.title);
// Build folder name with optional year and ASIN (matches file-organizer.ts logic)
let folderName = sanitizedTitle;
// Get ASIN and check for year in AudibleCache
const asin = request.audiobook.audibleAsin;
// Fetch year from audible cache if ASIN is available
let year: number | undefined;
if (asin) {
// Try to get year from AudibleCache if it exists
if (request.audiobook.audibleAsin) {
const audibleCache = await prisma.audibleCache.findUnique({
where: { asin },
where: { asin: request.audiobook.audibleAsin },
select: { releaseDate: true },
});
if (audibleCache?.releaseDate) {
year = new Date(audibleCache.releaseDate).getFullYear();
}
}
if (year) {
folderName = `${folderName} (${year})`;
}
// Build path using centralized function
const titleFolderPath = buildAudiobookPath(
mediaDir,
template,
{
author: request.audiobook.author,
title: request.audiobook.title,
narrator: request.audiobook.narrator || undefined,
asin: request.audiobook.audibleAsin || undefined,
year,
}
);
if (asin) {
folderName = `${folderName} ${asin}`;
}
// Build path: [media_dir]/[author]/[title (year) asin]/
const titleFolderPath = path.join(mediaDir, sanitizedAuthor, folderName);
// Check if folder exists
// Check if folder exists and delete it
try {
await fs.access(titleFolderPath);
@@ -235,20 +229,9 @@ export async function deleteRequest(
logger.info(`Deleted media directory: ${titleFolderPath}`);
filesDeleted = true;
} catch (accessError) {
// Folder doesn't exist - try without year/ASIN (fallback for older files)
const fallbackPath = path.join(mediaDir, sanitizedAuthor, sanitizedTitle);
try {
await fs.access(fallbackPath);
await fs.rm(fallbackPath, { recursive: true, force: true });
logger.info(`Deleted media directory (fallback path): ${fallbackPath}`);
filesDeleted = true;
} catch (fallbackError) {
// Neither path exists - that's okay
logger.info(
`Media directory not found (tried: ${titleFolderPath}, ${fallbackPath})`
);
filesDeleted = false;
}
// Folder doesn't exist - that's okay
logger.info(`Media directory not found: ${titleFolderPath}`);
filesDeleted = false;
}
} catch (error) {
logger.error(
@@ -265,6 +248,23 @@ export async function deleteRequest(
const configService = getConfigService();
const backendMode = await configService.getBackendMode();
// If backend is Audiobookshelf, delete the library item from ABS
if (backendMode === 'audiobookshelf' && request.audiobook.absItemId) {
try {
const { deleteABSItem } = await import('../services/audiobookshelf/api');
await deleteABSItem(request.audiobook.absItemId);
logger.info(
`Deleted Audiobookshelf library item ${request.audiobook.absItemId} for "${request.audiobook.title}"`
);
} catch (absError) {
logger.error(
`Error deleting Audiobookshelf library item ${request.audiobook.absItemId}`,
{ error: absError instanceof Error ? absError.message : String(absError) }
);
// Continue with deletion even if ABS deletion fails
}
}
// Delete ALL plex_library records matching this audiobook's title and author
// This handles cases where there might be duplicate library records
// and ensures the book doesn't show as "In Your Library" during searches
@@ -377,21 +377,3 @@ export async function deleteRequest(
};
}
}
/**
* Sanitize a path component (removes invalid characters)
*/
function sanitizePath(input: string): string {
return (
input
// Remove invalid path characters
.replace(/[<>:"/\\|?*]/g, '')
// Trim dots and spaces from start/end
.replace(/^[.\s]+|[.\s]+$/g, '')
// Collapse multiple spaces
.replace(/\s+/g, ' ')
// Limit length
.substring(0, 200)
.trim()
);
}
+231
View File
@@ -0,0 +1,231 @@
# Path Template Engine Utility
Location: `src/lib/utils/path-template.util.ts`
## Overview
Provides template variable substitution, validation, and preview generation for audiobook file organization paths.
## Features
1. **Template Variable Substitution** - Replace variables with actual values
2. **Template Validation** - Validate template syntax and characters
3. **Mock Preview Generation** - Generate example paths with sample data
4. **Path Sanitization** - Automatic removal of invalid file path characters
## Supported Variables
- `{author}` - Audiobook author name
- `{title}` - Audiobook title
- `{narrator}` - Audiobook narrator (optional)
- `{asin}` - Amazon ASIN identifier (optional)
## API Reference
### `substituteTemplate(template: string, variables: TemplateVariables): string`
Substitute template variables with actual values.
**Features:**
- Handles missing/null variables gracefully (omits them)
- Applies path sanitization to all substituted values
- Removes multiple consecutive spaces
- Normalizes path separators (converts backslashes to forward slashes)
**Example:**
```typescript
const result = substituteTemplate(
'{author}/{title}',
{ author: 'Brandon Sanderson', title: 'Mistborn' }
);
// Returns: "Brandon Sanderson/Mistborn"
```
### `validateTemplate(template: string): ValidationResult`
Validate a path template string.
**Checks for:**
- Valid variable names only (rejects unknown variables)
- No invalid file path characters outside of variables (`:`, `|`, `<`, `>`, `*`, `?`, `"`)
- Non-empty template
- Relative paths only (no absolute paths)
**Returns:**
```typescript
interface ValidationResult {
valid: boolean;
error?: string; // Helpful error message if invalid
}
```
**Example:**
```typescript
const result = validateTemplate('{author}/{title}');
// Returns: { valid: true }
const invalid = validateTemplate('{invalid}/{title}');
// Returns: { valid: false, error: "Unknown variable: {invalid}. Valid variables are: {author}, {title}, {narrator}, {asin}" }
```
### `generateMockPreviews(template: string): string[]`
Generate 2-3 example paths using mock audiobook data.
**Mock Examples:**
1. Brandon Sanderson / Mistborn: The Final Empire / Michael Kramer / B002UZMLXM
2. Douglas Adams / The Hitchhiker's Guide to the Galaxy / Stephen Fry / B0009JKV9W
3. Andy Weir / Project Hail Mary / (no narrator) / B08G9PRS1K
**Example:**
```typescript
const previews = generateMockPreviews('{author}/{title}');
// Returns:
// [
// "Brandon Sanderson/Mistborn The Final Empire",
// "Douglas Adams/The Hitchhiker's Guide to the Galaxy",
// "Andy Weir/Project Hail Mary"
// ]
```
### `getValidVariables(): string[]`
Get list of valid template variable names.
**Example:**
```typescript
const variables = getValidVariables();
// Returns: ['author', 'title', 'narrator', 'asin']
```
## Usage Examples
### Basic Template
```typescript
import { substituteTemplate } from '@/lib/utils/path-template.util';
const result = substituteTemplate(
'{author}/{title}',
{
author: 'Brandon Sanderson',
title: 'Mistborn: The Final Empire'
}
);
// Result: "Brandon Sanderson/Mistborn The Final Empire"
```
### Template with Optional Variables
```typescript
// With narrator
const withNarrator = substituteTemplate(
'{author}/{title}/{narrator}',
{
author: 'Douglas Adams',
title: "The Hitchhiker's Guide to the Galaxy",
narrator: 'Stephen Fry'
}
);
// Result: "Douglas Adams/The Hitchhiker's Guide to the Galaxy/Stephen Fry"
// Without narrator (gracefully omitted)
const withoutNarrator = substituteTemplate(
'{author}/{title}/{narrator}',
{
author: 'Andy Weir',
title: 'Project Hail Mary'
// No narrator
}
);
// Result: "Andy Weir/Project Hail Mary"
```
### Template Validation
```typescript
import { validateTemplate } from '@/lib/utils/path-template.util';
// Valid templates
validateTemplate('{author}/{title}');
// { valid: true }
validateTemplate('Audiobooks/{author}/{title}');
// { valid: true }
// Invalid templates
validateTemplate('{author}/{invalid}');
// { valid: false, error: "Unknown variable: {invalid}..." }
validateTemplate('/absolute/path/{author}');
// { valid: false, error: "Template must be a relative path..." }
validateTemplate('{author}|{title}');
// { valid: false, error: "Invalid characters found: |..." }
```
### Generate Previews
```typescript
import { generateMockPreviews } from '@/lib/utils/path-template.util';
const previews = generateMockPreviews('{author}/{title}/{narrator}');
// Returns 3 examples, including one without a narrator
previews.forEach(preview => console.log(preview));
// Brandon Sanderson/Mistborn The Final Empire/Michael Kramer
// Douglas Adams/The Hitchhiker's Guide to the Galaxy/Stephen Fry
// Andy Weir/Project Hail Mary
```
### Automatic Sanitization
```typescript
const result = substituteTemplate(
'{author}/{title}',
{
author: 'Author: <Test>',
title: 'Title|With*Invalid?Chars"'
}
);
// Result: "Author Test/TitleWithInvalidChars"
// Invalid characters automatically removed
```
## Path Sanitization Rules
The utility automatically sanitizes all substituted values:
1. **Removes invalid characters:** `<`, `>`, `:`, `"`, `/`, `\`, `|`, `?`, `*`
2. **Trims dots and spaces** from beginning and end
3. **Collapses multiple spaces** into single space
4. **Limits length** to 200 characters per component
5. **Normalizes path separators** (converts `\` to `/`)
## Integration Points
### File Organizer Service
The path template utility is used by `file-organizer.ts` to generate organized directory structures for downloaded audiobook files.
### Test Paths API
The utility is also used by the `/api/test-paths` endpoint to allow users to preview how their custom path templates will look before applying them.
## Testing
Comprehensive test suite located at: `tests/lib/utils/path-template.util.test.ts`
Run tests:
```bash
npm test -- path-template
```
## Type Definitions
```typescript
interface TemplateVariables {
author: string;
title: string;
narrator?: string;
asin?: string;
}
interface ValidationResult {
valid: boolean;
error?: string;
}
```
+57 -20
View File
@@ -21,6 +21,7 @@ import {
} from './chapter-merger';
import { prisma } from '../db';
import { downloadEbook } from '../services/ebook-scraper';
import { substituteTemplate, type TemplateVariables } from './path-template.util';
export interface AudiobookMetadata {
title: string;
@@ -66,6 +67,7 @@ export class FileOrganizer {
async organize(
downloadPath: string,
audiobook: AudiobookMetadata,
template: string,
loggerConfig?: LoggerConfig
): Promise<OrganizationResult> {
// Create logger if config provided
@@ -268,10 +270,12 @@ export class FileOrganizer {
// Build target directory
const targetPath = this.buildTargetPath(
this.mediaDir,
template,
audiobook.author,
audiobook.title,
audiobook.year,
audiobook.asin
audiobook.narrator,
audiobook.asin,
audiobook.year
);
await logger?.info(`Target path: ${targetPath}`);
@@ -542,31 +546,28 @@ export class FileOrganizer {
}
/**
* Build target path with sanitized names
* Format: Author/Title (Year) ASIN or Author/Title ASIN or Author/Title (Year)
* Build target path using template-based path building
* Uses the path template engine to substitute variables and sanitize paths
*/
private buildTargetPath(
baseDir: string,
template: string,
author: string,
title: string,
year?: number,
asin?: string
narrator?: string,
asin?: string,
year?: number
): string {
const authorClean = this.sanitizePath(author);
const titleClean = this.sanitizePath(title);
const variables: TemplateVariables = {
author,
title,
narrator,
asin,
year,
};
// Build folder name with optional year and ASIN
let folderName = titleClean;
if (year) {
folderName = `${folderName} (${year})`;
}
if (asin) {
folderName = `${folderName} ${asin}`;
}
return path.join(baseDir, authorClean, folderName);
const relativePath = substituteTemplate(template, variables);
return path.join(baseDir, relativePath);
}
/**
@@ -689,3 +690,39 @@ export async function getFileOrganizer(): Promise<FileOrganizer> {
return new FileOrganizer(mediaDir, tempDir);
}
/**
* Build audiobook path using template-based path building
* Standalone function for use by other modules (e.g., fetch-ebook route, request-delete service)
*
* @param baseDir - Base directory for audiobooks (e.g., /media/audiobooks)
* @param template - Path template string (e.g., "{author}/{title} {asin}")
* @param variables - Object containing variable values (author, title, narrator, asin)
* @returns Full path to audiobook directory
*
* @example
* ```typescript
* const path = buildAudiobookPath(
* '/media/audiobooks',
* '{author}/{title} {asin}',
* { author: 'Brandon Sanderson', title: 'Mistborn', asin: 'B002UZMLXM' }
* );
* // Returns: "/media/audiobooks/Brandon Sanderson/Mistborn B002UZMLXM"
* ```
*/
export function buildAudiobookPath(
baseDir: string,
template: string,
variables: { author: string; title: string; narrator?: string; asin?: string; year?: number }
): string {
const templateVars: TemplateVariables = {
author: variables.author,
title: variables.title,
narrator: variables.narrator,
asin: variables.asin,
year: variables.year,
};
const relativePath = substituteTemplate(template, templateVars);
return path.join(baseDir, relativePath);
}
+259
View File
@@ -0,0 +1,259 @@
/**
* Path Template Engine Utility
* Documentation: documentation/backend/services/file-organizer.md
*
* Provides template variable substitution, validation, and preview generation
* for audiobook file organization paths.
*/
/**
* Template variables for path substitution
*/
export interface TemplateVariables {
author: string;
title: string;
narrator?: string;
asin?: string;
year?: number;
}
/**
* Template validation result
*/
export interface ValidationResult {
valid: boolean;
error?: string;
}
/**
* Supported template variable names
*/
const VALID_VARIABLES = ['author', 'title', 'narrator', 'asin', 'year'];
/**
* Invalid file path characters (outside of template variables)
*/
const INVALID_PATH_CHARS = /[<>:"|?*]/;
/**
* Sanitize a path component by removing invalid characters
* Reuses logic from file-organizer.ts
*
* @param name - Path component to sanitize
* @returns Sanitized path component
*/
function sanitizePath(name: string): string {
return (
name
// Remove invalid filename characters
.replace(/[<>:"/\\|?*]/g, '')
// Remove leading/trailing dots and spaces
.trim()
.replace(/^\.+/, '')
.replace(/\.+$/, '')
// Collapse multiple spaces
.replace(/\s+/g, ' ')
// Limit length (255 chars max for most filesystems)
.slice(0, 200)
);
}
/**
* Substitute template variables with actual values
*
* Supported variables: {author}, {title}, {narrator}, {asin}
* - Handles missing/null variables gracefully (omits them)
* - Applies path sanitization to all substituted values
* - Removes multiple consecutive spaces after substitution
*
* @param template - Path template string (e.g., "{author}/{title}")
* @param variables - Object containing variable values
* @returns Substituted and sanitized path string
*
* @example
* ```typescript
* const result = substituteTemplate(
* "{author}/{title}",
* { author: "Brandon Sanderson", title: "Mistborn" }
* );
* // Returns: "Brandon Sanderson/Mistborn"
* ```
*/
export function substituteTemplate(
template: string,
variables: TemplateVariables
): string {
let result = template;
// Substitute each variable
for (const key of VALID_VARIABLES) {
const value = variables[key as keyof TemplateVariables];
const regex = new RegExp(`\\{${key}\\}`, 'g');
if (value !== undefined && value !== null) {
// Convert value to string and sanitize
const stringValue = String(value);
if (stringValue.trim()) {
const sanitizedValue = sanitizePath(stringValue.trim());
result = result.replace(regex, sanitizedValue);
} else {
// Remove the variable placeholder if value is empty
result = result.replace(regex, '');
}
} else {
// Remove the variable placeholder if value is missing
result = result.replace(regex, '');
}
}
// Clean up the result
result = result
// Remove multiple consecutive slashes (forward or backward)
.replace(/[\/\\]+/g, '/')
// Remove multiple consecutive spaces
.replace(/\s+/g, ' ')
// Remove leading/trailing slashes and spaces from each path component
.split('/')
.map(part => part.trim())
.filter(part => part.length > 0)
.join('/');
return result;
}
/**
* Validate a path template string
*
* Checks for:
* - Valid variable names only (rejects unknown variables)
* - No invalid file path characters outside of variables
* - Non-empty template
* - Relative paths only (no absolute paths)
*
* @param template - Path template string to validate
* @returns Validation result with error message if invalid
*
* @example
* ```typescript
* const result = validateTemplate("{author}/{title}");
* // Returns: { valid: true }
*
* const invalid = validateTemplate("{invalid}/{title}");
* // Returns: { valid: false, error: "Unknown variable: {invalid}" }
* ```
*/
export function validateTemplate(template: string): ValidationResult {
// Check for empty template
if (!template || template.trim().length === 0) {
return {
valid: false,
error: 'Template cannot be empty'
};
}
// Check for absolute paths
if (template.startsWith('/') || template.startsWith('\\') || /^[a-zA-Z]:/.test(template)) {
return {
valid: false,
error: 'Template must be a relative path (no absolute paths like "/" or "C:\\")'
};
}
// Extract all variables from template
const variableMatches = template.match(/\{[^}]+\}/g);
if (variableMatches) {
for (const match of variableMatches) {
const varName = match.slice(1, -1); // Remove { and }
if (!VALID_VARIABLES.includes(varName)) {
return {
valid: false,
error: `Unknown variable: {${varName}}. Valid variables are: ${VALID_VARIABLES.map(v => `{${v}}`).join(', ')}`
};
}
}
}
// Remove valid variables temporarily to check for invalid characters
let templateWithoutVars = template;
for (const varName of VALID_VARIABLES) {
templateWithoutVars = templateWithoutVars.replace(new RegExp(`\\{${varName}\\}`, 'g'), '');
}
// Check for invalid characters outside of variables
const invalidChars = templateWithoutVars.match(INVALID_PATH_CHARS);
if (invalidChars) {
return {
valid: false,
error: `Invalid characters found: ${[...new Set(invalidChars)].join(', ')}. These characters are not allowed in path templates.`
};
}
// Check for backslashes (Windows-style paths)
if (templateWithoutVars.includes('\\')) {
return {
valid: false,
error: 'Use forward slashes (/) for path separators, not backslashes (\\)'
};
}
return { valid: true };
}
/**
* Generate mock preview paths using sample audiobook data
*
* Creates 2-3 example paths to demonstrate how the template will look
* with real audiobook metadata.
*
* @param template - Path template string
* @returns Array of example paths (2-3 examples)
*
* @example
* ```typescript
* const previews = generateMockPreviews("{author}/{title}");
* // Returns:
* // [
* // "Brandon Sanderson/Mistborn The Final Empire",
* // "Douglas Adams/The Hitchhiker's Guide to the Galaxy",
* // "Andy Weir/Project Hail Mary"
* // ]
* ```
*/
export function generateMockPreviews(template: string): string[] {
const mockData: TemplateVariables[] = [
{
author: 'Brandon Sanderson',
title: 'Mistborn: The Final Empire',
narrator: 'Michael Kramer',
asin: 'B002UZMLXM',
year: 2006
},
{
author: 'Douglas Adams',
title: "The Hitchhiker's Guide to the Galaxy",
narrator: 'Stephen Fry',
asin: 'B0009JKV9W',
year: 2005
},
{
author: 'Andy Weir',
title: 'Project Hail Mary',
// No narrator for this example
asin: 'B08G9PRS1K',
year: 2021
}
];
return mockData.map(variables => substituteTemplate(template, variables));
}
/**
* Get list of valid template variable names
*
* @returns Array of valid variable names
*/
export function getValidVariables(): string[] {
return [...VALID_VARIABLES];
}