openaire-library/utils/subscribe/subscribe.service.ts

57 lines
2.1 KiB
TypeScript

import { Injectable } from '@angular/core';
import { Http, Response, Headers, RequestOptions } from '@angular/http';
import { Observable } from 'rxjs/Rx';
import {COOKIE} from "../../login/utils/helper.class"
@Injectable()
export class SubscribeService {
constructor(private http:Http) {
}
getCommunitySubscribers(pid:string, url:string){
return this.http.get(url+"/community/"+pid+"/subscribers")
.map(res => <any> res.json());
}
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.status && res.status != 200) {
return null;
}
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;
});
}
subscribeToCommunity(pid:string, email:string, url:string){
var subscriber = {"email":email};
return this.http.post(url+"/community/"+pid+"/subscribers", JSON.stringify(subscriber), this.getAuthOptionsWithBody())
.map(res => <any> res.json());
}
unSubscribeToCommunity(pid:string, email:string, url:string){
return this.http.post(url+"/community/"+pid+"/subscribers/delete", JSON.stringify([email]), this.getAuthOptionsWithBody())
.map(res => <any> res.json());
}
public getAuthOptionsWithBody():RequestOptions{
let headers = new Headers();
headers.append('Content-Type', 'application/json');
headers.append('X-XSRF-TOKEN', COOKIE.getCookie(COOKIE.cookieName_id));
let options = new RequestOptions({ headers: headers, withCredentials:true });
return options;
}
}