import {inject, Injectable, Injector} from '@angular/core'; import {HttpClient} from '@angular/common/http'; import {BehaviorSubject, Observable, throwError} from 'rxjs'; import {API_CONFIG} from '../../config/api-config'; import {RxStompService} from '../../shared/websocket/rx-stomp.service'; import {Library} from '../../book/model/library.model'; import {catchError} from 'rxjs/operators'; import {CbxPageSpread, CbxPageViewMode, PdfPageSpread, PdfPageViewMode} from '../../book/model/book.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'; entityId: number; preferences: EntityViewPreference; } export interface SidebarLibrarySorting { field: string; order: string; } export interface SidebarShelfSorting { field: string; order: string; } export interface PerBookSetting { pdf: string; epub: string; cbx: string; } export type PageSpread = 'off' | 'even' | 'odd'; export interface PdfReaderSetting { pageSpread: PageSpread; pageZoom: string; showSidebar: boolean; } export interface EpubReaderSetting { theme: string; font: string; fontSize: number; flow: string; lineHeight: number; margin: number; letterSpacing: number; } export interface CbxReaderSetting { pageSpread: CbxPageSpread; pageViewMode: CbxPageViewMode; } 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; filterSortingMode: 'alphabetical' | 'count'; metadataCenterViewMode: 'route' | 'dialog'; entityViewPreferences: EntityViewPreferences; tableColumnPreference?: TableColumnPreference[]; } 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; canManipulateLibrary: boolean; }; userSettings: UserSettings; provisioningMethod?: 'LOCAL' | 'OIDC' | 'REMOTE'; } @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 injector = inject(Injector); private rxStompService?: RxStompService; private userStateSubject = new BehaviorSubject(null); userState$ = this.userStateSubject.asObservable(); constructor() { this.getMyself().subscribe(user => { this.userStateSubject.next(user); this.startWebSocket(); }); } getCurrentUser(): User | null { return this.userStateSubject.getValue(); } 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: Partial): 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: any): void { const payload = { key, value }; this.http.put(`${this.userUrl}/${userId}/settings`, payload, { headers: {'Content-Type': 'application/json'}, responseType: 'text' as 'json' }).subscribe(() => { const currentUser = this.userStateSubject.getValue(); if (currentUser) { const updatedSettings = {...currentUser.userSettings, [key]: value}; const updatedUser = {...currentUser, settings: updatedSettings}; this.userStateSubject.next(updatedUser); } }); } private startWebSocket(): void { const token = this.getToken(); if (token) { const rxStompService = this.getRxStompService(); rxStompService.activate(); } } private getRxStompService(): RxStompService { if (!this.rxStompService) { this.rxStompService = this.injector.get(RxStompService); } return this.rxStompService; } getToken(): string | null { return localStorage.getItem('accessToken'); } }