Project

General

Profile

1
import {Injectable} from "@angular/core";
2
import {HttpClient} from "@angular/common/http";
3
import {Observable} from "rxjs";
4
import {EnvProperties} from "../../utils/properties/env-properties";
5
import {map} from "rxjs/operators";
6
import {properties} from "../../../../environments/environment";
7

    
8
export interface AnnotationTarget {
9
  id: string;
10
  url: string;
11
}
12

    
13
export interface Annotation {
14
  text: string;
15
  type: 'semantic' | 'keyword' | 'comment';
16
  targets?: AnnotationTarget[];
17
  targetSize?: number;
18
}
19

    
20
@Injectable({
21
  providedIn: "root"
22
})
23
export class AnnotationService {
24
  
25
  api = 'api/';
26
  
27
  constructor(private http: HttpClient) {
28
  }
29
  
30
  getAllAnnotations(source: string): Observable<Annotation[]> {
31
    let url = properties.b2noteAPIURL + this.api + 'annotations?target-id[]=' + encodeURIComponent(source);
32
    return this.http.get<Annotation[]>(url).pipe(map((response: any[]) => {
33
      return this.parseAnnotations(response);
34
    }));
35
  }
36
  
37
  getAnnotationTargets(value: string, type: 'semantic' | 'keyword' | 'comment'): Observable<AnnotationTarget[]> {
38
    let url = properties.b2noteAPIURL + this.api + 'annotations?value=' + encodeURIComponent(value) + '&type[]=' + type;
39
    return this.http.get<AnnotationTarget[]>(url).pipe(map((response: any[]) => {
40
      return this.parseAnnotationTargets(response);
41
    }));
42
  }
43
  
44
  private parseAnnotations(response: any[]): Annotation[] {
45
    let annotations: Annotation[] = [];
46
    if (response && response.length > 0) {
47
      response.forEach(value => {
48
        if (value.visibility === 'public') {
49
          let body = value.body;
50
          if (body.type === 'TextualBody') {
51
            if (body.purpose === 'tagging') {
52
              annotations.push({
53
                text: body.value,
54
                type: "keyword"
55
              });
56
            } else {
57
              annotations.push({
58
                text: body.value,
59
                type: "comment"
60
              });
61
            }
62
          } else {
63
            let items = body.items;
64
            let text: string = null;
65
            items.forEach(item => {
66
              if (item.type === 'TextualBody') {
67
                text = item.value;
68
              }
69
            });
70
            annotations.push({
71
              text: text,
72
              type: "semantic"
73
            });
74
          }
75
        }
76
      });
77
    }
78
    return annotations;
79
  }
80
  
81
  private parseAnnotationTargets(response: any[]): AnnotationTarget[] {
82
    let targets: AnnotationTarget[] = [];
83
    if (response && response.length > 0) {
84
      response.forEach(value => {
85
        if (value.visibility === 'public') {
86
          targets.push({
87
            id: value.target.id,
88
            url: value.target.source
89
          });
90
        }
91
      });
92
    }
93
    return  targets;
94
  }
95
}
(4-4/4)