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 {properties} from "../../../../environments/environment"; export interface AnnotationTarget { id: string; url: string; } export interface Annotation { text: string; type: 'semantic' | 'keyword' | 'comment'; targets?: AnnotationTarget[]; targetSize?: number; } @Injectable({ providedIn: "root" }) export class AnnotationService { api = 'api/'; constructor(private http: HttpClient) { } getAllAnnotations(source: string): Observable { let url = properties.b2noteAPIURL + this.api + 'annotations?target-id=' + encodeURIComponent(source); return this.http.get(url).pipe(map((response: any[]) => { return this.parseAnnotations(response); })); } getAnnotationsTargets(value: string, type: 'semantic' | 'keyword' | 'comment'): Observable { let url = properties.b2noteAPIURL + this.api + 'annotations?value=' + encodeURIComponent(value); return this.http.get(url).pipe(map((response: any[]) => { return this.parseAnnotationTargets(response, type); })); } private parseAnnotations(response: any[]): Annotation[] { let annotations: Annotation[] = []; if (response && response.length > 0) { response.forEach(value => { if (value.visibility === 'public') { 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 text: string = null; items.forEach(item => { if (item.type === 'TextualBody') { text = item.value; } }); annotations.push({ text: text, type: "semantic" }); } } }); } return annotations; } private parseAnnotationTargets(response: any[], type: 'semantic' | 'keyword' | 'comment'): AnnotationTarget[] { let targets: AnnotationTarget[] = []; if (response && response.length > 0) { response.forEach(value => { if (value.visibility === 'public') { let body = value.body; if (body.type === 'TextualBody') { if (body.purpose === 'tagging' && type === 'keyword') { targets.push({ id: value.target.id, url: value.target.source }); } else if(type === 'comment'){ targets.push({ id: value.target.id, url: value.target.source }); } } else if(type === 'semantic'){ targets.push({ id: value.target.id, url: value.target.source }); } } }); } return targets; } }