2018-03-07 12:08:03 +01:00
|
|
|
import { Injectable } from '@angular/core';
|
2019-02-25 16:28:14 +01:00
|
|
|
import { Http, Headers, RequestOptions } from '@angular/http';
|
2018-03-07 12:08:03 +01:00
|
|
|
import { CommunityInfo } from './communityInfo';
|
2019-03-07 16:43:54 +01:00
|
|
|
import {EnvProperties} from '../../utils/properties/env-properties';
|
2018-03-07 12:08:03 +01:00
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
export class CommunityService {
|
|
|
|
|
2019-03-07 16:43:54 +01:00
|
|
|
constructor(private http: Http) {
|
2018-03-07 12:08:03 +01:00
|
|
|
}
|
|
|
|
|
2019-03-07 16:43:54 +01:00
|
|
|
getCommunity(properties: EnvProperties, url: string) {
|
|
|
|
return this.http.get((properties.useCache) ? (properties.cacheUrl + encodeURIComponent(url)) : url)
|
2018-04-04 16:05:26 +02:00
|
|
|
.map(res => <any> res.json()).map(res => this.parseCommunity(res));
|
2018-03-07 12:08:03 +01:00
|
|
|
}
|
2018-03-20 12:40:39 +01:00
|
|
|
|
2019-03-07 16:43:54 +01:00
|
|
|
updateCommunity(url: string, community: any) {
|
|
|
|
const headers = new Headers({'Content-Type': 'application/json'});
|
|
|
|
const options = new RequestOptions({headers: headers});
|
|
|
|
const body = JSON.stringify(community);
|
|
|
|
return this.http.post(url, body, options);
|
2018-03-20 13:09:51 +01:00
|
|
|
/*.map(res => res.json())*/
|
2018-03-20 12:40:39 +01:00
|
|
|
}
|
|
|
|
|
2019-03-07 16:43:54 +01:00
|
|
|
isCommunityManager(properties: EnvProperties, url: string, manager: string) {
|
|
|
|
return this.http.get((properties.useCache) ? (properties.cacheUrl + encodeURIComponent(url)) : url)
|
|
|
|
.map(res => <any> res.json()).map(res =>
|
|
|
|
this.parseCommunity(res)).map(community => community.managers.indexOf(manager) !== -1);
|
2018-03-15 10:53:07 +01:00
|
|
|
}
|
2019-02-25 16:28:14 +01:00
|
|
|
|
2019-03-07 16:43:54 +01:00
|
|
|
isRIType(properties: EnvProperties, url: string) {
|
|
|
|
return this.http.get((properties.useCache) ? (properties.cacheUrl + encodeURIComponent(url)) : url)
|
|
|
|
.map(res => <any> res.json()).map(res =>
|
|
|
|
this.parseCommunity(res)).map(community =>
|
|
|
|
(community && community.type && community.type === 'ri'));
|
2018-03-27 10:22:55 +02:00
|
|
|
}
|
2019-02-25 16:28:14 +01:00
|
|
|
|
2019-03-07 16:43:54 +01:00
|
|
|
isCommunityType(properties: EnvProperties, url: string) {
|
|
|
|
return this.http.get((properties.useCache) ? (properties.cacheUrl + encodeURIComponent(url)) : url)
|
|
|
|
.map(res => <any> res.json()).map(res =>
|
|
|
|
this.parseCommunity(res)).map(community =>
|
|
|
|
(community && community.type && community.type === 'community'));
|
|
|
|
}
|
2018-04-16 13:49:35 +02:00
|
|
|
|
2019-03-07 16:43:54 +01:00
|
|
|
isSubscribedToCommunity(pid: string, email: string, url: string) {
|
|
|
|
return this.http.get(url + '/community/' + pid + '/subscribers')
|
|
|
|
.map(res => ((<any>res === '') ? {} : <any> res.json()))
|
2018-04-16 13:49:35 +02:00
|
|
|
.map(res => {
|
2019-03-07 16:43:54 +01:00
|
|
|
if (res.subscribers && res.subscribers != null) {
|
2018-04-16 13:49:35 +02:00
|
|
|
|
2019-03-07 16:43:54 +01:00
|
|
|
for (let i = 0; i < res.subscribers.length; i++ ) {
|
|
|
|
if (res.subscribers[i] != null && res.subscribers[i].email === email) {
|
2018-04-16 13:49:35 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
|
2019-02-14 11:15:44 +01:00
|
|
|
});
|
2018-04-16 13:49:35 +02:00
|
|
|
}
|
2019-02-25 16:28:14 +01:00
|
|
|
|
2019-03-07 16:43:54 +01:00
|
|
|
private parseCommunity(data: any): CommunityInfo {
|
2019-02-25 16:28:14 +01:00
|
|
|
|
2019-03-07 16:43:54 +01:00
|
|
|
const resData = Array.isArray(data) ? data[0] : data;
|
2019-02-25 16:28:14 +01:00
|
|
|
|
2019-03-07 16:43:54 +01:00
|
|
|
const community: CommunityInfo = new CommunityInfo();
|
2019-02-25 16:28:14 +01:00
|
|
|
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;
|
2019-03-07 16:43:54 +01:00
|
|
|
community['status'] = 'all';
|
|
|
|
if (resData.hasOwnProperty('status')) {
|
2019-02-25 16:28:14 +01:00
|
|
|
community['status'] = resData.status;
|
2019-03-07 16:43:54 +01:00
|
|
|
const status = ['all', 'hidden', 'manager'];
|
|
|
|
if (status.indexOf(community['status']) === -1) {
|
|
|
|
community['status'] = 'hidden';
|
2018-03-07 12:08:03 +01:00
|
|
|
}
|
2019-02-25 16:28:14 +01:00
|
|
|
}
|
|
|
|
if (resData.type != null) {
|
|
|
|
community['type'] = resData.type;
|
|
|
|
}
|
|
|
|
|
2019-03-07 16:43:54 +01:00
|
|
|
if (resData.managers != null) {
|
|
|
|
if (community['managers'] === undefined) {
|
2019-02-25 16:28:14 +01:00
|
|
|
community['managers'] = new Array<string>();
|
|
|
|
}
|
|
|
|
|
2019-03-07 16:43:54 +01:00
|
|
|
const managers = resData.managers;
|
|
|
|
const length = Array.isArray(managers) ? managers.length : 1;
|
2019-02-25 16:28:14 +01:00
|
|
|
|
2019-03-07 16:43:54 +01:00
|
|
|
for (let i = 0; i < length; i++) {
|
|
|
|
const manager = Array.isArray(managers) ? managers[i] : managers;
|
2019-02-25 16:28:14 +01:00
|
|
|
community.managers[i] = manager;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-07 16:43:54 +01:00
|
|
|
if (resData.subjects != null) {
|
|
|
|
if (community['subjects'] === undefined) {
|
2019-02-25 16:28:14 +01:00
|
|
|
community['subjects'] = new Array<string>();
|
|
|
|
}
|
|
|
|
|
2019-03-07 16:43:54 +01:00
|
|
|
const subjects = resData.subjects;
|
|
|
|
const length = Array.isArray(subjects) ? subjects.length : 1;
|
2019-02-25 16:28:14 +01:00
|
|
|
|
2019-03-07 16:43:54 +01:00
|
|
|
for (let i = 0; i < length; i++) {
|
|
|
|
const subject = Array.isArray(subjects) ? subjects[i] : subjects;
|
2019-02-25 16:28:14 +01:00
|
|
|
community.subjects[i] = subject;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return community;
|
2018-03-07 12:08:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|