openaire-library/landingPages/annotation/annotation.service.ts

81 lines
2.8 KiB
TypeScript

import {Injectable} from "@angular/core";
import {HttpClient} from "@angular/common/http";
import {Observable} from "rxjs";
import {EnvProperties} from "../../utils/properties/env-properties";
import {map} from "rxjs/operators";
import {response} from "express";
import {el} from "@angular/platform-browser/testing/src/browser_util";
export interface Annotation {
text: string;
type: 'semantic' | 'keyword' | 'comment';
urls?: string[];
}
@Injectable({
providedIn: "root"
})
export class AnnotationService {
constructor(private http: HttpClient) {
}
getAllAnnotations(properties: EnvProperties, source: string): Observable<Annotation[]> {
let url = properties.b2noteAPIURL + 'annotations?type[]=semantic&type[]=keyword&type[]=comment&format=json-ld&download=false&target-source=' + encodeURIComponent(source);
return this.http.get<Annotation[]>(url).pipe(map((response: any[]) => {
return this.parseAnnotations(response);
}));
}
private parseAnnotations(response: any[]): Annotation[] {
let annotations: Annotation[] = [];
if(response && response.length > 0) {
response.forEach(value => {
let body = value.body;
if(body.type === 'TextualBody') {
if(body.purpose === 'tagging') {
annotations.push({
text: body.value,
type: "keyword"
});
} else {
annotations.push({
text: body.value,
type: "comment"
});
}
} else {
let items = body.items;
let urls: string[] = [];
let text: string = null;
items.forEach(item => {
if(item.type === 'SpecificResource') {
urls.push(item.source);
} else if(item.type === 'TextualBody') {
text = item.value;
}
});
annotations.push({
text: text,
type: "semantic",
urls: urls
});
}
});
}
return annotations.sort((a, b) => {
if(a.type === b.type) {
return 0;
} else if (a.type === 'semantic') {
return -1;
} else if(b.type === 'semantic') {
return 1;
} else if(a.type === 'keyword') {
return -1;
} else if(b.type === 'keyword') {
return 1;
}
});
}
}