Implement OPDS v2 to return user-specific book feeds
This commit is contained in:
committed by
Aditya Chandel
parent
4cff107a67
commit
443dcad59a
@@ -0,0 +1,200 @@
|
||||
<div class="main-container enclosing-container">
|
||||
<div class="settings-header">
|
||||
<h2 class="settings-title">
|
||||
<i class="pi pi-server"></i>
|
||||
OPDS Settings
|
||||
</h2>
|
||||
<p class="settings-description">
|
||||
Manage your OPDS credentials and control how your book collection is shared with reading apps.
|
||||
By default, all libraries you have access to will be included in the OPDS feed.
|
||||
</p>
|
||||
</div>
|
||||
|
||||
@if (hasPermission) {
|
||||
<div class="settings-content">
|
||||
<div class="endpoint-section">
|
||||
<h3 class="section-title">
|
||||
<i class="pi pi-link"></i>
|
||||
OPDS Endpoint
|
||||
</h3>
|
||||
<div class="endpoint-form">
|
||||
<div class="endpoint-field">
|
||||
<input
|
||||
id="endpoint-url"
|
||||
fluid
|
||||
class="endpoint-input"
|
||||
type="text"
|
||||
pInputText
|
||||
[value]="opdsEndpoint"
|
||||
readonly/>
|
||||
<p-button
|
||||
icon="pi pi-copy"
|
||||
severity="info"
|
||||
outlined
|
||||
size="small"
|
||||
(onClick)="copyEndpoint()">
|
||||
</p-button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="users-section">
|
||||
<div class="section-header">
|
||||
<div class="section-title-group">
|
||||
<h3 class="section-title">
|
||||
<i class="pi pi-users"></i>
|
||||
OPDS Users
|
||||
</h3>
|
||||
<p-button
|
||||
icon="pi pi-plus"
|
||||
label="Add User"
|
||||
severity="success"
|
||||
size="small"
|
||||
(onClick)="showCreateUserDialog = true">
|
||||
</p-button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="table-card">
|
||||
<p-table
|
||||
[value]="users"
|
||||
[paginator]="users.length > 10"
|
||||
[rows]="10"
|
||||
[showCurrentPageReport]="true"
|
||||
currentPageReportTemplate="Showing {first} to {last} of {totalRecords} users">
|
||||
<ng-template pTemplate="header">
|
||||
<tr>
|
||||
<th>
|
||||
<div class="header-content">
|
||||
<i class="pi pi-user"></i>
|
||||
<span>Username</span>
|
||||
</div>
|
||||
</th>
|
||||
<th>
|
||||
<div class="header-content">
|
||||
<i class="pi pi-key"></i>
|
||||
<span>Password</span>
|
||||
</div>
|
||||
</th>
|
||||
<th class="actions-header">
|
||||
<div class="header-content">
|
||||
<i class="pi pi-cog"></i>
|
||||
<span>Actions</span>
|
||||
</div>
|
||||
</th>
|
||||
</tr>
|
||||
</ng-template>
|
||||
<ng-template pTemplate="body" let-user let-rowIndex="rowIndex">
|
||||
<tr>
|
||||
<td>
|
||||
<div class="user-info">
|
||||
<div class="user-avatar">
|
||||
{{ user.username.charAt(0).toUpperCase() }}
|
||||
</div>
|
||||
<span class="username">{{ user.username }}</span>
|
||||
</div>
|
||||
</td>
|
||||
<td>
|
||||
<div class="flex items-center gap-2">
|
||||
<p-password
|
||||
fluid
|
||||
class="w-32 md:w-56"
|
||||
[(ngModel)]="dummyPassword"
|
||||
[feedback]="false"
|
||||
size="small"
|
||||
[disabled]="true"
|
||||
[toggleMask]="false">
|
||||
</p-password>
|
||||
<i
|
||||
class="pi pi-info-circle text-gray-400"
|
||||
pTooltip="Passwords are hidden for security reasons. To change, delete the user and create a new one with a new password."
|
||||
tooltipPosition="right"
|
||||
style="cursor: pointer;">
|
||||
</i>
|
||||
</div>
|
||||
</td>
|
||||
<td class="actions-cell">
|
||||
<p-button
|
||||
icon="pi pi-trash"
|
||||
severity="danger"
|
||||
size="small"
|
||||
[outlined]="true"
|
||||
[rounded]="true"
|
||||
(onClick)="confirmDelete(user)"
|
||||
pTooltip="Delete user">
|
||||
</p-button>
|
||||
</td>
|
||||
</tr>
|
||||
</ng-template>
|
||||
<ng-template pTemplate="emptymessage">
|
||||
<tr>
|
||||
<td colspan="3">
|
||||
<div class="empty-message">
|
||||
<i class="pi pi-users"></i>
|
||||
<p class="empty-title">No users found</p>
|
||||
<p class="empty-subtitle">Create your first OPDS user to get started</p>
|
||||
</div>
|
||||
</td>
|
||||
</tr>
|
||||
</ng-template>
|
||||
</p-table>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<p-dialog
|
||||
header="Create New User"
|
||||
[(visible)]="showCreateUserDialog"
|
||||
[modal]="true"
|
||||
styleClass="user-dialog"
|
||||
[style]="{width: '400px'}">
|
||||
<div class="dialog-form">
|
||||
<div class="form-field">
|
||||
<label for="username">Username</label>
|
||||
<input
|
||||
id="username"
|
||||
type="text"
|
||||
pInputText
|
||||
[(ngModel)]="newUser.username"
|
||||
placeholder="Enter username"/>
|
||||
</div>
|
||||
<div class="form-field">
|
||||
<label for="password">Password</label>
|
||||
<input
|
||||
id="password"
|
||||
type="password"
|
||||
pInputText
|
||||
[(ngModel)]="newUser.password"
|
||||
placeholder="Enter password"/>
|
||||
</div>
|
||||
</div>
|
||||
<ng-template pTemplate="footer">
|
||||
<div class="dialog-actions">
|
||||
<p-button
|
||||
label="Cancel"
|
||||
severity="secondary"
|
||||
outlined="true"
|
||||
(onClick)="cancelCreateUser()">
|
||||
</p-button>
|
||||
<p-button
|
||||
label="Create"
|
||||
severity="success"
|
||||
[disabled]="!newUser.username || !newUser.password"
|
||||
(onClick)="createUser()">
|
||||
</p-button>
|
||||
</div>
|
||||
</ng-template>
|
||||
</p-dialog>
|
||||
|
||||
<p-confirmDialog></p-confirmDialog>
|
||||
} @else {
|
||||
<div class="px-4 py-4 rounded-lg bg-red-700/30 border border-red-600 text-red-200 flex items-center gap-2 max-w-lg">
|
||||
<i class="pi pi-lock text-red-400 text-xl"></i>
|
||||
<span>
|
||||
Access to OPDS is restricted.
|
||||
<br>
|
||||
Please contact your administrator to request permission.
|
||||
</span>
|
||||
</div>
|
||||
}
|
||||
</div>
|
||||
@@ -0,0 +1,268 @@
|
||||
.main-container {
|
||||
width: 100%;
|
||||
padding: 1rem;
|
||||
height: calc(100dvh - 10.5rem);
|
||||
overflow-y: auto;
|
||||
border-width: 1px;
|
||||
border-radius: 0.5rem;
|
||||
|
||||
@media (min-width: 768px) {
|
||||
height: calc(100dvh - 11.65rem);
|
||||
}
|
||||
}
|
||||
|
||||
.enclosing-container {
|
||||
border-color: var(--p-content-border-color);
|
||||
background: var(--p-content-background);
|
||||
}
|
||||
|
||||
.p-datatable th .header-content {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: flex-start;
|
||||
gap: 0.5rem;
|
||||
}
|
||||
|
||||
.p-datatable td {
|
||||
text-align: left;
|
||||
vertical-align: middle;
|
||||
}
|
||||
|
||||
.user-info {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 0.5rem;
|
||||
}
|
||||
|
||||
.table-card {
|
||||
border: 1px solid var(--p-content-border-color);
|
||||
border-radius: 8px;
|
||||
overflow: hidden;
|
||||
background: var(--p-content-background);
|
||||
}
|
||||
|
||||
.p-datatable {
|
||||
.p-datatable-table {
|
||||
border-collapse: separate;
|
||||
border-spacing: 0;
|
||||
}
|
||||
|
||||
.p-datatable-thead > tr > th {
|
||||
background: var(--p-surface-100);
|
||||
border-bottom: 2px solid var(--p-content-border-color);
|
||||
padding: 1rem;
|
||||
font-weight: 600;
|
||||
color: var(--p-text-color);
|
||||
}
|
||||
|
||||
.p-datatable-tbody > tr {
|
||||
border-bottom: 1px solid var(--p-surface-border);
|
||||
transition: background-color 0.2s;
|
||||
|
||||
&:hover {
|
||||
background: var(--p-surface-50);
|
||||
}
|
||||
|
||||
&:last-child {
|
||||
border-bottom: none;
|
||||
}
|
||||
}
|
||||
|
||||
.p-datatable-tbody > tr > td {
|
||||
padding: 1rem;
|
||||
border: none;
|
||||
}
|
||||
}
|
||||
|
||||
.user-avatar {
|
||||
width: 32px;
|
||||
height: 32px;
|
||||
border-radius: 50%;
|
||||
background: var(--p-primary-color);
|
||||
color: white;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
font-weight: 600;
|
||||
font-size: 0.875rem;
|
||||
}
|
||||
|
||||
.username {
|
||||
font-weight: 500;
|
||||
}
|
||||
|
||||
.password-field {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 0.5rem;
|
||||
}
|
||||
|
||||
.password-hidden, .password-visible {
|
||||
font-family: monospace;
|
||||
min-width: 80px;
|
||||
}
|
||||
|
||||
.actions-cell {
|
||||
text-align: center;
|
||||
}
|
||||
|
||||
.empty-message {
|
||||
text-align: center;
|
||||
padding: 2rem 1rem;
|
||||
color: var(--p-text-muted-color);
|
||||
|
||||
.pi {
|
||||
font-size: 3rem;
|
||||
margin-bottom: 1rem;
|
||||
color: var(--p-surface-400);
|
||||
}
|
||||
|
||||
.empty-title {
|
||||
font-size: 1.125rem;
|
||||
font-weight: 600;
|
||||
margin-bottom: 0.5rem;
|
||||
}
|
||||
|
||||
.empty-subtitle {
|
||||
font-size: 0.875rem;
|
||||
}
|
||||
}
|
||||
|
||||
.user-dialog {
|
||||
.p-dialog-header {
|
||||
border-bottom: 1px solid var(--p-surface-border);
|
||||
}
|
||||
|
||||
.p-dialog-content {
|
||||
padding: 1.5rem;
|
||||
}
|
||||
|
||||
.p-dialog-footer {
|
||||
border-top: 1px solid var(--p-surface-border);
|
||||
padding: 1rem 1.5rem;
|
||||
}
|
||||
}
|
||||
|
||||
.dialog-form {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 1.5rem;
|
||||
}
|
||||
|
||||
.form-field {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 0.5rem;
|
||||
|
||||
label {
|
||||
font-weight: 600;
|
||||
color: var(--p-text-color);
|
||||
font-size: 0.875rem;
|
||||
}
|
||||
|
||||
input {
|
||||
width: 100%;
|
||||
padding: 0.75rem;
|
||||
transition: border-color 0.2s;
|
||||
|
||||
&:focus {
|
||||
outline: none;
|
||||
border-color: var(--p-primary-color);
|
||||
box-shadow: 0 0 0 2px var(--p-primary-color-alpha-20);
|
||||
}
|
||||
|
||||
&::placeholder {
|
||||
color: var(--p-text-muted-color);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
.dialog-actions {
|
||||
display: flex;
|
||||
justify-content: flex-end;
|
||||
gap: 0.75rem;
|
||||
}
|
||||
|
||||
.settings-header {
|
||||
margin-top: 1rem;
|
||||
margin-bottom: 2rem;
|
||||
border-bottom: 1px solid var(--p-surface-border);
|
||||
}
|
||||
|
||||
.settings-title {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 0.75rem;
|
||||
font-size: 1.25rem;
|
||||
font-weight: 700;
|
||||
color: var(--p-text-color);
|
||||
margin: 0 0 0.75rem 0;
|
||||
|
||||
.pi {
|
||||
color: var(--p-primary-color);
|
||||
font-size: 1.25rem;
|
||||
}
|
||||
}
|
||||
|
||||
.settings-description {
|
||||
color: var(--p-text-muted-color);
|
||||
font-size: 0.875rem;
|
||||
line-height: 1.5;
|
||||
}
|
||||
|
||||
.settings-content {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 1rem;
|
||||
}
|
||||
|
||||
.endpoint-section {
|
||||
background: var(--p-content-background);
|
||||
border: 1px solid var(--p-surface-border);
|
||||
border-radius: 8px;
|
||||
|
||||
@media (min-width: 768px) {
|
||||
padding: 1rem;
|
||||
}
|
||||
}
|
||||
|
||||
.section-title {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 0.5rem;
|
||||
font-size: 1.125rem;
|
||||
font-weight: 600;
|
||||
color: var(--p-text-color);
|
||||
|
||||
.pi {
|
||||
color: var(--p-primary-color);
|
||||
}
|
||||
}
|
||||
|
||||
.endpoint-form {
|
||||
margin-top: 1rem;
|
||||
}
|
||||
|
||||
.endpoint-field {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 0.5rem;
|
||||
}
|
||||
|
||||
.endpoint-input {
|
||||
min-width: 9rem;
|
||||
max-width: 50rem;
|
||||
}
|
||||
|
||||
.users-section {
|
||||
@media (min-width: 768px) {
|
||||
padding: 1rem;
|
||||
}
|
||||
|
||||
.section-title-group {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: space-between;
|
||||
margin-bottom: 1rem;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,150 @@
|
||||
import {Component, inject, OnDestroy, OnInit} from '@angular/core';
|
||||
import {CommonModule} from '@angular/common';
|
||||
import {Button} from 'primeng/button';
|
||||
import {InputText} from 'primeng/inputtext';
|
||||
import {API_CONFIG} from '../../config/api-config';
|
||||
import {Tooltip} from 'primeng/tooltip';
|
||||
import {TableModule} from 'primeng/table';
|
||||
import {Dialog} from 'primeng/dialog';
|
||||
import {FormsModule} from '@angular/forms';
|
||||
import {ConfirmDialog} from 'primeng/confirmdialog';
|
||||
import {ConfirmationService, MessageService} from 'primeng/api';
|
||||
import {OpdsUserV2, OpdsUserV2CreateRequest, OpdsV2Service} from './opds-v2.service';
|
||||
import {catchError, filter, takeUntil, tap} from 'rxjs/operators';
|
||||
import {UserService} from '../user-management/user.service';
|
||||
import {of, Subject} from 'rxjs';
|
||||
import {Password} from 'primeng/password';
|
||||
|
||||
@Component({
|
||||
selector: 'app-opds-settings-v2',
|
||||
imports: [
|
||||
CommonModule,
|
||||
Button,
|
||||
InputText,
|
||||
Tooltip,
|
||||
Dialog,
|
||||
FormsModule,
|
||||
ConfirmDialog,
|
||||
TableModule,
|
||||
Password
|
||||
],
|
||||
providers: [ConfirmationService, MessageService],
|
||||
templateUrl: './opds-settings-v2.html',
|
||||
styleUrl: './opds-settings-v2.scss'
|
||||
})
|
||||
export class OpdsSettingsV2 implements OnInit, OnDestroy {
|
||||
|
||||
opdsEndpoint = `${API_CONFIG.BASE_URL}/api/v1/opds/catalog`;
|
||||
|
||||
private opdsService = inject(OpdsV2Service);
|
||||
private confirmationService = inject(ConfirmationService);
|
||||
private messageService = inject(MessageService);
|
||||
private userService = inject(UserService);
|
||||
|
||||
users: OpdsUserV2[] = [];
|
||||
loading = false;
|
||||
showCreateUserDialog = false;
|
||||
newUser: OpdsUserV2CreateRequest = {username: '', password: ''};
|
||||
passwordVisibility: boolean[] = [];
|
||||
hasPermission = false;
|
||||
|
||||
private readonly destroy$ = new Subject<void>();
|
||||
dummyPassword: string = "***********************";
|
||||
|
||||
ngOnInit(): void {
|
||||
this.loading = true;
|
||||
|
||||
this.userService.userState$.pipe(
|
||||
filter(state => !!state?.user && state.loaded),
|
||||
takeUntil(this.destroy$),
|
||||
tap(state => {
|
||||
this.hasPermission = !!(state.user?.permissions.canAccessOpds || state.user?.permissions.admin);
|
||||
}),
|
||||
filter(() => this.hasPermission),
|
||||
tap(() => this.loadUsers())
|
||||
).subscribe();
|
||||
}
|
||||
|
||||
private loadUsers(): void {
|
||||
this.opdsService.getUser().pipe(
|
||||
takeUntil(this.destroy$),
|
||||
catchError(err => {
|
||||
console.error('Error loading users:', err);
|
||||
this.showMessage('error', 'Error', 'Failed to load users');
|
||||
return of([]);
|
||||
})
|
||||
).subscribe(users => {
|
||||
this.users = users;
|
||||
this.passwordVisibility = new Array(users.length).fill(false);
|
||||
this.loading = false;
|
||||
});
|
||||
}
|
||||
|
||||
createUser(): void {
|
||||
if (!this.newUser.username || !this.newUser.password) return;
|
||||
|
||||
this.opdsService.createUser(this.newUser).pipe(
|
||||
takeUntil(this.destroy$),
|
||||
catchError(err => {
|
||||
console.error('Error creating user:', err);
|
||||
this.showMessage('error', 'Error', 'Failed to create user');
|
||||
return of(null);
|
||||
})
|
||||
).subscribe(user => {
|
||||
if (!user) return;
|
||||
this.users.push(user);
|
||||
this.resetCreateUserDialog();
|
||||
this.showMessage('success', 'Success', 'User created successfully');
|
||||
});
|
||||
}
|
||||
|
||||
confirmDelete(user: OpdsUserV2): void {
|
||||
this.confirmationService.confirm({
|
||||
message: `Are you sure you want to delete user "${user.username}"?`,
|
||||
header: 'Delete Confirmation',
|
||||
icon: 'pi pi-exclamation-triangle',
|
||||
acceptButtonStyleClass: 'p-button-danger',
|
||||
accept: () => this.deleteUser(user)
|
||||
});
|
||||
}
|
||||
|
||||
deleteUser(user: OpdsUserV2): void {
|
||||
if (!user.id) return;
|
||||
|
||||
this.opdsService.deleteCredential(user.id).pipe(
|
||||
takeUntil(this.destroy$),
|
||||
catchError(err => {
|
||||
console.error('Error deleting user:', err);
|
||||
this.showMessage('error', 'Error', 'Failed to delete user');
|
||||
return of(null);
|
||||
})
|
||||
).subscribe(() => {
|
||||
this.users = this.users.filter(u => u.id !== user.id);
|
||||
this.showMessage('success', 'Success', 'User deleted successfully');
|
||||
});
|
||||
}
|
||||
|
||||
cancelCreateUser(): void {
|
||||
this.resetCreateUserDialog();
|
||||
}
|
||||
|
||||
copyEndpoint(): void {
|
||||
navigator.clipboard.writeText(this.opdsEndpoint).then(() => {
|
||||
this.showMessage('success', 'Copied', 'OPDS endpoint copied to clipboard');
|
||||
});
|
||||
}
|
||||
|
||||
private resetCreateUserDialog(): void {
|
||||
this.showCreateUserDialog = false;
|
||||
this.newUser = {username: '', password: ''};
|
||||
}
|
||||
|
||||
private showMessage(severity: string, summary: string, detail: string): void {
|
||||
this.messageService.add({severity, summary, detail});
|
||||
}
|
||||
|
||||
ngOnDestroy(): void {
|
||||
this.destroy$.next();
|
||||
this.destroy$.complete();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
import {inject, Injectable} from '@angular/core';
|
||||
import {HttpClient} from '@angular/common/http';
|
||||
import {Observable} from 'rxjs';
|
||||
import {API_CONFIG} from '../../config/api-config';
|
||||
|
||||
export interface OpdsUserV2CreateRequest {
|
||||
username: string;
|
||||
password: string;
|
||||
}
|
||||
|
||||
export interface OpdsUserV2 {
|
||||
id: number;
|
||||
userId: number;
|
||||
username: string;
|
||||
}
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root'
|
||||
})
|
||||
export class OpdsV2Service {
|
||||
|
||||
private readonly baseUrl = `${API_CONFIG.BASE_URL}/api/v2/opds-users`;
|
||||
private http = inject(HttpClient);
|
||||
|
||||
getUser(): Observable<OpdsUserV2[]> {
|
||||
return this.http.get<OpdsUserV2[]>(this.baseUrl);
|
||||
}
|
||||
|
||||
createUser(user: OpdsUserV2CreateRequest): Observable<OpdsUserV2> {
|
||||
return this.http.post<OpdsUserV2>(this.baseUrl, user);
|
||||
}
|
||||
|
||||
deleteCredential(id: number): Observable<void> {
|
||||
return this.http.delete<void>(`${this.baseUrl}/${id}`);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user