feat: base64 uploads, reusable forms, Discord roles, and UX improvements (#66)
Security Scan and Upload / Security & DefectDojo Upload (push) Successful in 1m20s
Node.js CI / CI (push) Successful in 1m24s

## Summary

This PR includes multiple feature additions and fixes to improve the library application:

### 🎨 Base64 Image Upload Support
- Fixed Fastify body limit (1MB → 10MB) to accommodate base64-encoded images
- Corrected base64 size calculation and validation logic
- Improved error handling with proper 400 status codes and helpful messages
- Removed duplicate validation that was blocking uploads
- Users can now upload cover images up to 5MB (decoded size)

### 📝 Reusable Form Components
- Created 6 form components: `GameForm`, `BookForm`, `MusicForm`, `ShowForm`, `MangaForm`, `ArtForm`
- All forms support both 'add' and 'edit' modes with pre-population
- Integrated inline editing into all detail views (edit/delete buttons)
- Enhanced admin suggestions workflow with full forms instead of basic modals
- Added scroll-to-top when clicking edit in list views for better UX

### 🖼️ Default Cover Image
- Added beautiful library reading image as default cover for all media types
- Fixed static asset serving to use correct MIME types
- Updated all 12 components (6 list views + 6 detail views) to always show images

### 🔒 Tiered Rate Limiting
- Unauthenticated users: 100 requests/minute
- Authenticated users: 500 requests/minute (5x more lenient)
- Admin users: No rate limits (complete bypass via allowList)

### 🎮 Discord Integration
- Auto-assign library member role to users in NHCarrigan Discord server
- Checks server membership on every login
- Only assigns role if user is in server and doesn't have it yet
- Graceful error handling without blocking login
- Similar pattern to badge refresh flow

### 📚 Documentation
- Added comprehensive CLAUDE.md with:
  - Project structure and tech stack
  - Development workflow and commands
  - Database schema documentation
  - Authentication flow details
  - Security features
  - Code style conventions
  - Common gotchas and solutions

## Test Plan

- [x] Base64 image uploads work for cover images up to 5MB
- [x] Helpful error messages appear for validation failures
- [x] Edit/delete buttons appear on all detail views for admin users
- [x] Inline edit forms display and save correctly
- [x] Admin suggestions workflow uses full forms for all media types
- [x] Scroll-to-top works when editing from list views
- [x] Default cover image displays when no cover is provided
- [x] Static assets serve with correct MIME types
- [x] Rate limiting works correctly for different user types
- [x] Discord role assignment works on login
- [x] All builds pass without errors
- [x] No TypeScript errors

## Related Issues

Closes #65 - Base64 image upload issue

 This pull request was created with help from Hikari~ 🌸

Reviewed-on: #66
Co-authored-by: Hikari <hikari@nhcarrigan.com>
Co-committed-by: Hikari <hikari@nhcarrigan.com>
This commit was merged in pull request #66.
This commit is contained in:
2026-02-20 20:32:52 -08:00
committed by Naomi Carrigan
parent 208c11d153
commit 983b78b0e9
30 changed files with 4359 additions and 328 deletions
@@ -10,12 +10,18 @@ import { FormsModule } from '@angular/forms';
import { SuggestionService } from '../../services/suggestion.service';
import { AuthService } from '../../services/auth.service';
import { PaginationComponent } from '../shared/pagination.component';
import { Suggestion, SuggestionStatus, SuggestionEntity } from '@library/shared-types';
import { GameFormComponent } from '../shared/game-form.component';
import { BookFormComponent } from '../shared/book-form.component';
import { MusicFormComponent } from '../shared/music-form.component';
import { ShowFormComponent } from '../shared/show-form.component';
import { MangaFormComponent } from '../shared/manga-form.component';
import { ArtFormComponent } from '../shared/art-form.component';
import { Suggestion, SuggestionStatus, SuggestionEntity, CreateGameDto, UpdateGameDto, GameStatus, CreateBookDto, UpdateBookDto, BookStatus, CreateMusicDto, UpdateMusicDto, MusicStatus, MusicType, CreateShowDto, UpdateShowDto, ShowStatus, ShowType, CreateMangaDto, UpdateMangaDto, MangaStatus, CreateArtDto, UpdateArtDto } from '@library/shared-types';
@Component({
selector: 'app-admin-suggestions',
standalone: true,
imports: [CommonModule, FormsModule, PaginationComponent],
imports: [CommonModule, FormsModule, PaginationComponent, GameFormComponent, BookFormComponent, MusicFormComponent, ShowFormComponent, MangaFormComponent, ArtFormComponent],
template: `
<div class="container">
<div class="header-section">
@@ -231,158 +237,62 @@ import { Suggestion, SuggestionStatus, SuggestionEntity } from '@library/shared-
@if (showEditModal()) {
<div class="modal-overlay" (click)="closeEditModal()" (keyup.escape)="closeEditModal()" tabindex="0" role="button">
<div class="modal edit-modal" (click)="$event.stopPropagation()" (keyup)="$event.stopPropagation()" tabindex="-1">
<h3>Review & Edit Before Accepting</h3>
<p>Review and edit the details before adding to your collection.</p>
@if (editingSuggestion()) {
<form (ngSubmit)="confirmAcceptWithEdits()">
<div class="form-group">
<label for="edit-title">Title</label>
<input
type="text"
id="edit-title"
[(ngModel)]="editedData.title"
name="title"
required
>
</div>
@switch (editingSuggestion()!.entityType) {
@case (SuggestionEntity.book) {
<div class="form-group">
<label for="edit-author">Author</label>
<input
type="text"
id="edit-author"
[(ngModel)]="editedData.author"
name="author"
required
>
</div>
<div class="form-group">
<label for="edit-isbn">ISBN</label>
<input
type="text"
id="edit-isbn"
[(ngModel)]="editedData.isbn"
name="isbn"
>
</div>
}
@case (SuggestionEntity.game) {
<div class="form-group">
<label for="edit-platform">Platform</label>
<input
type="text"
id="edit-platform"
[(ngModel)]="editedData.platform"
name="platform"
>
</div>
}
@case (SuggestionEntity.music) {
<div class="form-group">
<label for="edit-artist">Artist</label>
<input
type="text"
id="edit-artist"
[(ngModel)]="editedData.artist"
name="artist"
required
>
</div>
<div class="form-group">
<label for="edit-type">Type</label>
<select id="edit-type" [(ngModel)]="editedData.type" name="type" required>
<option value="ALBUM">Album</option>
<option value="SINGLE">Single</option>
<option value="EP">EP</option>
</select>
</div>
}
@case (SuggestionEntity.art) {
<div class="form-group">
<label for="edit-artist">Artist</label>
<input
type="text"
id="edit-artist"
[(ngModel)]="editedData.artist"
name="artist"
required
>
</div>
<div class="form-group">
<label for="edit-description">Description</label>
<textarea
id="edit-description"
[(ngModel)]="editedData.description"
name="description"
rows="3"
></textarea>
</div>
<div class="form-group">
<label for="edit-imageUrl">Image URL</label>
<input
type="text"
id="edit-imageUrl"
[(ngModel)]="editedData.imageUrl"
name="imageUrl"
required
>
</div>
}
@case (SuggestionEntity.show) {
<div class="form-group">
<label for="edit-type">Type</label>
<select id="edit-type" [(ngModel)]="editedData.type" name="type" required>
<option value="TV_SERIES">TV Series</option>
<option value="ANIME">Anime</option>
<option value="FILM">Film</option>
<option value="DOCUMENTARY">Documentary</option>
</select>
</div>
}
@case (SuggestionEntity.manga) {
<div class="form-group">
<label for="edit-author">Author</label>
<input
type="text"
id="edit-author"
[(ngModel)]="editedData.author"
name="author"
required
>
</div>
}
}
<div class="form-group">
<label for="edit-notes">Notes</label>
<textarea
id="edit-notes"
[(ngModel)]="editedData.notes"
name="notes"
rows="3"
></textarea>
</div>
@if (editingSuggestion()!.entityType !== SuggestionEntity.art) {
<div class="form-group">
<label for="edit-coverImage">Cover Image URL</label>
<input
type="text"
id="edit-coverImage"
[(ngModel)]="editedData.coverImage"
name="coverImage"
>
</div>
}
<div class="modal-actions">
<button type="submit" class="btn btn-accept">Accept with Edits</button>
<button type="button" (click)="closeEditModal()" class="btn btn-secondary">Cancel</button>
</div>
</form>
@if (editingSuggestion()!.entityType === SuggestionEntity.game) {
<h3>Review & Edit Game Before Accepting</h3>
<p>Review and edit all the details before adding to your collection.</p>
<app-game-form
mode="add"
[initialData]="getGameInitialData(editingSuggestion()!)"
(formSubmit)="saveGameFromSuggestion($event)"
(formCancel)="closeEditModal()"
></app-game-form>
} @else if (editingSuggestion()!.entityType === SuggestionEntity.book) {
<h3>Review & Edit Book Before Accepting</h3>
<p>Review and edit all the details before adding to your collection.</p>
<app-book-form
mode="add"
[initialData]="getBookInitialData(editingSuggestion()!)"
(formSubmit)="saveBookFromSuggestion($event)"
(formCancel)="closeEditModal()"
></app-book-form>
} @else if (editingSuggestion()!.entityType === SuggestionEntity.music) {
<h3>Review & Edit Music Before Accepting</h3>
<p>Review and edit all the details before adding to your collection.</p>
<app-music-form
mode="add"
[initialData]="getMusicInitialData(editingSuggestion()!)"
(formSubmit)="saveMusicFromSuggestion($event)"
(formCancel)="closeEditModal()"
></app-music-form>
} @else if (editingSuggestion()!.entityType === SuggestionEntity.show) {
<h3>Review & Edit Show Before Accepting</h3>
<p>Review and edit all the details before adding to your collection.</p>
<app-show-form
mode="add"
[initialData]="getShowInitialData(editingSuggestion()!)"
(formSubmit)="saveShowFromSuggestion($event)"
(formCancel)="closeEditModal()"
></app-show-form>
} @else if (editingSuggestion()!.entityType === SuggestionEntity.manga) {
<h3>Review & Edit Manga Before Accepting</h3>
<p>Review and edit all the details before adding to your collection.</p>
<app-manga-form
mode="add"
[initialData]="getMangaInitialData(editingSuggestion()!)"
(formSubmit)="saveMangaFromSuggestion($event)"
(formCancel)="closeEditModal()"
></app-manga-form>
} @else if (editingSuggestion()!.entityType === SuggestionEntity.art) {
<h3>Review & Edit Art Before Accepting</h3>
<p>Review and edit all the details before adding to your collection.</p>
<app-art-form
mode="add"
[initialData]="getArtInitialData(editingSuggestion()!)"
(formSubmit)="saveArtFromSuggestion($event)"
(formCancel)="closeEditModal()"
></app-art-form>
}
}
</div>
</div>
@@ -810,59 +720,174 @@ export class AdminSuggestionsComponent implements OnInit {
return new Date(date).toLocaleDateString();
}
getGameInitialData(suggestion: Suggestion): Partial<CreateGameDto> {
const gameData = suggestion.gameData as any;
return {
title: suggestion.title,
platform: gameData?.platform || undefined,
status: GameStatus.backlog, // Default to backlog for new suggestions
notes: gameData?.notes || undefined,
coverImage: gameData?.coverImage || undefined,
tags: [],
links: []
};
}
async saveGameFromSuggestion(data: CreateGameDto | UpdateGameDto) {
const suggestion = this.editingSuggestion();
if (!suggestion) return;
try {
// Treat it as CreateGameDto since we're creating a new item from a suggestion
await this.suggestionService.acceptSuggestionWithEdits(suggestion.id, data as CreateGameDto);
alert(`"${(data as CreateGameDto).title}" has been added to your collection!`);
this.closeEditModal();
this.loadSuggestions();
} catch (error) {
alert('Failed to accept suggestion. Please try again.');
}
}
getBookInitialData(suggestion: Suggestion): Partial<CreateBookDto> {
const bookData = suggestion.bookData as any;
return {
title: suggestion.title,
author: bookData?.author || '',
isbn: bookData?.isbn || undefined,
status: BookStatus.toRead,
notes: bookData?.notes || undefined,
coverImage: bookData?.coverImage || undefined,
tags: [],
links: []
};
}
async saveBookFromSuggestion(data: CreateBookDto | UpdateBookDto) {
const suggestion = this.editingSuggestion();
if (!suggestion) return;
try {
await this.suggestionService.acceptSuggestionWithEdits(suggestion.id, data as CreateBookDto);
alert(`"${(data as CreateBookDto).title}" has been added to your collection!`);
this.closeEditModal();
this.loadSuggestions();
} catch (error) {
alert('Failed to accept suggestion. Please try again.');
}
}
getMusicInitialData(suggestion: Suggestion): Partial<CreateMusicDto> {
const musicData = suggestion.musicData as any;
return {
title: suggestion.title,
artist: musicData?.artist || '',
type: musicData?.type || MusicType.album,
status: MusicStatus.wantToListen,
notes: musicData?.notes || undefined,
coverArt: musicData?.coverArt || undefined,
tags: [],
links: []
};
}
async saveMusicFromSuggestion(data: CreateMusicDto | UpdateMusicDto) {
const suggestion = this.editingSuggestion();
if (!suggestion) return;
try {
await this.suggestionService.acceptSuggestionWithEdits(suggestion.id, data as CreateMusicDto);
alert(`"${(data as CreateMusicDto).title}" has been added to your collection!`);
this.closeEditModal();
this.loadSuggestions();
} catch (error) {
alert('Failed to accept suggestion. Please try again.');
}
}
getShowInitialData(suggestion: Suggestion): Partial<CreateShowDto> {
const showData = suggestion.showData as any;
return {
title: suggestion.title,
type: showData?.type || ShowType.tvSeries,
status: ShowStatus.wantToWatch,
notes: showData?.notes || undefined,
coverImage: showData?.coverImage || undefined,
tags: [],
links: []
};
}
async saveShowFromSuggestion(data: CreateShowDto | UpdateShowDto) {
const suggestion = this.editingSuggestion();
if (!suggestion) return;
try {
await this.suggestionService.acceptSuggestionWithEdits(suggestion.id, data as CreateShowDto);
alert(`"${(data as CreateShowDto).title}" has been added to your collection!`);
this.closeEditModal();
this.loadSuggestions();
} catch (error) {
alert('Failed to accept suggestion. Please try again.');
}
}
getMangaInitialData(suggestion: Suggestion): Partial<CreateMangaDto> {
const mangaData = suggestion.mangaData as any;
return {
title: suggestion.title,
author: mangaData?.author || '',
status: MangaStatus.wantToRead,
notes: mangaData?.notes || undefined,
coverImage: mangaData?.coverImage || undefined,
tags: [],
links: []
};
}
async saveMangaFromSuggestion(data: CreateMangaDto | UpdateMangaDto) {
const suggestion = this.editingSuggestion();
if (!suggestion) return;
try {
await this.suggestionService.acceptSuggestionWithEdits(suggestion.id, data as CreateMangaDto);
alert(`"${(data as CreateMangaDto).title}" has been added to your collection!`);
this.closeEditModal();
this.loadSuggestions();
} catch (error) {
alert('Failed to accept suggestion. Please try again.');
}
}
getArtInitialData(suggestion: Suggestion): Partial<CreateArtDto> {
const artData = suggestion.artData as any;
return {
title: suggestion.title,
artist: artData?.artist || '',
description: artData?.description || undefined,
imageUrl: artData?.imageUrl || '',
tags: [],
links: []
};
}
async saveArtFromSuggestion(data: CreateArtDto | UpdateArtDto) {
const suggestion = this.editingSuggestion();
if (!suggestion) return;
try {
await this.suggestionService.acceptSuggestionWithEdits(suggestion.id, data as CreateArtDto);
alert(`"${(data as CreateArtDto).title}" has been added to your collection!`);
this.closeEditModal();
this.loadSuggestions();
} catch (error) {
alert('Failed to accept suggestion. Please try again.');
}
}
async acceptSuggestion(suggestion: Suggestion) {
this.editingSuggestion.set(suggestion);
// Pre-populate the edit form with suggestion data
this.editedData = {
title: suggestion.title,
notes: '',
coverImage: '',
coverArt: ''
};
// Add entity-specific data
switch (suggestion.entityType) {
case SuggestionEntity.book:
const bookData = suggestion.bookData as any;
this.editedData.author = bookData?.author || '';
this.editedData.isbn = bookData?.isbn || '';
this.editedData.notes = bookData?.notes || '';
this.editedData.coverImage = bookData?.coverImage || '';
break;
case SuggestionEntity.game:
const gameData = suggestion.gameData as any;
this.editedData.platform = gameData?.platform || '';
this.editedData.notes = gameData?.notes || '';
this.editedData.coverImage = gameData?.coverImage || '';
break;
case SuggestionEntity.music:
const musicData = suggestion.musicData as any;
this.editedData.artist = musicData?.artist || '';
this.editedData.type = musicData?.type || 'ALBUM';
this.editedData.notes = musicData?.notes || '';
this.editedData.coverArt = musicData?.coverArt || '';
break;
case SuggestionEntity.art:
const artData = suggestion.artData as any;
this.editedData.artist = artData?.artist || '';
this.editedData.description = artData?.description || '';
this.editedData.imageUrl = artData?.imageUrl || '';
break;
case SuggestionEntity.show:
const showData = suggestion.showData as any;
this.editedData.type = showData?.type || 'TV_SERIES';
this.editedData.notes = showData?.notes || '';
this.editedData.coverImage = showData?.coverImage || '';
break;
case SuggestionEntity.manga:
const mangaData = suggestion.mangaData as any;
this.editedData.author = mangaData?.author || '';
this.editedData.notes = mangaData?.notes || '';
this.editedData.coverImage = mangaData?.coverImage || '';
break;
}
// For all entity types, we'll use the form components which have their own initialization logic
this.showEditModal.set(true);
}
@@ -884,20 +909,6 @@ export class AdminSuggestionsComponent implements OnInit {
this.editedData = {};
}
async confirmAcceptWithEdits() {
const suggestion = this.editingSuggestion();
if (!suggestion) return;
try {
await this.suggestionService.acceptSuggestionWithEdits(suggestion.id, this.editedData);
alert(`"${this.editedData.title}" has been added to your collection with your edits!`);
this.closeEditModal();
this.loadSuggestions();
} catch (error) {
alert('Failed to accept suggestion. Please try again.');
}
}
async confirmDecline() {
const suggestion = this.decliningsuggestion();
if (!suggestion) return;