From 58b780563c7fc4a1496f5c676f2d14faddb096e0 Mon Sep 17 00:00:00 2001 From: Melissa Zhang Date: Fri, 3 Jul 2020 17:32:40 -0700 Subject: merge with master --- src/server/ApiManagers/PDFManager.ts | 2 +- src/server/index.ts | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) (limited to 'src/server') diff --git a/src/server/ApiManagers/PDFManager.ts b/src/server/ApiManagers/PDFManager.ts index e028d628d..2b4212588 100644 --- a/src/server/ApiManagers/PDFManager.ts +++ b/src/server/ApiManagers/PDFManager.ts @@ -114,4 +114,4 @@ class NodeCanvasFactory { canvasAndContext.canvas = null; canvasAndContext.context = null; } -} \ No newline at end of file +} diff --git a/src/server/index.ts b/src/server/index.ts index 590affd06..083173bb5 100644 --- a/src/server/index.ts +++ b/src/server/index.ts @@ -104,6 +104,7 @@ function routeSetter({ isRelease, addSupervisedRoute, logRegistrationOutcome }: const serve: PublicHandler = ({ req, res }) => { const detector = new mobileDetect(req.headers['user-agent'] || ""); const filename = detector.mobile() !== null ? 'mobile/image.html' : 'index.html'; + console.log(detector.is("iPhone")); res.sendFile(path.join(__dirname, '../../deploy/' + filename)); }; -- cgit v1.2.3-70-g09d2 From 876437db1f7347787badbdd48ac751dba111a752 Mon Sep 17 00:00:00 2001 From: Melissa Zhang Date: Wed, 8 Jul 2020 08:57:21 -0700 Subject: store hypothesis API tokens to database --- src/client/util/CurrentUserUtils.ts | 2 +- src/server/ApiManagers/HypothesisManager.ts | 14 ++++------- src/server/database.ts | 39 ++++++++++++++++++++++++++++- 3 files changed, 44 insertions(+), 11 deletions(-) (limited to 'src/server') diff --git a/src/client/util/CurrentUserUtils.ts b/src/client/util/CurrentUserUtils.ts index 8099228c6..cb2a025cb 100644 --- a/src/client/util/CurrentUserUtils.ts +++ b/src/client/util/CurrentUserUtils.ts @@ -424,7 +424,7 @@ export class CurrentUserUtils { { title: "Drag a document previewer", label: "Prev", icon: "expand", click: 'openOnRight(getCopy(this.dragFactory, true))', drag: 'getCopy(this.dragFactory,true)', dragFactory: doc.emptyDocHolder as Doc }, { title: "Toggle a Calculator REPL", label: "repl", icon: "calculator", click: 'addOverlayWindow("ScriptingRepl", { x: 300, y: 100, width: 200, height: 200, title: "Scripting REPL" })' }, { title: "Connect a Google Account", label: "Google Account", icon: "external-link-alt", click: 'GoogleAuthenticationManager.Instance.fetchOrGenerateAccessToken(true)' }, - { title: "Connect a Hypothesis Account", label: "Hypothesis Account", icon: "heading", click: 'HypothesisAuthenticationManager.Instance.fetchOrGenerateAccessToken(true)' }, + { title: "Connect a Hypothesis Account", label: "Hypothesis Account", icon: "heading", click: 'HypothesisAuthenticationManager.Instance.fetchAccessToken(true)' }, ]; } diff --git a/src/server/ApiManagers/HypothesisManager.ts b/src/server/ApiManagers/HypothesisManager.ts index 33badbc42..73c707a55 100644 --- a/src/server/ApiManagers/HypothesisManager.ts +++ b/src/server/ApiManagers/HypothesisManager.ts @@ -13,11 +13,8 @@ export default class HypothesisManager extends ApiManager { method: Method.GET, subscription: "/readHypothesisAccessToken", secureHandler: async ({ user, res }) => { - if (existsSync(serverPathToFile(Directory.hypothesis, user.id))) { - const read = readFileSync(serverPathToFile(Directory.hypothesis, user.id), "base64") || ""; - console.log("READ = " + read); - res.send(read); - } else res.send(""); + const credentials = await Database.Auxiliary.HypothesisAccessToken.Fetch(user.id); + res.send(credentials?.hypothesisApiKey ?? ""); } }); @@ -25,9 +22,8 @@ export default class HypothesisManager extends ApiManager { method: Method.POST, subscription: "/writeHypothesisAccessToken", secureHandler: async ({ user, req, res }) => { - const write = req.body.authenticationCode; - console.log("WRITE = " + write); - res.send(await writeFile(serverPathToFile(Directory.hypothesis, user.id), write, "base64", () => { })); + await Database.Auxiliary.HypothesisAccessToken.Write(user.id, req.body.authenticationCode); + res.send(); } }); @@ -35,7 +31,7 @@ export default class HypothesisManager extends ApiManager { method: Method.GET, subscription: "/revokeHypothesisAccessToken", secureHandler: async ({ user, res }) => { - await Database.Auxiliary.GoogleAccessToken.Revoke("dash-hyp-" + user.id); + await Database.Auxiliary.HypothesisAccessToken.Revoke(user.id); res.send(); } }); diff --git a/src/server/database.ts b/src/server/database.ts index 2372cbcf2..7fbab357b 100644 --- a/src/server/database.ts +++ b/src/server/database.ts @@ -304,7 +304,8 @@ export namespace Database { */ export enum AuxiliaryCollections { GooglePhotosUploadHistory = "uploadedFromGooglePhotos", - GoogleAccess = "googleAuthentication" + GoogleAccess = "googleAuthentication", + HypothesisAccess = "hypothesisAuthentication" } /** @@ -405,6 +406,42 @@ export namespace Database { } + export namespace HypothesisAccessToken { + /** + * Format stored in database. + */ + interface StoredCredentials { + userId: string; + hypothesisApiKey: string; + _id?: string; + } + + /** + * Writes the @param enrichedCredentials to the database, associated + * with @param userId for later retrieval and updating. + */ + export const Write = async (userId: string, hypothesisApiKey: string) => { + return Instance.insert({ userId, hypothesisApiKey }, AuxiliaryCollections.HypothesisAccess); + }; + + /** + * Retrieves the credentials associaed with @param userId + * and optionally removes their database id according to @param removeId. + */ + export const Fetch = async (userId: string, removeId = true): Promise> => { + return SanitizedSingletonQuery({ userId }, AuxiliaryCollections.HypothesisAccess, removeId); + }; + + /** + * Revokes the credentials associated with @param userId. + */ + export const Revoke = async (userId: string) => { + const entry = await Fetch(userId, false); + if (entry) { + Instance.delete({ _id: entry._id }, AuxiliaryCollections.HypothesisAccess); + } + }; + } } } -- cgit v1.2.3-70-g09d2 From 467ddce06a4cf9e3b61abf733f20c60b257c94db Mon Sep 17 00:00:00 2001 From: Melissa Zhang Date: Wed, 8 Jul 2020 21:11:35 -0700 Subject: implemented much more link functionality, redirects to annotation URL when link followed --- src/client/apis/hypothesis/HypothesisApiUtils.ts | 28 ++++++++++- src/client/documents/Documents.ts | 19 -------- src/client/views/linking/LinkMenuItem.tsx | 3 +- src/client/views/nodes/DocumentLinksButton.tsx | 61 +++++++++++++++++------- src/client/views/nodes/DocumentView.tsx | 2 +- src/server/database.ts | 2 +- 6 files changed, 72 insertions(+), 43 deletions(-) (limited to 'src/server') diff --git a/src/client/apis/hypothesis/HypothesisApiUtils.ts b/src/client/apis/hypothesis/HypothesisApiUtils.ts index dc7e1f988..bf9f4ea99 100644 --- a/src/client/apis/hypothesis/HypothesisApiUtils.ts +++ b/src/client/apis/hypothesis/HypothesisApiUtils.ts @@ -1,7 +1,9 @@ +import { StrCast } from "../../../fields/Types"; + export namespace Hypothesis { - export const getAnnotation = async (username: String, searchParam: String) => { + export const getAnnotation = async (username: String, searchKeyWord: String) => { const base = 'https://api.hypothes.is/api/search'; - const request = base + `?user=acct:${username}@hypothes.is&text=${searchParam}`; + const request = base + `?user=acct:${username}@hypothes.is&text=${searchKeyWord}`; console.log("DASH Querying " + request); const response = await fetch(request); if (response.ok) { @@ -11,7 +13,29 @@ export namespace Hypothesis { } }; + export const getPlaceholderId = async (username: String, searchKeyWord: String) => { + const getResponse = await Hypothesis.getAnnotation(username, searchKeyWord); + const id = getResponse.rows.length > 0 ? getResponse.rows[0].id : undefined; + return StrCast(id); + }; + + // Send request to Hypothes.is client to modify a placeholder annotation into a hyperlink to Dash + export const dispatchLinkRequest = (title: string, url: string, annotationId: string) => { + console.log("DASH dispatching linkRequest"); + document.dispatchEvent(new CustomEvent<{ url: string, title: string, id: string }>("linkRequest", { + detail: { url: url, title: title, 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}`; }; + + // export const checkValidApiKey = async (apiKey: string) => { + // const response = await fetch("https://api.hypothes.is/api/profile", { + + // }); + // }; } \ No newline at end of file diff --git a/src/client/documents/Documents.ts b/src/client/documents/Documents.ts index 763321a85..7b85d2e46 100644 --- a/src/client/documents/Documents.ts +++ b/src/client/documents/Documents.ts @@ -925,25 +925,6 @@ export namespace DocUtils { return linkDoc; } - export function MakeHypothesisLink(source: { doc: Doc }, target: { doc: Doc }, linkRelationship: string = "", sourceAnnotationId: string, id?: string) { - const sv = DocumentManager.Instance.getDocumentView(source.doc); - if (sv && sv.props.ContainingCollectionDoc === target.doc) return; - if (target.doc === Doc.UserDoc()) return undefined; - - const linkDoc = Docs.Create.LinkDocument(source, target, { linkRelationship, layoutKey: "layout_linkView" }, id); - linkDoc.layout_linkView = Cast(Cast(Doc.UserDoc()["template-button-link"], Doc, null).dragFactory, Doc, null); - Doc.GetProto(linkDoc).title = ComputedField.MakeFunction('self.anchor1?.title +" (" + (self.linkRelationship||"to") +") " + self.anchor2?.title'); - - const sourceUrl = StrCast(source.doc.data.url); // The URL of the annotation's source web page - console.log("sourceAnnotationId, url", sourceAnnotationId, sourceUrl); - Doc.GetProto(linkDoc).annotationUrl = Hypothesis.makeAnnotationUrl(sourceAnnotationId, sourceUrl); - - Doc.GetProto(source.doc).links = ComputedField.MakeFunction("links(self)"); - Doc.GetProto(target.doc).links = ComputedField.MakeFunction("links(self)"); - - return linkDoc; - } - export function DocumentFromField(target: Doc, fieldKey: string, proto?: Doc, options?: DocumentOptions): Doc | undefined { let created: Doc | undefined; let layout: ((fieldKey: string) => string) | undefined; diff --git a/src/client/views/linking/LinkMenuItem.tsx b/src/client/views/linking/LinkMenuItem.tsx index 76f802c0c..9c21bca35 100644 --- a/src/client/views/linking/LinkMenuItem.tsx +++ b/src/client/views/linking/LinkMenuItem.tsx @@ -153,8 +153,7 @@ export class LinkMenuItem extends React.Component { LinkDocPreview.LinkInfo = undefined; const redirectUrl = StrCast(this.props.linkDoc.annotationUrl, null); - redirectUrl && (this.props.destinationDoc.data = new WebField(redirectUrl)); // If the link is to an annotation, go to annotation - console.log(redirectUrl, "redirectUrl"); + redirectUrl && (Doc.GetProto(this.props.destinationDoc).data = new WebField(redirectUrl)); // if destination is a Hypothes.is annotation, redirect website to the annotation's URL to scroll to the annotation if (this.props.linkDoc.follow) { if (this.props.linkDoc.follow === "Default") { diff --git a/src/client/views/nodes/DocumentLinksButton.tsx b/src/client/views/nodes/DocumentLinksButton.tsx index 61143a6af..839f83f78 100644 --- a/src/client/views/nodes/DocumentLinksButton.tsx +++ b/src/client/views/nodes/DocumentLinksButton.tsx @@ -1,7 +1,7 @@ import { action, computed, observable, runInAction } from "mobx"; import { observer } from "mobx-react"; import { Doc, DocListCast } from "../../../fields/Doc"; -import { emptyFunction, setupMoveUpEvents, returnFalse } from "../../../Utils"; +import { emptyFunction, setupMoveUpEvents, returnFalse, Utils } from "../../../Utils"; import { DragManager } from "../../util/DragManager"; import { UndoManager } from "../../util/UndoManager"; import './DocumentLinksButton.scss'; @@ -17,6 +17,7 @@ import { StrCast } from "../../../fields/Types"; import { LinkDescriptionPopup } from "./LinkDescriptionPopup"; import { LinkManager } from "../../util/LinkManager"; +import { Hypothesis } from "../../apis/hypothesis/HypothesisApiUtils"; const higflyout = require("@hig/flyout"); export const { anchorPoints } = higflyout; export const Flyout = higflyout.default; @@ -35,19 +36,25 @@ export class DocumentLinksButton extends React.Component { // event used by Hypothes.is plugin to tell Dash when an unlinked annotation has been created - // const annotatedUrl = e.details; - // SelectionManager.SelectedDocuments().forEach(action((element: DocumentView) => { - // DocumentLinksButton.StartLink = element; - // })); + // window.addEventListener("annotationCreated", (e: any) => { // event used by Hypothes.is plugin to tell Dash when an unlinked annotation has been created + // const id = e.details; + // const source = SelectionManager.SelectedDocuments()[0]; + // runInAction(() => { + // DocumentLinksButton.AnnotationId = id; + // DocumentLinksButton.StartLink = source; + // }); // }); - window.addEventListener("fakeLinkStarted", action((e: any) => { // event used by Hypothes.is plugin to tell Dash when an unlinked annotation has been created - console.log("Helo fake started link"); - const id = e.detail; + console.log("window", window); + window.addEventListener("fakeAnnotationCreated", async (e: any) => { // event used by Hypothes.is plugin to tell Dash when an unlinked annotation has been created + console.log("Helo fake annotation make"); + // const id = e.detail; + const id = await Hypothesis.getPlaceholderId("melissaz", "placeholder"); // delete once eventListening between client & Dash works const source = SelectionManager.SelectedDocuments()[0]; - DocumentLinksButton.AnnotationId = id; - DocumentLinksButton.StartLink = source; - })); + runInAction(() => { + DocumentLinksButton.AnnotationId = id; + DocumentLinksButton.StartLink = source; + }); + }); } @action @@ -114,9 +121,17 @@ export class DocumentLinksButton extends React.Component { LinkCreatedBox.popupX = e.screenX; @@ -139,15 +154,25 @@ export class DocumentLinksButton extends React.Component { if (DocumentLinksButton.StartLink === this.props.View) { DocumentLinksButton.StartLink = undefined; + DocumentLinksButton.AnnotationId = undefined; console.log("reset to undefined (finisheLinkClick)"); // action((e: React.PointerEvent) => { // Doc.UnBrushDoc(this.props.View.Document); // }); } else { if (DocumentLinksButton.StartLink && DocumentLinksButton.StartLink !== this.props.View) { - const linkDoc = DocumentLinksButton.AnnotationId ? - DocUtils.MakeHypothesisLink({ doc: DocumentLinksButton.StartLink.props.Document }, { doc: this.props.View.props.Document }, "hypothesis annotation", DocumentLinksButton.AnnotationId) : - DocUtils.MakeLink({ doc: DocumentLinksButton.StartLink.props.Document }, { doc: this.props.View.props.Document }, "long drag"); + const sourceDoc = DocumentLinksButton.StartLink.props.Document; + const targetDoc = this.props.View.props.Document; + const linkDoc = DocUtils.MakeLink({ doc: sourceDoc }, { doc: targetDoc }, DocumentLinksButton.AnnotationId ? "hypothes.is annotation" : "long drag"); + + // if the link is to a Hypothes.is annotation + if (DocumentLinksButton.AnnotationId) { + const sourceUrl = StrCast(sourceDoc.data.url); // the URL of the annotation's source web page + console.log("sourceAnnotationId, url", DocumentLinksButton.AnnotationId, sourceUrl); + Doc.GetProto(linkDoc as Doc).annotationUrl = Hypothesis.makeAnnotationUrl(DocumentLinksButton.AnnotationId, sourceUrl); // redirect web doc to this URL when following link + Hypothesis.dispatchLinkRequest(StrCast(targetDoc.title), Utils.prepend("/doc/" + targetDoc.Id), DocumentLinksButton.AnnotationId); // update and link placeholder annotation + } + LinkManager.currentLink = linkDoc; runInAction(() => { LinkCreatedBox.popupX = e.screenX; diff --git a/src/client/views/nodes/DocumentView.tsx b/src/client/views/nodes/DocumentView.tsx index c67267860..41308b3a4 100644 --- a/src/client/views/nodes/DocumentView.tsx +++ b/src/client/views/nodes/DocumentView.tsx @@ -793,7 +793,7 @@ export class DocumentView extends DocComponent(Docu cm.addItem({ description: "pretend we made an annotation", event: () => { - document.dispatchEvent(new CustomEvent("fakeLinkStarted", { + document.dispatchEvent(new CustomEvent("fakeAnnotationCreated", { detail: "fakefakefakeid", bubbles: true })); diff --git a/src/server/database.ts b/src/server/database.ts index 7fbab357b..767d38350 100644 --- a/src/server/database.ts +++ b/src/server/database.ts @@ -417,7 +417,7 @@ export namespace Database { } /** - * Writes the @param enrichedCredentials to the database, associated + * Writes the @param hypothesisApiKey to the database, associated * with @param userId for later retrieval and updating. */ export const Write = async (userId: string, hypothesisApiKey: string) => { -- cgit v1.2.3-70-g09d2 From 8594b7e8f3058a8d441413a033aee311ee59bdfd Mon Sep 17 00:00:00 2001 From: Melissa Zhang Date: Fri, 10 Jul 2020 16:26:08 -0700 Subject: hypothes.is authentication is fully functional, no longer need to manually input username/api key --- src/client/apis/HypothesisAuthenticationManager.tsx | 20 +++++++++++--------- src/client/apis/hypothesis/HypothesisApiUtils.ts | 21 ++++++++++++--------- src/client/views/nodes/DocumentLinksButton.tsx | 2 +- src/server/ApiManagers/HypothesisManager.ts | 4 ++-- src/server/database.ts | 5 +++-- 5 files changed, 29 insertions(+), 23 deletions(-) (limited to 'src/server') diff --git a/src/client/apis/HypothesisAuthenticationManager.tsx b/src/client/apis/HypothesisAuthenticationManager.tsx index b299f233e..f995dee3b 100644 --- a/src/client/apis/HypothesisAuthenticationManager.tsx +++ b/src/client/apis/HypothesisAuthenticationManager.tsx @@ -19,7 +19,7 @@ export default class HypothesisAuthenticationManager extends React.Component<{}> @observable private showPasteTargetState = false; @observable private success: Opt = undefined; @observable private displayLauncher = true; - @observable private credentials: string; + @observable private credentials: { username: string, apiKey: string } | undefined; private disposer: Opt; private set isOpen(value: boolean) { @@ -35,9 +35,10 @@ export default class HypothesisAuthenticationManager extends React.Component<{}> } public fetchAccessToken = async (displayIfFound = false) => { - let response: any = await Networking.FetchFromServer("/readHypothesisAccessToken"); + const jsonResponse = await Networking.FetchFromServer("/readHypothesisAccessToken"); + const response = jsonResponse !== "" ? JSON.parse(jsonResponse) : undefined; // if this is an authentication url, activate the UI to register the new access token - if (!response) { // new RegExp(AuthenticationUrl).test(response)) { + if (!response) { this.isOpen = true; this.authenticationLink = response; return new Promise(async resolve => { @@ -48,10 +49,11 @@ export default class HypothesisAuthenticationManager extends React.Component<{}> const userProfile = authenticationCode && await Hypothesis.fetchUser(authenticationCode); if (userProfile && userProfile.userid !== null) { this.disposer?.(); - Networking.PostToServer("/writeHypothesisAccessToken", { authenticationCode }); + const hypothesisUsername = Hypothesis.extractUsername(userProfile.userid); // extract username from profile + Networking.PostToServer("/writeHypothesisAccessToken", { authenticationCode, hypothesisUsername }); runInAction(() => { this.success = true; - this.credentials = Hypothesis.extractUsername(userProfile.userid); // extract username from profile + this.credentials = response; }); this.resetState(); resolve(authenticationCode); @@ -69,7 +71,7 @@ export default class HypothesisAuthenticationManager extends React.Component<{}> this.resetState(-1, -1); this.isOpen = true; } - return response.access_token; + return response; } resetState = action((visibleForMS: number = 3000, fadesOutInMS: number = 500) => { @@ -78,7 +80,7 @@ export default class HypothesisAuthenticationManager extends React.Component<{}> this.isOpen = false; this.success = undefined; this.displayLauncher = true; - this.credentials = ""; + this.credentials = undefined; this.shouldShowPasteTarget = false; this.authenticationCode = undefined; }); @@ -93,7 +95,7 @@ export default class HypothesisAuthenticationManager extends React.Component<{}> setTimeout(action(() => { this.success = undefined; this.displayLauncher = true; - this.credentials = ""; + this.credentials = undefined; }), fadesOutInMS); }), visibleForMS); } @@ -124,7 +126,7 @@ export default class HypothesisAuthenticationManager extends React.Component<{}> <> Welcome to Dash, {this.credentials} + >Welcome to Dash, {this.credentials.username}
HypothesisAuthenticationManager.Instance.fetchAccessToken(); + export const fetchAnnotation = async (annotationId: string) => { const response = await fetch(`https://api.hypothes.is/api/annotations/${annotationId}`); if (response.ok) { @@ -11,12 +15,12 @@ export namespace Hypothesis { }; /** - * Searches for annotations made by @param username that - * contain @param searchKeyWord + * Searches for annotations authored by the current user that contain @param searchKeyWord */ - export const searchAnnotation = async (username: string, searchKeyWord: string) => { + export const searchAnnotation = async (searchKeyWord: string) => { + const credentials = await getCredentials(); const base = 'https://api.hypothes.is/api/search'; - const request = base + `?user=acct:${username}@hypothes.is&text=${searchKeyWord}`; + const request = base + `?user=acct:${credentials.username}@hypothes.is&text=${searchKeyWord}`; console.log("DASH Querying " + request); const response = await fetch(request); if (response.ok) { @@ -40,8 +44,8 @@ export namespace Hypothesis { }; // Find the most recent placeholder annotation created, and return its ID - export const getPlaceholderId = async (username: string, searchKeyWord: string) => { - const getResponse = await Hypothesis.searchAnnotation(username, searchKeyWord); + export const getPlaceholderId = async (searchKeyWord: string) => { + const getResponse = await Hypothesis.searchAnnotation(searchKeyWord); const id = getResponse.rows.length > 0 ? getResponse.rows[0].id : undefined; const uri = getResponse.rows.length > 0 ? getResponse.rows[0].uri : undefined; return id ? { id, uri } : undefined; @@ -49,8 +53,7 @@ export namespace Hypothesis { // Send request to Hypothes.is client to modify a placeholder annotation into a hyperlink to Dash export const dispatchLinkRequest = async (title: string, url: string, annotationId: string) => { - const apiKey = "6879-DnMTKjWjnnLPa0Php7f5Ra2kunZ_X0tMRDbTF220_q0"; - + const credentials = await getCredentials(); const oldAnnotation = await fetchAnnotation(annotationId); const oldText = StrCast(oldAnnotation.text); const newHyperlink = `[${title}\n](${url})`; @@ -58,7 +61,7 @@ export namespace Hypothesis { console.log("DASH dispatching linkRequest"); document.dispatchEvent(new CustomEvent<{ newText: string, id: string, apiKey: string }>("linkRequest", { - detail: { newText: newText, id: annotationId, apiKey: apiKey }, + detail: { newText: newText, id: annotationId, apiKey: credentials.apiKey }, bubbles: true })); }; diff --git a/src/client/views/nodes/DocumentLinksButton.tsx b/src/client/views/nodes/DocumentLinksButton.tsx index 535711193..ab97e8531 100644 --- a/src/client/views/nodes/DocumentLinksButton.tsx +++ b/src/client/views/nodes/DocumentLinksButton.tsx @@ -41,7 +41,7 @@ export class DocumentLinksButton extends React.Component { if (e.origin === "http://localhost:1050" && e.data.message === "annotation created") { console.log("DASH RECEIVED MESSAGE:", e.data.message); - const response = await Hypothesis.getPlaceholderId("melissaz", "placeholder"); // delete once eventListening between client & Dash works + const response = await Hypothesis.getPlaceholderId("placeholder"); // delete once eventListening between client & Dash works const source = SelectionManager.SelectedDocuments()[0]; response && runInAction(() => { DocumentLinksButton.AnnotationId = response.id; diff --git a/src/server/ApiManagers/HypothesisManager.ts b/src/server/ApiManagers/HypothesisManager.ts index 73c707a55..370d02a49 100644 --- a/src/server/ApiManagers/HypothesisManager.ts +++ b/src/server/ApiManagers/HypothesisManager.ts @@ -14,7 +14,7 @@ export default class HypothesisManager extends ApiManager { subscription: "/readHypothesisAccessToken", secureHandler: async ({ user, res }) => { const credentials = await Database.Auxiliary.HypothesisAccessToken.Fetch(user.id); - res.send(credentials?.hypothesisApiKey ?? ""); + res.send(credentials ? { username: credentials.hypothesisUsername, apiKey: credentials.hypothesisApiKey } : ""); } }); @@ -22,7 +22,7 @@ export default class HypothesisManager extends ApiManager { method: Method.POST, subscription: "/writeHypothesisAccessToken", secureHandler: async ({ user, req, res }) => { - await Database.Auxiliary.HypothesisAccessToken.Write(user.id, req.body.authenticationCode); + await Database.Auxiliary.HypothesisAccessToken.Write(user.id, req.body.authenticationCode, req.body.hypothesisUsername); res.send(); } }); diff --git a/src/server/database.ts b/src/server/database.ts index 767d38350..456c1c254 100644 --- a/src/server/database.ts +++ b/src/server/database.ts @@ -413,6 +413,7 @@ export namespace Database { interface StoredCredentials { userId: string; hypothesisApiKey: string; + hypothesisUsername: string; _id?: string; } @@ -420,8 +421,8 @@ export namespace Database { * Writes the @param hypothesisApiKey to the database, associated * with @param userId for later retrieval and updating. */ - export const Write = async (userId: string, hypothesisApiKey: string) => { - return Instance.insert({ userId, hypothesisApiKey }, AuxiliaryCollections.HypothesisAccess); + export const Write = async (userId: string, hypothesisApiKey: string, hypothesisUsername: string) => { + return Instance.insert({ userId, hypothesisApiKey, hypothesisUsername }, AuxiliaryCollections.HypothesisAccess); }; /** -- cgit v1.2.3-70-g09d2 From ba8549220295d03fb7eb7d7d31c90af72b30b1a6 Mon Sep 17 00:00:00 2001 From: Melissa Zhang Date: Thu, 30 Jul 2020 10:38:57 -0700 Subject: remove unnecessary authentication/database stuff --- .../apis/HypothesisAuthenticationManager.tsx | 164 --------------------- src/client/apis/hypothesis/HypothesisUtils.ts | 18 +-- src/client/util/CurrentUserUtils.ts | 1 - src/client/util/SettingsManager.tsx | 6 - src/client/views/GlobalKeyHandler.ts | 2 - src/client/views/linking/LinkMenuItem.tsx | 2 +- src/server/ApiManagers/HypothesisManager.ts | 40 ----- src/server/ApiManagers/UploadManager.ts | 1 - src/server/apis/google/GoogleApiServerUtils.ts | 1 - src/server/database.ts | 38 ----- src/server/index.ts | 2 - 11 files changed, 4 insertions(+), 271 deletions(-) delete mode 100644 src/client/apis/HypothesisAuthenticationManager.tsx delete mode 100644 src/server/ApiManagers/HypothesisManager.ts (limited to 'src/server') diff --git a/src/client/apis/HypothesisAuthenticationManager.tsx b/src/client/apis/HypothesisAuthenticationManager.tsx deleted file mode 100644 index 653f21a7a..000000000 --- a/src/client/apis/HypothesisAuthenticationManager.tsx +++ /dev/null @@ -1,164 +0,0 @@ -import { observable, action, reaction, runInAction, IReactionDisposer } from "mobx"; -import { observer } from "mobx-react"; -import * as React from "react"; -import MainViewModal from "../views/MainViewModal"; -import { Opt } from "../../fields/Doc"; -import { Networking } from "../Network"; -import "./HypothesisAuthenticationManager.scss"; -import { Scripting } from "../util/Scripting"; -import { Hypothesis } from "./hypothesis/HypothesisUtils"; - -const prompt = "Paste authorization code here..."; - -@observer -export default class HypothesisAuthenticationManager extends React.Component<{}> { - public static Instance: HypothesisAuthenticationManager; - private authenticationLink: Opt = undefined; - @observable private openState = false; - @observable private authenticationCode: Opt = undefined; - @observable private showPasteTargetState = false; - @observable private success: Opt = undefined; - @observable private displayLauncher = true; - @observable private credentials: { username: string, apiKey: string } | undefined; - private disposer: Opt; - - private set isOpen(value: boolean) { - runInAction(() => this.openState = value); - } - - private set shouldShowPasteTarget(value: boolean) { - runInAction(() => this.showPasteTargetState = value); - } - - public cancel() { - this.openState && this.resetState(0, 0); - } - - public fetchAccessToken = async (displayIfFound = false) => { - const jsonResponse = await Networking.FetchFromServer("/readHypothesisAccessToken"); - const response = jsonResponse !== "" ? JSON.parse(jsonResponse) : undefined; - // if this is an authentication url, activate the UI to register the new access token - if (!response) { - this.isOpen = true; - this.authenticationLink = response; - return new Promise(async resolve => { - this.disposer?.(); - this.disposer = reaction( - () => this.authenticationCode, - async authenticationCode => { - const userProfile = authenticationCode && await Hypothesis.fetchUser(authenticationCode); - if (userProfile && userProfile.userid !== null) { - this.disposer?.(); - const hypothesisUsername = Hypothesis.extractUsername(userProfile.userid); // extract username from profile - Networking.PostToServer("/writeHypothesisAccessToken", { authenticationCode, hypothesisUsername }); - runInAction(() => { - this.success = true; - this.credentials = { username: hypothesisUsername, apiKey: authenticationCode! }; - }); - this.resetState(); - resolve(authenticationCode); - } - } - ); - }); - } - - if (displayIfFound) { - runInAction(() => { - this.success = true; - this.credentials = response; - }); - this.resetState(-1, -1); - this.isOpen = true; - } - return response; - } - - resetState = action((visibleForMS: number = 3000, fadesOutInMS: number = 500) => { - if (!visibleForMS && !fadesOutInMS) { - runInAction(() => { - this.isOpen = false; - this.success = undefined; - this.displayLauncher = true; - this.credentials = undefined; - this.shouldShowPasteTarget = false; - this.authenticationCode = undefined; - }); - return; - } - this.authenticationCode = undefined; - this.displayLauncher = false; - this.shouldShowPasteTarget = false; - if (visibleForMS > 0 && fadesOutInMS > 0) { - setTimeout(action(() => { - this.isOpen = false; - setTimeout(action(() => { - this.success = undefined; - this.displayLauncher = true; - this.credentials = undefined; - }), fadesOutInMS); - }), visibleForMS); - } - }); - - constructor(props: {}) { - super(props); - HypothesisAuthenticationManager.Instance = this; - } - - private get renderPrompt() { - return ( -
- - {this.displayLauncher ? : (null)} - {this.showPasteTargetState ? this.authenticationCode = e.currentTarget.value)} - placeholder={prompt} - /> : (null)} - {this.credentials ? - <> - Welcome to Dash, {this.credentials.username} - -
{ - await Networking.FetchFromServer("/revokeHypothesisAccessToken"); - this.resetState(0, 0); - }} - >Disconnect Account
- : (null)} -
- ); - } - - private get dialogueBoxStyle() { - const borderColor = this.success === undefined ? "black" : this.success ? "green" : "red"; - return { borderColor, transition: "0.2s borderColor ease", zIndex: 1002 }; - } - - render() { - return ( - this.isOpen = false)} - /> - ); - } - -} - -Scripting.addGlobal("HypothesisAuthenticationManager", HypothesisAuthenticationManager); \ No newline at end of file diff --git a/src/client/apis/hypothesis/HypothesisUtils.ts b/src/client/apis/hypothesis/HypothesisUtils.ts index 8eaad2905..a9d807976 100644 --- a/src/client/apis/hypothesis/HypothesisUtils.ts +++ b/src/client/apis/hypothesis/HypothesisUtils.ts @@ -1,5 +1,4 @@ import { StrCast, Cast } from "../../../fields/Types"; -import HypothesisAuthenticationManager from "../HypothesisAuthenticationManager"; import { SearchUtil } from "../../util/SearchUtil"; import { action } from "mobx"; import { Doc } from "../../../fields/Doc"; @@ -8,18 +7,6 @@ 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) => { @@ -40,6 +27,7 @@ export namespace Hypothesis { // Construct an URL which will automatically scroll the web page to a specific annotation's position export const makeAnnotationUrl = (annotationId: string, baseUrl: string) => { + console.log("baseUrl", baseUrl, annotationId); return `${baseUrl}#annotations:${annotationId}`; }; @@ -58,8 +46,8 @@ export namespace Hypothesis { 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? + console.log(uri, Cast(doc.data, WebField)?.url.href, uri === Cast(doc.data, WebField)?.url.href); + (uri === Cast(doc.data, WebField)?.url.href) && results.push(doc); // TODO check history? imperfect matches? }); })); diff --git a/src/client/util/CurrentUserUtils.ts b/src/client/util/CurrentUserUtils.ts index ff33d35e0..1fe611b12 100644 --- a/src/client/util/CurrentUserUtils.ts +++ b/src/client/util/CurrentUserUtils.ts @@ -441,7 +441,6 @@ export class CurrentUserUtils { { toolTip: "Drag a document previewer", title: "Prev", icon: "expand", click: 'openOnRight(getCopy(this.dragFactory, true))', drag: 'getCopy(this.dragFactory,true)', dragFactory: doc.emptyDocHolder as Doc }, { toolTip: "Toggle a Calculator REPL", title: "repl", icon: "calculator", click: 'addOverlayWindow("ScriptingRepl", { x: 300, y: 100, width: 200, height: 200, title: "Scripting REPL" })' }, { toolTip: "Connect a Google Account", title: "Google Account", icon: "external-link-alt", click: 'GoogleAuthenticationManager.Instance.fetchOrGenerateAccessToken(true)' }, - { toolTip: "Connect a Hypothesis Account", title: "Hypothesis Account", icon: "heading", click: 'HypothesisAuthenticationManager.Instance.fetchAccessToken(true)' }, ]; } diff --git a/src/client/util/SettingsManager.tsx b/src/client/util/SettingsManager.tsx index 90d59aa51..a9c2d5e15 100644 --- a/src/client/util/SettingsManager.tsx +++ b/src/client/util/SettingsManager.tsx @@ -12,7 +12,6 @@ import { CurrentUserUtils } from "./CurrentUserUtils"; import { Utils } from "../../Utils"; import { Doc } from "../../fields/Doc"; import GroupManager from "./GroupManager"; -import HypothesisAuthenticationManager from "../apis/HypothesisAuthenticationManager"; import GoogleAuthenticationManager from "../apis/GoogleAuthenticationManager"; import { togglePlaygroundMode } from "../../fields/util"; @@ -92,10 +91,6 @@ export default class SettingsManager extends React.Component<{}> { googleAuthorize = (event: any) => { GoogleAuthenticationManager.Instance.fetchOrGenerateAccessToken(true); } - @action - hypothesisAuthorize = (event: any) => { - HypothesisAuthenticationManager.Instance.fetchAccessToken(true); - } @action togglePlaygroundMode = () => { @@ -118,7 +113,6 @@ export default class SettingsManager extends React.Component<{}> { -