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
    urlSize?: number;
14
}
15

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

    
21

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

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