Files
grimmory/booklore-ui/src/app/settings/user-management/user.service.ts
T

200 lines
5.4 KiB
TypeScript

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';
import {filter, take} from 'rxjs/operators';
import {AuthService} from '../../core/service/auth.service';
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[];
koReaderEnabled: 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;
canManipulateLibrary: boolean;
canSyncKoReader: 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 userStateSubject = new BehaviorSubject<User | null>(null);
userState$ = this.userStateSubject.asObservable();
public setInitialUser(user: User): void {
this.userStateSubject.next(user);
}
getCurrentUser(): User | null {
return this.userStateSubject.getValue();
}
getMyself(): Observable<User> {
return this.http.get<User>(`${this.userUrl}/me`);
}
createUser(userData: Omit<User, 'id'>): Observable<void> {
return this.http.post<void>(this.apiUrl, userData);
}
getUsers(): Observable<User[]> {
return this.http.get<User[]>(this.userUrl);
}
updateUser(userId: number, updateData: Partial<User>): Observable<User> {
return this.http.put<User>(`${this.userUrl}/${userId}`, updateData);
}
deleteUser(userId: number): Observable<void> {
return this.http.delete<void>(`${this.userUrl}/${userId}`);
}
changeUserPassword(userId: number, newPassword: string): Observable<void> {
const payload = {
userId: userId,
newPassword: newPassword
};
return this.http.put<void>(`${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<void> {
const payload = {
currentPassword: currentPassword,
newPassword: newPassword
};
return this.http.put<void>(`${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<void>(`${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);
}
});
}
}