Project

General

Profile

1
import { Injectable } from '@angular/core';
2
import { Http,  Headers, RequestOptions } from '@angular/http';
3
import {HttpClient, HttpHeaders} from "@angular/common/http";
4
import { CommunityInfo } from './communityInfo';
5
import {EnvProperties} from '../../utils/properties/env-properties';
6
import {map} from "rxjs/operators";
7
import {COOKIE} from "../../login/utils/helper.class";
8

    
9
@Injectable()
10
export class CommunityService {
11

    
12
    constructor(private http: HttpClient) {
13
      }
14

    
15
    getCommunity(properties: EnvProperties, url: string) {
16
        return this.http.get((properties.useCache) ? (properties.cacheUrl + encodeURIComponent(url)) : url)
17
                        //.map(res => <any> res.json())
18
                        .pipe(map(res => this.parseCommunity(res)));
19
    }
20

    
21
    updateCommunity(url: string, community: any) {
22
        //const headers = new Headers({'Content-Type': 'application/json'});
23
        //const options = new RequestOptions({headers: headers});
24

    
25
        const options = {
26
          headers: new HttpHeaders({
27
            'Content-Type': 'application/json',
28
          })
29
        };
30

    
31
        const body = JSON.stringify(community);
32
        return this.http.post(url, body, options);
33
                          /*.map(res => res.json())*/
34
    }
35

    
36
    isCommunityManager(properties: EnvProperties, url: string, manager: string) {
37
      return this.http.get((properties.useCache) ? (properties.cacheUrl + encodeURIComponent(url)) : url)
38
                      //.map(res => <any> res.json())
39
                      .pipe(map(res => this.parseCommunity(res)))
40
                      .pipe(map(community => community.managers.indexOf(manager) !== -1));
41
    }
42

    
43
    isRIType(properties: EnvProperties, url: string) {
44
      return this.http.get((properties.useCache) ? (properties.cacheUrl + encodeURIComponent(url)) : url)
45
          //.map(res => <any> res.json())
46
          .pipe(map(res => this.parseCommunity(res)))
47
          .pipe(map(community => (community && community.type && community.type === 'ri')));
48
    }
49

    
50
    isCommunityType(properties: EnvProperties, url: string) {
51
        return this.http.get((properties.useCache) ? (properties.cacheUrl + encodeURIComponent(url)) : url)
52
            //.map(res => <any> res.json())
53
            .pipe(map(res => this.parseCommunity(res)))
54
            .pipe(map(community => (community && community.type && community.type === 'community')));
55
    }
56

    
57
    isSubscribedToCommunity(pid: string, email: string, url: string) {
58
      return this.http.get(url + '/community/' + pid + '/subscribers')
59
                      //.map(res =>  ((<any>res === '') ? {} : <any> res.json()))
60
                      .pipe(map(res => {
61
                        if (res['subscribers'] && res['subscribers'] != null) {
62

    
63
                          for (let i = 0; i < res['subscribers'].length; i++ ) {
64
                            if (res['subscribers'][i] != null && res['subscribers'][i].email === email) {
65
                              return true;
66
                            }
67
                          }
68
                        }
69
                        return false;
70

    
71
                      }));
72
    }
73

    
74
    private parseCommunity(data: any): CommunityInfo {
75

    
76
        const resData = Array.isArray(data) ? data[0] : data;
77

    
78
        const community: CommunityInfo = new CommunityInfo();
79
        community['title'] = resData.name;
80
        community['shortTitle'] = resData.shortName;
81
        community['communityId'] = resData.id;
82
        community['queryId'] = resData.queryId;
83
        community['logoUrl'] = resData.logoUrl;
84
        community['description'] = resData.description;
85
        community['date'] = resData.creationDate;
86
        community['zenodoCommunity'] = resData.zenodoCommunity;
87
        community['status'] = 'all';
88
        if (resData.hasOwnProperty('status')) {
89
          community['status'] = resData.status;
90
          const status = ['all', 'hidden', 'manager'];
91
          if (status.indexOf(community['status']) === -1) {
92
            community['status'] = 'hidden';
93
          }
94
        }
95
        if (resData.type != null) {
96
              community['type'] = resData.type;
97
        }
98

    
99
        if (resData.managers != null) {
100
            if (community['managers'] === undefined) {
101
              community['managers'] = new Array<string>();
102
            }
103

    
104
            const managers = resData.managers;
105
            const length = Array.isArray(managers) ? managers.length : 1;
106

    
107
            for (let i = 0; i < length; i++) {
108
              const manager = Array.isArray(managers) ? managers[i] : managers;
109
              community.managers[i] = manager;
110
            }
111
        }
112

    
113
        if (resData.subjects != null) {
114
            if (community['subjects'] === undefined) {
115
              community['subjects'] = new Array<string>();
116
            }
117

    
118
            const subjects = resData.subjects;
119
            const length = Array.isArray(subjects) ? subjects.length : 1;
120

    
121
            for (let i = 0; i < length; i++) {
122
              const subject = Array.isArray(subjects) ? subjects[i] : subjects;
123
              community.subjects[i] = subject;
124
            }
125
        }
126
        return community;
127
    }
128

    
129
}
(1-1/2)