aboutsummaryrefslogtreecommitdiff
path: root/src/server
diff options
context:
space:
mode:
Diffstat (limited to 'src/server')
-rw-r--r--src/server/RouteStore.ts5
-rw-r--r--src/server/authentication/controllers/WorkspacesMenu.tsx19
-rw-r--r--src/server/authentication/controllers/user_controller.ts3
-rw-r--r--src/server/authentication/models/current_user_utils.ts85
-rw-r--r--src/server/authentication/models/user_model.ts11
-rw-r--r--src/server/database.ts23
-rw-r--r--src/server/index.ts49
7 files changed, 114 insertions, 81 deletions
diff --git a/src/server/RouteStore.ts b/src/server/RouteStore.ts
index fb06b878b..fdf5b6a5c 100644
--- a/src/server/RouteStore.ts
+++ b/src/server/RouteStore.ts
@@ -15,10 +15,7 @@ export enum RouteStore {
// USER AND WORKSPACES
getCurrUser = "/getCurrentUser",
- addWorkspace = "/addWorkspaceId",
- getAllWorkspaces = "/getAllWorkspaceIds",
- getActiveWorkspace = "/getActiveWorkspaceId",
- setActiveWorkspace = "/setActiveWorkspaceId",
+ getUserDocumentId = "/getUserDocumentId",
updateCursor = "/updateCursor",
openDocumentWithId = "/doc/:docId",
diff --git a/src/server/authentication/controllers/WorkspacesMenu.tsx b/src/server/authentication/controllers/WorkspacesMenu.tsx
index 1533b1e62..8e14cf98e 100644
--- a/src/server/authentication/controllers/WorkspacesMenu.tsx
+++ b/src/server/authentication/controllers/WorkspacesMenu.tsx
@@ -9,30 +9,23 @@ import { KeyStore } from '../../../fields/KeyStore';
export interface WorkspaceMenuProps {
active: Document | undefined;
open: (workspace: Document) => void;
- new: (init: boolean) => void;
+ new: () => void;
allWorkspaces: Document[];
+ isShown: () => boolean;
+ toggle: () => void;
}
@observer
export class WorkspacesMenu extends React.Component<WorkspaceMenuProps> {
- static Instance: WorkspacesMenu;
- @observable private workspacesExposed: boolean = false;
-
constructor(props: WorkspaceMenuProps) {
super(props);
- WorkspacesMenu.Instance = this;
this.addNewWorkspace = this.addNewWorkspace.bind(this);
}
@action
addNewWorkspace() {
- this.props.new(false);
- this.toggle();
- }
-
- @action
- toggle() {
- this.workspacesExposed = !this.workspacesExposed;
+ this.props.new();
+ this.props.toggle();
}
render() {
@@ -45,7 +38,7 @@ export class WorkspacesMenu extends React.Component<WorkspaceMenuProps> {
borderRadius: 5,
position: "absolute",
top: 78,
- left: this.workspacesExposed ? 11 : -500,
+ left: this.props.isShown() ? 11 : -500,
background: "white",
border: "black solid 2px",
transition: "all 1s ease",
diff --git a/src/server/authentication/controllers/user_controller.ts b/src/server/authentication/controllers/user_controller.ts
index 2cef958e8..e365b8dce 100644
--- a/src/server/authentication/controllers/user_controller.ts
+++ b/src/server/authentication/controllers/user_controller.ts
@@ -11,6 +11,7 @@ import * as async from 'async';
import * as nodemailer from 'nodemailer';
import c = require("crypto");
import { RouteStore } from "../../RouteStore";
+import { Utils } from "../../../Utils";
/**
* GET /signup
@@ -54,7 +55,7 @@ export let postSignup = (req: Request, res: Response, next: NextFunction) => {
const user = new User({
email,
password,
- userDoc: "document here"
+ userDocumentId: Utils.GenerateGuid()
});
User.findOne({ email }, (err, existingUser) => {
diff --git a/src/server/authentication/models/current_user_utils.ts b/src/server/authentication/models/current_user_utils.ts
index cc433eb73..055e4cc97 100644
--- a/src/server/authentication/models/current_user_utils.ts
+++ b/src/server/authentication/models/current_user_utils.ts
@@ -1,29 +1,96 @@
import { DashUserModel } from "./user_model";
-import * as request from 'request'
+import * as rp from 'request-promise';
import { RouteStore } from "../../RouteStore";
import { ServerUtils } from "../../ServerUtil";
+import { Server } from "../../../client/Server";
+import { Document } from "../../../fields/Document";
+import { KeyStore } from "../../../fields/KeyStore";
+import { ListField } from "../../../fields/ListField";
+import { Documents } from "../../../client/documents/Documents";
+import { Schema, Attribute, AttributeGroup } from "../../../client/northstar/model/idea/idea";
export class CurrentUserUtils {
private static curr_email: string;
private static curr_id: string;
+ private static user_document: Document;
+ //TODO tfs: these should be temporary...
+ private static mainDocId: string | undefined;
+ private static activeSchema: Schema | undefined;
- public static get email() {
- return CurrentUserUtils.curr_email;
+ public static get email(): string {
+ return this.curr_email;
}
- public static get id() {
- return CurrentUserUtils.curr_id;
+ public static get id(): string {
+ return this.curr_id;
}
- public static loadCurrentUser() {
- request.get(ServerUtils.prepend(RouteStore.getCurrUser), (error, response, body) => {
- if (body) {
- let obj = JSON.parse(body);
+ public static get UserDocument(): Document {
+ return this.user_document;
+ }
+
+ public static get MainDocId(): string | undefined {
+ return this.mainDocId;
+ }
+
+ public static set MainDocId(id: string | undefined) {
+ this.mainDocId = id;
+ }
+
+ public static get ActiveSchema(): Schema | undefined {
+ return this.activeSchema;
+ }
+ public static GetAllNorthstarColumnAttributes() {
+ if (!this.ActiveSchema || !this.ActiveSchema.rootAttributeGroup) {
+ return [];
+ }
+ const recurs = (attrs: Attribute[], g: AttributeGroup) => {
+ if (g.attributes) {
+ attrs.push.apply(attrs, g.attributes);
+ if (g.attributeGroups) {
+ g.attributeGroups.forEach(ng => recurs(attrs, ng));
+ }
+ }
+ };
+ const allAttributes: Attribute[] = new Array<Attribute>();
+ recurs(allAttributes, this.ActiveSchema.rootAttributeGroup);
+ return allAttributes;
+ }
+
+ public static set ActiveSchema(id: Schema | undefined) {
+ this.activeSchema = id;
+ }
+ private static createUserDocument(id: string): Document {
+ let doc = new Document(id);
+
+ doc.Set(KeyStore.Workspaces, new ListField<Document>());
+ doc.Set(KeyStore.OptionalRightCollection, Documents.SchemaDocument([], { title: "Pending documents" }))
+ return doc;
+ }
+
+ public static loadCurrentUser(): Promise<any> {
+ let userPromise = rp.get(ServerUtils.prepend(RouteStore.getCurrUser)).then((response) => {
+ if (response) {
+ let obj = JSON.parse(response);
CurrentUserUtils.curr_id = obj.id as string;
CurrentUserUtils.curr_email = obj.email as string;
} else {
throw new Error("There should be a user! Why does Dash think there isn't one?")
}
});
+ let userDocPromise = rp.get(ServerUtils.prepend(RouteStore.getUserDocumentId)).then(id => {
+ if (id) {
+ return Server.GetField(id).then(field => {
+ if (field instanceof Document) {
+ this.user_document = field;
+ } else {
+ this.user_document = this.createUserDocument(id);
+ }
+ })
+ } else {
+ throw new Error("There should be a user id! Why does Dash think there isn't one?")
+ }
+ });
+ return Promise.all([userPromise, userDocPromise]);
}
} \ No newline at end of file
diff --git a/src/server/authentication/models/user_model.ts b/src/server/authentication/models/user_model.ts
index 3d4ed6896..81580aad5 100644
--- a/src/server/authentication/models/user_model.ts
+++ b/src/server/authentication/models/user_model.ts
@@ -21,9 +21,7 @@ export type DashUserModel = mongoose.Document & {
passwordResetToken: string | undefined,
passwordResetExpires: Date | undefined,
- allWorkspaceIds: Array<String>,
- activeWorkspaceId: String,
- activeUsersId: String,
+ userDocumentId: string;
profile: {
name: string,
@@ -49,12 +47,7 @@ const userSchema = new mongoose.Schema({
passwordResetToken: String,
passwordResetExpires: Date,
- allWorkspaceIds: {
- type: Array,
- default: []
- },
- activeWorkspaceId: String,
- activeUsersId: String,
+ userDocumentId: String,
facebook: String,
twitter: String,
diff --git a/src/server/database.ts b/src/server/database.ts
index 99b13805e..a42d29aac 100644
--- a/src/server/database.ts
+++ b/src/server/database.ts
@@ -21,7 +21,16 @@ export class Database {
let collection = this.db.collection('documents');
collection.updateOne({ _id: id }, { $set: value }, {
upsert: true
- }, callback);
+ }, (err, res) => {
+ if (err) {
+ console.log(err.message);
+ console.log(err.errmsg);
+ }
+ if (res) {
+ console.log(JSON.stringify(res.result));
+ }
+ callback()
+ });
}
}
@@ -32,11 +41,13 @@ export class Database {
}
}
- public deleteAll(collectionName: string = 'documents') {
- if (this.db) {
- let collection = this.db.collection(collectionName);
- collection.deleteMany({});
- }
+ public deleteAll(collectionName: string = 'documents'): Promise<any> {
+ return new Promise(res => {
+ if (this.db) {
+ let collection = this.db.collection(collectionName);
+ collection.deleteMany({}, res);
+ }
+ })
}
public insert(kvpairs: any) {
diff --git a/src/server/index.ts b/src/server/index.ts
index d1eb6847d..17d7432e0 100644
--- a/src/server/index.ts
+++ b/src/server/index.ts
@@ -156,16 +156,9 @@ addSecureRoute(
addSecureRoute(
Method.GET,
- (user, res) => res.send(user.activeWorkspaceId || ""),
+ (user, res) => res.send(user.userDocumentId || ""),
undefined,
- RouteStore.getActiveWorkspace,
-);
-
-addSecureRoute(
- Method.GET,
- (user, res) => res.send(JSON.stringify(user.allWorkspaceIds)),
- undefined,
- RouteStore.getAllWorkspaces
+ RouteStore.getUserDocumentId,
);
addSecureRoute(
@@ -185,28 +178,6 @@ addSecureRoute(
addSecureRoute(
Method.POST,
(user, res, req) => {
- user.update({ $set: { activeWorkspaceId: req.body.target } }, (err, raw) => {
- res.sendStatus(err ? 500 : 200);
- });
- },
- undefined,
- RouteStore.setActiveWorkspace
-);
-
-addSecureRoute(
- Method.POST,
- (user, res, req) => {
- user.update({ $push: { allWorkspaceIds: req.body.target } }, (err, raw) => {
- res.sendStatus(err ? 500 : 200);
- });
- },
- undefined,
- RouteStore.addWorkspace
-);
-
-addSecureRoute(
- Method.POST,
- (user, res, req) => {
let form = new formidable.IncomingForm()
form.uploadDir = __dirname + "/public/files/"
form.keepExtensions = true
@@ -252,13 +223,11 @@ app.use(RouteStore.corsProxy, (req, res) => {
});
app.get(RouteStore.delete, (req, res) => {
- deleteFields();
- res.redirect(RouteStore.home);
+ deleteFields().then(() => res.redirect(RouteStore.home));
});
app.get(RouteStore.deleteAll, (req, res) => {
- deleteAll();
- res.redirect(RouteStore.home);
+ deleteAll().then(() => res.redirect(RouteStore.home));
});
app.use(wdm(compiler, {
@@ -291,13 +260,15 @@ server.on("connection", function (socket: Socket) {
})
function deleteFields() {
- Database.Instance.deleteAll();
+ return Database.Instance.deleteAll();
}
function deleteAll() {
- Database.Instance.deleteAll();
- Database.Instance.deleteAll('sessions');
- Database.Instance.deleteAll('users');
+ return Database.Instance.deleteAll().then(() => {
+ return Database.Instance.deleteAll('sessions')
+ }).then(() => {
+ return Database.Instance.deleteAll('users')
+ });
}
function barReceived(guid: String) {