DocSpace-client/packages/shared/utils/desktop.ts

149 lines
3.5 KiB
TypeScript
Raw Normal View History

2020-11-13 16:05:49 +00:00
import isEmpty from "lodash/isEmpty";
2020-11-23 14:28:24 +00:00
import omit from "lodash/omit";
import { toastr } from "../components/toast";
import { TTranslation } from "../types";
import { TUser } from "../api/people/types";
import { ThemeKeys } from "../enums";
import { desktopConstants, getEditorTheme } from "./common";
import { checkIsSSR } from "./device";
const isSSR = checkIsSSR();
2020-12-04 09:59:48 +00:00
2020-12-28 07:40:50 +00:00
export function regDesktop(
user: TUser,
isEncryption: boolean,
keys: string[],
setEncryptionKeys: (value: string[]) => void,
isEditor: boolean,
getEncryptionAccess: (callback?: () => void) => void,
t: TTranslation,
2020-12-28 07:40:50 +00:00
) {
if (!isSSR) {
const data = {
displayName: user.displayName,
email: user.email,
domain: desktopConstants.domain,
provider: desktopConstants.provider,
userId: user.id,
uiTheme: getEditorTheme(user.theme || ThemeKeys.BaseStr),
};
let extendedData;
2020-11-13 16:05:49 +00:00
if (isEncryption) {
2020-11-16 14:42:29 +00:00
extendedData = {
...data,
encryptionKeys: {
cryptoEngineId: desktopConstants.cryptoEngineId,
},
2020-11-16 14:42:29 +00:00
};
if (!isEmpty(keys)) {
const filteredKeys = omit(keys, ["userId"]);
extendedData = {
...extendedData,
encryptionKeys: { ...extendedData.encryptionKeys, ...filteredKeys },
};
}
} else {
extendedData = { ...data };
2020-11-13 18:45:28 +00:00
}
2020-11-13 16:05:49 +00:00
window.AscDesktopEditor?.execCommand(
"portal:login",
JSON.stringify(extendedData),
);
2020-11-13 16:05:49 +00:00
if (isEncryption) {
window.cloudCryptoCommand = (type, params, callback) => {
switch (type) {
case "encryptionKeys": {
setEncryptionKeys(params);
break;
}
case "updateEncryptionKeys": {
setEncryptionKeys(params);
break;
}
case "relogin": {
// toastr.info(t("Common:EncryptionKeysReload"));
// relogin();
break;
}
case "getsharingkeys":
if (!isEditor || typeof getEncryptionAccess !== "function") {
callback({});
return;
}
getEncryptionAccess(callback);
break;
default:
break;
2020-11-19 15:30:29 +00:00
}
};
}
window.onSystemMessage = (e) => {
let message = e.opMessage;
switch (e.type) {
case "operation":
if (!message) {
switch (e.opType) {
case 0:
message = t("Common:EncryptionFilePreparing");
break;
case 1:
message = t("Common:EncryptingFile");
break;
default:
message = t("Common:LoadingProcessing");
}
}
toastr.info(message);
2020-11-19 15:30:29 +00:00
break;
default:
break;
}
};
}
2020-11-12 08:50:21 +00:00
}
2020-11-03 06:52:05 +00:00
2020-11-12 08:50:21 +00:00
export function relogin() {
if (!isSSR)
setTimeout(() => {
const data = {
domain: desktopConstants.domain,
onsuccess: "reload",
};
window.AscDesktopEditor.execCommand(
"portal:logout",
JSON.stringify(data),
);
}, 1000);
2020-11-12 08:50:21 +00:00
}
2020-11-05 11:13:06 +00:00
2020-11-12 08:50:21 +00:00
export function checkPwd() {
if (!isSSR) {
const data = {
domain: desktopConstants.domain,
emailInput: "login",
pwdInput: "password",
};
window.AscDesktopEditor.execCommand(
"portal:checkpwd",
JSON.stringify(data),
);
}
2020-11-12 08:50:21 +00:00
}
2020-11-03 06:52:05 +00:00
2020-11-12 08:50:21 +00:00
export function logout() {
if (!isSSR) {
const data = {
domain: desktopConstants.domain,
};
window.AscDesktopEditor.execCommand("portal:logout", JSON.stringify(data));
}
2020-11-03 06:52:05 +00:00
}