From fabb86b78874a38619d821b315eaf9ce204e1afe Mon Sep 17 00:00:00 2001 From: Melissa Zhang Date: Tue, 28 Jul 2020 21:27:38 -0700 Subject: "handle editing annotations in client, bypass authorization issues" --- .../apis/HypothesisAuthenticationManager.tsx | 2 +- src/client/apis/hypothesis/HypothesisApiUtils.ts | 116 --------------------- src/client/apis/hypothesis/HypothesisUtils.ts | 77 ++++++++++++++ src/client/documents/Documents.ts | 1 - src/client/views/MainView.tsx | 2 +- src/client/views/linking/LinkMenuItem.tsx | 2 +- src/client/views/nodes/DocumentLinksButton.tsx | 2 +- src/client/views/nodes/DocumentView.tsx | 1 - 8 files changed, 81 insertions(+), 122 deletions(-) delete mode 100644 src/client/apis/hypothesis/HypothesisApiUtils.ts create mode 100644 src/client/apis/hypothesis/HypothesisUtils.ts (limited to 'src') diff --git a/src/client/apis/HypothesisAuthenticationManager.tsx b/src/client/apis/HypothesisAuthenticationManager.tsx index d4a81b3eb..653f21a7a 100644 --- a/src/client/apis/HypothesisAuthenticationManager.tsx +++ b/src/client/apis/HypothesisAuthenticationManager.tsx @@ -6,7 +6,7 @@ import { Opt } from "../../fields/Doc"; import { Networking } from "../Network"; import "./HypothesisAuthenticationManager.scss"; import { Scripting } from "../util/Scripting"; -import { Hypothesis } from "./hypothesis/HypothesisApiUtils"; +import { Hypothesis } from "./hypothesis/HypothesisUtils"; const prompt = "Paste authorization code here..."; diff --git a/src/client/apis/hypothesis/HypothesisApiUtils.ts b/src/client/apis/hypothesis/HypothesisApiUtils.ts deleted file mode 100644 index e2fb91af9..000000000 --- a/src/client/apis/hypothesis/HypothesisApiUtils.ts +++ /dev/null @@ -1,116 +0,0 @@ -import { StrCast, Cast } from "../../../fields/Types"; -import HypothesisAuthenticationManager from "../HypothesisAuthenticationManager"; -import { SearchUtil } from "../../util/SearchUtil"; -import { action } from "mobx"; -import { Doc } from "../../../fields/Doc"; -import { DocumentType } from "../../documents/DocumentTypes"; -import { WebField } from "../../../fields/URLField"; -import { DocumentManager } from "../../util/DocumentManager"; - -export namespace Hypothesis { - - const getCredentials = async () => HypothesisAuthenticationManager.Instance.fetchAccessToken(); - - export const fetchAnnotation = async (annotationId: string) => { - const response = await fetch(`https://api.hypothes.is/api/annotations/${annotationId}`); - if (response.ok) { - return response.json(); - } else { - throw new Error('DASH: Error in fetchAnnotation GET request'); - } - }; - - /** - * Searches for annotations authored by the current user that contain @param searchKeyWord - */ - export const searchAnnotation = async (searchKeyWord: string) => { - const credentials = await getCredentials(); - const base = 'https://api.hypothes.is/api/search'; - const request = base + `?user=acct:${credentials.username}@hypothes.is&text=${searchKeyWord}`; - console.log("DASH Querying " + request); - const response = await fetch(request); - if (response.ok) { - return response.json(); - } else { - throw new Error('DASH: Error in searchAnnotation GET request'); - } - }; - - export const fetchUser = async (apiKey: string) => { - const response = await fetch('https://api.hypothes.is/api/profile', { - headers: { - 'Authorization': `Bearer ${apiKey}`, - }, - }); - if (response.ok) { - return response.json(); - } else { - throw new Error('DASH: Error in fetchUser GET request'); - } - }; - - export const editAnnotation = async (annotationId: string, newText: string) => { - console.log("DASH dispatching editAnnotation"); - const credentials = await getCredentials(); - document.dispatchEvent(new CustomEvent<{ newText: string, id: string, apiKey: string }>("editAnnotation", { - detail: { newText: newText, id: annotationId, apiKey: credentials.apiKey }, - bubbles: true - })); - }; - - /** - * Edit an annotation with ID @param annotationId to add a hyperlink to a Dash document, which needs to be - * written in the format [@param title](@param url) - */ - export const makeLink = async (title: string, url: string, annotationId: string) => { - const oldAnnotation = await fetchAnnotation(annotationId); - const oldText = StrCast(oldAnnotation.text); - const newHyperlink = `[${title}\n](${url})`; - const newText = oldText === "placeholder" ? newHyperlink : oldText + '\n\n' + newHyperlink; // if this is not the first link in the annotation, add link on new line - await editAnnotation(annotationId, newText); - }; - - export const deleteLink = async (annotationId: string, linkUrl: string) => { - const annotation = await fetchAnnotation(annotationId); - const regex = new RegExp(`\\[[^\\]]*\\]\\(${linkUrl}\\)`); // finds the link (written in [title](hyperlink) format) to be deleted - const out = annotation.text.replace(regex, ""); - editAnnotation(annotationId, out); - }; - - // Construct an URL which will scroll the web page to a specific annotation's position - export const makeAnnotationUrl = (annotationId: string, baseUrl: string) => { - return `https://hyp.is/${annotationId}/${baseUrl}`; // embeds the generic version of Hypothes.is client, not the Dash version - // return baseUrl + '#annotations:' + annotationId; - }; - - // Extract username from Hypothe.is's userId format - export const extractUsername = (userid: string) => { - const regex = new RegExp('(?<=\:)(.*?)(?=\@)/'); - return regex.exec(userid)![0]; - }; - - // Return corres - export const getSourceWebDoc = async (uri: string) => { - const results: Doc[] = []; - await SearchUtil.Search("web", true).then(action(async (res: SearchUtil.DocSearchResult) => { - const docs = await Promise.all(res.docs.map(async doc => (await Cast(doc.extendsDoc, Doc)) || doc)); - const filteredDocs = docs.filter(doc => - doc.type === DocumentType.WEB && doc.data - ); - filteredDocs.forEach(doc => { - console.log(Cast(doc.data, WebField)?.url.href); - if (uri === Cast(doc.data, WebField)?.url.href) results.push(doc); // TODO check history? imperfect matches? - }); - })); - - // TODO: open & return new Web doc with given uri if no matching Web docs are found - return results.length ? DocumentManager.Instance.getFirstDocumentView(results[0]) : undefined; - }; - - export const scrollToAnnotation = (annotationId: string) => { - document.dispatchEvent(new CustomEvent("scrollToAnnotation", { - detail: annotationId, - bubbles: true - })); - }; -} \ No newline at end of file diff --git a/src/client/apis/hypothesis/HypothesisUtils.ts b/src/client/apis/hypothesis/HypothesisUtils.ts new file mode 100644 index 000000000..e15620a91 --- /dev/null +++ b/src/client/apis/hypothesis/HypothesisUtils.ts @@ -0,0 +1,77 @@ +import { StrCast, Cast } from "../../../fields/Types"; +import HypothesisAuthenticationManager from "../HypothesisAuthenticationManager"; +import { SearchUtil } from "../../util/SearchUtil"; +import { action } from "mobx"; +import { Doc } from "../../../fields/Doc"; +import { DocumentType } from "../../documents/DocumentTypes"; +import { WebField } from "../../../fields/URLField"; +import { DocumentManager } from "../../util/DocumentManager"; + +export namespace Hypothesis { + export const fetchUser = async (apiKey: string) => { + const response = await fetch('https://api.hypothes.is/api/profile', { + headers: { + 'Authorization': `Bearer ${apiKey}`, + }, + }); + if (response.ok) { + return response.json(); + } else { + throw new Error('DASH: Error in fetchUser GET request'); + } + }; + + // Send Hypothes.is client request to edit an annotation to add a Dash hyperlink + export const makeLink = async (title: string, url: string, annotationId: string) => { + const newHyperlink = `[${title}\n](${url})`; + document.dispatchEvent(new CustomEvent<{ newHyperlink: string, id: string }>("addLink", { + detail: { newHyperlink: newHyperlink, id: annotationId }, + bubbles: true + })); + }; + + // Send Hypothes.is client request to edit an annotation to find and remove a dash hyperlink + export const deleteLink = async (annotationId: string, linkUrl: string) => { + document.dispatchEvent(new CustomEvent<{ targetUrl: string, id: string }>("deleteLink", { + detail: { targetUrl: linkUrl, id: annotationId }, + bubbles: true + })); + }; + + // Construct an URL which will scroll the web page to a specific annotation's position + export const makeAnnotationUrl = (annotationId: string, baseUrl: string) => { + return `https://hyp.is/${annotationId}/${baseUrl}`; // embeds the generic version of Hypothes.is client, not the Dash version + // return baseUrl + '#annotations:' + annotationId; + }; + + // Extract username from Hypothe.is's userId format + export const extractUsername = (userid: string) => { + const regex = new RegExp('(?<=\:)(.*?)(?=\@)/'); + return regex.exec(userid)![0]; + }; + + // Return corres + export const getSourceWebDoc = async (uri: string) => { + const results: Doc[] = []; + await SearchUtil.Search("web", true).then(action(async (res: SearchUtil.DocSearchResult) => { + const docs = await Promise.all(res.docs.map(async doc => (await Cast(doc.extendsDoc, Doc)) || doc)); + const filteredDocs = docs.filter(doc => + doc.type === DocumentType.WEB && doc.data + ); + filteredDocs.forEach(doc => { + console.log(Cast(doc.data, WebField)?.url.href); + if (uri === Cast(doc.data, WebField)?.url.href) results.push(doc); // TODO check history? imperfect matches? + }); + })); + + // TODO: open & return new Web doc with given uri if no matching Web docs are found + return results.length ? DocumentManager.Instance.getFirstDocumentView(results[0]) : undefined; + }; + + export const scrollToAnnotation = (annotationId: string) => { + document.dispatchEvent(new CustomEvent("scrollToAnnotation", { + detail: annotationId, + bubbles: true + })); + }; +} \ No newline at end of file diff --git a/src/client/documents/Documents.ts b/src/client/documents/Documents.ts index f1d4ec46c..529a25bd9 100644 --- a/src/client/documents/Documents.ts +++ b/src/client/documents/Documents.ts @@ -50,7 +50,6 @@ import { DashWebRTCVideo } from "../views/webcam/DashWebRTCVideo"; import { DocumentType } from "./DocumentTypes"; import { Networking } from "../Network"; import { Upload } from "../../server/SharedMediaTypes"; -import { Hypothesis } from "../apis/hypothesis/HypothesisApiUtils"; const path = require('path'); export interface DocumentOptions { diff --git a/src/client/views/MainView.tsx b/src/client/views/MainView.tsx index 64538d015..5bf9ac2a3 100644 --- a/src/client/views/MainView.tsx +++ b/src/client/views/MainView.tsx @@ -66,7 +66,7 @@ import { LinkDescriptionPopup } from './nodes/LinkDescriptionPopup'; import FormatShapePane from "./collections/collectionFreeForm/FormatShapePane"; import HypothesisAuthenticationManager from '../apis/HypothesisAuthenticationManager'; import CollectionMenu from './collections/CollectionMenu'; -import { Hypothesis } from '../apis/hypothesis/HypothesisApiUtils'; +import { Hypothesis } from '../apis/hypothesis/HypothesisUtils'; import { SelectionManager } from '../util/SelectionManager'; @observer diff --git a/src/client/views/linking/LinkMenuItem.tsx b/src/client/views/linking/LinkMenuItem.tsx index 8f7a2481d..475133010 100644 --- a/src/client/views/linking/LinkMenuItem.tsx +++ b/src/client/views/linking/LinkMenuItem.tsx @@ -15,7 +15,7 @@ import { setupMoveUpEvents, emptyFunction, Utils } from '../../../Utils'; import { DocumentView } from '../nodes/DocumentView'; import { DocumentLinksButton } from '../nodes/DocumentLinksButton'; import { LinkDocPreview } from '../nodes/LinkDocPreview'; -import { Hypothesis } from '../../apis/hypothesis/HypothesisApiUtils'; +import { Hypothesis } from '../../apis/hypothesis/HypothesisUtils'; import { Id } from '../../../fields/FieldSymbols'; import { Tooltip } from '@material-ui/core'; import { DocumentType } from '../../documents/DocumentTypes'; diff --git a/src/client/views/nodes/DocumentLinksButton.tsx b/src/client/views/nodes/DocumentLinksButton.tsx index 8b4dc2957..e68a85664 100644 --- a/src/client/views/nodes/DocumentLinksButton.tsx +++ b/src/client/views/nodes/DocumentLinksButton.tsx @@ -17,7 +17,7 @@ import { StrCast } from "../../../fields/Types"; import { LinkDescriptionPopup } from "./LinkDescriptionPopup"; import { LinkManager } from "../../util/LinkManager"; -import { Hypothesis } from "../../apis/hypothesis/HypothesisApiUtils"; +import { Hypothesis } from "../../apis/hypothesis/HypothesisUtils"; import { Id } from "../../../fields/FieldSymbols"; import { Tooltip } from "@material-ui/core"; const higflyout = require("@hig/flyout"); diff --git a/src/client/views/nodes/DocumentView.tsx b/src/client/views/nodes/DocumentView.tsx index 555e58bb0..998c6798e 100644 --- a/src/client/views/nodes/DocumentView.tsx +++ b/src/client/views/nodes/DocumentView.tsx @@ -42,7 +42,6 @@ import React = require("react"); import { DocumentLinksButton } from './DocumentLinksButton'; import { MobileInterface } from '../../../mobile/MobileInterface'; import { LinkCreatedBox } from './LinkCreatedBox'; -import { Hypothesis } from '../../apis/hypothesis/HypothesisApiUtils'; import { LinkDescriptionPopup } from './LinkDescriptionPopup'; import { LinkManager } from '../../util/LinkManager'; -- cgit v1.2.3-70-g09d2