From 7ca179f3e816b49dbae9846b1b943f660ab1fa60 Mon Sep 17 00:00:00 2001 From: "argiro.kokogiannaki" Date: Thu, 12 Nov 2020 15:41:03 +0000 Subject: [PATCH] [Library|Trunk] code clean up - Connect related: -remove unused imports, code, files (Old search pages for results, dataproviders, map search page, etc) -remove Freeguard from modules -unsubscribe all subscriptions - remove old properties json files git-svn-id: https://svn.driver.research-infrastructures.eu/driver/dnet40/modules/uoa-services-library/trunk/ng-openaire-library/src/app@59825 d315682c-612b-4755-9ff5-7f18f6832af3 --- connect/affiliations/affiliation.service.ts | 15 ++++-- .../approvedByCommunity/approved.component.ts | 1 - connect/communities/communities.service.ts | 14 +++-- connect/community/community.service.ts | 15 ++++-- .../communityGuard/connectSubscriber.guard.ts | 54 ++++++------------- connect/communityGuard/isCommunityOrAdmin.ts | 16 +++++- connect/projects/searchProjects.service.ts | 1 - connect/results/resultInfo.ts | 2 + .../mailPrefs.component.ts | 50 +++++++++-------- .../userEmailPreferences/mailPrefs.service.ts | 6 +-- .../searchZenodoCommunities.service.ts | 1 - .../zenodo-communities.service.ts | 3 -- 12 files changed, 90 insertions(+), 88 deletions(-) diff --git a/connect/affiliations/affiliation.service.ts b/connect/affiliations/affiliation.service.ts index cd7d522a..bd65e472 100644 --- a/connect/affiliations/affiliation.service.ts +++ b/connect/affiliations/affiliation.service.ts @@ -1,6 +1,6 @@ import {Injectable} from '@angular/core'; import {HttpClient, HttpHeaders} from '@angular/common/http'; -import {BehaviorSubject, Observable} from 'rxjs'; +import {BehaviorSubject, Observable, Subscriber} from 'rxjs'; import {Affiliation} from '../../utils/entities/CuratorInfo'; import {properties} from "../../../../environments/environment"; @@ -8,13 +8,20 @@ import {properties} from "../../../../environments/environment"; export class AffiliationService { private affiliationsSubject: BehaviorSubject = new BehaviorSubject([]); - + sub; constructor(private http: HttpClient) { } - + ngOnDestroy() { + this.clearSubscriptions(); + } + clearSubscriptions(){ + if (this.sub instanceof Subscriber) { + this.sub.unsubscribe(); + } + } public initAffiliations(communityId: string): void { let url = properties.communityAPI + communityId + "/organizations"; - this.http.get((properties.useCache) ? (properties.cacheUrl + encodeURIComponent(url)) : url).subscribe((affiliations) => { + this.sub = this.http.get((properties.useCache) ? (properties.cacheUrl + encodeURIComponent(url)) : url).subscribe((affiliations) => { this.affiliationsSubject.next(affiliations); }, error => { diff --git a/connect/approvedByCommunity/approved.component.ts b/connect/approvedByCommunity/approved.component.ts index ed0520aa..470978ab 100644 --- a/connect/approvedByCommunity/approved.component.ts +++ b/connect/approvedByCommunity/approved.component.ts @@ -1,5 +1,4 @@ import { Component, Input } from '@angular/core'; -import { Location } from '@angular/common'; import { ActivatedRoute } from '@angular/router'; import { EnvProperties} from '../../utils/properties/env-properties'; diff --git a/connect/communities/communities.service.ts b/connect/communities/communities.service.ts index 9fff177f..92c5f6fa 100644 --- a/connect/communities/communities.service.ts +++ b/connect/communities/communities.service.ts @@ -3,7 +3,7 @@ import {HttpClient} from "@angular/common/http"; import {CommunityInfo} from '../community/communityInfo'; import {EnvProperties} from '../../utils/properties/env-properties'; -import {BehaviorSubject} from "rxjs"; +import {BehaviorSubject, Subscriber} from "rxjs"; import {map} from "rxjs/operators"; @Injectable() @@ -14,9 +14,17 @@ export class CommunitiesService { constructor(private http: HttpClient) { this.communities = new BehaviorSubject([]); } - + sub; + ngOnDestroy() { + this.clearSubscriptions(); + } + clearSubscriptions(){ + if (this.sub instanceof Subscriber) { + this.sub.unsubscribe(); + } + } updateCommunities(properties: EnvProperties, url: string) { - this.getCommunities(properties, url).subscribe(res => { + this.sub = this.getCommunities(properties, url).subscribe(res => { this.communities.next(res); }, error => { this.communities.error(error); diff --git a/connect/community/community.service.ts b/connect/community/community.service.ts index 55201250..e4b77a80 100644 --- a/connect/community/community.service.ts +++ b/connect/community/community.service.ts @@ -3,7 +3,7 @@ import {HttpClient, HttpHeaders} from "@angular/common/http"; import { CommunityInfo } from './communityInfo'; import {EnvProperties} from '../../utils/properties/env-properties'; import {map} from "rxjs/operators"; -import {BehaviorSubject, from} from "rxjs"; +import {BehaviorSubject, from, Subscriber} from "rxjs"; import {properties} from "../../../../environments/environment"; @Injectable({ providedIn: 'root' }) @@ -15,10 +15,18 @@ export class CommunityService { constructor(private http: HttpClient) { this.community = new BehaviorSubject(null); } - + sub; + ngOnDestroy() { + this.clearSubscriptions(); + } + clearSubscriptions(){ + if (this.sub instanceof Subscriber) { + this.sub.unsubscribe(); + } + } getCommunityByService(properties: EnvProperties, url: string) { this.promise = new Promise(resolve => { - this.getCommunity(properties, url).subscribe(res => { + this.sub = this.getCommunity(properties, url).subscribe(res => { this.community.next(res); resolve(); }, @@ -35,6 +43,7 @@ export class CommunityService { } await this.promise; + this.clearSubscriptions(); return this.community.getValue(); } diff --git a/connect/communityGuard/connectSubscriber.guard.ts b/connect/communityGuard/connectSubscriber.guard.ts index 73b1a07c..bd197eda 100644 --- a/connect/communityGuard/connectSubscriber.guard.ts +++ b/connect/communityGuard/connectSubscriber.guard.ts @@ -1,53 +1,29 @@ -import {filter, map, mergeMap, take, tap} from 'rxjs/operators'; +import {take, tap} from 'rxjs/operators'; import {Injectable} from '@angular/core'; -import {Router, CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot, CanLoad, Route} from '@angular/router'; -import {Observable, of} from 'rxjs'; -import {Session} from '../../login/utils/helper.class'; +import {Router, CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot} from '@angular/router'; +import {Observable} from 'rxjs'; import {LoginErrorCodes} from '../../login/utils/guardHelper.class'; -import {CommunityService} from '../community/community.service'; -import {ConnectHelper} from '../connectHelper'; -import {UserManagementService} from "../../services/user-management.service"; import {SubscribeService} from "../../utils/subscribe/subscribe.service"; import {properties} from "../../../../environments/environment"; @Injectable() export class ConnectSubscriberGuard implements CanActivate { constructor(private router: Router, - private communityService: CommunityService, - private subscribeService: SubscribeService, - private userManagementService: UserManagementService) { + private subscribeService: SubscribeService) { } - - check(community: string, path: string): Observable | boolean { - let errorCode = LoginErrorCodes.NOT_LOGIN; - let email = null; - const subscribed = this.userManagementService.getUserInfo(false).pipe(take(1),map(user => { - if (user) { - errorCode = LoginErrorCodes.NOT_SUBSCRIBER; - email = user.email; - let communityDomain = ConnectHelper.getCommunityFromDomain(properties.domain); - if (communityDomain) { - community = communityDomain; - } - if (Session.isSubscribedTo('community', community, user)) { - return of(true); - } - return this.subscribeService.isSubscribedToCommunity(properties, community).pipe(take(1)); - } else { - return of(false); + + check(community: string, path: string): Observable { + let errorCode = LoginErrorCodes.NOT_SUBSCRIBER; + return this.subscribeService.isSubscribedToCommunity(properties, community).pipe(take(1), tap(subscribed =>{ + if(!subscribed){ + this.router.navigate(['/user-info'], { + queryParams: { + 'errorCode': errorCode, + 'redirectUrl': path + } + }); } - }), mergeMap(authorized => { - return authorized; })); - subscribed.pipe(filter(subscribed => !subscribed)).subscribe(() => { - this.router.navigate(['/user-info'], { - queryParams: { - 'errorCode': errorCode, - 'redirectUrl': path - } - }) - }); - return subscribed; } canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable | boolean { diff --git a/connect/communityGuard/isCommunityOrAdmin.ts b/connect/communityGuard/isCommunityOrAdmin.ts index e82bb931..eea25847 100644 --- a/connect/communityGuard/isCommunityOrAdmin.ts +++ b/connect/communityGuard/isCommunityOrAdmin.ts @@ -1,7 +1,7 @@ import {Injectable} from '@angular/core'; import {ActivatedRouteSnapshot, CanActivate, Router, RouterStateSnapshot} from '@angular/router'; import {Observable} from 'rxjs'; -import {filter, map, mergeMap} from "rxjs/operators"; +import {filter, map, mergeMap, take} from "rxjs/operators"; import {UserManagementService} from "../../services/user-management.service"; import {EnvironmentSpecificService} from "../../utils/properties/environment-specific.service"; import {Session} from "../../login/utils/helper.class"; @@ -13,7 +13,19 @@ export class IsCommunityOrAdmin implements CanActivate { private userManagementService: UserManagementService, private propertiesService: EnvironmentSpecificService) { } - + /* + //TODO add login guard and simplify this method + check(community: string): Observable | boolean { + return this.userManagementService.getUserInfo(false).pipe(take(1),map(user => { + if (community && community !== 'undefined' || Session.isPortalAdministrator(user)) { + return true; + } else { + this.router.navigate(['/errorCommunity']); + return false; + } + })); + } + */ check(community: string): Observable | boolean { if (community && community !== 'undefined') { return true; diff --git a/connect/projects/searchProjects.service.ts b/connect/projects/searchProjects.service.ts index 64c31cd0..c580d190 100644 --- a/connect/projects/searchProjects.service.ts +++ b/connect/projects/searchProjects.service.ts @@ -1,5 +1,4 @@ import {Injectable} from '@angular/core'; -import {Http} from '@angular/http'; import {HttpClient} from "@angular/common/http"; import{EnvProperties} from '../../utils/properties/env-properties'; import {map} from "rxjs/operators"; diff --git a/connect/results/resultInfo.ts b/connect/results/resultInfo.ts index cbead405..be7cd48a 100644 --- a/connect/results/resultInfo.ts +++ b/connect/results/resultInfo.ts @@ -1,3 +1,4 @@ +/* export class ResultInfo { title: string; id: string; @@ -7,3 +8,4 @@ export class ResultInfo { description: string; type: string; } +*/ diff --git a/connect/userEmailPreferences/mailPrefs.component.ts b/connect/userEmailPreferences/mailPrefs.component.ts index 0837a202..84874100 100644 --- a/connect/userEmailPreferences/mailPrefs.component.ts +++ b/connect/userEmailPreferences/mailPrefs.component.ts @@ -1,9 +1,6 @@ -import {Component, ViewChild, Input} from '@angular/core'; +import {Component, Input} from '@angular/core'; import {Location} from '@angular/common'; -import {Observable} from 'rxjs'; import {ActivatedRoute, Router} from '@angular/router'; -import {ModalLoading} from '../../utils/modal/loading.component'; -import {AlertModal} from '../../utils/modal/alert'; import {Session} from '../../login/utils/helper.class'; import {EnvProperties} from '../../utils/properties/env-properties'; import {MailPrefsService} from './mailPrefs.service'; @@ -11,6 +8,8 @@ import {ConnectHelper} from '../connectHelper'; import {ErrorCodes} from '../../utils/properties/errorCodes'; import {ErrorMessagesComponent} from '../../utils/errorMessages.component'; import {LoginErrorCodes} from '../../login/utils/guardHelper.class'; +import {properties} from "../../../../environments/environment"; +import {Subscriber} from "rxjs"; declare var UIkit: any; @@ -22,7 +21,7 @@ declare var UIkit: any; }) export class MailPrefsComponent { properties:EnvProperties; - sub: any; + subscriptions = []; public communityId: string; public preferencesFor: string = "community"; public status: number; @@ -47,19 +46,15 @@ export class MailPrefsComponent { } ngOnInit() { - this.route.data - .subscribe((data: { envSpecific: EnvProperties }) => { - this.properties = data.envSpecific; - this.sub = this.route.queryParams.subscribe(params => { - this.communityId = ConnectHelper.getCommunityFromDomain(this.properties.domain); - if(!this.communityId) { - this.communityId = params['communityId']; - } + this.properties = properties; + this.subscriptions.push(this.route.queryParams.subscribe(params => { + this.communityId = ConnectHelper.getCommunityFromDomain(this.properties.domain); + if(!this.communityId) { + this.communityId = params['communityId']; + } + this.getEmailPreferences(); + })); - this.getEmailPreferences(); - - }); - }); } @@ -75,7 +70,7 @@ export class MailPrefsComponent { if(this.communityId && this.communityId != "openaire") { this.preferencesFor = "community"; - this._mailPrefsService.getUserEmailPreferencesForCommunity(this.communityId, this.properties.claimsAPIURL).subscribe( + this.subscriptions.push(this._mailPrefsService.getUserEmailPreferencesForCommunity(this.communityId, this.properties.claimsAPIURL).subscribe( data => { if(data.code == "204") { this.status = this.errorCodes.NONE; @@ -90,10 +85,10 @@ export class MailPrefsComponent { this.handleErrors(err); this.handleError("Error getting user email preferences for community with id: "+this.communityId, err); } - ); + )); } else { this.preferencesFor = "project"; - this._mailPrefsService.getUserEmailPreferencesForOpenaire(this.properties.claimsAPIURL).subscribe( + this.subscriptions.push(this._mailPrefsService.getUserEmailPreferencesForOpenaire(this.properties.claimsAPIURL).subscribe( data => { if(data.code == "204") { @@ -113,7 +108,7 @@ export class MailPrefsComponent { this.handleErrors(err); this.handleError("Error getting user email preferences for openaire", err); } - ); + )); } } } @@ -160,7 +155,7 @@ export class MailPrefsComponent { this.status = this.errorCodes.LOADING; this.savedMessage = ""; - this._mailPrefsService.saveUserEmailPreferences(this.notifications[index], this.properties.claimsAPIURL).subscribe( + this.subscriptions.push(this._mailPrefsService.saveUserEmailPreferences(this.notifications[index], this.properties.claimsAPIURL).subscribe( data => { this.initialNotifications[index] = JSON.parse(JSON.stringify( this.notifications[index] )); @@ -179,7 +174,7 @@ export class MailPrefsComponent { this.handleError("Error saving user email preferences: "+JSON.stringify(this.notifications[index]), err); this.status = this.errorCodes.NOT_SAVED; } - ); + )); } else { /*UIkit.notification({ @@ -221,11 +216,14 @@ export class MailPrefsComponent { } */ ngOnDestroy() { - if(this.sub) { - this.sub.unsubscribe(); - } + this.subscriptions.forEach(subscription => { + if (subscription instanceof Subscriber) { + subscription.unsubscribe(); + } + }); } + handleErrors(err){ //this.showErrorMessage = true; //try{ diff --git a/connect/userEmailPreferences/mailPrefs.service.ts b/connect/userEmailPreferences/mailPrefs.service.ts index 30da8a8b..5170f76d 100644 --- a/connect/userEmailPreferences/mailPrefs.service.ts +++ b/connect/userEmailPreferences/mailPrefs.service.ts @@ -1,10 +1,6 @@ import {Injectable} from '@angular/core'; -import {Http, RequestOptions, Headers, Response} from '@angular/http'; import {HttpClient, HttpErrorResponse} from "@angular/common/http"; -import {Observable, throwError} from 'rxjs'; -//import { HttpErrorResponse } from '@angular/common/http'; - -import {EnvProperties} from '../../utils/properties/env-properties'; +import {throwError} from 'rxjs'; import { CustomOptions } from '../../services/servicesUtils/customOptions.class'; @Injectable() diff --git a/connect/zenodoCommunities/searchZenodoCommunities.service.ts b/connect/zenodoCommunities/searchZenodoCommunities.service.ts index a8fccb47..02fb8fda 100644 --- a/connect/zenodoCommunities/searchZenodoCommunities.service.ts +++ b/connect/zenodoCommunities/searchZenodoCommunities.service.ts @@ -1,5 +1,4 @@ import {Injectable} from '@angular/core'; -import {Http} from '@angular/http'; import {HttpClient} from "@angular/common/http"; import{EnvProperties} from '../../utils/properties/env-properties'; diff --git a/connect/zenodoCommunities/zenodo-communities.service.ts b/connect/zenodoCommunities/zenodo-communities.service.ts index b64f16d5..bf790f27 100644 --- a/connect/zenodoCommunities/zenodo-communities.service.ts +++ b/connect/zenodoCommunities/zenodo-communities.service.ts @@ -1,8 +1,5 @@ import {Injectable} from '@angular/core'; -import {Http, Response} from '@angular/http'; -import {Headers, RequestOptions} from '@angular/http'; import {HttpClient} from "@angular/common/http"; -import {Observable} from 'rxjs'; import {ZenodoCommunityInfo} from './zenodoCommunityInfo'; import {EnvProperties} from '../../utils/properties/env-properties';