From 49f2043f42ebe7bf7d19a04cde588f4559e1e480 Mon Sep 17 00:00:00 2001 From: Dani Palou Date: Tue, 4 May 2021 13:18:58 +0200 Subject: [PATCH] MOBILE-3713 login: Migrate login cron handler --- src/addons/notes/services/notes-sync.ts | 6 +- .../comments/services/comments-sync.ts | 6 +- src/core/features/login/login.module.ts | 14 ++++- .../features/login/services/handlers/cron.ts | 58 +++++++++++++++++++ 4 files changed, 77 insertions(+), 7 deletions(-) create mode 100644 src/core/features/login/services/handlers/cron.ts diff --git a/src/addons/notes/services/notes-sync.ts b/src/addons/notes/services/notes-sync.ts index 29dbefe92..2df96b2ce 100644 --- a/src/addons/notes/services/notes-sync.ts +++ b/src/addons/notes/services/notes-sync.ts @@ -45,17 +45,17 @@ export class AddonNotesSyncProvider extends CoreSyncBaseProvider { - return this.syncOnSites('all notes', this.syncAllNotesFunc.bind(this, siteId, force), siteId); + return this.syncOnSites('all notes', this.syncAllNotesFunc.bind(this, !!force), siteId); } /** * Synchronize all the notes in a certain site * - * @param siteId Site ID to sync. * @param force Wether to force sync not depending on last execution. + * @param siteId Site ID to sync. * @return Promise resolved if sync is successful, rejected if sync fails. */ - protected async syncAllNotesFunc(siteId: string, force: boolean): Promise { + protected async syncAllNotesFunc(force: boolean, siteId: string): Promise { const notesArray = await Promise.all([ AddonNotesOffline.getAllNotes(siteId), AddonNotesOffline.getAllDeletedNotes(siteId), diff --git a/src/core/features/comments/services/comments-sync.ts b/src/core/features/comments/services/comments-sync.ts index 1c91862d6..1055c8c4b 100644 --- a/src/core/features/comments/services/comments-sync.ts +++ b/src/core/features/comments/services/comments-sync.ts @@ -44,17 +44,17 @@ export class CoreCommentsSyncProvider extends CoreSyncBaseProvider { - return this.syncOnSites('all comments', this.syncAllCommentsFunc.bind(this, siteId, force), siteId); + return this.syncOnSites('all comments', this.syncAllCommentsFunc.bind(this, !!force), siteId); } /** * Synchronize all the comments in a certain site * - * @param siteId Site ID to sync. * @param force Wether to force sync not depending on last execution. + * @param siteId Site ID to sync. * @return Promise resolved if sync is successful, rejected if sync fails. */ - private async syncAllCommentsFunc(siteId: string, force: boolean): Promise { + private async syncAllCommentsFunc(force: boolean, siteId: string): Promise { const comments = await CoreCommentsOffline.getAllComments(siteId); const commentsUnique: { [syncId: string]: (CoreCommentsDBRecord | CoreCommentsDeletedDBRecord) } = {}; diff --git a/src/core/features/login/login.module.ts b/src/core/features/login/login.module.ts index 75a8e77c7..235541150 100644 --- a/src/core/features/login/login.module.ts +++ b/src/core/features/login/login.module.ts @@ -12,12 +12,14 @@ // See the License for the specific language governing permissions and // limitations under the License. -import { NgModule } from '@angular/core'; +import { APP_INITIALIZER, NgModule } from '@angular/core'; import { Routes } from '@angular/router'; import { AppRoutingModule } from '@/app/app-routing.module'; import { CoreLoginHelperProvider } from './services/login-helper'; import { CoreRedirectGuard } from '@guards/redirect'; +import { CoreLoginCronHandler } from './services/handlers/cron'; +import { CoreCronDelegate } from '@services/cron'; export const CORE_LOGIN_SERVICES = [ CoreLoginHelperProvider, @@ -34,5 +36,15 @@ const appRoutes: Routes = [ @NgModule({ imports: [AppRoutingModule.forChild(appRoutes)], + providers: [ + { + provide: APP_INITIALIZER, + multi: true, + deps: [], + useFactory: () => async () => { + CoreCronDelegate.register(CoreLoginCronHandler.instance); + }, + }, + ], }) export class CoreLoginModule {} diff --git a/src/core/features/login/services/handlers/cron.ts b/src/core/features/login/services/handlers/cron.ts new file mode 100644 index 000000000..826306c60 --- /dev/null +++ b/src/core/features/login/services/handlers/cron.ts @@ -0,0 +1,58 @@ +// (C) Copyright 2015 Moodle Pty Ltd. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +import { Injectable } from '@angular/core'; +import { CoreSitePublicConfigResponse } from '@classes/site'; +import { CoreCronHandler } from '@services/cron'; +import { CoreSites } from '@services/sites'; +import { CoreUtils } from '@services/utils/utils'; +import { makeSingleton } from '@singletons'; + +/** + * Cron handler to log out sites when does not meet the app requirements. + */ +@Injectable({ providedIn: 'root' }) +export class CoreLoginCronHandlerService implements CoreCronHandler { + + name = 'CoreLoginCronHandler'; + + /** + * @inheritdoc + */ + async execute(siteId?: string): Promise { + siteId = siteId || CoreSites.getCurrentSiteId(); + if (!siteId) { + return; + } + + // Check logged in site minimun required version. + // Do not check twice in the same 10 minutes. + const site = await CoreSites.getSite(siteId); + + const config = await CoreUtils.ignoreErrors(site.getPublicConfig(), > {}); + + CoreUtils.ignoreErrors(CoreSites.checkApplication( config)); + } + + /** + * @inheritdoc + */ + isSync(): boolean { + // Defined to true to be checked on sync site. + return true; + } + +} + +export const CoreLoginCronHandler = makeSingleton(CoreLoginCronHandlerService);