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