import {inject, Injectable} from '@angular/core'; import {HttpClient} from '@angular/common/http'; import {BehaviorSubject, Observable, throwError} from 'rxjs'; import {API_CONFIG} from '../../../core/config/api-config'; import {Library} from '../../book/model/library.model'; import {catchError, distinctUntilChanged, finalize, shareReplay, tap} from 'rxjs/operators'; import {AuthService} from '../../../shared/service/auth.service'; import {DashboardConfig} from '../../dashboard/models/dashboard-config.model'; export interface EntityViewPreferences { global: EntityViewPreference; overrides: EntityViewPreferenceOverride[]; } export interface EntityViewPreference { sortKey: string; sortDir: 'ASC' | 'DESC'; view: 'GRID' | 'TABLE'; coverSize: number; seriesCollapsed: boolean; } export interface EntityViewPreferenceOverride { entityType: 'LIBRARY' | 'SHELF' | 'MAGIC_SHELF'; entityId: number; preferences: EntityViewPreference; } export interface SidebarLibrarySorting { field: string; order: string; } export interface SidebarShelfSorting { field: string; order: string; } export interface SidebarMagicShelfSorting { field: string; order: string; } export interface PerBookSetting { pdf: string; epub: string; cbx: string; } export type PageSpread = 'off' | 'even' | 'odd'; export type BookFilterMode = 'and' | 'or' | 'single'; export type FilterSortingMode = 'alphabetical' | 'count'; export enum CbxBackgroundColor { GRAY = 'GRAY', BLACK = 'BLACK', WHITE = 'WHITE' } export interface PdfReaderSetting { pageSpread: PageSpread; pageZoom: string; showSidebar: boolean; } export enum CbxPageViewMode { SINGLE_PAGE = 'SINGLE_PAGE', TWO_PAGE = 'TWO_PAGE', } export enum CbxPageSpread { EVEN = 'EVEN', ODD = 'ODD', } export enum PdfPageViewMode { SINGLE_PAGE = 'SINGLE_PAGE', TWO_PAGE = 'TWO_PAGE', } export enum PdfPageSpread { EVEN = 'EVEN', ODD = 'ODD', } export enum CbxFitMode { ACTUAL_SIZE = 'ACTUAL_SIZE', FIT_PAGE = 'FIT_PAGE', FIT_WIDTH = 'FIT_WIDTH', FIT_HEIGHT = 'FIT_HEIGHT', AUTO = 'AUTO' } export enum CbxScrollMode { PAGINATED = 'PAGINATED', INFINITE = 'INFINITE' } export interface EpubReaderSetting { theme: string; font: string; fontSize: number; flow: string; spread: string; lineHeight: number; margin: number; letterSpacing: number; customFontId?: number | null; } export interface CbxReaderSetting { pageSpread: CbxPageSpread; pageViewMode: CbxPageViewMode; fitMode: CbxFitMode; scrollMode?: CbxScrollMode; backgroundColor?: CbxBackgroundColor; } export interface NewPdfReaderSetting { pageSpread: PdfPageSpread; pageViewMode: PdfPageViewMode; } export interface TableColumnPreference { field: string; visible: boolean; order: number; } export interface UserSettings { perBookSetting: PerBookSetting; pdfReaderSetting: PdfReaderSetting; epubReaderSetting: EpubReaderSetting; cbxReaderSetting: CbxReaderSetting; newPdfReaderSetting: NewPdfReaderSetting; sidebarLibrarySorting: SidebarLibrarySorting; sidebarShelfSorting: SidebarShelfSorting; sidebarMagicShelfSorting: SidebarMagicShelfSorting; filterMode: BookFilterMode; filterSortingMode: FilterSortingMode; metadataCenterViewMode: 'route' | 'dialog'; enableSeriesView: boolean; entityViewPreferences: EntityViewPreferences; tableColumnPreference?: TableColumnPreference[]; dashboardConfig?: DashboardConfig; koReaderEnabled: boolean; autoSaveMetadata: boolean; } export interface User { id: number; username: string; name: string; email: string; assignedLibraries: Library[]; permissions: { admin: boolean; canUpload: boolean; canDownload: boolean; canEmailBook: boolean; canDeleteBook: boolean; canEditMetadata: boolean; canManageLibrary: boolean; canManageMetadataConfig: boolean; canSyncKoReader: boolean; canSyncKobo: boolean; canAccessOpds: boolean; canAccessBookdrop: boolean; canAccessLibraryStats: boolean; canAccessUserStats: boolean; canAccessTaskManager: boolean; canManageEmailConfig: boolean; canManageGlobalPreferences: boolean; canManageIcons: boolean; canManageFonts: boolean; demoUser: boolean; canBulkAutoFetchMetadata: boolean; canBulkCustomFetchMetadata: boolean; canBulkEditMetadata: boolean; canBulkRegenerateCover: boolean; canMoveOrganizeFiles: boolean; canBulkLockUnlockMetadata: boolean; canBulkResetBookloreReadProgress?: boolean; canBulkResetKoReaderReadProgress?: boolean; canBulkResetBookReadStatus?: boolean; }; userSettings: UserSettings; provisioningMethod?: 'LOCAL' | 'OIDC' | 'REMOTE'; } export interface UserState { user: User | null; loaded: boolean; error: string | null; } export interface UserUpdateRequest { name?: string; email?: string; permissions?: User['permissions']; assignedLibraries?: number[]; } @Injectable({ providedIn: 'root' }) export class UserService { private readonly apiUrl = `${API_CONFIG.BASE_URL}/api/v1/auth/register`; private readonly userUrl = `${API_CONFIG.BASE_URL}/api/v1/users`; private http = inject(HttpClient); private authService = inject(AuthService); userStateSubject = new BehaviorSubject({ user: null, loaded: false, error: null, }); private loading$: Observable | null = null; constructor() { this.authService.token$.pipe( distinctUntilChanged() ).subscribe(token => { if (token === null) { this.userStateSubject.next({ user: null, loaded: true, error: null, }); this.loading$ = null; } else { const current = this.userStateSubject.value; if (current.loaded && !current.user) { this.userStateSubject.next({ user: null, loaded: false, error: null, }); this.loading$ = null; } } }); } userState$ = this.userStateSubject.asObservable().pipe( tap(state => { if (!state.loaded && !state.error && !this.loading$) { this.loading$ = this.fetchMyself().pipe( shareReplay(1), finalize(() => (this.loading$ = null)) ); this.loading$.subscribe(); } }) ); private fetchMyself(): Observable { return this.http.get(`${this.userUrl}/me`).pipe( tap(user => this.userStateSubject.next({user, loaded: true, error: null})), catchError(err => { const curr = this.userStateSubject.value; this.userStateSubject.next({user: curr.user, loaded: true, error: err.message}); throw err; }) ); } public setInitialUser(user: User): void { this.userStateSubject.next({user, loaded: true, error: null}); } getCurrentUser(): User | null { return this.userStateSubject.value.user; } getMyself(): Observable { return this.http.get(`${this.userUrl}/me`); } createUser(userData: Omit): Observable { return this.http.post(this.apiUrl, userData); } getUsers(): Observable { return this.http.get(this.userUrl); } updateUser(userId: number, updateData: UserUpdateRequest): Observable { return this.http.put(`${this.userUrl}/${userId}`, updateData); } deleteUser(userId: number): Observable { return this.http.delete(`${this.userUrl}/${userId}`); } changeUserPassword(userId: number, newPassword: string): Observable { const payload = { userId: userId, newPassword: newPassword }; return this.http.put(`${this.userUrl}/change-user-password`, payload).pipe( catchError((error) => { const errorMessage = error?.error?.message || 'An unexpected error occurred. Please try again.'; return throwError(() => new Error(errorMessage)); }) ); } changePassword(currentPassword: string, newPassword: string): Observable { const payload = { currentPassword: currentPassword, newPassword: newPassword }; return this.http.put(`${this.userUrl}/change-password`, payload).pipe( catchError((error) => { const errorMessage = error?.error?.message || 'An unexpected error occurred. Please try again.'; return throwError(() => new Error(errorMessage)); }) ); } updateUserSetting(userId: number, key: string, value: unknown): void { const payload = { key, value }; this.http.put(`${this.userUrl}/${userId}/settings`, payload, { headers: {'Content-Type': 'application/json'}, responseType: 'text' as 'json' }).subscribe(() => { const currentState = this.userStateSubject.value; if (currentState.user) { const updatedSettings = {...currentState.user.userSettings, [key]: value}; const updatedUser = {...currentState.user, userSettings: updatedSettings}; this.userStateSubject.next({...currentState, user: updatedUser}); } }); } }