1 |
61381
|
k.triantaf
|
import {Injectable} from '@angular/core';
|
2 |
|
|
import {HttpClient, HttpHeaders} from "@angular/common/http";
|
3 |
|
|
import {CommunityInfo} from './communityInfo';
|
4 |
|
|
import {map} from "rxjs/operators";
|
5 |
|
|
import {BehaviorSubject, from, Observable, Subscriber} from "rxjs";
|
6 |
|
|
import {properties} from "../../../../environments/environment";
|
7 |
|
|
import {StringUtils} from "../../utils/string-utils.class";
|
8 |
|
|
|
9 |
|
|
@Injectable({providedIn: 'root'})
|
10 |
|
|
export class CommunityService {
|
11 |
|
|
|
12 |
|
|
public community: BehaviorSubject<CommunityInfo> = new BehaviorSubject(null);
|
13 |
|
|
public communityId: string = null;
|
14 |
|
|
private promise: Promise<boolean> = null;
|
15 |
|
|
private subs = [];
|
16 |
|
|
|
17 |
|
|
constructor(private http: HttpClient) {}
|
18 |
|
|
|
19 |
|
|
clearSubscriptions() {
|
20 |
|
|
this.subs.forEach(subscription => {
|
21 |
|
|
if (subscription instanceof Subscriber) {
|
22 |
|
|
subscription.unsubscribe();
|
23 |
|
|
}
|
24 |
|
|
});
|
25 |
|
|
}
|
26 |
|
|
|
27 |
|
|
public getCommunityAsObservable() {
|
28 |
|
|
return this.community.asObservable();
|
29 |
|
|
}
|
30 |
|
|
|
31 |
|
|
setCommunity(community: CommunityInfo) {
|
32 |
|
|
this.community.next(community);
|
33 |
|
|
}
|
34 |
|
|
|
35 |
|
|
getCommunity(communityId: string, refresh = false) {
|
36 |
|
|
if (this.communityId !== communityId || !this.community.value || refresh) {
|
37 |
|
|
this.communityId = communityId;
|
38 |
|
|
this.promise = new Promise<any>((resolve, reject) => {
|
39 |
|
|
this.subs.push(this.http.get<CommunityInfo>(properties.communityAPI + communityId)
|
40 |
|
|
.pipe(map(community => this.parseCommunity(community))).subscribe(community => {
|
41 |
|
|
this.community.next(community);
|
42 |
|
|
resolve();
|
43 |
|
|
},
|
44 |
|
|
error => {
|
45 |
|
|
this.community.next(null);
|
46 |
|
|
resolve();
|
47 |
|
|
}));
|
48 |
|
|
});
|
49 |
|
|
}
|
50 |
|
|
return from(this.getCommunityAsync());
|
51 |
|
|
}
|
52 |
|
|
|
53 |
|
|
async getCommunityAsync() {
|
54 |
|
|
await this.promise;
|
55 |
|
|
return this.community.getValue();
|
56 |
|
|
}
|
57 |
|
|
|
58 |
|
|
private checkIsUpload(response: CommunityInfo | CommunityInfo[]): any | any[] {
|
59 |
|
|
if (Array.isArray(response)) {
|
60 |
|
|
response.forEach(value => {
|
61 |
|
|
value.isUpload = value.logoUrl && !StringUtils.isValidUrl(value.logoUrl);
|
62 |
|
|
});
|
63 |
|
|
} else {
|
64 |
|
|
response.isUpload = response.logoUrl && !StringUtils.isValidUrl(response.logoUrl);
|
65 |
|
|
}
|
66 |
|
|
return response;
|
67 |
|
|
}
|
68 |
|
|
|
69 |
|
|
|
70 |
|
|
updateCommunity(url: string, community: any) {
|
71 |
|
|
if (!community.logoUrl) {
|
72 |
|
|
community.logoUrl = '';
|
73 |
|
|
}
|
74 |
|
|
const options = {
|
75 |
|
|
headers: new HttpHeaders({
|
76 |
|
|
'Content-Type': 'application/json',
|
77 |
|
|
})
|
78 |
|
|
};
|
79 |
|
|
return this.http.post(url, community, options);
|
80 |
|
|
}
|
81 |
|
|
|
82 |
|
|
public parseCommunity(data: any): CommunityInfo {
|
83 |
|
|
|
84 |
|
|
const resData = Array.isArray(data) ? data[0] : data;
|
85 |
|
|
|
86 |
|
|
const community: CommunityInfo = new CommunityInfo();
|
87 |
|
|
community['title'] = resData.name;
|
88 |
|
|
community['shortTitle'] = resData.shortName;
|
89 |
|
|
community['communityId'] = resData.id;
|
90 |
|
|
community['queryId'] = resData.queryId;
|
91 |
|
|
community['logoUrl'] = resData.logoUrl;
|
92 |
|
|
community['description'] = resData.description;
|
93 |
|
|
community['date'] = resData.creationDate;
|
94 |
|
|
community['zenodoCommunity'] = resData.zenodoCommunity;
|
95 |
|
|
community['status'] = 'all';
|
96 |
|
|
if (resData.hasOwnProperty('status')) {
|
97 |
|
|
community['status'] = resData.status;
|
98 |
|
|
const status = ['all', 'hidden', 'manager'];
|
99 |
|
|
if (status.indexOf(community['status']) === -1) {
|
100 |
|
|
community['status'] = 'hidden';
|
101 |
|
|
}
|
102 |
|
|
}
|
103 |
|
|
if (resData.type != null) {
|
104 |
|
|
community['type'] = resData.type;
|
105 |
|
|
}
|
106 |
|
|
|
107 |
|
|
if (resData.managers != null) {
|
108 |
|
|
if (community['managers'] === undefined) {
|
109 |
|
|
community['managers'] = new Array<string>();
|
110 |
|
|
}
|
111 |
|
|
|
112 |
|
|
const managers = resData.managers;
|
113 |
|
|
const length = Array.isArray(managers) ? managers.length : 1;
|
114 |
|
|
|
115 |
|
|
for (let i = 0; i < length; i++) {
|
116 |
|
|
const manager = Array.isArray(managers) ? managers[i] : managers;
|
117 |
|
|
community.managers[i] = manager;
|
118 |
|
|
}
|
119 |
|
|
}
|
120 |
|
|
|
121 |
|
|
if (resData.subjects != null) {
|
122 |
|
|
if (community['subjects'] === undefined) {
|
123 |
|
|
community['subjects'] = new Array<string>();
|
124 |
|
|
}
|
125 |
|
|
|
126 |
|
|
const subjects = resData.subjects;
|
127 |
|
|
const length = Array.isArray(subjects) ? subjects.length : 1;
|
128 |
|
|
|
129 |
|
|
for (let i = 0; i < length; i++) {
|
130 |
|
|
const subject = Array.isArray(subjects) ? subjects[i] : subjects;
|
131 |
|
|
community.subjects[i] = subject;
|
132 |
|
|
}
|
133 |
|
|
}
|
134 |
|
|
return this.checkIsUpload(community);
|
135 |
|
|
}
|
136 |
|
|
|
137 |
|
|
isRIType(community: string): Observable<boolean> {
|
138 |
|
|
return this.getCommunity(community).pipe(map(community => community.type === 'ri'));
|
139 |
|
|
}
|
140 |
|
|
|
141 |
|
|
isCommunityType(community: string): Observable<boolean> {
|
142 |
|
|
return this.getCommunity(community).pipe(map(community => community.type === 'community'));
|
143 |
|
|
}
|
144 |
|
|
}
|