Kavita+ Reset License & Discord Integration (#2516)

This commit is contained in:
Joe Milazzo 2024-01-04 14:53:15 -06:00 committed by GitHub
parent 138794ffed
commit c37596889a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
15 changed files with 230 additions and 86 deletions

View file

@ -15,6 +15,7 @@ import { AgeRating } from '../_models/metadata/age-rating';
import { AgeRestriction } from '../_models/metadata/age-restriction';
import { TextResonse } from '../_types/text-response';
import {takeUntilDestroyed} from "@angular/core/rxjs-interop";
import {ToastrService} from "ngx-toastr";
export enum Role {
Admin = 'Admin',
@ -30,6 +31,8 @@ export enum Role {
export class AccountService {
private readonly destroyRef = inject(DestroyRef);
private readonly toastr = inject(ToastrService);
baseUrl = environment.apiUrl;
userKey = 'kavita-user';
public static lastLoginKey = 'kavita-lastlogin';
@ -88,6 +91,10 @@ export class AccountService {
return this.httpClient.delete<string>(this.baseUrl + 'license', TextResonse);
}
resetLicense(license: string, email: string) {
return this.httpClient.post<string>(this.baseUrl + 'license/reset', {license, email}, TextResonse);
}
hasValidLicense(forceCheck: boolean = false) {
return this.httpClient.get<string>(this.baseUrl + 'license/valid-license?forceCheck=' + forceCheck, TextResonse)
.pipe(
@ -109,8 +116,8 @@ export class AccountService {
);
}
updateUserLicense(license: string, email: string) {
return this.httpClient.post<string>(this.baseUrl + 'license', {license, email}, TextResonse)
updateUserLicense(license: string, email: string, discordId?: string) {
return this.httpClient.post<string>(this.baseUrl + 'license', {license, email, discordId}, TextResonse)
.pipe(map(res => res === "true"));
}

View file

@ -46,6 +46,13 @@ enum TabID {
})
export class DashboardComponent implements OnInit {
private readonly cdRef = inject(ChangeDetectorRef);
protected readonly route = inject(ActivatedRoute);
protected readonly navService = inject(NavService);
private readonly titleService = inject(Title);
protected readonly TabID = TabID;
tabs: Array<{title: string, fragment: string}> = [
{title: 'general-tab', fragment: TabID.General},
{title: 'users-tab', fragment: TabID.Users},
@ -59,14 +66,8 @@ export class DashboardComponent implements OnInit {
];
active = this.tabs[0];
private readonly cdRef = inject(ChangeDetectorRef);
private readonly translocoService = inject(TranslocoService);
get TabID() {
return TabID;
}
constructor(public route: ActivatedRoute, private titleService: Title, public navService: NavService) {
constructor() {
this.route.fragment.subscribe(frag => {
const tab = this.tabs.filter(item => item.fragment === frag);
if (tab.length > 0) {

View file

@ -7,16 +7,15 @@
<h4 id="license-key-header">{{t('title')}}</h4>
</div>
<div class="col-2 text-end">
<ng-container *ngIf="hasLicense; else noLicense">
<ng-container *ngIf="hasValidLicense; else invalidLicenseBuy">
@if (hasLicense) {
@if (hasValidLicense) {
<a class="btn btn-primary btn-sm me-1" [href]="manageLink" target="_blank" rel="noreferrer nofollow">{{t('manage')}}</a>
</ng-container>
<ng-template #invalidLicenseBuy>
} @else {
<a class="btn btn-primary btn-sm me-1"
[ngbTooltip]="t('invalid-license-tooltip')"
href="mailto:kavitareader@gmail.com?subject=Kavita+Subscription+Renewal&body=Description%3A%0D%0A%0D%0ALicense%20Key%3A%0D%0A%0D%0AYour%20Email%3A"
>{{t('renew')}}</a>
</ng-template>
}
<button class="btn btn-secondary btn-sm me-1" style="width: 58px" (click)="validateLicense()">
<span *ngIf="!isChecking">{{t('check')}}</span>
<app-loading [loading]="isChecking" size="spinner-border-sm"></app-loading>
@ -25,16 +24,15 @@
<span *ngIf="!isViewMode">{{t('cancel')}}</span>
<span *ngIf="isViewMode">{{t('edit')}}</span>
</button>
</ng-container>
<ng-template #noLicense>
} @else {
<a class="btn btn-secondary btn-sm me-1" [href]="buyLink" target="_blank" rel="noreferrer nofollow">{{t('buy')}}</a>
<button class="btn btn-primary btn-sm" (click)="toggleViewMode()">{{isViewMode ? t('activate') : t('cancel')}}</button>
</ng-template>
}
</div>
</div>
</div>
<ng-container *ngIf="isViewMode">
@if (isViewMode) {
<div class="container-fluid row">
<span class="col-12">
<ng-container *ngIf="hasLicense; else noToken">
@ -57,7 +55,8 @@
<ng-template #noToken>{{t('no-license-key')}}</ng-template>
</span>
</div>
</ng-container>
}
<div #collapse="ngbCollapse" [(ngbCollapse)]="isViewMode">
<form [formGroup]="formGroup">
@ -70,12 +69,23 @@
<label for="email">{{t('activate-email-label')}}</label>
<input id="email" type="email" class="form-control" formControlName="email" autocomplete="off"/>
</div>
<div class="form-group mb-3">
<label for="discordId">{{t('activate-discordId-label')}}</label><i class="fa fa-circle-info ms-1" aria-hidden="true" [ngbTooltip]="t('activate-discordId-tooltip')"></i>
<input id="discordId" type="text" class="form-control" formControlName="discordId" autocomplete="off"/>
</div>
</form>
<div class="col-auto d-flex d-md-block justify-content-sm-center text-md-end mb-3">
<button type="button" class="flex-fill btn btn-danger me-1" aria-describedby="license-key-header" (click)="deleteLicense()">
<button type="button" class="flex-fill btn btn-danger me-1" aria-describedby="license-key-header"
(click)="deleteLicense()">
{{t('activate-delete')}}
</button>
<button type="submit" class="flex-fill btn btn-primary" aria-describedby="license-key-header" [disabled]="!formGroup.get('email')?.value || !formGroup.get('licenseKey')?.value" (click)="saveForm()">
<button type="button" class="flex-fill btn btn-danger me-1" aria-describedby="license-key-header"
[ngbTooltip]="t('activate-reset--tooltip')"
[disabled]="!formGroup.get('email')?.value || !formGroup.get('licenseKey')?.value" (click)="resetLicense()">
{{t('activate-reset')}}
</button>
<button type="submit" class="flex-fill btn btn-primary" aria-describedby="license-key-header"
[disabled]="!formGroup.get('email')?.value || !formGroup.get('licenseKey')?.value" (click)="saveForm()">
<span *ngIf="!isSaving">{{t('activate-save')}}</span>
<app-loading [loading]="isSaving" size="spinner-border-sm"></app-loading>
</button>

View file

@ -1,7 +1,7 @@
import {
ChangeDetectionStrategy,
ChangeDetectorRef,
Component,
Component, inject,
OnInit
} from '@angular/core';
import { FormControl, FormGroup, Validators, ReactiveFormsModule } from "@angular/forms";
@ -14,17 +14,23 @@ import { NgbTooltip, NgbCollapse } from '@ng-bootstrap/ng-bootstrap';
import { NgIf } from '@angular/common';
import {environment} from "../../../environments/environment";
import {translate, TranslocoDirective} from "@ngneat/transloco";
import {catchError} from "rxjs";
@Component({
selector: 'app-license',
templateUrl: './license.component.html',
styleUrls: ['./license.component.scss'],
changeDetection: ChangeDetectionStrategy.OnPush,
standalone: true,
selector: 'app-license',
templateUrl: './license.component.html',
styleUrls: ['./license.component.scss'],
changeDetection: ChangeDetectionStrategy.OnPush,
standalone: true,
imports: [NgIf, NgbTooltip, LoadingComponent, NgbCollapse, ReactiveFormsModule, TranslocoDirective]
})
export class LicenseComponent implements OnInit {
private readonly cdRef = inject(ChangeDetectorRef);
private readonly toastr = inject(ToastrService);
private readonly confirmService = inject(ConfirmService);
protected readonly accountService = inject(AccountService);
formGroup: FormGroup = new FormGroup({});
isViewMode: boolean = true;
@ -38,13 +44,11 @@ export class LicenseComponent implements OnInit {
constructor(public accountService: AccountService, private scrobblingService: ScrobblingService,
private toastr: ToastrService, private readonly cdRef: ChangeDetectorRef,
private confirmService: ConfirmService) { }
ngOnInit(): void {
this.formGroup.addControl('licenseKey', new FormControl('', [Validators.required]));
this.formGroup.addControl('email', new FormControl('', [Validators.required]));
this.formGroup.addControl('discordId', new FormControl('', []));
this.accountService.hasAnyLicense().subscribe(res => {
this.hasLicense = res;
this.cdRef.markForCheck();
@ -59,13 +63,14 @@ export class LicenseComponent implements OnInit {
resetForm() {
this.formGroup.get('licenseKey')?.setValue('');
this.formGroup.get('email')?.setValue('');
this.formGroup.get('discordId')?.setValue('');
this.cdRef.markForCheck();
}
saveForm() {
this.isSaving = true;
this.cdRef.markForCheck();
this.accountService.updateUserLicense(this.formGroup.get('licenseKey')!.value.trim(), this.formGroup.get('email')!.value.trim())
this.accountService.updateUserLicense(this.formGroup.get('licenseKey')!.value.trim(), this.formGroup.get('email')!.value.trim(), this.formGroup.get('discordId')!.value.trim())
.subscribe(() => {
this.accountService.hasValidLicense(true).subscribe(isValid => {
this.hasValidLicense = isValid;
@ -81,13 +86,13 @@ export class LicenseComponent implements OnInit {
this.cdRef.markForCheck();
});
}, err => {
this.isSaving = false;
this.cdRef.markForCheck();
if (err.hasOwnProperty('error')) {
this.toastr.error(JSON.parse(err['error'])['message']);
this.toastr.error(JSON.parse(err['error']));
} else {
this.toastr.error(translate('toasts.k+-error'));
}
this.isSaving = false;
this.cdRef.markForCheck();
});
}
@ -101,7 +106,16 @@ export class LicenseComponent implements OnInit {
this.toggleViewMode();
this.validateLicense();
});
}
async resetLicense() {
if (!await this.confirmService.confirm(translate('toasts.k+-reset-key'))) {
return;
}
this.accountService.resetLicense(this.formGroup.get('licenseKey')!.value.trim(), this.formGroup.get('email')!.value.trim()).subscribe(() => {
this.toastr.success(translate('toasts.k+-reset-key-success'));
});
}

View file

@ -206,6 +206,8 @@ export class SideNavComponent implements OnInit {
}
showLess() {
this.filterQuery = '';
this.cdRef.markForCheck();
this.showAllSubject.next(false);
}

View file

@ -590,7 +590,11 @@
"activate-description": "Enter the License Key and Email used to register with Stripe",
"activate-license-label": "License Key",
"activate-email-label": "{{common.email}}",
"activate-delete": "Delete",
"activate-discordId-label": "Discord UserId",
"activate-discordId-tooltip": "Link your Discord Account with Kavita+. This grants you access to hidden channels to help shape Kavita",
"activate-delete": "{{common.delete}}",
"activate-reset": "{{common.reset}}",
"activate-reset--tooltip": "Untie your license with this server. Requires both License and Email.",
"activate-save": "{{common.save}}"
},
@ -1955,6 +1959,8 @@
"k+-unlocked": "Kavita+ unlocked!",
"k+-error": "There was an error when activating your license. Please try again.",
"k+-delete-key": "This will only delete Kavita's license key and allow a buy link to show. This will not cancel your subscription! Use this only if directed by support!",
"k+-reset-key": "This will untie your key from a server and allow you to re-register a Kavita instance.",
"k+-reset-key-success": "Your license has been un-registered. Use Edit button to re-register your instance and re-activate Kavita+",
"library-deleted": "Library {{name}} has been removed",
"copied-to-clipboard": "Copied to clipboard",
"book-settings-info": "You can modify book settings, save those settings for all books, and view table of contents from the drawer.",