diff --git a/src/app/app.component.ts b/src/app/app.component.ts index e9f0ab290..fb16efba9 100644 --- a/src/app/app.component.ts +++ b/src/app/app.component.ts @@ -10,6 +10,7 @@ import { ThemeService } from 'src/app/services/theme.service'; import { environment } from 'src/environments/environment'; import { Storage } from '@ionic/storage'; import { ChatController } from './controller/chat'; +import { LoggingInterceptorService } from 'src/app/services/logging-interceptor.service' const CUSTOM_DATE_FORMATS: NgxMatDateFormats = { parse: { @@ -38,35 +39,26 @@ export class AppComponent { private statusBar: StatusBar, public ThemeService: ThemeService, private storage: Storage, - private ChatSystemService: ChatSystemService + private ChatSystemService: ChatSystemService, + private LoggingInterceptorService: LoggingInterceptorService ) { + this.initializeApp(); this.storage.set('version', environment.version).then(() => {}) - ChatController.ChatSystemService = this.ChatSystemService + } initializeApp() { this.platform.ready().then(() => { this.statusBar.styleDefault(); - /* this.splashScreen.hide(); */ - + if (this.platform.is("tablet")) { window.screen.orientation.unlock(); } else if( this.platform.is("mobile")) { window.screen.orientation.lock('portrait'); } - if (this.platform.is('desktop') || this.platform.is('mobileweb')) { - - } else { - try { - // this.sqliteservice.databaseConn(); - } catch (error) { - - } - } - }); } } diff --git a/src/app/app.module.ts b/src/app/app.module.ts index 41e994d7e..c0db000d6 100644 --- a/src/app/app.module.ts +++ b/src/app/app.module.ts @@ -7,7 +7,7 @@ import { StatusBar } from '@ionic-native/status-bar/ngx'; import { AppRoutingModule } from './app-routing.module'; import { AppComponent } from './app.component'; -import { HttpClientModule } from '@angular/common/http'; +import { HTTP_INTERCEPTORS, HttpClientModule } from '@angular/common/http'; import { InAppBrowser } from '@ionic-native/in-app-browser/ngx'; @@ -78,6 +78,7 @@ import * as Sentry from '@sentry/capacitor'; import { Integration } from '@sentry/types'; import { BrowserTracing } from '@sentry/tracing'; import { EditorModule } from '@tinymce/tinymce-angular'; +import { LoggingInterceptorService } from './services/logging-interceptor.service'; // import { ServiceWorkerModule } from '@angular/service-worker'; // import { AngularFireModule } from '@angular/fire'; @@ -194,6 +195,7 @@ import { FirebaseX } from '@ionic-native/firebase-x/ngx'; */ NgxExtendedPdfViewerModule, FileOpener, DocumentViewer, + { provide: HTTP_INTERCEPTORS, useClass: LoggingInterceptorService, multi: true }, ], bootstrap: [AppComponent], diff --git a/src/app/services/background.service.ts b/src/app/services/background.service.ts index ca5021972..7a6958674 100644 --- a/src/app/services/background.service.ts +++ b/src/app/services/background.service.ts @@ -21,7 +21,14 @@ export class BackgroundService { private themeservice: ThemeService, private storageservice: StorageService, private http: HttpClient, - ) { } + ) { + + window.addEventListener('focus', (event) => { + if(this.status == 'offline') { + this.tryToReachTheServer() + } + }); + } online() { if(this.status == 'online') { @@ -68,28 +75,36 @@ export class BackgroundService { return false } + const hasReachedTheServer = await this.tryToReachTheServer() + + if(!hasReachedTheServer) { + this.status = 'offline' + document.body.style.setProperty(`--color`, "#ffb703"); + document.body.style.setProperty(`--color2`, "#ffb703"); + document.body.style.setProperty(`--color3`, "#ffb703"); + document.body.style.setProperty(`--color4`, "#ffb703"); + document.body.style.setProperty(`--color5`, "#ffb703"); + this.storageservice.store('networkCheckStore','offline'); + this.callBacks.forEach((e) => { + if (e.type == 'Offline') { + e.funx() + } + }) + } + } + + + async tryToReachTheServer() { let opts = { headers: {}, } try { await this.http.post(environment.apiURL + "UserAuthentication/Login", '', opts).toPromise(); + return true } catch (error) { if(error.status != 400) { - - this.status = 'offline' - document.body.style.setProperty(`--color`, "#ffb703"); - document.body.style.setProperty(`--color2`, "#ffb703"); - document.body.style.setProperty(`--color3`, "#ffb703"); - document.body.style.setProperty(`--color4`, "#ffb703"); - document.body.style.setProperty(`--color5`, "#ffb703"); - this.storageservice.store('networkCheckStore','offline'); - this.callBacks.forEach((e) => { - if (e.type == 'Offline') { - e.funx() - } - }) - + return false } } } diff --git a/src/app/services/chat/rochet-chat-connector.service.ts b/src/app/services/chat/rochet-chat-connector.service.ts index 37ec3f90c..4f6b49ccd 100644 --- a/src/app/services/chat/rochet-chat-connector.service.ts +++ b/src/app/services/chat/rochet-chat-connector.service.ts @@ -5,6 +5,7 @@ import { deepFind } from 'src/plugin/deep' import { environment } from 'src/environments/environment'; import { SessionStore } from 'src/app/store/session.service'; import { chatHistory, Rooms } from 'src/app/models/chatMethod'; +import { BackgroundService } from '../background.service'; @Injectable({ providedIn: 'root' @@ -14,7 +15,7 @@ export class RochetChatConnectorService { isLogin = false; loginResponse = {} - constructor() { + constructor(private backgroundservice: BackgroundService,) { } connect() { @@ -752,6 +753,7 @@ export class RochetChatConnectorService { }, onopen: async ()=> { this.ws.connected = true + this.backgroundservice.online(); setTimeout(()=>{ this.ws.wsMsgQueue() @@ -804,6 +806,9 @@ export class RochetChatConnectorService { }, onmessage: async (event: any)=> { + + this.backgroundservice.online(); + const data = JSON.parse(event.data) for (const [key, value] of Object.entries(this.wsCallbacks)) { diff --git a/src/app/services/logging-interceptor.service.spec.ts b/src/app/services/logging-interceptor.service.spec.ts new file mode 100644 index 000000000..481c7c387 --- /dev/null +++ b/src/app/services/logging-interceptor.service.spec.ts @@ -0,0 +1,16 @@ +import { TestBed } from '@angular/core/testing'; + +import { LoggingInterceptorService } from './logging-interceptor.service'; + +describe('LoggingInterceptorService', () => { + let service: LoggingInterceptorService; + + beforeEach(() => { + TestBed.configureTestingModule({}); + service = TestBed.inject(LoggingInterceptorService); + }); + + it('should be created', () => { + expect(service).toBeTruthy(); + }); +}); diff --git a/src/app/services/logging-interceptor.service.ts b/src/app/services/logging-interceptor.service.ts new file mode 100644 index 000000000..bcd311290 --- /dev/null +++ b/src/app/services/logging-interceptor.service.ts @@ -0,0 +1,50 @@ +import { HttpHandler, HttpRequest, HttpResponse } from '@angular/common/http'; +import { Injectable } from '@angular/core'; +import { finalize, tap } from 'rxjs/operators'; +import { BackgroundService } from './background.service'; + +@Injectable({ + providedIn: 'root' +}) +export class LoggingInterceptorService { + + constructor( + private backgroundservice: BackgroundService, + ) {} + + intercept(req: HttpRequest, next: HttpHandler) { + const started = Date.now(); + let ok: string; + + // extend server response observable with logging + return next.handle(req) + .pipe( + tap({ + // Succeeds when there is a response; ignore other events + next: (event) => { + + ok = event instanceof HttpResponse ? 'succeeded' : '' + + if(ok == 'succeeded' || (typeof event['status'] == 'number' && event['status'] !=0)) { + // set to online + this.backgroundservice.online(); + } + + }, + // Operation failed; error is an HttpErrorResponse + error: (error) => { + if (error.status === 0 && error.error instanceof ProgressEvent && !req.url.includes('UserAuthentication/Login')) { + // A client-side or network error occurred. Handle it accordingly. + this.backgroundservice.offline(); + } + } + }), + // Log when response observable either completes or errors + finalize(() => { + // const elapsed = Date.now() - started; + // const msg = `${req.method} "${req.urlWithParams}" + // ${ok} in ${elapsed} ms.`; + }) + ); + } +} diff --git a/version/git-version.ts b/version/git-version.ts index bfd57f53f..0e44a09d5 100644 --- a/version/git-version.ts +++ b/version/git-version.ts @@ -1,12 +1,12 @@ export let versionData = { - "shortSHA": "2fdffbecf", - "SHA": "2fdffbecf975e72161646c5a6e94bf2041a81cf5", - "branch": "feature/gabinete-search", + "shortSHA": "12178d6c3", + "SHA": "12178d6c3a15b71e7f1e30ed721b685025f2e4d7", + "branch": "feature/interceptor", "lastCommitAuthor": "'Peter Maquiran'", - "lastCommitTime": "'Mon Jul 10 15:09:04 2023 +0100'", - "lastCommitMessage": "feature/gabinete-search", - "lastCommitNumber": "5045", + "lastCommitTime": "'Tue Jul 11 10:25:16 2023 +0100'", + "lastCommitMessage": "add model", + "lastCommitNumber": "5046", "change": "", - "changeStatus": "On branch feature/gabinete-search\nChanges to be committed:\n (use \"git restore --staged ...\" to unstage)\n\tnew file: src/app/models/agenda/AgendaModels.ts\n\tmodified: src/app/models/beast-orm.ts\n\tmodified: src/app/pages/events/events.page.ts\n\tmodified: src/app/pages/publications/publications.page.ts\n\tmodified: src/app/pages/publications/view-publications/view-publications.page.ts\n\tmodified: src/app/services/processes.service.ts\n\tmodified: src/app/shared/publication/view-publications/view-publications.page.ts\n\tmodified: src/plugin/src/models/model.js\n\tmodified: src/plugin/src/tsconfig.tsbuildinfo", + "changeStatus": "On branch feature/interceptor\nChanges to be committed:\n (use \"git restore --staged ...\" to unstage)\n\tmodified: src/app/app.component.ts\n\tmodified: src/app/app.module.ts\n\tmodified: src/app/services/background.service.ts\n\tmodified: src/app/services/chat/rochet-chat-connector.service.ts\n\tnew file: src/app/services/logging-interceptor.service.spec.ts\n\tnew file: src/app/services/logging-interceptor.service.ts\n\tmodified: version/git-version.ts", "changeAuthor": "peter.maquiran" } \ No newline at end of file