backend cli: add editing_session list viewer command (and minor instanceId fixes) (#525)

This commit is contained in:
Eric Doughty-Papassideris
2024-09-15 00:03:49 +02:00
parent 53b34a0962
commit 1e16e179b0
3 changed files with 65 additions and 3 deletions
@@ -0,0 +1,54 @@
import yargs from "yargs";
import runWithPlatform from "../../lib/run-with-platform";
import { TdrivePlatform } from "../../../core/platform/platform";
import { DatabaseServiceAPI } from "../../../core/platform/services/database/api";
import {
DriveFile,
EditingSessionKeyFormat,
TYPE,
} from "../../../services/documents/entities/drive-file";
async function report(platform: TdrivePlatform) {
const drivesRepo = await platform
.getProvider<DatabaseServiceAPI>("database")
.getRepository<DriveFile>(TYPE, DriveFile);
const editedFiled = (await drivesRepo.find({ editing_session_key: { $ne: null } })).getEntities();
console.error("DriveFiles with non null editing_session_key (url encoded):");
console.error("");
editedFiled.forEach(dfile => {
console.error(`- ${dfile.name} (${dfile.id}) has key:`);
const parsed = EditingSessionKeyFormat.parse(dfile.editing_session_key);
console.error(` - URL encoded: ${encodeURIComponent(dfile.editing_session_key)}`);
console.error(` - applicationId: ${parsed.applicationId}`);
console.error(` - companyId: ${parsed.companyId}`);
console.error(` - instanceId: ${parsed.instanceId}`);
console.error(
` - userId: ${parsed.userId} (${
parsed.userId === dfile.creator ? "same as creator ID" : "not the creator"
})`,
);
console.error(
` - timestamp: ${parsed.timestamp.toISOString()} (${Math.floor(
(new Date().getTime() - parsed.timestamp.getTime()) / 1000,
)}s ago)`,
);
});
if (!editedFiled.length) console.error(" (no DriveFile currently has an editing_session_key)");
}
const command: yargs.CommandModule<unknown, unknown> = {
command: "list",
describe: `
List current DriveFile items that have an editing_session_key set
`.trim(),
builder: {},
handler: async _argv => {
await runWithPlatform("editing_session list", async ({ spinner: _spinner, platform }) => {
console.error("\n");
await report(platform);
console.error("\n");
});
},
};
export default command;
@@ -135,7 +135,8 @@ const OnlyOfficeSafeDocKeyBase64 = {
},
};
function checkFieldValue(field: string, value: string) {
function checkFieldValue(field: string, value: string, required: boolean = true) {
if (!required && !value) return;
if (!/^[0-9a-zA-Z_-]+$/m.test(value))
throw new Error(
`Invalid ${field} value (${JSON.stringify(
@@ -143,7 +144,12 @@ function checkFieldValue(field: string, value: string) {
)}). Must be short and only alpha numeric or '_' and '-'`,
);
}
/** Reference implementation for generating then parsing the {@link DriveFile.editing_session_key} field */
/**
* Reference implementation for generating then parsing the {@link DriveFile.editing_session_key} field.
*
* Fields should be explicit, `instanceId` is for the case when we have multiple
* clients
*/
export const EditingSessionKeyFormat = {
// OnlyOffice key limits: 128 chars, [0-9a-zA-Z.=_-]
// See https://api.onlyoffice.com/editors/config/document#key
@@ -161,7 +167,7 @@ export const EditingSessionKeyFormat = {
overrideTimeStamp?: Date,
) {
checkFieldValue("applicationId", applicationId);
checkFieldValue("instanceId", instanceId);
checkFieldValue("instanceId", instanceId, false);
const isoUTCDateNoSpecialCharsNoMS = (overrideTimeStamp ?? new Date())
.toISOString()
.replace(/\..+$/, "")
@@ -959,6 +959,8 @@ export class DocumentsService {
* with only that key provided.
* @param id DriveFile ID of the document to begin editing
* @param editorApplicationId Editor/Application/Plugin specific identifier
* @param appInstanceId For that `editorApplicationId` a unique identifier
* when multiple instances are running. Unused today.
* @param context
* @returns An object in the format `{}` with the unique identifier for the
* editing session