2020-11-10 00:20:38 +03:00
|
|
|
import * as Environment from "~/node_common/environment";
|
|
|
|
import * as Utilities from "~/node_common/utilities";
|
|
|
|
import * as Data from "~/node_common/data";
|
|
|
|
import * as Constants from "~/node_common/constants";
|
|
|
|
import * as Serializers from "~/node_common/serializers";
|
|
|
|
import * as Strings from "~/common/strings";
|
|
|
|
import * as Websocket from "~/node_common/nodejs-websocket";
|
2021-06-11 22:25:58 +03:00
|
|
|
import * as Logging from "~/common/logging";
|
2021-03-07 23:53:54 +03:00
|
|
|
import * as Window from "~/common/window";
|
2020-11-10 00:20:38 +03:00
|
|
|
|
2021-04-21 03:14:43 +03:00
|
|
|
import WebSocket from "ws";
|
|
|
|
|
2020-11-10 00:20:38 +03:00
|
|
|
const websocketSend = async (type, data) => {
|
|
|
|
if (Strings.isEmpty(Environment.PUBSUB_SECRET)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-04-24 05:24:03 +03:00
|
|
|
let ws = Websocket.get();
|
2020-11-10 00:20:38 +03:00
|
|
|
if (!ws) {
|
2021-02-03 07:40:24 +03:00
|
|
|
ws = Websocket.create();
|
|
|
|
await Window.delay(2000);
|
2020-11-10 00:20:38 +03:00
|
|
|
}
|
2020-11-25 06:39:35 +03:00
|
|
|
|
2020-11-10 00:20:38 +03:00
|
|
|
const encryptedData = await Utilities.encryptWithSecret(
|
|
|
|
JSON.stringify(data),
|
|
|
|
Environment.PUBSUB_SECRET
|
|
|
|
);
|
|
|
|
|
|
|
|
// NOTE(jim): Only allow this to be passed around encrypted.
|
2021-04-30 22:14:14 +03:00
|
|
|
if (ws && ws.readyState === WebSocket.OPEN) {
|
2021-04-21 03:14:43 +03:00
|
|
|
ws.send(
|
|
|
|
JSON.stringify({
|
|
|
|
type,
|
|
|
|
iv: encryptedData.iv,
|
|
|
|
data: encryptedData.hex,
|
|
|
|
})
|
|
|
|
);
|
|
|
|
}
|
2020-11-10 00:20:38 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
export const updateUser = async (user, action) => {
|
|
|
|
if (!user || !action) return;
|
2020-11-25 06:39:35 +03:00
|
|
|
|
2021-06-11 22:25:58 +03:00
|
|
|
Logging.log(`Search is updating user ...`);
|
2020-11-25 06:39:35 +03:00
|
|
|
|
2021-03-07 23:53:54 +03:00
|
|
|
let data;
|
|
|
|
if (Array.isArray(user)) {
|
|
|
|
data = user.map((item) => {
|
|
|
|
return { ...Serializers.sanitizeUser(item), type: "USER" };
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
data = { ...Serializers.sanitizeUser(user), type: "USER" };
|
|
|
|
}
|
|
|
|
|
|
|
|
websocketSend("UPDATE", {
|
2020-11-10 00:20:38 +03:00
|
|
|
id: "LENS",
|
2021-03-07 23:53:54 +03:00
|
|
|
data: { action, data },
|
|
|
|
});
|
2020-11-10 00:20:38 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
export const updateSlate = async (slate, action) => {
|
|
|
|
if (!slate || !action) return;
|
2020-11-25 06:39:35 +03:00
|
|
|
|
2021-06-11 22:25:58 +03:00
|
|
|
Logging.log(`Search is updating slate ...`);
|
2020-11-25 06:39:35 +03:00
|
|
|
|
2021-03-07 23:53:54 +03:00
|
|
|
let data;
|
|
|
|
if (Array.isArray(slate)) {
|
|
|
|
data = slate.map((item) => {
|
|
|
|
return { ...Serializers.sanitizeSlate(item), type: "SLATE" };
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
data = { ...Serializers.sanitizeSlate(slate), type: "SLATE" };
|
|
|
|
}
|
|
|
|
|
|
|
|
websocketSend("UPDATE", {
|
|
|
|
id: "LENS",
|
|
|
|
data: { action, data },
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
export const updateFile = async (file, action) => {
|
|
|
|
if (!file || !action) return;
|
|
|
|
|
2021-06-11 22:25:58 +03:00
|
|
|
Logging.log(`Search is updating file ...`);
|
2021-03-07 23:53:54 +03:00
|
|
|
|
|
|
|
let data;
|
|
|
|
if (Array.isArray(file)) {
|
|
|
|
data = file.map((item) => {
|
|
|
|
return { ...Serializers.sanitizeFile(item), type: "FILE" };
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
data = { ...Serializers.sanitizeFile(file), type: "FILE" };
|
|
|
|
}
|
|
|
|
|
|
|
|
websocketSend("UPDATE", {
|
2020-11-10 00:20:38 +03:00
|
|
|
id: "LENS",
|
2021-03-07 23:53:54 +03:00
|
|
|
data: { action, data },
|
|
|
|
});
|
2020-11-10 00:20:38 +03:00
|
|
|
};
|