import {Injectable} from '@angular/core'; import {HttpClient, HttpHeaders} from "@angular/common/http"; import {CommunityInfo} from './communityInfo'; import {map} from "rxjs/operators"; import {BehaviorSubject, from, Observable, Subscriber} from "rxjs"; import {properties} from "../../../../environments/environment"; import {StringUtils} from "../../utils/string-utils.class"; @Injectable({providedIn: 'root'}) export class CommunityService { public community: BehaviorSubject = new BehaviorSubject(null); public communityId: string = null; private promise: Promise = null; private subs = []; constructor(private http: HttpClient) {} clearSubscriptions() { this.subs.forEach(subscription => { if (subscription instanceof Subscriber) { subscription.unsubscribe(); } }); } public getCommunityAsObservable() { return this.community.asObservable(); } setCommunity(community: CommunityInfo) { this.community.next(community); } getCommunityInfo(communityId: string) { return this.http.get(properties.communityAPI + communityId) .pipe(map(community => this.parseCommunity(community))); } getCommunity(communityId: string, refresh = false) { if (this.communityId !== communityId || !this.community.value || refresh) { this.communityId = communityId; this.promise = new Promise((resolve, reject) => { this.subs.push(this.getCommunityInfo(communityId).subscribe(community => { this.community.next(community); resolve(); }, error => { this.community.next(null); resolve(); })); }); } return from(this.getCommunityAsync()); } async getCommunityAsync() { await this.promise; return this.community.getValue(); } updateCommunity(url: string, community: any) { if (!community.logoUrl) { community.logoUrl = ''; } const options = { headers: new HttpHeaders({ 'Content-Type': 'application/json', }) }; return this.http.post(url, community, options); } public 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 CommunityInfo.checkIsUpload(community); } isRIType(community: string): Observable { return this.getCommunity(community).pipe(map(community => community.type === 'ri')); } isCommunityType(community: string): Observable { return this.getCommunity(community).pipe(map(community => community.type === 'community')); } }