Commit 27ce4cd3 authored by Alejandro Celaya's avatar Alejandro Celaya Committed by Alejandro Celaya

Migrate AnnotationsService to TS

parent adbad6f8
import { generateHexString } from '../../shared/random'; import { generateHexString } from '../../shared/random';
import type { AnnotationData } from '../../types/annotator';
import type { Annotation, SavedAnnotation } from '../../types/api';
import type { AnnotationEventType } from '../../types/config';
import * as metadata from '../helpers/annotation-metadata'; import * as metadata from '../helpers/annotation-metadata';
import { import {
defaultPermissions, defaultPermissions,
privatePermissions, privatePermissions,
sharedPermissions, sharedPermissions,
} from '../helpers/permissions'; } from '../helpers/permissions';
import type { SidebarStore } from '../store';
/** import type { AnnotationActivityService } from './annotation-activity';
* @typedef {import('../../types/api').Annotation} Annotation import type { APIService } from './api';
* @typedef {import('../../types/annotator').AnnotationData} AnnotationData
* @typedef {import('../../types/api').SavedAnnotation} SavedAnnotation
*/
/** /**
* A service for creating, updating and persisting annotations both in the * A service for creating, updating and persisting annotations both in the
...@@ -18,12 +18,15 @@ import { ...@@ -18,12 +18,15 @@ import {
*/ */
// @inject // @inject
export class AnnotationsService { export class AnnotationsService {
/** private _activity: AnnotationActivityService;
* @param {import('./api').APIService} api private _api: APIService;
* @param {import('./annotation-activity').AnnotationActivityService} annotationActivity private _store: SidebarStore;
* @param {import('../store').SidebarStore} store
*/ constructor(
constructor(annotationActivity, api, store) { annotationActivity: AnnotationActivityService,
api: APIService,
store: SidebarStore
) {
this._activity = annotationActivity; this._activity = annotationActivity;
this._api = api; this._api = api;
this._store = store; this._store = store;
...@@ -32,11 +35,9 @@ export class AnnotationsService { ...@@ -32,11 +35,9 @@ export class AnnotationsService {
/** /**
* Apply changes for the given `annotation` from its draft in the store (if * Apply changes for the given `annotation` from its draft in the store (if
* any) and return a new object with those changes integrated. * any) and return a new object with those changes integrated.
*
* @param {Annotation} annotation
*/ */
_applyDraftChanges(annotation) { private _applyDraftChanges(annotation: Annotation): Annotation {
const changes = {}; const changes: Partial<Annotation> = {};
const draft = this._store.getDraft(annotation); const draft = this._store.getDraft(annotation);
if (draft) { if (draft) {
...@@ -53,12 +54,11 @@ export class AnnotationsService { ...@@ -53,12 +54,11 @@ export class AnnotationsService {
/** /**
* Extend new annotation objects with defaults and permissions. * Extend new annotation objects with defaults and permissions.
*
* @param {Omit<AnnotationData, '$tag'>} annotationData
* @param {Date} now
* @return {Annotation}
*/ */
_initialize(annotationData, now = new Date()) { private _initialize(
annotationData: Omit<AnnotationData, '$tag'>,
now = new Date()
): Annotation {
const defaultPrivacy = this._store.getDefault('annotationPrivacy'); const defaultPrivacy = this._store.getDefault('annotationPrivacy');
const groupid = this._store.focusedGroupId(); const groupid = this._store.focusedGroupId();
const profile = this._store.profile(); const profile = this._store.profile();
...@@ -109,11 +109,8 @@ export class AnnotationsService { ...@@ -109,11 +109,8 @@ export class AnnotationsService {
* Populate a new annotation object from `annotation` and add it to the store. * Populate a new annotation object from `annotation` and add it to the store.
* Create a draft for it unless it's a highlight and clear other empty * Create a draft for it unless it's a highlight and clear other empty
* drafts out of the way. * drafts out of the way.
*
* @param {Omit<AnnotationData, '$tag'>} annotationData
* @param {Date} now
*/ */
create(annotationData, now = new Date()) { create(annotationData: Omit<AnnotationData, '$tag'>, now = new Date()) {
const annotation = this._initialize(annotationData, now); const annotation = this._initialize(annotationData, now);
this._store.addAnnotations([annotation]); this._store.addAnnotations([annotation]);
...@@ -170,10 +167,8 @@ export class AnnotationsService { ...@@ -170,10 +167,8 @@ export class AnnotationsService {
/** /**
* Delete an annotation via the API and update the store. * Delete an annotation via the API and update the store.
*
* @param {SavedAnnotation} annotation
*/ */
async delete(annotation) { async delete(annotation: SavedAnnotation) {
await this._api.annotation.delete({ id: annotation.id }); await this._api.annotation.delete({ id: annotation.id });
this._activity.reportActivity('delete', annotation); this._activity.reportActivity('delete', annotation);
this._store.removeAnnotations([annotation]); this._store.removeAnnotations([annotation]);
...@@ -181,10 +176,8 @@ export class AnnotationsService { ...@@ -181,10 +176,8 @@ export class AnnotationsService {
/** /**
* Flag an annotation for review by a moderator. * Flag an annotation for review by a moderator.
*
* @param {SavedAnnotation} annotation
*/ */
async flag(annotation) { async flag(annotation: SavedAnnotation) {
await this._api.annotation.flag({ id: annotation.id }); await this._api.annotation.flag({ id: annotation.id });
this._activity.reportActivity('flag', annotation); this._activity.reportActivity('flag', annotation);
this._store.updateFlagStatus(annotation.id, true); this._store.updateFlagStatus(annotation.id, true);
...@@ -192,11 +185,8 @@ export class AnnotationsService { ...@@ -192,11 +185,8 @@ export class AnnotationsService {
/** /**
* Create a reply to `annotation` by the user `userid` and add to the store. * Create a reply to `annotation` by the user `userid` and add to the store.
*
* @param {SavedAnnotation} annotation
* @param {string} userid
*/ */
reply(annotation, userid) { reply(annotation: SavedAnnotation, userid: string) {
const replyAnnotation = { const replyAnnotation = {
group: annotation.group, group: annotation.group,
permissions: metadata.isPublic(annotation) permissions: metadata.isPublic(annotation)
...@@ -213,13 +203,10 @@ export class AnnotationsService { ...@@ -213,13 +203,10 @@ export class AnnotationsService {
* Save new (or update existing) annotation. On success, * Save new (or update existing) annotation. On success,
* the annotation's `Draft` will be removed and the annotation added * the annotation's `Draft` will be removed and the annotation added
* to the store. * to the store.
*
* @param {Annotation} annotation
*/ */
async save(annotation) { async save(annotation: Annotation) {
let saved; let saved: Promise<Annotation>;
/** @type {import('../../types/config').AnnotationEventType} */ let eventType: AnnotationEventType;
let eventType;
const annotationWithChanges = this._applyDraftChanges(annotation); const annotationWithChanges = this._applyDraftChanges(annotation);
...@@ -234,8 +221,7 @@ export class AnnotationsService { ...@@ -234,8 +221,7 @@ export class AnnotationsService {
eventType = 'update'; eventType = 'update';
} }
/** @type {Annotation} */ let savedAnnotation: Annotation;
let savedAnnotation;
this._store.annotationSaveStarted(annotation); this._store.annotationSaveStarted(annotation);
try { try {
savedAnnotation = await saved; savedAnnotation = await saved;
...@@ -246,9 +232,9 @@ export class AnnotationsService { ...@@ -246,9 +232,9 @@ export class AnnotationsService {
// Copy local/internal fields from the original annotation to the saved // Copy local/internal fields from the original annotation to the saved
// version. // version.
for (let [key, value] of Object.entries(annotation)) { for (const [key, value] of Object.entries(annotation)) {
if (key.startsWith('$')) { if (key.startsWith('$')) {
const fields = /** @type {Record<string, any>} */ (savedAnnotation); const fields: Record<string, any> = savedAnnotation;
fields[key] = value; fields[key] = value;
} }
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment