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";
|
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;
|
2020-07-17 01:08:10 +02:00
|
|
|
private promise: Promise<boolean> = 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;
|
2021-01-21 16:15:53 +01:00
|
|
|
this.promise = new Promise<any>((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;
|
|
|
|
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
|
|
|
|
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();
|
|
|
|
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<string>();
|
2020-07-17 01:08:10 +02:00
|
|
|
}
|
2021-01-21 16:15:53 +01:00
|
|
|
|
|
|
|
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;
|
2020-07-17 01:08:10 +02:00
|
|
|
}
|
2021-01-21 16:15:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (resData.subjects != null) {
|
|
|
|
if (community['subjects'] === undefined) {
|
|
|
|
community['subjects'] = new Array<string>();
|
2020-07-17 01:08:10 +02:00
|
|
|
}
|
2021-01-21 16:15:53 +01:00
|
|
|
|
|
|
|
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;
|
2020-07-17 01:08:10 +02:00
|
|
|
}
|
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
|
|
|
}
|