Skip to content

Commit

Permalink
A few fixes for the caching logic and isValidVersion
Browse files Browse the repository at this point in the history
  • Loading branch information
majora2007 committed Jan 4, 2025
1 parent fe45232 commit 23beb95
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 28 deletions.
1 change: 1 addition & 0 deletions API/Extensions/ApplicationServiceExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ public static void AddApplicationServices(this IServiceCollection services, ICon
{
options.UseInMemory(EasyCacheProfiles.Favicon);
options.UseInMemory(EasyCacheProfiles.License);
options.UseInMemory(EasyCacheProfiles.LicenseInfo);
options.UseInMemory(EasyCacheProfiles.Library);
options.UseInMemory(EasyCacheProfiles.RevokedJwt);

Expand Down
5 changes: 3 additions & 2 deletions API/Services/Plus/LicenseService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -268,12 +268,13 @@ public async Task<bool> ResetLicense(string license, string email)

// Fill out the ValidVersion
var releases = await versionUpdaterService.GetAllReleases(3);
response.IsValidVersion = releases.Any(r => r.UpdateVersion == BuildInfo.Version.ToString());
response.IsValidVersion = releases.Where(r => !r.IsPrerelease)
.Any(r => r.UpdateVersion == BuildInfo.Version.ToString());

response.HasLicense = hasLicense;

// Cache if the license is valid here as well
var licenseProvider = cachingProviderFactory.GetCachingProvider(EasyCacheProfiles.LicenseInfo);
var licenseProvider = cachingProviderFactory.GetCachingProvider(EasyCacheProfiles.License);
await licenseProvider.SetAsync(CacheKey, response.IsActive, _licenseCacheTimeout);

// Cache the license info if IsActive and ExpirationDate > DateTime.UtcNow + 2
Expand Down
10 changes: 7 additions & 3 deletions UI/Web/src/app/_services/license.service.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
import { Injectable } from '@angular/core';
import {inject, Injectable} from '@angular/core';
import {HttpClient} from "@angular/common/http";
import {catchError, map, ReplaySubject, tap, throwError} from "rxjs";
import {catchError, map, of, ReplaySubject, tap, throwError} from "rxjs";
import {environment} from "../../environments/environment";
import { TextResonse } from '../_types/text-response';
import {LicenseInfo} from "../_models/kavitaplus/license-info";
import {translate} from "@jsverse/transloco";
import {ConfirmService} from "../shared/confirm.service";

@Injectable({
providedIn: 'root'
})
export class LicenseService {
private readonly confirmService = inject(ConfirmService);
private readonly httpClient = inject(HttpClient);

baseUrl = environment.apiUrl;

private readonly hasValidLicenseSource = new ReplaySubject<boolean>(1);
Expand All @@ -17,7 +22,6 @@ export class LicenseService {
*/
public readonly hasValidLicense$ = this.hasValidLicenseSource.asObservable();

constructor(private httpClient: HttpClient) {}

/**
* Delete the license from the server and update hasValidLicenseSource to false
Expand Down
49 changes: 26 additions & 23 deletions UI/Web/src/app/admin/license/license.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +118,11 @@
</div>
</form>

<div class="setting-section-break"></div>


@if (hasLicense) {
<div class="setting-section-break"></div>

<div class="row g-0 mt-3">
@if (licenseInfo) {
<h3 class="container-fluid">{{t('info-title')}}</h3>
Expand All @@ -130,7 +132,7 @@ <h3 class="container-fluid">{{t('info-title')}}</h3>
@if (isChecking) {
{{null | defaultValue}}
} @else {
<i class="fas {{hasValidLicense ? 'fa-check-circle' : 'fa-circle-xmark'}}">
<i class="fas {{hasValidLicense ? 'fa-check-circle' : 'fa-circle-xmark error'}}">
<span class="visually-hidden">{{hasValidLicense ? t('valid') : t('invalid')}]</span>
</i>
}
Expand All @@ -141,24 +143,29 @@ <h3 class="container-fluid">{{t('info-title')}}</h3>
<div class="mb-2">
<app-setting-item [canEdit]="false" [showEdit]="false" [title]="t('supported-version-label')">
<ng-template #view>
<i class="fas {{licenseInfo.isVersionValid ? 'fa-check-circle' : 'fa-circle-xmark'}}">
<i class="fas {{licenseInfo.isVersionValid ? 'fa-check-circle' : 'fa-circle-xmark error'}}">
<span class="visually-hidden">{{isVersionValid ? t('valid') : t('invalid')}]</span>
</i>
</ng-template>
</app-setting-item>
</div>

<app-setting-item [canEdit]="false" [showEdit]="false" [title]="t('expiration-label')">
<ng-template #view>
{{licenseInfo.expirationDate | utcToLocalTime | defaultValue}}
</ng-template>
</app-setting-item>
<div class="mb-2">
<app-setting-item [canEdit]="false" [showEdit]="false" [title]="t('expiration-label')">
<ng-template #view>
{{licenseInfo.expirationDate | utcToLocalTime | defaultValue}}
</ng-template>
</app-setting-item>
</div>

<div class="mb-2">
<app-setting-item [canEdit]="false" [showEdit]="false" [title]="t('total-subbed-months-label')">
<ng-template #view>
{{licenseInfo.totalMonthsSubbed | number}}
</ng-template>
</app-setting-item>
</div>

<app-setting-item [canEdit]="false" [showEdit]="false" [title]="t('total-subbed-months-label')">
<ng-template #view>
{{licenseInfo.totalMonthsSubbed | number}}
</ng-template>
</app-setting-item>

<app-setting-item [canEdit]="false" [showEdit]="false" [title]="t('email-label')">
<ng-template #view>
Expand All @@ -173,6 +180,7 @@ <h3 class="container-fluid">{{t('info-title')}}</h3>
</app-setting-item>
}


<div class="setting-section-break"></div>

<!-- Actions around license -->
Expand All @@ -186,17 +194,12 @@ <h3 class="container-fluid">{{t('actions-title')}}</h3>
</app-setting-button>
</div>

<!-- TODO: Check if we can make this hasLicense only -->
@if (hasLicense) {
<div class="mt-2 mb-2">
<app-setting-button [subtitle]="t('manage-tooltip')">
<a class="btn btn-primary btn-sm mt-1" [href]="manageLink" target="_blank" rel="noreferrer nofollow">{{t('manage')}}</a>
</app-setting-button>
</div>
}
<div class="mt-2 mb-2">
<app-setting-button [subtitle]="t('manage-tooltip')">
<a class="btn btn-primary btn-sm mt-1" [href]="manageLink" target="_blank" rel="noreferrer nofollow">{{t('manage')}}</a>
</app-setting-button>
</div>

</div>

<!-- <button class="btn btn-secondary" (click)="toggleViewMode()">Toggle View Mode</button>-->
}
</ng-container>
3 changes: 3 additions & 0 deletions UI/Web/src/app/admin/license/license.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ export class LicenseComponent implements OnInit {
if (err['error'][0] === '{') {
this.toastr.error(JSON.parse(err['error']));
} else {

this.toastr.error(err['error']);
}
} else {
Expand Down Expand Up @@ -168,6 +169,8 @@ export class LicenseComponent implements OnInit {
switchMap(_ => this.licenseService.licenseInfo(forceCheck)),
tap(licenseInfo => {
this.licenseInfo = licenseInfo;
this.hasValidLicense = licenseInfo?.isActive || false;
this.isChecking = false;
this.cdRef.markForCheck();
})
)
Expand Down

0 comments on commit 23beb95

Please sign in to comment.