mirror of
https://github.com/kikootwo/ReadMeABook.git
synced 2026-06-02 20:30:10 +00:00
Add extensible notification providers + UI/API
Introduce a provider-based notification system and wire it through the API and admin UI. Added INotificationProvider + notification service implementation and providers (apprise, discord, ntfy, pushover), plus a GET /api/admin/notifications/providers endpoint to expose provider metadata. Refactored code to use provider type strings (removed enum coupling), updated masking/encryption calls, and simplified the test notification endpoint to accept backendId or type+config and call sendToBackend directly. UI: NotificationsTab now fetches provider metadata and renders provider cards and dynamic config forms (fields driven by provider metadata). Added config field rendering, improved backend cards, and edit/delete actions. APIs: New providers route, updated admin notification CRUD routes to validate provider types dynamically, updated test route schema. Added download-client categories POST API to fetch categories from clients and wired postImportCategory handling in download-client routes. Other notable changes: BookDate now fetches Claude models dynamically from Anthropic's Models API; added paginated model fetch helper. Added ALLOW_WEAK_PASSWORD flag exposure to auth providers and password change logic. Doc updates and various tests added/updated. File-organization doc clarifies EPERM fix using stream-based copy.
This commit is contained in:
@@ -75,7 +75,7 @@ docker-compose logs -f app
|
||||
## 📊 Feature Highlights
|
||||
|
||||
### AI-Powered Recommendations
|
||||
- **Providers:** OpenAI (GPT-4o+) or Claude (Sonnet 4.5, Opus 4, Haiku)
|
||||
- **Providers:** OpenAI (GPT-4+) or Claude (dynamically fetched from Anthropic Models API)
|
||||
- **Personalization:** Based on your Plex library + swipe history
|
||||
- **Context:** Max 50 books (40 library + 10 swipes)
|
||||
- **Filtering:** Excludes books already in library, already requested, or already swiped
|
||||
|
||||
@@ -1,14 +1,14 @@
|
||||
# Notification System
|
||||
|
||||
**Status:** ✅ Implemented | Extensible notification system with Discord and Pushover support
|
||||
**Status:** ✅ Implemented | Extensible notification system with Discord, ntfy, and Pushover support
|
||||
|
||||
## Overview
|
||||
Sends notifications for audiobook request events (pending approval, approved, available, error) to configured backends. Non-blocking, atomic per-backend failure handling. Proper notification timing for all request flows including interactive search.
|
||||
|
||||
## Key Details
|
||||
- **Backends:** Discord (webhooks), Pushover (API)
|
||||
- **Backends:** Apprise (API), Discord (webhooks), ntfy (API), Pushover (API)
|
||||
- **Events:** request_pending_approval, request_approved, request_available, request_error
|
||||
- **Encryption:** AES-256-GCM for sensitive config (webhook URLs, API keys)
|
||||
- **Encryption:** AES-256-GCM for sensitive config (webhook URLs, API keys, notification URLs)
|
||||
- **Delivery:** Async via Bull job queue (priority 5)
|
||||
- **Failure Handling:** Non-blocking, Promise.allSettled (one backend fails, others succeed)
|
||||
|
||||
@@ -17,7 +17,7 @@ Sends notifications for audiobook request events (pending approval, approved, av
|
||||
```prisma
|
||||
model NotificationBackend {
|
||||
id String @id @default(uuid())
|
||||
type String // 'discord' | 'pushover'
|
||||
type String // 'apprise' | 'discord' | 'ntfy' | 'pushover'
|
||||
name String // User-friendly label
|
||||
config Json // Encrypted sensitive values
|
||||
events Json // Array of subscribed events
|
||||
@@ -70,7 +70,9 @@ model NotificationBackend {
|
||||
## Configuration Encryption
|
||||
|
||||
**Encrypted Values:**
|
||||
- Apprise: `urls`, `authToken`
|
||||
- Discord: `webhookUrl`
|
||||
- ntfy: `accessToken`
|
||||
- Pushover: `userKey`, `appToken`
|
||||
|
||||
**Pattern:** `iv:authTag:encryptedData` (base64)
|
||||
@@ -81,12 +83,26 @@ model NotificationBackend {
|
||||
|
||||
## Message Formatting
|
||||
|
||||
**Apprise (JSON via Apprise API):**
|
||||
- Type: info (pending), success (approved/available), failure (error)
|
||||
- Modes: Stateless (send URLs directly) or Stateful (use persistent configKey, optional tag filter)
|
||||
- Endpoint: `{serverUrl}/notify/` (stateless) or `{serverUrl}/notify/{configKey}` (stateful)
|
||||
- Auth: Optional Bearer token via `authToken` config field
|
||||
- Format: Event title + book details + user + error (if applicable)
|
||||
|
||||
**Discord (Rich Embeds):**
|
||||
- Color-coded by event (yellow=pending, green=approved, blue=available, red=error)
|
||||
- Fields: Title, Author, Requested By, Error (if applicable)
|
||||
- Footer: Request ID
|
||||
- Timestamp: Event time
|
||||
|
||||
**ntfy (JSON with Tags):**
|
||||
- Tags: mailbox_with_mail, white_check_mark, tada, x (rendered as emojis by ntfy)
|
||||
- Priority: Default (3) for pending/approved, High (4) for available/error
|
||||
- Format: Event title + book details + user + error (if applicable)
|
||||
- Auth: Optional Bearer token via `accessToken` config field
|
||||
- Server: Configurable `serverUrl` (default: https://ntfy.sh)
|
||||
|
||||
**Pushover (Plain Text with Emojis):**
|
||||
- Emojis: 📬 📬 🎉 ❌
|
||||
- Priority: Normal (0) for pending/approved, High (1) for available/error
|
||||
@@ -154,15 +170,49 @@ model NotificationBackend {
|
||||
|
||||
**Queue Method:** `addNotificationJob(event, requestId, title, author, userName, message?)`
|
||||
|
||||
## Architecture
|
||||
|
||||
**Provider Pattern:** `INotificationProvider` interface + registry (matches `IAuthProvider` pattern)
|
||||
|
||||
```
|
||||
src/lib/services/notification/
|
||||
INotificationProvider.ts # Interface + shared types
|
||||
notification.service.ts # Core service with registry
|
||||
index.ts # Re-exports
|
||||
providers/
|
||||
apprise.provider.ts # Apprise API (100+ services)
|
||||
discord.provider.ts # Discord webhook
|
||||
ntfy.provider.ts # ntfy API
|
||||
pushover.provider.ts # Pushover API
|
||||
```
|
||||
|
||||
**Registry:** Module-level `Map<string, INotificationProvider>` with `registerProvider()` / `getProvider()`
|
||||
|
||||
**INotificationProvider interface:**
|
||||
- `type: string` — provider identifier (registry key)
|
||||
- `sensitiveFields: string[]` — fields needing encryption/masking
|
||||
- `metadata: ProviderMetadata` — self-describing UI/validation metadata
|
||||
- `send(config, payload): Promise<void>` — receives decrypted config
|
||||
|
||||
**ProviderMetadata:** `{ type, displayName, description, iconLabel, iconColor, configFields[] }`
|
||||
**ProviderConfigField:** `{ name, label, type, required, placeholder?, defaultValue?, options? }`
|
||||
|
||||
**Helper functions:**
|
||||
- `getRegisteredProviderTypes(): string[]` — all registered type keys
|
||||
- `getAllProviderMetadata(): ProviderMetadata[]` — metadata for all providers
|
||||
|
||||
**API Endpoint:** `GET /api/admin/notifications/providers` — returns all provider metadata (admin-only)
|
||||
|
||||
## Extensibility
|
||||
|
||||
**Adding New Backend (e.g., Email):**
|
||||
1. Add 'email' to NotificationBackendType enum
|
||||
2. Create EmailConfig interface
|
||||
3. Add encryption logic for smtpPassword
|
||||
4. Implement sendEmail() method in NotificationService
|
||||
5. Add email card to type selector (green "E" badge)
|
||||
6. Add email form fields to modal
|
||||
**Adding New Backend (2 steps):**
|
||||
1. Create `providers/email.provider.ts` implementing `INotificationProvider`:
|
||||
- Set `type = 'email'`, `sensitiveFields = ['smtpPassword']`
|
||||
- Set `metadata` with displayName, description, iconLabel, iconColor, configFields
|
||||
- Implement `send()` with email-specific logic
|
||||
2. Register in `notification.service.ts`: `registerProvider(new EmailProvider())` + re-export from `index.ts`
|
||||
|
||||
No UI changes, no API route changes, no Zod schema changes needed — the UI renders dynamically from provider metadata.
|
||||
|
||||
**Adding New Event (e.g., download_complete):**
|
||||
1. Add 'download_complete' to NotificationEvent enum
|
||||
@@ -173,7 +223,7 @@ model NotificationBackend {
|
||||
## Tech Stack
|
||||
- Bull (job queue)
|
||||
- Node.js crypto (AES-256-GCM encryption)
|
||||
- Discord webhooks, Pushover API
|
||||
- Apprise API, Discord webhooks, ntfy API, Pushover API
|
||||
- React (UI), Tailwind CSS (styling)
|
||||
|
||||
## Related
|
||||
|
||||
@@ -200,32 +200,23 @@ export async function POST(req: NextRequest) {
|
||||
.map((m: any) => ({ id: m.id, name: m.id }));
|
||||
|
||||
} else if (provider === 'claude') {
|
||||
// Claude: Hardcoded list (Anthropic doesn't have a models API endpoint)
|
||||
models = [
|
||||
{ id: 'claude-sonnet-4-5-20250929', name: 'Claude Sonnet 4.5' },
|
||||
{ id: 'claude-3-7-sonnet-20250219', name: 'Claude 3.7 Sonnet' },
|
||||
{ id: 'claude-3-5-haiku-20241022', name: 'Claude 3.5 Haiku' },
|
||||
{ id: 'claude-opus-4-20250514', name: 'Claude Opus 4' },
|
||||
];
|
||||
|
||||
// Test connection with a simple API call
|
||||
const response = await fetch('https://api.anthropic.com/v1/messages', {
|
||||
method: 'POST',
|
||||
// Claude: Fetch models dynamically from the Anthropic Models API
|
||||
const response = await fetch('https://api.anthropic.com/v1/models?limit=1000', {
|
||||
headers: {
|
||||
'x-api-key': apiKey,
|
||||
'anthropic-version': '2023-06-01',
|
||||
'content-type': 'application/json'
|
||||
},
|
||||
body: JSON.stringify({
|
||||
model: 'claude-3-5-haiku-20241022',
|
||||
max_tokens: 10,
|
||||
messages: [{ role: 'user', content: 'Hi' }]
|
||||
})
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
return NextResponse.json({ error: 'Invalid Claude API key' }, { status: 400 });
|
||||
}
|
||||
|
||||
const data = await response.json();
|
||||
models = data.data.map((m: any) => ({
|
||||
id: m.id,
|
||||
name: m.display_name || m.id,
|
||||
}));
|
||||
} else {
|
||||
return NextResponse.json({ error: 'Invalid provider' }, { status: 400 });
|
||||
}
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
Personalized audiobook discovery using OpenAI/Claude APIs. Admin configures AI provider globally. Users swipe through recommendations based on their individual Plex library + swipe history. Right swipe creates request, left rejects, up dismisses.
|
||||
|
||||
## Key Details
|
||||
- **AI Providers:** OpenAI (GPT-4o+), Claude (Sonnet 4.5, Opus 4, Haiku)
|
||||
- **AI Providers:** OpenAI (GPT-4+), Claude (dynamically fetched from Anthropic Models API)
|
||||
- **Configuration:** Global admin-managed (provider, model, API key), per-user preferences (library scope, custom prompt)
|
||||
- **Personalization:** Each user receives recommendations based on their own library, ratings, swipe history, and custom preferences
|
||||
- **Library Scopes (per-user):**
|
||||
|
||||
@@ -208,7 +208,7 @@ async function organize(
|
||||
|
||||
## Fixed Issues ✅
|
||||
|
||||
**1. EPERM errors** - Fixed with `fs.readFile/writeFile` instead of `copyFile`
|
||||
**1. EPERM errors** - Fixed with stream-based copy (`pipeline` + `createReadStream`/`createWriteStream`) instead of `fs.copyFile()` which uses `copy_file_range()` — a syscall that returns EPERM on cross-export NFS4 and some FUSE mounts
|
||||
**2. Immediate deletion** - Changed to copy-only, scheduled cleanup after seeding
|
||||
**3. Files moved not copied** - Now copies to support seeding
|
||||
**4. Single file downloads** - Now supports files directly in downloads folder (not just directories)
|
||||
|
||||
Reference in New Issue
Block a user