Revision 60274
Added by Konstantinos Triantafyllou about 3 years ago
community.service.ts | ||
---|---|---|
1 |
import { Injectable } from '@angular/core';
|
|
1 |
import {Injectable} from '@angular/core';
|
|
2 | 2 |
import {HttpClient, HttpHeaders} from "@angular/common/http"; |
3 |
import { CommunityInfo } from './communityInfo';
|
|
3 |
import {CommunityInfo} from './communityInfo';
|
|
4 | 4 |
import {EnvProperties} from '../../utils/properties/env-properties'; |
5 | 5 |
import {map} from "rxjs/operators"; |
6 | 6 |
import {BehaviorSubject, from, Subscriber} from "rxjs"; |
7 | 7 |
import {properties} from "../../../../environments/environment"; |
8 |
import {HelperFunctions} from "../../utils/HelperFunctions.class"; |
|
8 | 9 |
|
9 |
@Injectable({ providedIn: 'root' })
|
|
10 |
@Injectable({providedIn: 'root'})
|
|
10 | 11 |
export class CommunityService { |
11 |
|
|
12 |
|
|
12 | 13 |
public community: BehaviorSubject<CommunityInfo> = null; |
13 | 14 |
private promise: Promise<boolean> = null; |
14 |
|
|
15 |
private sub; |
|
16 |
|
|
15 | 17 |
constructor(private http: HttpClient) { |
16 | 18 |
this.community = new BehaviorSubject(null); |
17 | 19 |
} |
18 |
sub; |
|
20 |
|
|
19 | 21 |
ngOnDestroy() { |
20 | 22 |
this.clearSubscriptions(); |
21 | 23 |
} |
22 |
clearSubscriptions(){ |
|
24 |
|
|
25 |
clearSubscriptions() { |
|
23 | 26 |
if (this.sub instanceof Subscriber) { |
24 | 27 |
this.sub.unsubscribe(); |
25 | 28 |
} |
26 | 29 |
} |
30 |
|
|
31 |
// TODO Remove these functions |
|
27 | 32 |
getCommunityByService(properties: EnvProperties, url: string) { |
28 | 33 |
this.promise = new Promise<any>(resolve => { |
29 | 34 |
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 |
}) |
|
35 |
this.community.next(res);
|
|
36 |
resolve();
|
|
37 |
},
|
|
38 |
error => {
|
|
39 |
this.community.error(error);
|
|
40 |
resolve();
|
|
41 |
})
|
|
37 | 42 |
}); |
38 | 43 |
} |
39 |
|
|
40 |
async getCommunityByStateAsync(properties: EnvProperties, url: string) {
|
|
41 |
if(!this.promise) { |
|
44 |
|
|
45 |
async getCommunityByStateAsync(properties: EnvProperties, url: string) { |
|
46 |
if (!this.promise) {
|
|
42 | 47 |
this.getCommunityByService(properties, url); |
43 | 48 |
} |
44 |
|
|
49 |
|
|
45 | 50 |
await this.promise; |
46 | 51 |
this.clearSubscriptions(); |
47 | 52 |
return this.community.getValue(); |
48 | 53 |
} |
49 |
|
|
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 |
|
|
50 | 67 |
getCommunityByState(properties: EnvProperties, url: string) { |
51 | 68 |
return from(this.getCommunityByStateAsync(properties, url)); |
52 | 69 |
} |
53 |
|
|
70 |
|
|
54 | 71 |
getCommunity(properties: EnvProperties, url: string) { |
55 |
return this.http.get((properties.useCache) ? (properties.cacheUrl + encodeURIComponent(url)) : url)
|
|
56 |
.pipe(map(res => this.parseCommunity(res)));
|
|
72 |
return this.http.get((properties.useCache) ? (properties.cacheUrl + encodeURIComponent(url)) : url) |
|
73 |
.pipe(map(res => this.parseCommunity(res))); |
|
57 | 74 |
} |
58 |
|
|
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 |
|
|
59 | 100 |
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())*/
|
|
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())*/ |
|
72 | 113 |
} |
73 |
|
|
114 |
|
|
74 | 115 |
async isCommunityManagerByStateAsync(properties: EnvProperties, url: string, manager: string) { |
75 |
if(!this.promise) { |
|
116 |
if (!this.promise) {
|
|
76 | 117 |
this.getCommunityByService(properties, url); |
77 | 118 |
} |
78 |
|
|
119 |
|
|
79 | 120 |
await this.promise; |
80 | 121 |
let community: CommunityInfo = this.community.getValue(); |
81 | 122 |
return (community.managers.indexOf(manager) !== -1); |
82 | 123 |
} |
83 |
|
|
124 |
|
|
84 | 125 |
isCommunityManagerByState(properties: EnvProperties, url: string, manager: string) { |
85 | 126 |
return from(this.isCommunityManagerByStateAsync(properties, url, manager)); |
86 | 127 |
} |
87 |
|
|
128 |
|
|
88 | 129 |
/** |
89 | 130 |
* @deprecated |
90 | 131 |
*/ |
91 | 132 |
isCommunityManager(properties: EnvProperties, url: string, manager: string) { |
92 | 133 |
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));
|
|
134 |
//.map(res => <any> res.json()) |
|
135 |
.pipe(map(res => this.parseCommunity(res))) |
|
136 |
.pipe(map(community => community.managers.indexOf(manager) !== -1)); |
|
96 | 137 |
} |
97 |
|
|
138 |
|
|
98 | 139 |
async isTypeByStateAsync(properties: EnvProperties, url: string, type: string) { |
99 |
if(!this.promise) { |
|
140 |
if (!this.promise) {
|
|
100 | 141 |
this.getCommunityByService(properties, url); |
101 | 142 |
} |
102 |
|
|
143 |
|
|
103 | 144 |
await this.promise; |
104 | 145 |
let community: CommunityInfo = this.community.getValue(); |
105 | 146 |
return (community && community.type && community.type === type); |
106 | 147 |
} |
107 |
|
|
148 |
|
|
108 | 149 |
isRITypeByState(properties: EnvProperties, url: string) { |
109 | 150 |
return from(this.isTypeByStateAsync(properties, url, "ri")); |
110 | 151 |
} |
111 |
|
|
152 |
|
|
112 | 153 |
isCommunityTypeByState(properties: EnvProperties, url: string) { |
113 | 154 |
return from(this.isTypeByStateAsync(properties, url, "community")); |
114 | 155 |
} |
115 |
|
|
156 |
|
|
116 | 157 |
/** |
117 | 158 |
* @deprecated |
118 | 159 |
*/ |
119 | 160 |
isRIType(properties: EnvProperties, url: string) { |
120 | 161 |
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')));
|
|
162 |
//.map(res => <any> res.json()) |
|
163 |
.pipe(map(res => this.parseCommunity(res))) |
|
164 |
.pipe(map(community => (community && community.type && community.type === 'ri'))); |
|
124 | 165 |
} |
125 |
|
|
166 |
|
|
126 | 167 |
/** |
127 | 168 |
* @deprecated |
128 | 169 |
*/ |
129 | 170 |
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')));
|
|
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'))); |
|
134 | 175 |
} |
135 |
|
|
176 |
|
|
136 | 177 |
/** |
137 |
* @deprecated |
|
138 |
*/ |
|
178 |
* @deprecated
|
|
179 |
*/
|
|
139 | 180 |
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 |
}));
|
|
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 |
})); |
|
154 | 195 |
} |
155 |
|
|
196 |
|
|
156 | 197 |
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 |
} |
|
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'; |
|
176 | 216 |
} |
177 |
if (resData.type != null) { |
|
178 |
community['type'] = resData.type; |
|
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>(); |
|
179 | 225 |
} |
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 |
} |
|
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; |
|
193 | 233 |
} |
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 |
} |
|
234 |
} |
|
235 |
|
|
236 |
if (resData.subjects != null) { |
|
237 |
if (community['subjects'] === undefined) { |
|
238 |
community['subjects'] = new Array<string>(); |
|
207 | 239 |
} |
208 |
return community; |
|
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; |
|
209 | 250 |
} |
210 |
|
|
251 |
|
|
211 | 252 |
} |
Also available in: Unified diff
[Library | Trunk]: Create subscriber invite and subscribers components.