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