openaire-library/connect/community/community.service.ts

112 lines
4.2 KiB
TypeScript
Raw Normal View History

import { Injectable } from '@angular/core';
import { Http, Headers, RequestOptions } from '@angular/http';
import { CommunityInfo } from './communityInfo';
import{EnvProperties} from '../../utils/properties/env-properties';
@Injectable()
export class CommunityService {
constructor(private http:Http) {
}
getCommunity(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));
}
updateCommunity(url: string, community:any) {
let headers = new Headers({'Content-Type': 'application/json'});
let options = new RequestOptions({headers: headers});
let body = JSON.stringify(community);
return this.http.post(url, body, options)
/*.map(res => res.json())*/
}
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);
}
iscommunityRI(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"));
}
isSubscribedToCommunity(pid:string, email:string, url:string){
return this.http.get(url+"/community/"+pid+"/subscribers")
.map(res => ((<any>res =="")?{}:<any> res.json()))
.map(res => {
if(res.subscribers && res.subscribers != null){
for(var i =0; i< res.subscribers.length; i++ ){
if(res.subscribers[i]!=null && res.subscribers[i].email == email){
return true;
}
}
}
return false;
});
}
private parseCommunity(data:any): CommunityInfo {
let resData = Array.isArray(data) ? data[0] : data;
let community: CommunityInfo = new CommunityInfo();
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;
community['status'] = "all";
if(resData.hasOwnProperty('status')){
community['status'] = resData.status;
let status = ["all","hidden","manager"];
if(status.indexOf(community['status']) ==-1){
community['status'] = "hidden";
}
}
if (resData.type != null) {
community['type'] = resData.type;
}
if(resData.managers != null) {
if(community['managers'] == undefined) {
community['managers'] = new Array<string>();
}
let managers = resData.managers;
let length = Array.isArray(managers) ? managers.length : 1;
for(let i=0; i<length; i++) {
let manager = Array.isArray(managers) ? managers[i] : managers;
community.managers[i] = manager;
}
}
if(resData.subjects != null) {
if(community['subjects'] == undefined) {
community['subjects'] = new Array<string>();
}
let subjects = resData.subjects;
let length = Array.isArray(subjects) ? subjects.length : 1;
for(let i=0; i<length; i++) {
let subject = Array.isArray(subjects) ? subjects[i] : subjects;
community.subjects[i] = subject;
}
}
return community;
}
}