2020-01-28 15:09:32 +01:00
|
|
|
import {Injectable} from "@angular/core";
|
|
|
|
import {HttpClient} from "@angular/common/http";
|
|
|
|
import {Observable} from "rxjs";
|
2020-04-07 19:09:31 +02:00
|
|
|
import {EnvProperties} from "../../utils/properties/env-properties";
|
|
|
|
import {map} from "rxjs/operators";
|
2020-11-25 17:09:36 +01:00
|
|
|
import {properties} from "../../../../environments/environment";
|
|
|
|
|
|
|
|
export interface AnnotationTarget {
|
|
|
|
id: string;
|
|
|
|
url: string;
|
|
|
|
}
|
2020-04-07 19:09:31 +02:00
|
|
|
|
|
|
|
export interface Annotation {
|
2020-11-25 17:09:36 +01:00
|
|
|
text: string;
|
|
|
|
type: 'semantic' | 'keyword' | 'comment';
|
|
|
|
targets?: AnnotationTarget[];
|
|
|
|
targetSize?: number;
|
2020-04-07 19:09:31 +02:00
|
|
|
}
|
2020-01-28 15:09:32 +01:00
|
|
|
|
|
|
|
@Injectable({
|
2020-11-25 17:09:36 +01:00
|
|
|
providedIn: "root"
|
2020-01-28 15:09:32 +01:00
|
|
|
})
|
|
|
|
export class AnnotationService {
|
2020-11-25 17:09:36 +01:00
|
|
|
|
|
|
|
api = 'api/';
|
|
|
|
|
|
|
|
constructor(private http: HttpClient) {
|
|
|
|
}
|
|
|
|
|
|
|
|
getAllAnnotations(source: string): Observable<Annotation[]> {
|
2020-11-26 18:03:50 +01:00
|
|
|
let url = properties.b2noteAPIURL + this.api + 'annotations?target-id[]=' + encodeURIComponent(source);
|
2020-11-25 17:09:36 +01:00
|
|
|
return this.http.get<Annotation[]>(url).pipe(map((response: any[]) => {
|
|
|
|
return this.parseAnnotations(response);
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
2020-11-26 18:03:50 +01:00
|
|
|
getAnnotationTargets(value: string, type: 'semantic' | 'keyword' | 'comment'): Observable<AnnotationTarget[]> {
|
|
|
|
let url = properties.b2noteAPIURL + this.api + 'annotations?value=' + encodeURIComponent(value) + '&type[]=' + type;
|
2020-11-25 17:09:36 +01:00
|
|
|
return this.http.get<AnnotationTarget[]>(url).pipe(map((response: any[]) => {
|
2020-11-26 18:03:50 +01:00
|
|
|
return this.parseAnnotationTargets(response);
|
2020-11-25 17:09:36 +01:00
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
|
|
|
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"
|
2020-04-07 19:09:31 +02:00
|
|
|
});
|
2020-11-25 17:09:36 +01:00
|
|
|
}
|
2020-04-07 19:09:31 +02:00
|
|
|
}
|
2020-11-25 17:09:36 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
return annotations;
|
|
|
|
}
|
|
|
|
|
2020-11-26 18:03:50 +01:00
|
|
|
private parseAnnotationTargets(response: any[]): AnnotationTarget[] {
|
2020-11-25 17:09:36 +01:00
|
|
|
let targets: AnnotationTarget[] = [];
|
|
|
|
if (response && response.length > 0) {
|
|
|
|
response.forEach(value => {
|
|
|
|
if (value.visibility === 'public') {
|
2020-11-26 18:03:50 +01:00
|
|
|
targets.push({
|
|
|
|
id: value.target.id,
|
|
|
|
url: value.target.source
|
|
|
|
});
|
2020-11-25 17:09:36 +01:00
|
|
|
}
|
|
|
|
});
|
2020-01-28 15:09:32 +01:00
|
|
|
}
|
2020-11-25 17:09:36 +01:00
|
|
|
return targets;
|
|
|
|
}
|
2020-05-29 11:50:49 +02:00
|
|
|
}
|