Skip to content

Commit

Permalink
Detect And Alert New Version #138
Browse files Browse the repository at this point in the history
  • Loading branch information
haimkastner committed May 23, 2020
1 parent 61e3133 commit 7e76d49
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 5 deletions.
6 changes: 5 additions & 1 deletion frontend/src/app/core/sidebar/sidebar.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -120,12 +120,16 @@
<table style="margin: auto; text-align: right">
<tr>
<td><b>{{ 'VERSION' | translate }} </b></td>
<td><i dir="ltr">{{ currentVersionName }}</i></td>
<td><i dir="ltr"><a target="_blank" href="https://github.com/casanet/casanet-server/releases/tag/{{currentVersionName}}">{{ currentVersionName }}</a><a target="_blank" href="https://github.com/casanet/casanet-server/commit/{{currentVersionCommitHash}}"> ({{currentVersionCommitHash}})</a></i></td>
</tr>
<tr>
<td><b>{{ 'RELEASE_DATE' | translate }} </b></td>
<td><i>{{ currentVersionReleaseDate }}</i></td>
</tr>
<tr *ngIf="latestVersion">
<td><b style="color: crimson;">{{ 'AVAILABLE_VERSION' | translate }} </b></td>
<td><i dir="ltr"><a target="_blank" href="https://github.com/casanet/casanet-server/releases/tag/{{latestVersion}}">{{latestVersion}}</a></i></td>
</tr>
</table>
</div>
<button mat-icon-button matTooltipPosition="below" matTooltip="{{'UPDATE_TO_THE_LAST_VERSION' | translate}}"
Expand Down
41 changes: 39 additions & 2 deletions frontend/src/app/core/sidebar/sidebar.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ import { TranslatePipe } from '../../translate.pipe';
})
export class SidebarComponent implements OnInit, OnDestroy {

private updateToast: typeof swal;

remoteConnectionSubscription: Subscription;
remoteConnection: RemoteConnectionStatus;
iftttIntegration: boolean;
Expand All @@ -36,8 +38,11 @@ export class SidebarComponent implements OnInit, OnDestroy {
userProfileSubscription: Subscription;

currentVersionName: string;
currentVersionCommitHash: string;
currentVersionReleaseDate: string;

latestVersion = '';
LatestVersionSubscription: Subscription;

private translatePipe: TranslatePipe;

Expand All @@ -48,6 +53,15 @@ export class SidebarComponent implements OnInit, OnDestroy {

this.translatePipe = new TranslatePipe(this.translateService);

this.updateToast = swal.mixin({
toast: true,
position: 'bottom-start',
confirmButtonText: this.translatePipe.transform('UPDATE_NOW'),
cancelButtonText: this.translatePipe.transform('CLOSE'),
showConfirmButton: true,
showCancelButton: true,
timer: 1000 * 60 * 2
});

this.remoteConnectionSubscription =
this.settingsService.remoteStatusFeed.subscribe((remoteConnection) => {
Expand All @@ -58,6 +72,26 @@ export class SidebarComponent implements OnInit, OnDestroy {
this.authService.userProfile.subscribe((userProfile) => {
this.userProfile = userProfile;
});

this.LatestVersionSubscription =
this.settingsService.isUpToDateFeed.subscribe((latestVersion) => {
if (latestVersion) {
setTimeout(async () => {
const update = await this.updateToast({
type: 'info',
title: this.translatePipe.transform('NEW_VERSION_AVAILABLE'),
// tslint:disable-next-line: max-line-length
html: `${this.translatePipe.transform('VERSION')} <a target="_blank" href="https://github.com/casanet/casanet-server/releases/tag/${latestVersion}">${latestVersion}</a> ${this.translatePipe.transform('IS_AVAILABLE')}`,
});

if (!update.dismiss) {
this.updateVertionToLast();
}
}, 1000 * 10);
}
this.latestVersion = latestVersion;
});

this.loadRemoteHostName();
this.loadIftttIntegration();
this.loadCurrentVersion();
Expand All @@ -69,12 +103,14 @@ export class SidebarComponent implements OnInit, OnDestroy {
ngOnDestroy(): void {
this.remoteConnectionSubscription.unsubscribe();
this.userProfileSubscription.unsubscribe();
this.LatestVersionSubscription.unsubscribe();
}

private async loadCurrentVersion() {
try {
const currVersion = await this.settingsService.getCurrentVersion();
this.currentVersionName = `${currVersion.version} (${currVersion.commintHash})`;
this.currentVersionName = currVersion.version;
this.currentVersionCommitHash = currVersion.commintHash;
this.currentVersionReleaseDate = new Date(currVersion.timestamp).toLocaleDateString();
} catch (error) {
this.currentVersionName = 'unknown';
Expand Down Expand Up @@ -255,7 +291,8 @@ export class SidebarComponent implements OnInit, OnDestroy {
text: this.translatePipe.transform('UPDATING_WARNING'),
showLoaderOnConfirm: true,
confirmButtonText: this.translatePipe.transform('UPDATE'),
showCancelButton: false,
cancelButtonText: this.translatePipe.transform('CANCEL'),
showCancelButton: true,
allowOutsideClick: () => !swal.isLoading(),
preConfirm: async () => {
try {
Expand Down
35 changes: 33 additions & 2 deletions frontend/src/app/services/settings.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,47 @@ import {
UpdateResults,
VersionInfo,
VersionUpdateStatus,
ProgressStatus
ProgressStatus,
User
} from '../../../../backend/src/models/sharedInterfaces';
import { HttpClient, HttpResponse, HttpErrorResponse } from '@angular/common/http';
import { DeepCopy } from '../../../../backend/src/utilities/deepCopy';
import { ToasterAndErrorsService } from './toaster-and-errors.service';
import { environment } from '../../environments/environment';
import { AuthService } from './auth.service';

@Injectable({
providedIn: 'root'
})
export class SettingsService {

private readonly DETECT_LATEST_VERSION_ACTIVATION_MS = 1000 * 60 * 60 * 24 * 7; // ms s m h d w

public onlineFeed: BehaviorSubject<boolean> = new BehaviorSubject<boolean>(true);
public isUpToDateFeed: BehaviorSubject<string> = new BehaviorSubject<string>('');
public remoteStatusFeed: BehaviorSubject<RemoteConnectionStatus> = new BehaviorSubject<RemoteConnectionStatus>('notConfigured');

private intervalHandler: NodeJS.Timer;

constructor(private toastrAndErrorsService: ToasterAndErrorsService,
private authService: AuthService,
private httpClient: HttpClient) {
this.onlineAck();
}

authService.userProfile.subscribe(async (user: User) => {
if (this.intervalHandler) {
clearInterval(this.intervalHandler);
}

if (user.scope === 'adminAuth') {
await this.detectIsVersionUpToDate();
this.intervalHandler = setInterval(async () => {
await this.detectIsVersionUpToDate();
},
this.DETECT_LATEST_VERSION_ACTIVATION_MS);
}
});
}

private async onlineAck() {

Expand Down Expand Up @@ -189,6 +209,17 @@ export class SettingsService {
window.open(`${environment.baseUrl}/logs`);
}

public async detectIsVersionUpToDate() {
try {
const latestVersion = await this.httpClient.get<string>(`${environment.baseUrl}/version/is-up-date`, {
withCredentials: true
}).toPromise();
this.isUpToDateFeed.next(latestVersion ? latestVersion : '');
} catch (error) {
console.log(error);
}
}

public async downloadBackup() {
window.open(`${environment.baseUrl}/backup`);
}
Expand Down
5 changes: 5 additions & 0 deletions frontend/src/assets/i18n/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,11 @@
"DOWNLOAD_LOGS" : "Download Logs",
"MUSIC" : "Music",
"DOWNLOAD_BACKUP" : "Download backup copy",
"LATEST_VERSION" : "Latest Version",
"AVAILABLE_VERSION" : "Available Version",
"NEW_VERSION_AVAILABLE" : "New Version Available",
"IS_AVAILABLE" : "is available",
"UPDATE_NOW" : "Update Now",
"notConfigured": "Remote server not configured",
"cantReachRemoteServer": "Remote server offline",
"authorizationFail": "Local server fail to authenticate",
Expand Down
5 changes: 5 additions & 0 deletions frontend/src/assets/i18n/he.json
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,11 @@
"OVERRIDE_LOCKS" : "דרוס נעילות",
"MUSIC" : "מוזיקה",
"DOWNLOAD_BACKUP" : "הורד עותק גיבוי",
"LATEST_VERSION" : "גרסה עדכנית",
"AVAILABLE_VERSION" : "גרסה זמינה",
"NEW_VERSION_AVAILABLE" : "גרסה חדשה זמינה",
"IS_AVAILABLE" : "זמינה",
"UPDATE_NOW" : "עדכן עכשיו",
"notConfigured": "לא הוגדר שרת מרוחק",
"cantReachRemoteServer": "שרת מרוחק לא נגיש",
"authorizationFail": "אימות מול שרת מרוחק נכשל",
Expand Down

0 comments on commit 7e76d49

Please sign in to comment.