import { Injectable } from '@angular/core'; 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 {properties} from "../../../../environments/environment"; @Injectable({ providedIn: 'root' }) export class CommunityService { public community: BehaviorSubject = null; private promise: Promise = null; constructor(private http: HttpClient) { this.community = new BehaviorSubject(null); } getCommunityByService(properties: EnvProperties, url: string) { this.promise = new Promise(resolve => { this.getCommunity(properties, url).subscribe(res => { this.community.next(res); resolve(); }, error => { this.community.error(error); resolve(); }) }); } async getCommunityByStateAsync(properties: EnvProperties, url: string) { if(!this.promise) { this.getCommunityByService(properties, url); } await this.promise; return this.community.getValue(); } getCommunityByState(properties: EnvProperties, url: string) { return from(this.getCommunityByStateAsync(properties, url)); } getCommunity(properties: EnvProperties, url: string) { return this.http.get((properties.useCache) ? (properties.cacheUrl + encodeURIComponent(url)) : url) .pipe(map(res => this.parseCommunity(res))); } updateCommunity(url: string, community: any) { //const headers = new Headers({'Content-Type': 'application/json'}); //const options = new RequestOptions({headers: headers}); const options = { headers: new HttpHeaders({ 'Content-Type': 'application/json', }) }; const body = JSON.stringify(community); return this.http.post(url, body, options); /*.map(res => res.json())*/ } async isCommunityManagerByStateAsync(properties: EnvProperties, url: string, manager: string) { if(!this.promise) { this.getCommunityByService(properties, url); } await this.promise; let community: CommunityInfo = this.community.getValue(); return (community.managers.indexOf(manager) !== -1); } isCommunityManagerByState(properties: EnvProperties, url: string, manager: string) { return from(this.isCommunityManagerByStateAsync(properties, url, manager)); } /** * @deprecated */ isCommunityManager(properties: EnvProperties, url: string, manager: string) { return this.http.get((properties.useCache) ? (properties.cacheUrl + encodeURIComponent(url)) : url) //.map(res => res.json()) .pipe(map(res => this.parseCommunity(res))) .pipe(map(community => community.managers.indexOf(manager) !== -1)); } async isTypeByStateAsync(properties: EnvProperties, url: string, type: string) { if(!this.promise) { this.getCommunityByService(properties, url); } await this.promise; let community: CommunityInfo = this.community.getValue(); return (community && community.type && community.type === type); } isRITypeByState(properties: EnvProperties, url: string) { return from(this.isTypeByStateAsync(properties, url, "ri")); } isCommunityTypeByState(properties: EnvProperties, url: string) { return from(this.isTypeByStateAsync(properties, url, "community")); } /** * @deprecated */ isRIType(properties: EnvProperties, url: string) { return this.http.get((properties.useCache) ? (properties.cacheUrl + encodeURIComponent(url)) : url) //.map(res => res.json()) .pipe(map(res => this.parseCommunity(res))) .pipe(map(community => (community && community.type && community.type === 'ri'))); } /** * @deprecated */ isCommunityType(properties: EnvProperties, url: string) { return this.http.get((properties.useCache) ? (properties.cacheUrl + encodeURIComponent(url)) : url) //.map(res => res.json()) .pipe(map(res => this.parseCommunity(res))) .pipe(map(community => (community && community.type && community.type === 'community'))); } /** * @deprecated */ isSubscribedToCommunity(pid: string, email: string, url: string) { return this.http.get(url + '/'+ properties.adminToolsPortalType +'/' + pid + '/subscribers') //.map(res => ((res === '') ? {} : res.json())) .pipe(map(res => { if (res['subscribers'] && res['subscribers'] != null) { for (let i = 0; i < res['subscribers'].length; i++ ) { if (res['subscribers'][i] != null && res['subscribers'][i].email === email) { return true; } } } return false; })); } private parseCommunity(data: any): CommunityInfo { const resData = Array.isArray(data) ? data[0] : data; const community: CommunityInfo = new CommunityInfo(); community['title'] = resData.name; community['shortTitle'] = resData.shortName; community['communityId'] = resData.id; community['queryId'] = resData.queryId; community['logoUrl'] = resData.logoUrl; community['description'] = resData.description; community['date'] = resData.creationDate; community['zenodoCommunity'] = resData.zenodoCommunity; community['status'] = 'all'; if (resData.hasOwnProperty('status')) { community['status'] = resData.status; const status = ['all', 'hidden', 'manager']; if (status.indexOf(community['status']) === -1) { community['status'] = 'hidden'; } } if (resData.type != null) { community['type'] = resData.type; } if (resData.managers != null) { if (community['managers'] === undefined) { community['managers'] = new Array(); } const managers = resData.managers; const length = Array.isArray(managers) ? managers.length : 1; for (let i = 0; i < length; i++) { const manager = Array.isArray(managers) ? managers[i] : managers; community.managers[i] = manager; } } if (resData.subjects != null) { if (community['subjects'] === undefined) { community['subjects'] = new Array(); } const subjects = resData.subjects; const length = Array.isArray(subjects) ? subjects.length : 1; for (let i = 0; i < length; i++) { const subject = Array.isArray(subjects) ? subjects[i] : subjects; community.subjects[i] = subject; } } return community; } }