2021-01-21 16:15:53 +01:00
|
|
|
import {Injectable} from '@angular/core';
|
2019-06-03 15:20:36 +02:00
|
|
|
import {HttpClient, HttpHeaders} from "@angular/common/http";
|
2021-01-21 16:15:53 +01:00
|
|
|
import {CommunityInfo} from './communityInfo';
|
2019-06-03 15:20:36 +02:00
|
|
|
import {map} from "rxjs/operators";
|
2021-02-19 18:50:34 +01:00
|
|
|
import {BehaviorSubject, from, Observable, Subscriber} from "rxjs";
|
2020-09-24 13:18:24 +02:00
|
|
|
import {properties} from "../../../../environments/environment";
|
2021-02-16 11:25:23 +01:00
|
|
|
import {StringUtils} from "../../utils/string-utils.class";
|
2023-03-09 15:07:39 +01:00
|
|
|
import {SelectionCriteria} from "../../utils/entities/contentProvider";
|
2018-03-07 12:08:03 +01:00
|
|
|
|
2021-01-21 16:15:53 +01:00
|
|
|
@Injectable({providedIn: 'root'})
|
2018-03-07 12:08:03 +01:00
|
|
|
export class CommunityService {
|
2021-01-21 16:15:53 +01:00
|
|
|
|
2021-03-29 09:39:48 +02:00
|
|
|
public community: BehaviorSubject<CommunityInfo> = new BehaviorSubject(null);
|
|
|
|
public communityId: string = null;
|
2022-09-23 11:24:15 +02:00
|
|
|
private promise: Promise<void> = null;
|
2021-03-29 09:39:48 +02:00
|
|
|
private subs = [];
|
2021-01-21 16:15:53 +01:00
|
|
|
|
2021-03-29 09:39:48 +02:00
|
|
|
constructor(private http: HttpClient) {}
|
2021-01-21 16:15:53 +01:00
|
|
|
|
|
|
|
clearSubscriptions() {
|
2021-03-26 16:45:30 +01:00
|
|
|
this.subs.forEach(subscription => {
|
|
|
|
if (subscription instanceof Subscriber) {
|
|
|
|
subscription.unsubscribe();
|
|
|
|
}
|
|
|
|
});
|
2020-11-12 16:41:03 +01:00
|
|
|
}
|
2021-01-21 16:15:53 +01:00
|
|
|
|
|
|
|
public getCommunityAsObservable() {
|
|
|
|
return this.community.asObservable();
|
|
|
|
}
|
|
|
|
|
|
|
|
setCommunity(community: CommunityInfo) {
|
|
|
|
this.community.next(community);
|
|
|
|
}
|
2021-03-29 09:39:48 +02:00
|
|
|
|
2021-11-30 17:21:35 +01:00
|
|
|
getCommunityInfo(communityId: string) {
|
|
|
|
return this.http.get<CommunityInfo>(properties.communityAPI + communityId)
|
|
|
|
.pipe(map(community => this.parseCommunity(community)));
|
|
|
|
}
|
|
|
|
|
2021-02-19 19:08:49 +01:00
|
|
|
getCommunity(communityId: string, refresh = false) {
|
2021-03-29 09:39:48 +02:00
|
|
|
if (this.communityId !== communityId || !this.community.value || refresh) {
|
|
|
|
this.communityId = communityId;
|
2022-09-23 11:24:15 +02:00
|
|
|
this.promise = new Promise<void>((resolve, reject) => {
|
2021-11-30 17:21:35 +01:00
|
|
|
this.subs.push(this.getCommunityInfo(communityId).subscribe(community => {
|
2021-01-21 16:15:53 +01:00
|
|
|
this.community.next(community);
|
|
|
|
resolve();
|
|
|
|
},
|
|
|
|
error => {
|
|
|
|
this.community.next(null);
|
2021-01-22 19:09:44 +01:00
|
|
|
resolve();
|
2021-03-26 16:45:30 +01:00
|
|
|
}));
|
2021-01-21 16:15:53 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
return from(this.getCommunityAsync());
|
2020-07-17 01:08:10 +02:00
|
|
|
}
|
2021-01-21 16:15:53 +01:00
|
|
|
|
|
|
|
async getCommunityAsync() {
|
|
|
|
await this.promise;
|
2022-07-04 12:12:42 +02:00
|
|
|
// this.clearSubscriptions();
|
2021-01-21 16:15:53 +01:00
|
|
|
return this.community.getValue();
|
|
|
|
}
|
|
|
|
|
2020-07-17 01:08:10 +02:00
|
|
|
updateCommunity(url: string, community: any) {
|
2021-03-29 09:39:48 +02:00
|
|
|
if (!community.logoUrl) {
|
2021-02-16 11:25:23 +01:00
|
|
|
community.logoUrl = '';
|
|
|
|
}
|
2021-01-21 16:15:53 +01:00
|
|
|
const options = {
|
|
|
|
headers: new HttpHeaders({
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
})
|
|
|
|
};
|
2021-03-29 09:51:45 +02:00
|
|
|
return this.http.post(url, community, options);
|
2020-07-17 01:08:10 +02:00
|
|
|
}
|
2021-01-21 16:15:53 +01:00
|
|
|
|
2023-03-09 15:29:25 +01:00
|
|
|
public updateSubjects(subjects: string[], fos: string[], sdg: string[]) {
|
2023-03-09 15:07:39 +01:00
|
|
|
let communityInfo: CommunityInfo = this.community.value;
|
|
|
|
communityInfo.subjects = subjects;
|
|
|
|
communityInfo.fos = fos;
|
|
|
|
communityInfo.sdg = sdg;
|
|
|
|
this.community.next(communityInfo);
|
|
|
|
}
|
|
|
|
|
|
|
|
public updateAdvancedCriteria(selectionCriteria: SelectionCriteria) {
|
|
|
|
let communityInfo: CommunityInfo = this.community.value;
|
|
|
|
communityInfo.selectionCriteria = selectionCriteria;
|
|
|
|
this.community.next(communityInfo);
|
|
|
|
}
|
|
|
|
|
2021-03-01 18:27:45 +01:00
|
|
|
public parseCommunity(data: any): CommunityInfo {
|
2021-01-21 16:15:53 +01:00
|
|
|
const resData = Array.isArray(data) ? data[0] : data;
|
|
|
|
const community: CommunityInfo = new CommunityInfo();
|
2023-03-09 15:07:39 +01:00
|
|
|
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';
|
|
|
|
community.type = resData.type;
|
2021-01-21 16:15:53 +01:00
|
|
|
if (resData.hasOwnProperty('status')) {
|
2023-03-09 15:07:39 +01:00
|
|
|
community.status = resData.status;
|
2021-01-21 16:15:53 +01:00
|
|
|
const status = ['all', 'hidden', 'manager'];
|
|
|
|
if (status.indexOf(community['status']) === -1) {
|
2023-03-09 15:07:39 +01:00
|
|
|
community.status = 'hidden';
|
2021-01-21 16:15:53 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (resData.subjects != null) {
|
2023-03-09 15:07:39 +01:00
|
|
|
community.subjects = Array.isArray(resData.subjects)?resData.subjects:[resData.subjects];
|
2023-03-09 15:29:25 +01:00
|
|
|
} else {
|
|
|
|
community.subjects = [];
|
|
|
|
}
|
|
|
|
if (resData.sdg != null) {
|
|
|
|
community.sdg = Array.isArray(resData.sdg)?resData.sdg:[resData.sdg];
|
|
|
|
} else {
|
|
|
|
community.sdg = [];
|
|
|
|
}
|
|
|
|
if (resData.fos != null) {
|
|
|
|
community.fos = Array.isArray(resData.fos)?resData.fos:[resData.fos];
|
|
|
|
} else {
|
|
|
|
community.fos = [];
|
|
|
|
}
|
2023-03-09 15:07:39 +01:00
|
|
|
if (resData.advancedConstraint != null) {
|
|
|
|
community.selectionCriteria = resData.advancedConstraint;
|
|
|
|
} else {
|
|
|
|
community.selectionCriteria = new SelectionCriteria();
|
2021-01-21 16:15:53 +01:00
|
|
|
}
|
2021-11-12 11:36:16 +01:00
|
|
|
return CommunityInfo.checkIsUpload(community);
|
2020-07-17 01:08:10 +02:00
|
|
|
}
|
2021-01-21 16:15:53 +01:00
|
|
|
|
2021-02-19 18:50:34 +01:00
|
|
|
isRIType(community: string): Observable<boolean> {
|
2021-02-19 19:08:49 +01:00
|
|
|
return this.getCommunity(community).pipe(map(community => community.type === 'ri'));
|
2021-02-19 18:50:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
isCommunityType(community: string): Observable<boolean> {
|
2021-02-19 19:08:49 +01:00
|
|
|
return this.getCommunity(community).pipe(map(community => community.type === 'community'));
|
2021-02-19 18:50:34 +01:00
|
|
|
}
|
2018-03-07 12:08:03 +01:00
|
|
|
}
|