MOBILE-3713 login: Migrate login cron handler

main
Dani Palou 2021-05-04 13:18:58 +02:00
parent c99487ef5e
commit 49f2043f42
4 changed files with 77 additions and 7 deletions

View File

@ -45,17 +45,17 @@ export class AddonNotesSyncProvider extends CoreSyncBaseProvider<AddonNotesSyncR
* @return Promise resolved if sync is successful, rejected if sync fails.
*/
syncAllNotes(siteId?: string, force?: boolean): Promise<void> {
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<void> {
protected async syncAllNotesFunc(force: boolean, siteId: string): Promise<void> {
const notesArray = await Promise.all([
AddonNotesOffline.getAllNotes(siteId),
AddonNotesOffline.getAllDeletedNotes(siteId),

View File

@ -44,17 +44,17 @@ export class CoreCommentsSyncProvider extends CoreSyncBaseProvider<CoreCommentsS
* @return Promise resolved if sync is successful, rejected if sync fails.
*/
syncAllComments(siteId?: string, force?: boolean): Promise<void> {
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<void> {
private async syncAllCommentsFunc(force: boolean, siteId: string): Promise<void> {
const comments = await CoreCommentsOffline.getAllComments(siteId);
const commentsUnique: { [syncId: string]: (CoreCommentsDBRecord | CoreCommentsDeletedDBRecord) } = {};

View File

@ -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 {}

View File

@ -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<void> {
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(), <Partial<CoreSitePublicConfigResponse>> {});
CoreUtils.ignoreErrors(CoreSites.checkApplication(<any> config));
}
/**
* @inheritdoc
*/
isSync(): boolean {
// Defined to true to be checked on sync site.
return true;
}
}
export const CoreLoginCronHandler = makeSingleton(CoreLoginCronHandlerService);