From 04a881bd949341a1cc6580d72527532df8f5bb8e Mon Sep 17 00:00:00 2001
From: Sam Wilkins <samwilkins333@gmail.com>
Date: Tue, 8 Oct 2019 19:33:50 -0400
Subject: fixes for sharing logic and UI

---
 src/client/util/SharingManager.tsx | 93 +++++++++++++++++++++-----------------
 1 file changed, 52 insertions(+), 41 deletions(-)

(limited to 'src/client/util/SharingManager.tsx')

diff --git a/src/client/util/SharingManager.tsx b/src/client/util/SharingManager.tsx
index 1cde2aa8e..0b8b03b51 100644
--- a/src/client/util/SharingManager.tsx
+++ b/src/client/util/SharingManager.tsx
@@ -2,7 +2,7 @@ import { observable, runInAction, action, autorun } from "mobx";
 import * as React from "react";
 import MainViewModal from "../views/MainViewModal";
 import { CurrentUserUtils } from "../../server/authentication/models/current_user_utils";
-import { Doc, Opt } from "../../new_fields/Doc";
+import { Doc, Opt, DocListCastAsync } from "../../new_fields/Doc";
 import { DocServer } from "../DocServer";
 import { Cast, StrCast } from "../../new_fields/Types";
 import { listSpec } from "../../new_fields/Schema";
@@ -23,6 +23,7 @@ import { DocumentManager } from "./DocumentManager";
 import { CollectionVideoView } from "../views/collections/CollectionVideoView";
 import { CollectionPDFView } from "../views/collections/CollectionPDFView";
 import { CollectionView } from "../views/collections/CollectionView";
+import { RefField } from "../../new_fields/RefField";
 
 library.add(fa.faCopy);
 
@@ -49,11 +50,16 @@ const SharingKey = "sharingPermissions";
 const PublicKey = "publicLinkPermissions";
 const DefaultColor = "black";
 
+interface ValidatedUser {
+    user: User;
+    notificationDoc: Doc;
+}
+
 @observer
 export default class SharingManager extends React.Component<{}> {
     public static Instance: SharingManager;
     @observable private isOpen = false;
-    @observable private users: User[] = [];
+    @observable private users: ValidatedUser[] = [];
     @observable private targetDoc: Doc | undefined;
     @observable private targetDocView: DocumentView | undefined;
     @observable private copied = false;
@@ -101,52 +107,55 @@ export default class SharingManager extends React.Component<{}> {
 
     populateUsers = async () => {
         let userList = await RequestPromise.get(Utils.prepend(RouteStore.getUsers));
-        runInAction(() => {
-            this.users = (JSON.parse(userList) as User[]).filter(({ email }) => email !== Doc.CurrentUserEmail);
+        const raw = JSON.parse(userList) as User[];
+        const evaluating = raw.map(async user => {
+            let isCandidate = user.email !== Doc.CurrentUserEmail;
+            if (isCandidate) {
+                const userDocument = await DocServer.GetRefField(user.userDocumentId);
+                if (userDocument instanceof Doc) {
+                    const notificationDoc = await Cast(userDocument.optionalRightCollection, Doc);
+                    runInAction(() => {
+                        if (notificationDoc instanceof Doc) {
+                            this.users.push({ user, notificationDoc });
+                        }
+                    });
+                }
+            }
         });
+        return Promise.all(evaluating);
     }
 
-    setInternalSharing = async (user: User, state: string) => {
+    setInternalSharing = async (validated: ValidatedUser, state: string) => {
         if (!this.sharingDoc) {
-            console.log("SHARING ABORTED!");
-            return;
-        }
-        let sharingDoc = await this.sharingDoc;
-        sharingDoc[user.userDocumentId] = state;
-        const userDocument = await DocServer.GetRefField(user.userDocumentId);
-        if (!(userDocument instanceof Doc)) {
-            console.log(`Couldn't get user document of user ${user.email}`);
-            return;
+            return console.log("SHARING ABORTED!");
         }
+        const { user, notificationDoc } = validated;
+        this.sharingDoc[user.userDocumentId] = state;
         let target = this.targetDoc;
         if (!target) {
-            console.log("SharingManager trying to share an undefined document!!");
+            return console.log("SharingManager trying to share an undefined document!!");
+        }
+        const data = await DocListCastAsync(notificationDoc.data);
+        if (!data) {
+            console.log("UNABLE TO ACCESS NOTIFICATION DATA");
             return;
         }
-        const notifDoc = await Cast(userDocument.optionalRightCollection, Doc);
-        if (notifDoc instanceof Doc) {
-            const data = await Cast(notifDoc.data, listSpec(Doc));
-            if (!data) {
-                console.log("UNABLE TO ACCESS NOTIFICATION DATA");
-                return;
+        console.log(`Attempting to set permissions to ${state} for the document ${target[Id]}`);
+        if (state !== SharingPermissions.None) {
+            const sharedDoc = Doc.MakeAlias(target);
+            if (data) {
+                data.push(sharedDoc);
+            } else {
+                notificationDoc.data = new List([sharedDoc]);
             }
-            console.log(`Attempting to set permissions to ${state} for the document ${target[Id]}`);
-            if (state !== SharingPermissions.None) {
-                const sharedDoc = Doc.MakeAlias(target);
-                if (data) {
-                    data.push(sharedDoc);
-                } else {
-                    notifDoc.data = new List([sharedDoc]);
-                }
+        } else {
+            let dataDocs = (await Promise.all(data.map(doc => doc))).map(doc => Doc.GetProto(doc));
+            if (dataDocs.includes(target)) {
+                console.log("Searching in ", dataDocs, "for", target);
+                dataDocs.splice(dataDocs.indexOf(target), 1);
+                console.log("SUCCESSFULLY UNSHARED DOC");
             } else {
-                let dataDocs = (await Promise.all(data.map(doc => doc))).map(doc => Doc.GetProto(doc));
-                if (dataDocs.includes(target)) {
-                    console.log("Searching in ", dataDocs, "for", target);
-                    dataDocs.splice(dataDocs.indexOf(target), 1);
-                    console.log("SUCCESSFULLY UNSHARED DOC");
-                } else {
-                    console.log("DIDN'T THINK WE HAD IT, SO NOT SUCCESSFULLY UNSHARED");
-                }
+                console.log("DIDN'T THINK WE HAD IT, SO NOT SUCCESSFULLY UNSHARED");
             }
         }
     }
@@ -214,6 +223,8 @@ export default class SharingManager extends React.Component<{}> {
     }
 
     private get sharingInterface() {
+        const existOtherUsers = this.users.length > 0;
+        console.log(existOtherUsers);
         return (
             <div className={"sharing-interface"}>
                 <p className={"share-link"}>Manage the public link to {this.focusOn("this document...")}</p>
@@ -247,9 +258,9 @@ export default class SharingManager extends React.Component<{}> {
                 </div>
                 <div className={"hr-substitute"} />
                 <p className={"share-individual"}>Privately share {this.focusOn("this document")} with an individual...</p>
-                <div className={"users-list"} style={{ display: this.users.length ? "block" : "flex" }}>
-                    {!this.users.length ? "There are no other users in your database." :
-                        this.users.map(user => {
+                <div className={"users-list"} style={{ display: existOtherUsers ? "block" : "flex", minHeight: existOtherUsers ? undefined : 200 }}>
+                    {!existOtherUsers ? "There are no other users in your database." :
+                        this.users.map(({ user, notificationDoc }) => {
                             return (
                                 <div
                                     key={user.email}
@@ -262,7 +273,7 @@ export default class SharingManager extends React.Component<{}> {
                                             color: this.sharingDoc ? ColorMapping.get(StrCast(this.sharingDoc[user.userDocumentId], SharingPermissions.None)) : DefaultColor,
                                             borderColor: this.sharingDoc ? ColorMapping.get(StrCast(this.sharingDoc[user.userDocumentId], SharingPermissions.None)) : DefaultColor
                                         }}
-                                        onChange={e => this.setInternalSharing(user, e.currentTarget.value)}
+                                        onChange={e => this.setInternalSharing({ user, notificationDoc }, e.currentTarget.value)}
                                     >
                                         {this.sharingOptions}
 
-- 
cgit v1.2.3-70-g09d2


From 5a8f2d9272753c51e40e47d44e2727b6ec8b72ee Mon Sep 17 00:00:00 2001
From: Sam Wilkins <samwilkins333@gmail.com>
Date: Tue, 8 Oct 2019 19:35:01 -0400
Subject: removed console log

---
 src/client/util/SharingManager.tsx | 1 -
 1 file changed, 1 deletion(-)

(limited to 'src/client/util/SharingManager.tsx')

diff --git a/src/client/util/SharingManager.tsx b/src/client/util/SharingManager.tsx
index 0b8b03b51..b5e29d050 100644
--- a/src/client/util/SharingManager.tsx
+++ b/src/client/util/SharingManager.tsx
@@ -224,7 +224,6 @@ export default class SharingManager extends React.Component<{}> {
 
     private get sharingInterface() {
         const existOtherUsers = this.users.length > 0;
-        console.log(existOtherUsers);
         return (
             <div className={"sharing-interface"}>
                 <p className={"share-link"}>Manage the public link to {this.focusOn("this document...")}</p>
-- 
cgit v1.2.3-70-g09d2

