feat(content-restrictions): add age rating and content rating support (#2619)

This commit is contained in:
ACX
2026-02-05 17:10:44 -07:00
committed by GitHub
parent 89b4319970
commit 9f9c762180
42 changed files with 1755 additions and 17 deletions
@@ -0,0 +1,41 @@
export enum ContentRestrictionType {
CATEGORY = 'CATEGORY',
TAG = 'TAG',
MOOD = 'MOOD',
AGE_RATING = 'AGE_RATING',
CONTENT_RATING = 'CONTENT_RATING'
}
export enum ContentRestrictionMode {
EXCLUDE = 'EXCLUDE',
ALLOW_ONLY = 'ALLOW_ONLY'
}
export enum ContentRating {
EVERYONE = 'EVERYONE',
TEEN = 'TEEN',
MATURE = 'MATURE',
ADULT = 'ADULT',
EXPLICIT = 'EXPLICIT'
}
export interface ContentRestriction {
id?: number;
userId: number;
restrictionType: ContentRestrictionType;
mode: ContentRestrictionMode;
value: string;
createdAt?: string;
}
export const CONTENT_RATINGS = Object.values(ContentRating);
export const AGE_RATING_OPTIONS = [
{ value: '0', label: 'All Ages' },
{ value: '6', label: '6+' },
{ value: '10', label: '10+' },
{ value: '13', label: '13+' },
{ value: '16', label: '16+' },
{ value: '18', label: '18+' },
{ value: '21', label: '21+' }
];
@@ -0,0 +1,33 @@
import {inject, Injectable} from '@angular/core';
import {HttpClient} from '@angular/common/http';
import {Observable} from 'rxjs';
import {API_CONFIG} from '../../../core/config/api-config';
import {ContentRestriction} from './content-restriction.model';
@Injectable({
providedIn: 'root'
})
export class ContentRestrictionService {
private readonly baseUrl = `${API_CONFIG.BASE_URL}/api/v1/users`;
private http = inject(HttpClient);
getUserRestrictions(userId: number): Observable<ContentRestriction[]> {
return this.http.get<ContentRestriction[]>(`${this.baseUrl}/${userId}/content-restrictions`);
}
addRestriction(userId: number, restriction: ContentRestriction): Observable<ContentRestriction> {
return this.http.post<ContentRestriction>(`${this.baseUrl}/${userId}/content-restrictions`, restriction);
}
updateRestrictions(userId: number, restrictions: ContentRestriction[]): Observable<ContentRestriction[]> {
return this.http.put<ContentRestriction[]>(`${this.baseUrl}/${userId}/content-restrictions`, restrictions);
}
deleteRestriction(userId: number, restrictionId: number): Observable<void> {
return this.http.delete<void>(`${this.baseUrl}/${userId}/content-restrictions/${restrictionId}`);
}
deleteAllRestrictions(userId: number): Observable<void> {
return this.http.delete<void>(`${this.baseUrl}/${userId}/content-restrictions`);
}
}
@@ -0,0 +1,113 @@
<div class="content-restrictions-editor">
@if (isEditing) {
<div class="add-restriction-form">
<div class="form-row">
<div class="form-field">
<label>Type</label>
<p-select
[options]="restrictionTypes"
[(ngModel)]="newRestriction.restrictionType"
optionLabel="label"
optionValue="value"
placeholder="Select type"
appendTo="body">
</p-select>
</div>
<div class="form-field">
<label>Mode</label>
<p-select
[options]="restrictionModes"
[(ngModel)]="newRestriction.mode"
optionLabel="label"
optionValue="value"
placeholder="Select mode"
appendTo="body">
</p-select>
</div>
<div class="form-field form-field-wide">
<label>Value</label>
<p-select
[options]="getValueOptions()"
[(ngModel)]="newRestriction.value"
optionLabel="label"
optionValue="value"
placeholder="Select value"
[filter]="true"
filterPlaceholder="Search..."
appendTo="body">
</p-select>
</div>
<div class="form-field form-field-button">
<p-button
icon="pi pi-plus"
severity="success"
[outlined]="true"
(onClick)="addRestriction()"
pTooltip="Add restriction">
</p-button>
</div>
</div>
</div>
}
@if (restrictions.length === 0) {
<div class="empty-state">
<i class="pi pi-check-circle"></i>
<p>No content restrictions configured</p>
<span class="empty-hint">This user can access all content in their assigned libraries</span>
</div>
} @else {
<div class="restrictions-list">
@if (getExcludeRestrictions().length > 0) {
<div class="restrictions-group">
<h5 class="group-title">
<i class="pi pi-ban"></i>
Hidden Content (Exclude)
</h5>
<p class="group-description">Books matching these criteria will be hidden from this user</p>
<div class="restrictions-chips">
@for (restriction of getExcludeRestrictions(); track restriction.id) {
<div class="restriction-chip mode-exclude">
<span class="chip-type">{{ getRestrictionTypeLabel(restriction.restrictionType) }}</span>
<span class="chip-value">{{ restriction.value }}</span>
@if (isEditing) {
<button class="chip-remove" (click)="removeRestriction(restriction)">
<i class="pi pi-times"></i>
</button>
}
</div>
}
</div>
</div>
}
@if (getAllowOnlyRestrictions().length > 0) {
<div class="restrictions-group">
<h5 class="group-title">
<i class="pi pi-check"></i>
Allowed Content Only
</h5>
<p class="group-description">Only books matching these criteria will be visible to this user</p>
<div class="restrictions-chips">
@for (restriction of getAllowOnlyRestrictions(); track restriction.id) {
<div class="restriction-chip mode-allow">
<span class="chip-type">{{ getRestrictionTypeLabel(restriction.restrictionType) }}</span>
<span class="chip-value">{{ restriction.value }}</span>
@if (isEditing) {
<button class="chip-remove" (click)="removeRestriction(restriction)">
<i class="pi pi-times"></i>
</button>
}
</div>
}
</div>
</div>
}
</div>
}
<div class="restrictions-info">
<i class="pi pi-info-circle"></i>
<span>Exclude rules take priority over Allow Only rules for safety</span>
</div>
</div>
@@ -0,0 +1,187 @@
.content-restrictions-editor {
display: flex;
flex-direction: column;
gap: 1rem;
}
.add-restriction-form {
background: var(--surface-ground);
border-radius: 8px;
padding: 1rem;
border: 1px solid var(--surface-border);
.form-row {
display: flex;
gap: 0.75rem;
align-items: flex-end;
flex-wrap: wrap;
.form-field {
display: flex;
flex-direction: column;
gap: 0.25rem;
min-width: 140px;
label {
font-size: 0.75rem;
font-weight: 500;
color: var(--text-color-secondary);
text-transform: uppercase;
letter-spacing: 0.5px;
}
&.form-field-wide {
flex: 1;
min-width: 200px;
}
&.form-field-button {
min-width: auto;
}
}
}
}
.empty-state {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
padding: 2rem;
text-align: center;
color: var(--text-color-secondary);
i {
font-size: 2rem;
margin-bottom: 0.5rem;
color: var(--green-500);
}
p {
margin: 0;
font-weight: 500;
}
.empty-hint {
font-size: 0.85rem;
margin-top: 0.25rem;
opacity: 0.8;
}
}
.restrictions-list {
display: flex;
flex-direction: column;
gap: 1rem;
}
.restrictions-group {
background: var(--surface-ground);
border-radius: 8px;
padding: 1rem;
border: 1px solid var(--surface-border);
.group-title {
display: flex;
align-items: center;
gap: 0.5rem;
margin: 0 0 0.25rem 0;
font-size: 0.9rem;
font-weight: 600;
i {
font-size: 0.9rem;
}
}
.group-description {
margin: 0 0 0.75rem 0;
font-size: 0.8rem;
color: var(--text-color-secondary);
}
}
.restrictions-chips {
display: flex;
flex-wrap: wrap;
gap: 0.5rem;
}
.restriction-chip {
display: inline-flex;
align-items: center;
gap: 0.5rem;
padding: 0.35rem 0.75rem;
border-radius: 20px;
font-size: 0.85rem;
transition: all 0.2s ease;
&.mode-exclude {
background: rgba(239, 68, 68, 0.1);
border: 1px solid rgba(239, 68, 68, 0.3);
color: var(--red-500);
.chip-type {
color: var(--red-400);
}
}
&.mode-allow {
background: rgba(34, 197, 94, 0.1);
border: 1px solid rgba(34, 197, 94, 0.3);
color: var(--green-500);
.chip-type {
color: var(--green-400);
}
}
.chip-type {
font-size: 0.7rem;
text-transform: uppercase;
font-weight: 600;
letter-spacing: 0.5px;
}
.chip-value {
font-weight: 500;
}
.chip-remove {
display: flex;
align-items: center;
justify-content: center;
width: 18px;
height: 18px;
border: none;
background: transparent;
cursor: pointer;
border-radius: 50%;
padding: 0;
margin-left: 0.25rem;
transition: all 0.2s ease;
i {
font-size: 0.7rem;
}
&:hover {
background: rgba(0, 0, 0, 0.1);
}
}
}
.restrictions-info {
display: flex;
align-items: center;
gap: 0.5rem;
padding: 0.75rem;
background: rgba(59, 130, 246, 0.1);
border-radius: 6px;
font-size: 0.8rem;
color: var(--blue-500);
i {
font-size: 0.9rem;
}
}
@@ -0,0 +1,232 @@
import {Component, EventEmitter, inject, Input, OnChanges, OnInit, Output, SimpleChanges} from '@angular/core';
import {CommonModule} from '@angular/common';
import {FormsModule} from '@angular/forms';
import {Button} from 'primeng/button';
import {Select} from 'primeng/select';
import {MessageService} from 'primeng/api';
import {Tooltip} from 'primeng/tooltip';
import {
AGE_RATING_OPTIONS,
CONTENT_RATINGS,
ContentRestriction,
ContentRestrictionMode,
ContentRestrictionType
} from '../content-restriction.model';
import {ContentRestrictionService} from '../content-restriction.service';
import {BookService} from '../../../book/service/book.service';
import {filter, take} from 'rxjs/operators';
import {Book} from '../../../book/model/book.model';
@Component({
selector: 'app-content-restrictions-editor',
standalone: true,
imports: [
CommonModule,
FormsModule,
Button,
Select,
Tooltip
],
templateUrl: './content-restrictions-editor.component.html',
styleUrls: ['./content-restrictions-editor.component.scss']
})
export class ContentRestrictionsEditorComponent implements OnInit, OnChanges {
@Input() userId!: number;
@Input() isEditing = false;
@Output() restrictionsChanged = new EventEmitter<ContentRestriction[]>();
private contentRestrictionService = inject(ContentRestrictionService);
private bookService = inject(BookService);
private messageService = inject(MessageService);
restrictions: ContentRestriction[] = [];
availableCategories: string[] = [];
availableTags: string[] = [];
availableMoods: string[] = [];
newRestriction: Partial<ContentRestriction> = {
restrictionType: ContentRestrictionType.CATEGORY,
mode: ContentRestrictionMode.EXCLUDE,
value: ''
};
restrictionTypes = [
{label: 'Category/Genre', value: ContentRestrictionType.CATEGORY},
{label: 'Tag', value: ContentRestrictionType.TAG},
{label: 'Mood', value: ContentRestrictionType.MOOD},
{label: 'Age Rating', value: ContentRestrictionType.AGE_RATING},
{label: 'Content Rating', value: ContentRestrictionType.CONTENT_RATING}
];
restrictionModes = [
{label: 'Exclude (Hide matching)', value: ContentRestrictionMode.EXCLUDE},
{label: 'Allow Only (Show only matching)', value: ContentRestrictionMode.ALLOW_ONLY}
];
ageRatingOptions = AGE_RATING_OPTIONS;
contentRatingOptions = CONTENT_RATINGS.map(r => ({label: r, value: r}));
ngOnInit() {
this.loadRestrictions();
this.loadAvailableValues();
}
ngOnChanges(changes: SimpleChanges) {
if (changes['userId'] && !changes['userId'].firstChange) {
this.loadRestrictions();
}
}
loadRestrictions() {
if (!this.userId) return;
this.contentRestrictionService.getUserRestrictions(this.userId).subscribe({
next: (restrictions) => {
this.restrictions = restrictions;
this.restrictionsChanged.emit(this.restrictions);
},
error: () => {
this.messageService.add({
severity: 'error',
summary: 'Error',
detail: 'Failed to load content restrictions'
});
}
});
}
loadAvailableValues() {
this.bookService.bookState$.pipe(
filter(state => !!state.books && state.books.length > 0),
take(1)
).subscribe(state => {
const books: Book[] = state.books || [];
const categories = new Set<string>();
const tags = new Set<string>();
const moods = new Set<string>();
books.forEach((book: Book) => {
book.metadata?.categories?.forEach((c: string) => categories.add(c));
book.metadata?.tags?.forEach((t: string) => tags.add(t));
book.metadata?.moods?.forEach((m: string) => moods.add(m));
});
this.availableCategories = Array.from(categories).sort();
this.availableTags = Array.from(tags).sort();
this.availableMoods = Array.from(moods).sort();
});
}
getValueOptions(): {label: string, value: string}[] {
switch (this.newRestriction.restrictionType) {
case ContentRestrictionType.CATEGORY:
return this.availableCategories.map(c => ({label: c, value: c}));
case ContentRestrictionType.TAG:
return this.availableTags.map(t => ({label: t, value: t}));
case ContentRestrictionType.MOOD:
return this.availableMoods.map(m => ({label: m, value: m}));
case ContentRestrictionType.AGE_RATING:
return this.ageRatingOptions.map(o => ({label: o.label, value: o.value}));
case ContentRestrictionType.CONTENT_RATING:
return this.contentRatingOptions;
default:
return [];
}
}
addRestriction() {
if (!this.newRestriction.value || !this.newRestriction.restrictionType || !this.newRestriction.mode) {
this.messageService.add({
severity: 'warn',
summary: 'Warning',
detail: 'Please select all fields'
});
return;
}
const exists = this.restrictions.some(r =>
r.restrictionType === this.newRestriction.restrictionType &&
r.value === this.newRestriction.value
);
if (exists) {
this.messageService.add({
severity: 'warn',
summary: 'Warning',
detail: 'This restriction already exists'
});
return;
}
const restriction: ContentRestriction = {
userId: this.userId,
restrictionType: this.newRestriction.restrictionType!,
mode: this.newRestriction.mode!,
value: this.newRestriction.value!
};
this.contentRestrictionService.addRestriction(this.userId, restriction).subscribe({
next: (added) => {
this.restrictions.push(added);
this.restrictionsChanged.emit(this.restrictions);
this.newRestriction.value = '';
this.messageService.add({
severity: 'success',
summary: 'Success',
detail: 'Content restriction added'
});
},
error: () => {
this.messageService.add({
severity: 'error',
summary: 'Error',
detail: 'Failed to add content restriction'
});
}
});
}
removeRestriction(restriction: ContentRestriction) {
if (!restriction.id) return;
this.contentRestrictionService.deleteRestriction(this.userId, restriction.id).subscribe({
next: () => {
this.restrictions = this.restrictions.filter(r => r.id !== restriction.id);
this.restrictionsChanged.emit(this.restrictions);
this.messageService.add({
severity: 'success',
summary: 'Success',
detail: 'Content restriction removed'
});
},
error: () => {
this.messageService.add({
severity: 'error',
summary: 'Error',
detail: 'Failed to remove content restriction'
});
}
});
}
getRestrictionTypeLabel(type: ContentRestrictionType): string {
const found = this.restrictionTypes.find(t => t.value === type);
return found?.label || type;
}
getModeLabel(mode: ContentRestrictionMode): string {
return mode === ContentRestrictionMode.EXCLUDE ? 'Exclude' : 'Allow Only';
}
getModeClass(mode: ContentRestrictionMode): string {
return mode === ContentRestrictionMode.EXCLUDE ? 'mode-exclude' : 'mode-allow';
}
getExcludeRestrictions(): ContentRestriction[] {
return this.restrictions.filter(r => r.mode === ContentRestrictionMode.EXCLUDE);
}
getAllowOnlyRestrictions(): ContentRestriction[] {
return this.restrictions.filter(r => r.mode === ContentRestrictionMode.ALLOW_ONLY);
}
}
@@ -397,6 +397,20 @@
</div>
</div>
</div>
<div class="content-restrictions-section">
<h4 class="details-title">
<i class="pi pi-eye-slash"></i>
Content Restrictions
</h4>
<p class="section-description">
Configure which content this user can see based on categories, tags, moods, or age ratings.
</p>
<app-content-restrictions-editor
[userId]="user.id"
[isEditing]="user.isEditing ?? false">
</app-content-restrictions-editor>
</div>
</div>
}
</div>
@@ -347,6 +347,34 @@
gap: 1rem;
}
.content-restrictions-section {
display: flex;
flex-direction: column;
gap: 0.75rem;
margin-top: 0.5rem;
padding-top: 1rem;
border-top: 1px solid var(--p-content-border-color);
.section-description {
font-size: settings.$settings-description-size;
color: var(--p-text-muted-color);
margin: 0 0 0.5rem 0;
padding-left: 1.5rem;
@media (max-width: 600px) {
padding-left: 0;
}
}
app-content-restrictions-editor {
padding-left: 1.5rem;
@media (max-width: 600px) {
padding-left: 0;
}
}
}
.details-title {
display: flex;
align-items: center;
@@ -17,6 +17,7 @@ import {filter, take, takeUntil} from 'rxjs/operators';
import {Subject} from 'rxjs';
import {Tooltip} from 'primeng/tooltip';
import {DialogLauncherService} from '../../../shared/services/dialog-launcher.service';
import {ContentRestrictionsEditorComponent} from './content-restrictions-editor/content-restrictions-editor.component';
interface UserWithEditing extends User {
isEditing?: boolean;
@@ -37,7 +38,8 @@ interface UserWithEditing extends User {
InputText,
LowerCasePipe,
TitleCasePipe,
Tooltip
Tooltip,
ContentRestrictionsEditorComponent
],
templateUrl: './user-management.component.html',
styleUrls: ['./user-management.component.scss'],