1
|
import {Injectable} from '@angular/core';
|
2
|
import {HttpClient, HttpHeaders} from "@angular/common/http";
|
3
|
import {CommunityInfo} from './communityInfo';
|
4
|
import {EnvProperties} from '../../utils/properties/env-properties';
|
5
|
import {map} from "rxjs/operators";
|
6
|
import {BehaviorSubject, from, Subscriber} from "rxjs";
|
7
|
import {properties} from "../../../../environments/environment";
|
8
|
import {HelperFunctions} from "../../utils/HelperFunctions.class";
|
9
|
|
10
|
@Injectable({providedIn: 'root'})
|
11
|
export class CommunityService {
|
12
|
|
13
|
public community: BehaviorSubject<CommunityInfo> = null;
|
14
|
private promise: Promise<boolean> = null;
|
15
|
private sub;
|
16
|
|
17
|
constructor(private http: HttpClient) {
|
18
|
this.community = new BehaviorSubject(null);
|
19
|
}
|
20
|
|
21
|
ngOnDestroy() {
|
22
|
this.clearSubscriptions();
|
23
|
}
|
24
|
|
25
|
clearSubscriptions() {
|
26
|
if (this.sub instanceof Subscriber) {
|
27
|
this.sub.unsubscribe();
|
28
|
}
|
29
|
}
|
30
|
|
31
|
// TODO Remove these functions
|
32
|
getCommunityByService(properties: EnvProperties, url: string) {
|
33
|
this.promise = new Promise<any>(resolve => {
|
34
|
this.sub = this.getCommunity(properties, url).subscribe(res => {
|
35
|
this.community.next(res);
|
36
|
resolve();
|
37
|
},
|
38
|
error => {
|
39
|
this.community.error(error);
|
40
|
resolve();
|
41
|
})
|
42
|
});
|
43
|
}
|
44
|
|
45
|
async getCommunityByStateAsync(properties: EnvProperties, url: string) {
|
46
|
if (!this.promise) {
|
47
|
this.getCommunityByService(properties, url);
|
48
|
}
|
49
|
|
50
|
await this.promise;
|
51
|
this.clearSubscriptions();
|
52
|
return this.community.getValue();
|
53
|
}
|
54
|
|
55
|
public getCommunityAsObservable() {
|
56
|
return this.community.asObservable();
|
57
|
}
|
58
|
|
59
|
setCommunity(community: CommunityInfo) {
|
60
|
this.community.next(community);
|
61
|
}
|
62
|
|
63
|
private formalize(element: any) {
|
64
|
return HelperFunctions.copy(element);
|
65
|
}
|
66
|
|
67
|
getCommunityByState(properties: EnvProperties, url: string) {
|
68
|
return from(this.getCommunityByStateAsync(properties, url));
|
69
|
}
|
70
|
|
71
|
getCommunity(properties: EnvProperties, url: string) {
|
72
|
return this.http.get((properties.useCache) ? (properties.cacheUrl + encodeURIComponent(url)) : url)
|
73
|
.pipe(map(res => this.parseCommunity(res)));
|
74
|
}
|
75
|
|
76
|
// TODO remove NEW from function names
|
77
|
getCommunityNew(communityId: string) {
|
78
|
if(!this.community.value || this.community.value.communityId !== communityId) {
|
79
|
this.promise = new Promise<any>((resolve, reject) => {
|
80
|
this.sub = this.http.get<CommunityInfo>(properties.communityAPI + communityId)
|
81
|
.pipe(map(community => this.formalize(this.parseCommunity(community)))).subscribe(community => {
|
82
|
this.community.next(community);
|
83
|
resolve();
|
84
|
},
|
85
|
error => {
|
86
|
this.community.next(null);
|
87
|
reject();
|
88
|
})
|
89
|
});
|
90
|
}
|
91
|
return from(this.getCommunityAsync());
|
92
|
}
|
93
|
|
94
|
async getCommunityAsync() {
|
95
|
await this.promise;
|
96
|
this.clearSubscriptions();
|
97
|
return this.community.getValue();
|
98
|
}
|
99
|
|
100
|
updateCommunity(url: string, community: any) {
|
101
|
//const headers = new Headers({'Content-Type': 'application/json'});
|
102
|
//const options = new RequestOptions({headers: headers});
|
103
|
|
104
|
const options = {
|
105
|
headers: new HttpHeaders({
|
106
|
'Content-Type': 'application/json',
|
107
|
})
|
108
|
};
|
109
|
|
110
|
const body = JSON.stringify(community);
|
111
|
return this.http.post(url, body, options);
|
112
|
/*.map(res => res.json())*/
|
113
|
}
|
114
|
|
115
|
async isCommunityManagerByStateAsync(properties: EnvProperties, url: string, manager: string) {
|
116
|
if (!this.promise) {
|
117
|
this.getCommunityByService(properties, url);
|
118
|
}
|
119
|
|
120
|
await this.promise;
|
121
|
let community: CommunityInfo = this.community.getValue();
|
122
|
return (community.managers.indexOf(manager) !== -1);
|
123
|
}
|
124
|
|
125
|
isCommunityManagerByState(properties: EnvProperties, url: string, manager: string) {
|
126
|
return from(this.isCommunityManagerByStateAsync(properties, url, manager));
|
127
|
}
|
128
|
|
129
|
/**
|
130
|
* @deprecated
|
131
|
*/
|
132
|
isCommunityManager(properties: EnvProperties, url: string, manager: string) {
|
133
|
return this.http.get((properties.useCache) ? (properties.cacheUrl + encodeURIComponent(url)) : url)
|
134
|
//.map(res => <any> res.json())
|
135
|
.pipe(map(res => this.parseCommunity(res)))
|
136
|
.pipe(map(community => community.managers.indexOf(manager) !== -1));
|
137
|
}
|
138
|
|
139
|
async isTypeByStateAsync(properties: EnvProperties, url: string, type: string) {
|
140
|
if (!this.promise) {
|
141
|
this.getCommunityByService(properties, url);
|
142
|
}
|
143
|
|
144
|
await this.promise;
|
145
|
let community: CommunityInfo = this.community.getValue();
|
146
|
return (community && community.type && community.type === type);
|
147
|
}
|
148
|
|
149
|
isRITypeByState(properties: EnvProperties, url: string) {
|
150
|
return from(this.isTypeByStateAsync(properties, url, "ri"));
|
151
|
}
|
152
|
|
153
|
isCommunityTypeByState(properties: EnvProperties, url: string) {
|
154
|
return from(this.isTypeByStateAsync(properties, url, "community"));
|
155
|
}
|
156
|
|
157
|
/**
|
158
|
* @deprecated
|
159
|
*/
|
160
|
isRIType(properties: EnvProperties, url: string) {
|
161
|
return this.http.get((properties.useCache) ? (properties.cacheUrl + encodeURIComponent(url)) : url)
|
162
|
//.map(res => <any> res.json())
|
163
|
.pipe(map(res => this.parseCommunity(res)))
|
164
|
.pipe(map(community => (community && community.type && community.type === 'ri')));
|
165
|
}
|
166
|
|
167
|
/**
|
168
|
* @deprecated
|
169
|
*/
|
170
|
isCommunityType(properties: EnvProperties, url: string) {
|
171
|
return this.http.get((properties.useCache) ? (properties.cacheUrl + encodeURIComponent(url)) : url)
|
172
|
//.map(res => <any> res.json())
|
173
|
.pipe(map(res => this.parseCommunity(res)))
|
174
|
.pipe(map(community => (community && community.type && community.type === 'community')));
|
175
|
}
|
176
|
|
177
|
/**
|
178
|
* @deprecated
|
179
|
*/
|
180
|
isSubscribedToCommunity(pid: string, email: string, url: string) {
|
181
|
return this.http.get(url + '/' + properties.adminToolsPortalType + '/' + pid + '/subscribers')
|
182
|
//.map(res => ((<any>res === '') ? {} : <any> res.json()))
|
183
|
.pipe(map(res => {
|
184
|
if (res['subscribers'] && res['subscribers'] != null) {
|
185
|
|
186
|
for (let i = 0; i < res['subscribers'].length; i++) {
|
187
|
if (res['subscribers'][i] != null && res['subscribers'][i].email === email) {
|
188
|
return true;
|
189
|
}
|
190
|
}
|
191
|
}
|
192
|
return false;
|
193
|
|
194
|
}));
|
195
|
}
|
196
|
|
197
|
private parseCommunity(data: any): CommunityInfo {
|
198
|
|
199
|
const resData = Array.isArray(data) ? data[0] : data;
|
200
|
|
201
|
const community: CommunityInfo = new CommunityInfo();
|
202
|
community['title'] = resData.name;
|
203
|
community['shortTitle'] = resData.shortName;
|
204
|
community['communityId'] = resData.id;
|
205
|
community['queryId'] = resData.queryId;
|
206
|
community['logoUrl'] = resData.logoUrl;
|
207
|
community['description'] = resData.description;
|
208
|
community['date'] = resData.creationDate;
|
209
|
community['zenodoCommunity'] = resData.zenodoCommunity;
|
210
|
community['status'] = 'all';
|
211
|
if (resData.hasOwnProperty('status')) {
|
212
|
community['status'] = resData.status;
|
213
|
const status = ['all', 'hidden', 'manager'];
|
214
|
if (status.indexOf(community['status']) === -1) {
|
215
|
community['status'] = 'hidden';
|
216
|
}
|
217
|
}
|
218
|
if (resData.type != null) {
|
219
|
community['type'] = resData.type;
|
220
|
}
|
221
|
|
222
|
if (resData.managers != null) {
|
223
|
if (community['managers'] === undefined) {
|
224
|
community['managers'] = new Array<string>();
|
225
|
}
|
226
|
|
227
|
const managers = resData.managers;
|
228
|
const length = Array.isArray(managers) ? managers.length : 1;
|
229
|
|
230
|
for (let i = 0; i < length; i++) {
|
231
|
const manager = Array.isArray(managers) ? managers[i] : managers;
|
232
|
community.managers[i] = manager;
|
233
|
}
|
234
|
}
|
235
|
|
236
|
if (resData.subjects != null) {
|
237
|
if (community['subjects'] === undefined) {
|
238
|
community['subjects'] = new Array<string>();
|
239
|
}
|
240
|
|
241
|
const subjects = resData.subjects;
|
242
|
const length = Array.isArray(subjects) ? subjects.length : 1;
|
243
|
|
244
|
for (let i = 0; i < length; i++) {
|
245
|
const subject = Array.isArray(subjects) ? subjects[i] : subjects;
|
246
|
community.subjects[i] = subject;
|
247
|
}
|
248
|
}
|
249
|
return community;
|
250
|
}
|
251
|
|
252
|
}
|