import { Injectable } from '@angular/core'; import { Http, Response, Headers, RequestOptions } from '@angular/http'; import { HttpClient } from '@angular/common/http'; import { HttpHeaders } from '@angular/common/http'; import { Observable } from 'rxjs'; import {COOKIE} from "../../login/utils/helper.class" import { map } from "rxjs/operators"; import {CustomOptions} from "../../services/servicesUtils/customOptions.class"; @Injectable() export class SubscribeService { constructor(private http:HttpClient) { } getCommunitySubscribers(pid:string, url:string){ return this.http.get(url+"/community/"+pid+"/subscribers"); //.map(res => res.json()); } isSubscribedToCommunity(pid:string, email:string, url:string){ return this.http.get(url+"/community/"+pid+"/subscribers") //.map(res => ((res =="")?{}: res.json())) .pipe(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), CustomOptions.getAuthOptionsWithBody()); //.map(res => res.json()); } unSubscribeToCommunity(pid:string, email:string, url:string){ return this.http.post(url+"/community/"+pid+"/subscribers/delete", JSON.stringify([email]), CustomOptions.getAuthOptionsWithBody()); //.map(res => res.json()); } getCommunitiesSubscribedTo(email:string, url:string){ return this.http.get(url+"/subscriber/communities?email="+email); //.map(res => 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; } */ public getAuthOptionsWithBody():any { return { headers: new HttpHeaders({ 'Content-Type': 'application/json', 'X-XSRF-TOKEN': COOKIE.getCookie(COOKIE.cookieName_id) }), withCredentials: true }; } }