1
|
import { Injectable } from '@angular/core';
|
2
|
import { Http, Response, Headers, RequestOptions } from '@angular/http';
|
3
|
import { Observable } from 'rxjs/Rx';
|
4
|
import {COOKIE} from "../../login/utils/helper.class"
|
5
|
|
6
|
|
7
|
@Injectable()
|
8
|
export class SubscribeService {
|
9
|
|
10
|
constructor(private http:Http) {
|
11
|
}
|
12
|
getCommunitySubscribers(pid:string, url:string){
|
13
|
return this.http.get(url+"/community/"+pid+"/subscribers")
|
14
|
.map(res => <any> res.json()).do(res => {console.log(res)}).do(res => {console.log(res)});
|
15
|
}
|
16
|
|
17
|
isSubscribedToCommunity(pid:string, email:string, url:string){
|
18
|
return this.http.get(url+"/community/"+pid+"/subscribers")
|
19
|
.map(res => ((<any>res =="")?{}:<any> res.json()))
|
20
|
|
21
|
.map(res => {
|
22
|
if(res.status && res.status != 200) {
|
23
|
return null;
|
24
|
}
|
25
|
if(res.subscribers && res.subscribers != null){
|
26
|
|
27
|
for(var i =0; i< res.subscribers.length; i++ ){
|
28
|
if(res.subscribers[i]!=null && res.subscribers[i].email == email){
|
29
|
return true;
|
30
|
}
|
31
|
}
|
32
|
}
|
33
|
return false;
|
34
|
|
35
|
}).do(res => {console.log("Response is "+res)});
|
36
|
}
|
37
|
subscribeToCommunity(pid:string, email:string, url:string){
|
38
|
var subscriber = {"email":email};
|
39
|
return this.http.post(url+"/community/"+pid+"/subscribers", JSON.stringify(subscriber), this.getAuthOptionsWithBody())
|
40
|
.map(res => <any> res.json())
|
41
|
.do(res => {console.log("Response is "+res)});
|
42
|
}
|
43
|
unSubscribeToCommunity(pid:string, email:string, url:string){
|
44
|
|
45
|
return this.http.post(url+"/community/"+pid+"/subscribers/delete", JSON.stringify([email]), this.getAuthOptionsWithBody())
|
46
|
.map(res => <any> res.json())
|
47
|
.do(res => {console.log("Response is "+res)});
|
48
|
}
|
49
|
|
50
|
public getAuthOptionsWithBody():RequestOptions{
|
51
|
let headers = new Headers();
|
52
|
headers.append('Content-Type', 'application/json');
|
53
|
headers.append('X-XSRF-TOKEN', COOKIE.getCookie(COOKIE.cookieName_id));
|
54
|
let options = new RequestOptions({ headers: headers, withCredentials:true });
|
55
|
return options;
|
56
|
}
|
57
|
|
58
|
}
|