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 {response} from "express";
7
import {el} from "@angular/platform-browser/testing/src/browser_util";
8

    
9
export interface Annotation {
10
    text: string;
11
    type: 'semantic' | 'keyword' | 'comment';
12
    urls?: string[];
13
}
14

    
15
@Injectable({
16
    providedIn: "root"
17
})
18
export class AnnotationService {
19

    
20

    
21
    constructor(private http: HttpClient) {
22
    }
23

    
24
    getAllAnnotations(properties: EnvProperties, source: string): Observable<Annotation[]> {
25
        let url = properties.b2noteAPIURL + 'annotations?type[]=semantic&type[]=keyword&type[]=comment&format=json-ld&download=false&target-source=' + encodeURIComponent(source);
26
        return this.http.get<Annotation[]>(url).pipe(map((response: any[]) => {
27
            return this.parseAnnotations(response);
28
        }));
29
    }
30
    
31
    private parseAnnotations(response: any[]): Annotation[] {
32
        let annotations: Annotation[] = [];
33
        if(response && response.length > 0) {
34
            response.forEach(value => {
35
                let body = value.body;
36
                if(body.type === 'TextualBody') {
37
                    if(body.purpose === 'tagging') {
38
                        annotations.push({
39
                            text: body.value,
40
                            type: "keyword"
41
                        });
42
                    } else {
43
                        annotations.push({
44
                            text: body.value,
45
                            type: "comment"
46
                        });
47
                    }
48
                } else {
49
                    let items = body.items;
50
                    let urls: string[] = [];
51
                    let text: string = null;
52
                    items.forEach(item => {
53
                        if(item.type === 'SpecificResource') {
54
                            urls.push(item.source);
55
                        } else if(item.type === 'TextualBody') {
56
                            text = item.value;
57
                        }
58
                    });
59
                    annotations.push({
60
                        text: text,
61
                        type: "semantic",
62
                        urls: urls
63
                    });
64
                }
65
            });
66
        }
67
        return annotations.sort((a, b) => {
68
            if(a.type === b.type) {
69
                return 0;
70
            } else if (a.type === 'semantic') {
71
                return -1;
72
            } else if(b.type === 'semantic') {
73
                return 1;
74
            } else if(a.type === 'keyword') {
75
                return  -1;
76
            } else if(b.type === 'keyword') {
77
                return  1;
78
            }
79
        });
80
    }
81
}
(4-4/4)