aboutsummaryrefslogtreecommitdiff
path: root/src/server
diff options
context:
space:
mode:
Diffstat (limited to 'src/server')
-rw-r--r--src/server/RouteSubscriber.ts26
-rw-r--r--src/server/authentication/models/current_user_utils.ts299
-rw-r--r--src/server/index.ts340
3 files changed, 384 insertions, 281 deletions
diff --git a/src/server/RouteSubscriber.ts b/src/server/RouteSubscriber.ts
new file mode 100644
index 000000000..e49be8af5
--- /dev/null
+++ b/src/server/RouteSubscriber.ts
@@ -0,0 +1,26 @@
+export default class RouteSubscriber {
+ private _root: string;
+ private requestParameters: string[] = [];
+
+ constructor(root: string) {
+ this._root = root;
+ }
+
+ add(...parameters: string[]) {
+ this.requestParameters.push(...parameters);
+ return this;
+ }
+
+ public get root() {
+ return this._root;
+ }
+
+ public get build() {
+ let output = this._root;
+ if (this.requestParameters.length) {
+ output = `${output}/:${this.requestParameters.join("/:")}`;
+ }
+ return output;
+ }
+
+} \ No newline at end of file
diff --git a/src/server/authentication/models/current_user_utils.ts b/src/server/authentication/models/current_user_utils.ts
index 3858907ba..3c4a46ed8 100644
--- a/src/server/authentication/models/current_user_utils.ts
+++ b/src/server/authentication/models/current_user_utils.ts
@@ -1,18 +1,18 @@
-import { action, computed, observable, runInAction } from "mobx";
+import { action, computed, observable, reaction, runInAction } from "mobx";
import * as rp from 'request-promise';
import { DocServer } from "../../../client/DocServer";
import { Docs } from "../../../client/documents/Documents";
import { Attribute, AttributeGroup, Catalog, Schema } from "../../../client/northstar/model/idea/idea";
import { ArrayUtil } from "../../../client/northstar/utils/ArrayUtil";
-import { CollectionViewType } from "../../../client/views/collections/CollectionBaseView";
-import { CollectionView } from "../../../client/views/collections/CollectionView";
+import { UndoManager } from "../../../client/util/UndoManager";
import { Doc, DocListCast } from "../../../new_fields/Doc";
import { List } from "../../../new_fields/List";
import { listSpec } from "../../../new_fields/Schema";
-import { Cast, StrCast, PromiseValue } from "../../../new_fields/Types";
+import { ScriptField, ComputedField } from "../../../new_fields/ScriptField";
+import { Cast, PromiseValue } from "../../../new_fields/Types";
import { Utils } from "../../../Utils";
import { RouteStore } from "../../RouteStore";
-import { ScriptField } from "../../../new_fields/ScriptField";
+import { InkingControl } from "../../../client/views/InkingControl";
export class CurrentUserUtils {
private static curr_id: string;
@@ -20,167 +20,184 @@ export class CurrentUserUtils {
private static mainDocId: string | undefined;
public static get id() { return this.curr_id; }
- @computed public static get UserDocument() { return Doc.UserDoc(); }
public static get MainDocId() { return this.mainDocId; }
public static set MainDocId(id: string | undefined) { this.mainDocId = id; }
+ @computed public static get UserDocument() { return Doc.UserDoc(); }
@observable public static GuestTarget: Doc | undefined;
@observable public static GuestWorkspace: Doc | undefined;
private static createUserDocument(id: string): Doc {
let doc = new Doc(id, true);
- doc.viewType = CollectionViewType.Tree;
- doc.layout = CollectionView.LayoutString();
doc.title = Doc.CurrentUserEmail;
- this.updateUserDocument(doc);
- doc.data = new List<Doc>();
- doc.gridGap = 5;
- doc.xMargin = 5;
- doc.yMargin = 5;
- doc.height = 42;
- doc.boxShadow = "0 0";
- doc.convertToButtons = true; // for CollectionLinearView used as the docButton layout
- doc.optionalRightCollection = Docs.Create.StackingDocument([], { title: "New mobile uploads" });
- return doc;
+ return this.updateUserDocument(doc);// this should be the last
}
- static updateUserDocument(doc: Doc) {
+ // a default set of note types .. not being used yet...
+ static setupNoteTypes(doc: Doc) {
+ let notes = [
+ Docs.Create.TextDocument({ title: "Note", backgroundColor: "yellow", isTemplate: true }),
+ Docs.Create.TextDocument({ title: "Idea", backgroundColor: "pink", isTemplate: true }),
+ Docs.Create.TextDocument({ title: "Topic", backgroundColor: "lightBlue", isTemplate: true }),
+ Docs.Create.TextDocument({ title: "Person", backgroundColor: "lightGreen", isTemplate: true })
+ ];
+ doc.noteTypes = Docs.Create.TreeDocument(notes, { title: "Note Types", height: 75 });
+ }
+
+ // setup the "creator" buttons for the sidebar-- eg. the default set of draggable document creation tools
+ static setupCreatorButtons(doc: Doc) {
+ doc.activePen = doc;
+ let docProtoData: { title: string, icon: string, drag?: string, click?: string, unchecked?: string, activePen?: Doc, backgroundColor?: string }[] = [
+ { title: "collection", icon: "folder", drag: 'Docs.Create.FreeformDocument([], { nativeWidth: undefined, nativeHeight: undefined, width: 150, height: 100, title: "freeform" })' },
+ { title: "web page", icon: "globe-asia", drag: 'Docs.Create.WebDocument("https://en.wikipedia.org/wiki/Hedgehog", { width: 300, height: 300, title: "New Webpage" })' },
+ { title: "image", icon: "cat", drag: 'Docs.Create.ImageDocument("https://upload.wikimedia.org/wikipedia/commons/thumb/3/3a/Cat03.jpg/1200px-Cat03.jpg", { width: 200, title: "an image of a cat" })' },
+ { title: "button", icon: "bolt", drag: 'Docs.Create.ButtonDocument({ width: 150, height: 50, title: "Button" })' },
+ { title: "presentation", icon: "tv", drag: 'Doc.UserDoc().curPresentation = Docs.Create.PresDocument(new List<Doc>(), { width: 200, height: 500, title: "a presentation trail" })' },
+ { title: "import folder", icon: "cloud-upload-alt", drag: 'Docs.Create.DirectoryImportDocument({ title: "Directory Import", width: 400, height: 400 })' },
+ { title: "pen", icon: "pen-nib", click: 'activatePen(this.activePen.pen = sameDocs(this.activePen.pen, this) ? undefined : this,2, this.backgroundColor)', backgroundColor: "blue", unchecked: `!sameDocs(this.activePen.pen, this)`, activePen: doc },
+ { title: "highlighter", icon: "pen", click: 'activateBrush(this.activePen.pen = sameDocs(this.activePen.pen, this) ? undefined : this,20,this.backgroundColor)', backgroundColor: "yellow", unchecked: `!sameDocs(this.activePen.pen, this)`, activePen: doc },
+ { title: "eraser", icon: "eraser", click: 'activateEraser(this.activePen.pen = sameDocs(this.activePen.pen, this) ? undefined : this);', unchecked: `!sameDocs(this.activePen.pen, this)`, activePen: doc },
+ { title: "none", icon: "pause", click: 'deactivateInk();this.activePen.pen = this;', unchecked: `!sameDocs(this.activePen.pen, this)`, activePen: doc },
+ ];
+ return docProtoData.map(data => Docs.Create.FontIconDocument({
+ nativeWidth: 100, nativeHeight: 100, width: 100, height: 100, dropAction: data.click ? "copy" : undefined, title: data.title, icon: data.icon,
+ onDragStart: data.drag ? ScriptField.MakeFunction(data.drag) : undefined, onClick: data.click ? ScriptField.MakeScript(data.click) : undefined,
+ unchecked: data.unchecked ? ComputedField.MakeFunction(data.unchecked) : undefined, activePen: data.activePen,
+ backgroundColor: data.backgroundColor
+ }));
+ }
+
+ // setup the Creator button which will display the creator panel. This panel will include the drag creators and the color picker. when clicked, this panel will be displayed in the target container (ie, sidebarContainer)
+ static setupCreatePanel(sidebarContainer: Doc, doc: Doc) {
+ // setup a masonry view of all he creators
+ const dragCreators = Docs.Create.MasonryDocument(CurrentUserUtils.setupCreatorButtons(doc), {
+ width: 500, autoHeight: true, columnWidth: 35, ignoreClick: true, lockedPosition: true, chromeStatus: "disabled", title: "buttons"
+ });
+ // setup a color picker
+ const color = Docs.Create.ColorDocument({
+ title: "color picker", width: 400, removeDropProperties: new List<string>(["dropAction", "forceActive"])
+ });
+ color.dropAction = "alias"; // these must be set on the view document so they can't be part of the creator above.
+ color.forceActive = true;
+ return Docs.Create.ButtonDocument({
+ width: 35, height: 35, borderRounding: "50%", boxShadow: "2px 2px 1px", title: "Create", targetContainer: sidebarContainer,
+ panel: Docs.Create.StackingDocument([dragCreators, color], {
+ width: 500, height: 800, chromeStatus: "disabled", title: "creator stack"
+ }),
+ onClick: ScriptField.MakeScript("this.targetContainer.proto = this.panel")
+ });
+ }
+
+ // setup the Library button which will display the library panel. This panel includes a collection of workspaces, documents, and recently closed views
+ static setupLibraryPanel(sidebarContainer: Doc, doc: Doc) {
// setup workspaces library item
- if (doc.workspaces === undefined) {
- const workspaces = Docs.Create.TreeDocument([], { title: "WORKSPACES", height: 100 });
- workspaces.boxShadow = "0 0";
- doc.workspaces = workspaces;
- }
- PromiseValue(Cast(doc.workspaces, Doc)).then(workspaces => {
- if (workspaces) {
- workspaces.backgroundColor = "#eeeeee";
- workspaces.preventTreeViewOpen = true;
- workspaces.forceActive = true;
- workspaces.lockedPosition = true;
- if (StrCast(workspaces.title) === "Workspaces") {
- workspaces.title = "WORKSPACES";
- }
- }
+ doc.workspaces = Docs.Create.TreeDocument([], {
+ title: "WORKSPACES", height: 100, forceActive: true, boxShadow: "0 0", lockedPosition: true, backgroundColor: "#eeeeee"
});
- // setup notes list
- if (doc.noteTypes === undefined) {
- let notes = [Docs.Create.TextDocument({ title: "Note", backgroundColor: "yellow", isTemplate: true }),
- Docs.Create.TextDocument({ title: "Idea", backgroundColor: "pink", isTemplate: true }),
- Docs.Create.TextDocument({ title: "Topic", backgroundColor: "lightBlue", isTemplate: true }),
- Docs.Create.TextDocument({ title: "Person", backgroundColor: "lightGreen", isTemplate: true })];
- const noteTypes = Docs.Create.TreeDocument(notes, { title: "Note Types", height: 75 });
- doc.noteTypes = noteTypes;
- }
- PromiseValue(Cast(doc.noteTypes, Doc)).then(noteTypes => noteTypes && PromiseValue(noteTypes.data).then(DocListCast));
+ doc.documents = Docs.Create.TreeDocument([], {
+ title: "DOCUMENTS", gridGap: 5, xMargin: 5, yMargin: 5, height: 42, width: 100, boxShadow: "0 0", backgroundColor: "#eeeeee", preventTreeViewOpen: true, forceActive: true, lockedPosition: true
+ });
// setup Recently Closed library item
- if (doc.recentlyClosed === undefined) {
- const recentlyClosed = Docs.Create.TreeDocument([], { title: "Recently Closed".toUpperCase(), height: 75 });
- recentlyClosed.boxShadow = "0 0";
- doc.recentlyClosed = recentlyClosed;
- }
- PromiseValue(Cast(doc.recentlyClosed, Doc)).then(recent => {
- if (recent) {
- recent.backgroundColor = "#eeeeee";
- recent.preventTreeViewOpen = true;
- recent.forceActive = true;
- recent.lockedPosition = true;
- if (StrCast(recent.title) === "Recently Closed") {
- recent.title = "RECENTLY CLOSED";
- }
- }
+ doc.recentlyClosed = Docs.Create.TreeDocument([], {
+ title: "Recently Closed".toUpperCase(), height: 75, boxShadow: "0 0", preventTreeViewOpen: true, forceActive: true, lockedPosition: true, backgroundColor: "#eeeeee"
});
+ return Docs.Create.ButtonDocument({
+ width: 50, height: 35, borderRounding: "50%", boxShadow: "2px 2px 1px", title: "Library",
+ panel: Docs.Create.TreeDocument([doc.workspaces as Doc, doc, doc.recentlyClosed as Doc], {
+ title: "Library", xMargin: 5, yMargin: 5, gridGap: 5, forceActive: true, dropAction: "alias", lockedPosition: true
+ }),
+ targetContainer: sidebarContainer,
+ onClick: ScriptField.MakeScript("this.targetContainer.proto = this.panel")
+ });
+ }
- if (doc.curPresentation === undefined) {
- const curPresentation = Docs.Create.PresDocument(new List<Doc>(), { title: "Presentation" });
- curPresentation.boxShadow = "0 0";
- doc.curPresentation = curPresentation;
- }
+ // setup the Search button which will display the search panel.
+ static setupSearchPanel(sidebarContainer: Doc) {
+ return Docs.Create.ButtonDocument({
+ width: 50, height: 35, borderRounding: "50%", boxShadow: "2px 2px 1px", title: "Search",
+ panel: Docs.Create.QueryDocument({
+ title: "search stack", ignoreClick: true
+ }),
+ targetContainer: sidebarContainer,
+ onClick: ScriptField.MakeScript("this.targetContainer.proto = this.panel")
+ });
+ }
- if (doc.Library === undefined) {
- let Search = Docs.Create.ButtonDocument({ width: 50, height: 35, borderRounding: "50%", boxShadow: "2px 2px 1px", title: "Search" });
- let Library = Docs.Create.ButtonDocument({ width: 50, height: 35, borderRounding: "50%", boxShadow: "2px 2px 1px", title: "Library" });
- let Create = Docs.Create.ButtonDocument({ width: 35, height: 35, borderRounding: "50%", boxShadow: "2px 2px 1px", title: "Create" });
- if (doc.sidebarContainer === undefined) {
- doc.sidebarContainer = new Doc();
- (doc.sidebarContainer as Doc).chromeStatus = "disabled";
- }
+ // setup the list of sidebar mode buttons which determine what is displayed in the sidebar
+ static setupSidebarButtons(doc: Doc) {
+ doc.sidebarContainer = new Doc();
+ (doc.sidebarContainer as Doc).chromeStatus = "disabled";
- const library = Docs.Create.TreeDocument([doc.workspaces as Doc, doc, doc.recentlyClosed as Doc], { title: "Library" });
- library.forceActive = true;
- library.lockedPosition = true;
- library.gridGap = 5;
- library.xMargin = 5;
- library.yMargin = 5;
- library.dropAction = "alias";
- Library.targetContainer = doc.sidebarContainer;
- Library.library = library;
- Library.onClick = ScriptField.MakeScript("this.targetContainer.proto = this.library");
+ doc.CreateBtn = this.setupCreatePanel(doc.sidebarContainer as Doc, doc);
+ doc.LibraryBtn = this.setupLibraryPanel(doc.sidebarContainer as Doc, doc);
+ doc.SearchBtn = this.setupSearchPanel(doc.sidebarContainer as Doc);
- const searchBox = Docs.Create.QueryDocument({ title: "search stack" });
- searchBox.ignoreClick = true;
- Search.searchBox = searchBox;
- Search.targetContainer = doc.sidebarContainer;
- Search.onClick = ScriptField.MakeScript("this.targetContainer.proto = this.searchBox");
+ // Finally, setup the list of buttons to display in the sidebar
+ doc.sidebarButtons = Docs.Create.StackingDocument([doc.SearchBtn as Doc, doc.LibraryBtn as Doc, doc.CreateBtn as Doc], {
+ width: 500, height: 80, boxShadow: "0 0", sectionFilter: "title", hideHeadings: true, ignoreClick: true,
+ backgroundColor: "lightgrey", chromeStatus: "disabled", title: "library stack"
+ });
+ }
- let createCollection = Docs.Create.FontIconDocument({ nativeWidth: 100, nativeHeight: 100, width: 100, height: 100, title: "Collection", icon: "folder" });
- createCollection.onDragStart = ScriptField.MakeFunction('Docs.Create.FreeformDocument([], { nativeWidth: undefined, nativeHeight: undefined, width: 150, height: 100, title: "freeform" })');
- let createWebPage = Docs.Create.FontIconDocument({ nativeWidth: 100, nativeHeight: 100, width: 100, height: 100, title: "Web Page", icon: "globe-asia" });
- createWebPage.onDragStart = ScriptField.MakeFunction('Docs.Create.WebDocument("https://en.wikipedia.org/wiki/Hedgehog", { width: 300, height: 300, title: "New Webpage" })');
- let createCatImage = Docs.Create.FontIconDocument({ nativeWidth: 100, nativeHeight: 100, width: 100, height: 100, title: "Image", icon: "cat" });
- createCatImage.onDragStart = ScriptField.MakeFunction('Docs.Create.ImageDocument("https://upload.wikimedia.org/wikipedia/commons/thumb/3/3a/Cat03.jpg/1200px-Cat03.jpg", { width: 200, title: "an image of a cat" })');
- let createButton = Docs.Create.FontIconDocument({ nativeWidth: 100, nativeHeight: 100, width: 100, height: 100, title: "Button", icon: "bolt" });
- createButton.onDragStart = ScriptField.MakeFunction('Docs.Create.ButtonDocument({ width: 150, height: 50, title: "Button" })');
- let createPresentation = Docs.Create.FontIconDocument({ nativeWidth: 100, nativeHeight: 100, width: 100, height: 100, title: "Presentation", icon: "tv" });
- createPresentation.onDragStart = ScriptField.MakeFunction('Doc.UserDoc().curPresentation = Docs.Create.PresDocument(new List<Doc>(), { width: 200, height: 500, title: "a presentation trail" })');
- let createFolderImport = Docs.Create.FontIconDocument({ nativeWidth: 100, nativeHeight: 100, width: 100, height: 100, title: "Import Folder", icon: "cloud-upload-alt" });
- createFolderImport.onDragStart = ScriptField.MakeFunction('Docs.Create.DirectoryImportDocument({ title: "Directory Import", width: 400, height: 400 })');
- const dragCreators = Docs.Create.MasonryDocument([createCollection, createWebPage, createCatImage, createButton, createPresentation, createFolderImport], { width: 500, autoHeight: true, columnWidth: 35, ignoreClick: true, lockedPosition: true, chromeStatus: "disabled", title: "buttons" });
- const color = Docs.Create.ColorDocument({ title: "color picker", width: 400 });
- color.dropAction = "alias";
- color.ignoreClick = true;
- color.removeDropProperties = new List<string>(["dropAction", "ignoreClick"]);
- const creators = Docs.Create.StackingDocument([dragCreators, color], { width: 500, height: 800, chromeStatus: "disabled", title: "creator stack" });
- Create.targetContainer = doc.sidebarContainer;
- Create.creators = creators;
- Create.onClick = ScriptField.MakeScript("this.targetContainer.proto = this.creators");
+ /// sets up the default list of buttons to be shown in the expanding button menu at the bottom of the Dash window
+ static setupExpandingButtons(doc: Doc) {
+ doc.undoBtn = Docs.Create.FontIconDocument(
+ { nativeWidth: 100, nativeHeight: 100, width: 100, height: 100, dropAction: "alias", onClick: ScriptField.MakeScript("undo()"), title: "undo button", icon: "undo-alt" });
+ doc.redoBtn = Docs.Create.FontIconDocument(
+ { nativeWidth: 100, nativeHeight: 100, width: 100, height: 100, dropAction: "alias", onClick: ScriptField.MakeScript("redo()"), title: "redo button", icon: "redo-alt" });
- const libraryButtons = Docs.Create.StackingDocument([Search, Library, Create], { width: 500, height: 80, chromeStatus: "disabled", title: "library stack" });
- libraryButtons.sectionFilter = "title";
- libraryButtons.boxShadow = "0 0";
- libraryButtons.ignoreClick = true;
- libraryButtons.hideHeadings = true;
- libraryButtons.backgroundColor = "lightgrey";
+ doc.expandingButtons = Docs.Create.LinearDocument([doc.undoBtn as Doc, doc.redoBtn as Doc], {
+ title: "expanding buttons", gridGap: 5, xMargin: 5, yMargin: 5, height: 42, width: 100, boxShadow: "0 0",
+ backgroundColor: "black", preventTreeViewOpen: true, forceActive: true, lockedPosition: true, convertToButtons: true
+ });
+ }
- doc.libraryButtons = libraryButtons;
- doc.Library = Library;
- doc.Create = Create;
- doc.Search = Search;
- }
- PromiseValue(Cast(doc.libraryButtons, Doc)).then(libraryButtons => { });
- PromiseValue(Cast(doc.Library, Doc)).then(library => library && library.library && library.targetContainer && (library.onClick as ScriptField).script.run({ this: library }));
- PromiseValue(Cast(doc.Create, Doc)).then(async create => create && create.creators && create.targetContainer);
- PromiseValue(Cast(doc.Search, Doc)).then(async search => search && search.searchBox && search.targetContainer);
+ // sets up the default set of documents to be shown in the Overlay layer
+ static setupOverlays(doc: Doc) {
+ doc.overlays = Docs.Create.FreeformDocument([], { title: "Overlays", backgroundColor: "#aca3a6" });
+ doc.linkFollowBox = Docs.Create.LinkFollowBoxDocument({ x: 250, y: 20, width: 500, height: 370, title: "Link Follower" });
+ Doc.AddDocToList(doc.overlays as Doc, "data", doc.linkFollowBox as Doc);
+ }
- if (doc.overlays === undefined) {
- const overlays = Docs.Create.FreeformDocument([], { title: "Overlays" });
- Doc.GetProto(overlays).backgroundColor = "#aca3a6";
- doc.overlays = overlays;
- }
+ // the initial presentation Doc to use
+ static setupDefaultPresentation(doc: Doc) {
+ doc.curPresentation = Docs.Create.PresDocument(new List<Doc>(), { title: "Presentation", boxShadow: "0 0" });
+ }
- if (doc.linkFollowBox === undefined) {
- PromiseValue(Cast(doc.overlays, Doc)).then(overlays => overlays && Doc.AddDocToList(overlays, "data", doc.linkFollowBox = Docs.Create.LinkFollowBoxDocument({ x: 250, y: 20, width: 500, height: 370, title: "Link Follower" })));
- }
+ static setupMobileUploads(doc: Doc) {
+ doc.optionalRightCollection = Docs.Create.StackingDocument([], { title: "New mobile uploads" });
+ }
+
+ static updateUserDocument(doc: Doc) {
+ new InkingControl();
+ (doc.optionalRightCollection === undefined) && CurrentUserUtils.setupMobileUploads(doc);
+ (doc.noteTypes === undefined) && CurrentUserUtils.setupNoteTypes(doc);
+ (doc.overlays === undefined) && CurrentUserUtils.setupOverlays(doc);
+ (doc.expandingButtons === undefined) && CurrentUserUtils.setupExpandingButtons(doc);
+ (doc.curPresentation === undefined) && CurrentUserUtils.setupDefaultPresentation(doc);
+ (doc.sidebarButtons === undefined) && CurrentUserUtils.setupSidebarButtons(doc);
- doc.title = "DOCUMENTS";
- doc.backgroundColor = "#eeeeee";
- doc.width = 100;
- doc.preventTreeViewOpen = true;
- doc.forceActive = true;
- doc.lockedPosition = true;
+ // this is equivalent to using PrefetchProxies to make sure all the sidebarButtons and noteType internal Doc's have been retrieved.
+ PromiseValue(Cast(doc.noteTypes, Doc)).then(noteTypes => noteTypes && PromiseValue(noteTypes.data).then(DocListCast));
+ PromiseValue(Cast(doc.sidebarButtons, Doc)).then(stackingDoc => {
+ stackingDoc && PromiseValue(Cast(stackingDoc.data, listSpec(Doc))).then(sidebarButtons => {
+ sidebarButtons && sidebarButtons.map((sidebarBtn, i) => {
+ sidebarBtn && PromiseValue(Cast(sidebarBtn, Doc)).then(async btn => {
+ btn && btn.panel && btn.targetContainer && i === 1 && (btn.onClick as ScriptField).script.run({ this: btn });
+ });
+ });
+ });
+ });
+
+ // setup reactions to change the highlights on the undo/redo buttons -- would be better to encode this in the undo/redo buttons, but the undo/redo stacks are not wired up that way yet
+ doc.undoBtn && reaction(() => UndoManager.undoStack.slice(), () => (doc.undoBtn as Doc).opacity = UndoManager.CanUndo() ? 1 : 0.4, { fireImmediately: true });
+ doc.redoBtn && reaction(() => UndoManager.redoStack.slice(), () => (doc.redoBtn as Doc).opacity = UndoManager.CanRedo() ? 1 : 0.4, { fireImmediately: true });
+
+ return doc;
}
public static loadCurrentUser() {
@@ -200,12 +217,8 @@ export class CurrentUserUtils {
await rp.get(Utils.prepend(RouteStore.getUserDocumentId)).then(id => {
if (id && id !== "guest") {
return DocServer.GetRefField(id).then(async field => {
- if (field instanceof Doc) {
- await this.updateUserDocument(field);
- runInAction(() => Doc.SetUserDoc(field));
- } else {
- runInAction(() => Doc.SetUserDoc(this.createUserDocument(id)));
- }
+ let userDoc = field instanceof Doc ? await this.updateUserDocument(field) : this.createUserDocument(id);
+ runInAction(() => Doc.SetUserDoc(userDoc));
});
} else {
throw new Error("There should be a user id! Why does Dash think there isn't one?");
diff --git a/src/server/index.ts b/src/server/index.ts
index 010a851bc..c1dba2976 100644
--- a/src/server/index.ts
+++ b/src/server/index.ts
@@ -55,6 +55,7 @@ import { ParsedPDF } from "./PdfTypes";
import { reject } from 'bluebird';
import { ExifData } from 'exif';
import { Result } from '../client/northstar/model/idea/idea';
+import RouteSubscriber from './RouteSubscriber';
const download = (url: string, dest: fs.PathLike) => request.get(url).pipe(fs.createWriteStream(dest));
let youtubeApiKey: string;
@@ -106,6 +107,26 @@ enum Method {
POST
}
+export type ValidationHandler = (user: DashUserModel, req: express.Request, res: express.Response) => any | Promise<any>;
+export type RejectionHandler = (req: express.Request, res: express.Response) => any | Promise<any>;
+export type ErrorHandler = (req: express.Request, res: express.Response, error: any) => any | Promise<any>;
+
+const LoginRedirect: RejectionHandler = (_req, res) => res.redirect(RouteStore.login);
+
+export interface RouteInitializer {
+ method: Method;
+ subscribers: string | RouteSubscriber | (string | RouteSubscriber)[];
+ onValidation: ValidationHandler;
+ onRejection?: RejectionHandler;
+ onError?: ErrorHandler;
+}
+
+const isSharedDocAccess = (target: string) => {
+ const shared = qs.parse(qs.extract(target), { sort: false }).sharing === "true";
+ const docAccess = target.startsWith("/doc/");
+ return shared && docAccess;
+};
+
/**
* Please invoke this function when adding a new route to Dash's server.
* It ensures that any requests leading to or containing user-sensitive information
@@ -115,22 +136,40 @@ enum Method {
* @param onRejection an optional callback invoked on return if no user is found to be logged in
* @param subscribers the forward slash prepended path names (reference and add to RouteStore.ts) that will all invoke the given @param handler
*/
-function addSecureRoute(method: Method,
- handler: (user: DashUserModel, res: express.Response, req: express.Request) => void,
- onRejection: (res: express.Response, req: express.Request) => any = res => res.redirect(RouteStore.login),
- ...subscribers: string[]
-) {
- let abstracted = (req: express.Request, res: express.Response) => {
- let sharing = qs.parse(qs.extract(req.originalUrl), { sort: false }).sharing === "true";
- sharing = sharing && req.originalUrl.startsWith("/doc/");
- if (req.user || sharing) {
- handler(req.user as any, res, req);
+function addSecureRoute(initializer: RouteInitializer) {
+ const { method, subscribers, onValidation, onRejection, onError } = initializer;
+ let abstracted = async (req: express.Request, res: express.Response) => {
+ const { user, originalUrl: target } = req;
+ if (user || isSharedDocAccess(target)) {
+ try {
+ await onValidation(user as any, req, res);
+ } catch (e) {
+ if (onError) {
+ onError(req, res, e);
+ } else {
+ _error(res, `The server encountered an internal error handling ${target}.`, e);
+ }
+ }
} else {
- req.session!.target = req.originalUrl;
- onRejection(res, req);
+ req.session!.target = target;
+ try {
+ await (onRejection || LoginRedirect)(req, res);
+ } catch (e) {
+ if (onError) {
+ onError(req, res, e);
+ } else {
+ _error(res, `The server encountered an internal error when rejecting ${target}.`, e);
+ }
+ }
}
};
- subscribers.forEach(route => {
+ const subscribe = (subscriber: RouteSubscriber | string) => {
+ let route: string;
+ if (typeof subscriber === "string") {
+ route = subscriber;
+ } else {
+ route = subscriber.build;
+ }
switch (method) {
case Method.GET:
app.get(route, abstracted);
@@ -139,7 +178,12 @@ function addSecureRoute(method: Method,
app.post(route, abstracted);
break;
}
- });
+ };
+ if (Array.isArray(subscribers)) {
+ subscribers.forEach(subscribe);
+ } else {
+ subscribe(subscribers);
+ }
}
// STATIC FILE SERVING
@@ -323,13 +367,17 @@ app.get("/serializeDoc/:docId", async (req, res) => {
export type Hierarchy = { [id: string]: string | Hierarchy };
export type ZipMutator = (file: Archiver.Archiver) => void | Promise<void>;
-app.get(`${RouteStore.imageHierarchyExport}/:docId`, async (req, res) => {
- const id = req.params.docId;
- const hierarchy: Hierarchy = {};
- await targetedVisitorRecursive(id, hierarchy);
- BuildAndDispatchZip(res, async zip => {
- await hierarchyTraverserRecursive(zip, hierarchy);
- });
+addSecureRoute({
+ method: Method.GET,
+ subscribers: new RouteSubscriber(RouteStore.imageHierarchyExport).add('docId'),
+ onValidation: async (_user, req, res) => {
+ const id = req.params.docId;
+ const hierarchy: Hierarchy = {};
+ await targetedVisitorRecursive(id, hierarchy);
+ BuildAndDispatchZip(res, async zip => {
+ await hierarchyTraverserRecursive(zip, hierarchy);
+ });
+ }
});
const BuildAndDispatchZip = async (res: Response, mutator: ZipMutator): Promise<void> => {
@@ -576,50 +624,49 @@ function LoadPage(file: string, pageNumber: number, res: Response) {
});
}
-// anyone attempting to navigate to localhost at this port will
-// first have to login
-addSecureRoute(
- Method.GET,
- (user, res) => res.redirect(RouteStore.home),
- undefined,
- RouteStore.root
-);
-
-addSecureRoute(
- Method.GET,
- async (_, res) => {
+/**
+ * Anyone attempting to navigate to localhost at this port will
+ * first have to log in.
+ */
+addSecureRoute({
+ method: Method.GET,
+ subscribers: RouteStore.root,
+ onValidation: (_user, _req, res) => res.redirect(RouteStore.home)
+});
+
+addSecureRoute({
+ method: Method.GET,
+ subscribers: RouteStore.getUsers,
+ onValidation: async (_user, _req, res) => {
const cursor = await Database.Instance.query({}, { email: 1, userDocumentId: 1 }, "users");
const results = await cursor.toArray();
res.send(results.map(user => ({ email: user.email, userDocumentId: user.userDocumentId })));
},
- undefined,
- RouteStore.getUsers
-);
+});
-addSecureRoute(
- Method.GET,
- (user, res, req) => {
+addSecureRoute({
+ method: Method.GET,
+ subscribers: [RouteStore.home, RouteStore.openDocumentWithId],
+ onValidation: (_user, req, res) => {
let detector = new mobileDetect(req.headers['user-agent'] || "");
let filename = detector.mobile() !== null ? 'mobile/image.html' : 'index.html';
res.sendFile(path.join(__dirname, '../../deploy/' + filename));
},
- undefined,
- RouteStore.home, RouteStore.openDocumentWithId
-);
-
-addSecureRoute(
- Method.GET,
- (user, res) => res.send(user.userDocumentId),
- (res) => res.send(undefined),
- RouteStore.getUserDocumentId,
-);
-
-addSecureRoute(
- Method.GET,
- (user, res) => { res.send(JSON.stringify({ id: user.id, email: user.email })); },
- (res) => res.send(JSON.stringify({ id: "__guest__", email: "" })),
- RouteStore.getCurrUser
-);
+});
+
+addSecureRoute({
+ method: Method.GET,
+ subscribers: RouteStore.getUserDocumentId,
+ onValidation: (user, _req, res) => res.send(user.userDocumentId),
+ onRejection: (_req, res) => res.send(undefined)
+});
+
+addSecureRoute({
+ method: Method.GET,
+ subscribers: RouteStore.getCurrUser,
+ onValidation: (user, _req, res) => { res.send(JSON.stringify(user)); },
+ onRejection: (_req, res) => res.send(JSON.stringify({ id: "__guest__", email: "" }))
+});
const ServicesApiKeyMap = new Map<string, string | undefined>([
["face", process.env.FACE],
@@ -627,10 +674,14 @@ const ServicesApiKeyMap = new Map<string, string | undefined>([
["handwriting", process.env.HANDWRITING]
]);
-addSecureRoute(Method.GET, (user, res, req) => {
- let service = req.params.requestedservice;
- res.send(ServicesApiKeyMap.get(service));
-}, undefined, `${RouteStore.cognitiveServices}/:requestedservice`);
+addSecureRoute({
+ method: Method.GET,
+ subscribers: new RouteSubscriber(RouteStore.cognitiveServices).add('requestedservice'),
+ onValidation: (_user, req, res) => {
+ let service = req.params.requestedservice;
+ res.send(ServicesApiKeyMap.get(service));
+ }
+});
class NodeCanvasFactory {
create = (width: number, height: number) => {
@@ -668,10 +719,10 @@ interface ImageFileResponse {
exif: Opt<DashUploadUtils.EnrichedExifData>;
}
-// SETTERS
-app.post(
- RouteStore.upload,
- (req, res) => {
+addSecureRoute({
+ method: Method.POST,
+ subscribers: RouteStore.upload,
+ onValidation: (_user, req, res) => {
let form = new formidable.IncomingForm();
form.uploadDir = uploadDirectory;
form.keepExtensions = true;
@@ -704,20 +755,25 @@ app.post(
_success(res, results);
});
}
-);
+});
-app.post(RouteStore.inspectImage, async (req, res) => {
- const { source } = req.body;
- if (typeof source === "string") {
- const uploadInformation = await DashUploadUtils.UploadImage(source);
- return res.send(await DashUploadUtils.InspectImage(uploadInformation.mediaPaths[0]));
+addSecureRoute({
+ method: Method.POST,
+ subscribers: RouteStore.inspectImage,
+ onValidation: async (_user, req, res) => {
+ const { source } = req.body;
+ if (typeof source === "string") {
+ const uploadInformation = await DashUploadUtils.UploadImage(source);
+ return res.send(await DashUploadUtils.InspectImage(uploadInformation.mediaPaths[0]));
+ }
+ res.send({});
}
- res.send({});
});
-addSecureRoute(
- Method.POST,
- (user, res, req) => {
+addSecureRoute({
+ method: Method.POST,
+ subscribers: RouteStore.dataUriToImage,
+ onValidation: (_user, req, res) => {
const uri = req.body.uri;
const filename = req.body.name;
if (!uri || !filename) {
@@ -750,10 +806,9 @@ addSecureRoute(
}
res.send("/files/" + filename + ext);
});
- },
- undefined,
- RouteStore.dataUriToImage
-);
+ }
+});
+
// AUTHENTICATION
// Sign Up
@@ -792,29 +847,27 @@ app.use(RouteStore.corsProxy, (req, res) => {
}).pipe(res);
});
-addSecureRoute(
- Method.GET,
- (user, res, req) => {
+addSecureRoute({
+ method: Method.GET,
+ subscribers: RouteStore.delete,
+ onValidation: (_user, _req, res) => {
if (release) {
return _permission_denied(res, deletionPermissionError);
}
deleteFields().then(() => res.redirect(RouteStore.home));
- },
- undefined,
- RouteStore.delete
-);
+ }
+});
-addSecureRoute(
- Method.GET,
- (_user, res, _req) => {
+addSecureRoute({
+ method: Method.GET,
+ subscribers: RouteStore.deleteAll,
+ onValidation: (_user, _req, res) => {
if (release) {
return _permission_denied(res, deletionPermissionError);
}
deleteAll().then(() => res.redirect(RouteStore.home));
- },
- undefined,
- RouteStore.deleteAll
-);
+ }
+});
app.use(wdm(compiler, { publicPath: config.output.publicPath }));
@@ -945,20 +998,28 @@ app.post(RouteStore.googleDocs + "/:sector/:action", (req, res) => {
});
});
-app.get(RouteStore.readGoogleAccessToken, async (req, res) => {
- const userId = req.header("userId")!;
- const token = await Database.Auxiliary.GoogleAuthenticationToken.Fetch(userId);
- const information = { credentialsPath, userId };
- if (!token) {
- return res.send(await GoogleApiServerUtils.GenerateAuthenticationUrl(information));
+addSecureRoute({
+ method: Method.GET,
+ subscribers: RouteStore.readGoogleAccessToken,
+ onValidation: async (user, _req, res) => {
+ const userId = user.id;
+ const token = await Database.Auxiliary.GoogleAuthenticationToken.Fetch(userId);
+ const information = { credentialsPath, userId };
+ if (!token) {
+ return res.send(await GoogleApiServerUtils.GenerateAuthenticationUrl(information));
+ }
+ GoogleApiServerUtils.RetrieveAccessToken(information).then(token => res.send(token));
}
- GoogleApiServerUtils.RetrieveAccessToken(information).then(token => res.send(token));
});
-app.post(RouteStore.writeGoogleAccessToken, async (req, res) => {
- const userId = req.header("userId")!;
- const information = { credentialsPath, userId };
- res.send(await GoogleApiServerUtils.ProcessClientSideCode(information, req.body.authenticationCode));
+addSecureRoute({
+ method: Method.POST,
+ subscribers: RouteStore.writeGoogleAccessToken,
+ onValidation: async (user, req, res) => {
+ const userId = user.id;
+ const information = { credentialsPath, userId };
+ res.send(await GoogleApiServerUtils.ProcessClientSideCode(information, req.body.authenticationCode));
+ }
});
const tokenError = "Unable to successfully upload bytes for all images!";
@@ -972,47 +1033,50 @@ export interface NewMediaItem {
};
}
-app.post(RouteStore.googlePhotosMediaUpload, async (req, res) => {
- const { media } = req.body;
- const userId = req.header("userId");
-
- if (!userId) {
- return _error(res, userIdError);
- }
-
- await GooglePhotosUploadUtils.initialize({ credentialsPath, userId });
-
- let failed: number[] = [];
+addSecureRoute({
+ method: Method.POST,
+ subscribers: RouteStore.googlePhotosMediaUpload,
+ onValidation: async (user, req, res) => {
+ const { media } = req.body;
+ const userId = user.id;
+ if (!userId) {
+ return _error(res, userIdError);
+ }
- const newMediaItems = await BatchedArray.from<GooglePhotosUploadUtils.MediaInput>(media, { batchSize: 25 }).batchedMapPatientInterval(
- { magnitude: 100, unit: TimeUnit.Milliseconds },
- async (batch: GooglePhotosUploadUtils.MediaInput[]) => {
- const newMediaItems: NewMediaItem[] = [];
- for (let index = 0; index < batch.length; index++) {
- const element = batch[index];
- const uploadToken = await GooglePhotosUploadUtils.DispatchGooglePhotosUpload(element.url);
- if (!uploadToken) {
- failed.push(index);
- } else {
- newMediaItems.push({
- description: element.description,
- simpleMediaItem: { uploadToken }
- });
+ await GooglePhotosUploadUtils.initialize({ credentialsPath, userId });
+
+ let failed: number[] = [];
+
+ const newMediaItems = await BatchedArray.from<GooglePhotosUploadUtils.MediaInput>(media, { batchSize: 25 }).batchedMapPatientInterval(
+ { magnitude: 100, unit: TimeUnit.Milliseconds },
+ async (batch: GooglePhotosUploadUtils.MediaInput[]) => {
+ const newMediaItems: NewMediaItem[] = [];
+ for (let index = 0; index < batch.length; index++) {
+ const element = batch[index];
+ const uploadToken = await GooglePhotosUploadUtils.DispatchGooglePhotosUpload(element.url);
+ if (!uploadToken) {
+ failed.push(index);
+ } else {
+ newMediaItems.push({
+ description: element.description,
+ simpleMediaItem: { uploadToken }
+ });
+ }
}
+ return newMediaItems;
}
- return newMediaItems;
+ );
+
+ const failedCount = failed.length;
+ if (failedCount) {
+ console.error(`Unable to upload ${failedCount} image${failedCount === 1 ? "" : "s"} to Google's servers`);
}
- );
- const failedCount = failed.length;
- if (failedCount) {
- console.log(`Unable to upload ${failedCount} image${failedCount === 1 ? "" : "s"} to Google's servers`);
+ GooglePhotosUploadUtils.CreateMediaItems(newMediaItems, req.body.album).then(
+ result => _success(res, { results: result.newMediaItemResults, failed }),
+ error => _error(res, mediaError, error)
+ );
}
-
- GooglePhotosUploadUtils.CreateMediaItems(newMediaItems, req.body.album).then(
- result => _success(res, { results: result.newMediaItemResults, failed }),
- error => _error(res, mediaError, error)
- );
});
interface MediaItem {