Skip to content

Commit

Permalink
πŸ”€ Merge branch '525-515-548-523-onlyoffice-rework' into release/v1.0.…
Browse files Browse the repository at this point in the history
…4-rc2
  • Loading branch information
ericlinagora committed Sep 26, 2024
2 parents 966173c + 9018222 commit f453ebc
Show file tree
Hide file tree
Showing 16 changed files with 286 additions and 59 deletions.
85 changes: 84 additions & 1 deletion Documentation/docs/plugins.md
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,15 @@ ended with and without a new version.

See operations `beginEditing` and `updateEditing` in `tdrive/backend/node/src/services/documents/services/index.ts` for operations available on that key.

Basic flow is:
- Call `beginEditing`
- If a key previously was set for that file, uses the `/check` endpoint of the plugin to update the key status first
- Twake Drive generates an editing session key and returns it (or returns the existing one).
- Optionally call `updateEditing?keepEditing=true` with a file stream to generate intermediary versions
- End the session by calling `updateEditing` (without `keepEditing=true`). If a file is provided, a new (and final) FileVersion
is created with that content. Otherwise, the editing session is cleared without creating a new version.


#### API to expose by the application

Authentication from the Twake Drive backend is a JWT with the property `type` being
Expand All @@ -161,7 +170,81 @@ multiple parallel requests for the same key gracefully.
- `{ status: 'unknown' }`: the key isn't known and maybe used for a new session
- `{ status: 'updated' }`: the key needed updating but is now invalid
- `{ status: 'expired' }`: the key was already used in a finished session and can't be used again
- `{ status: 'live' }`: the key is valid and current and should be used again for the same file
- `{ status: 'live' }`: the key is valid and current and should be used again for the same file (if multiple writers are allowed)

#### Example flow of editing session

```mermaid
sequenceDiagram
autonumber
actor User as User/Browser
participant Front as Frontend
box Server side
participant Back as Backend
participant Conn as onlyoffice<br>connector
participant OO as Only Office<br>Edition Server
end
User->>Front: Open in<br>editor
Front->>Back: Get plugin config
Front->>User: Open plugin config<br>editor.edition_url
User->>Conn: Open editor_url<br>(proxied by backend)
Conn->>Back: beginEditing
alt existing key
Back->>Conn: checkSessionStatus
Conn->>OO: getForgotten
note over Conn, OO: recover forgotten<br>process, and new key
Conn->>OO: info command
note right of Conn: decide status of key<br>live or stale
note over Conn, OO: detect ended but<br>not changed keys
note over Conn, OO: normal callback processing with<br>update if required
OO->>Conn: callback with key status
Conn->>Back: key status<br>(live/expired/updated/etc)
end
activate Back
Back->>Conn: editing_session_key
Conn->>User: HTML host for Editor with<br>special callback URL
User->>OO: Load JS Editor directly from OO server
activate User
loop User editing
User->>User: Furious Document<br>Editing
User-->>OO: Periodic saving
end
deactivate User
note left of User: Closes editor
note over User,OO: 10 seconds after last user closes their editor
OO->>Conn: callback to save the new version<br>or close without changes
Conn->>Back: updateEditing?keepEditing=false<br>with URL to new version from OO
deactivate Back
```

#### Batch processing of unknown keys

Periodically, the plugin, and twake drive, should run batch cleanup operations on editing session keys
to ensure they are live, or removed, as they may block other operations until then.

Here is an example initiated by the plugin:

```mermaid
sequenceDiagram
autonumber
actor User as User/Browser
participant Front as Frontend
box Server side
participant Back as Backend
participant Conn as onlyoffice<br>connector
participant OO as Only Office<br>Edition Server
end
alt Periodic scheduled task
Conn->>OO: getForgottenList
loop Each forgotten file
Conn->>Back: Save new version<br>end editing session
Conn->>OO: Delete forgotten file
end
end
```

### Example: OnlyOffice plugin

Expand Down
34 changes: 34 additions & 0 deletions tdrive/backend/node/src/cli/cmds/editing_session_cmds/parse.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import yargs from "yargs";

import { NonPlatformCommandYargsBuilder } from "../../utils/non-plaform-command-yargs-builder";
import { EditingSessionKeyFormat } from "../../../services/documents/entities/drive-file";

interface ParseArguments {
editing_session_key: string;
}

const command: yargs.CommandModule<unknown, unknown> = {
command: "parse <editing_session_key>",
describe: `
Parse the provided editing_session_key and output json data (to stderr)
`.trim(),

builder: {
...NonPlatformCommandYargsBuilder,
},
handler: async argv => {
const args = argv as unknown as ParseArguments;
const parsed = EditingSessionKeyFormat.parse(decodeURIComponent("" + args.editing_session_key));
console.error(
JSON.stringify(
{
ageH: (new Date().getTime() - parsed.timestamp.getTime()) / (60 * 60 * 1000),
...parsed,
},
null,
2,
),
);
},
};
export default command;
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ interface RequestEditorQuery {
office_token: string;
company_id: string;
file_id: string;
drive_file_id: string;
}

/**
Expand Down Expand Up @@ -102,6 +103,7 @@ class BrowserEditorController {
makeURLTo.editorAbsolute({
token,
file_id,
drive_file_id,
editing_session_key: editingSessionKey,
company_id,
preview,
Expand Down Expand Up @@ -131,7 +133,7 @@ class BrowserEditorController {
throw new Error('Cant start editing without "editing session key"');
}

const initResponse = await editorService.init(company_id, file_name, file_id, user, preview, drive_file_id || file_id);
const initResponse = await editorService.init(company_id, file_name, file_id, user, preview, drive_file_id);

const inPageToken = jwt.sign(
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,14 @@ import * as Utils from '@/utils';
interface RequestQuery {
company_id: string;
file_id: string;
drive_file_id: string;
token: string;
}

interface RenameRequestBody {
name: string;
}

/** These expose a OnlyOffice document storage service methods, called by the OnlyOffice document editing service
* to load and save files
*/
Expand All @@ -29,15 +34,27 @@ class OnlyOfficeController {
const { token } = req.query;

const officeTokenPayload = jwt.verify(token, CREDENTIALS_SECRET) as OfficeToken;
const { company_id, file_id, in_page_token } = officeTokenPayload;
const { company_id, file_id, drive_file_id, in_page_token } = officeTokenPayload;

// check token is an in_page_token
if (!in_page_token) throw new Error('Invalid token, must be a in_page_token');

let fileId = file_id;
if (drive_file_id) {
//Get the drive file
const driveFile = await driveService.get({
company_id,
drive_file_id,
});
if (driveFile) {
fileId = driveFile?.item?.last_version_cache?.file_metadata?.external_id;
}
}

if (!file_id) throw new Error(`File id is missing in the last version cache for ${JSON.stringify(file_id)}`);
const file = await fileService.download({
company_id,
file_id: file_id,
file_id: fileId,
});

file.pipe(res);
Expand Down Expand Up @@ -132,6 +149,25 @@ class OnlyOfficeController {
next(error || 'error');
}
};

/** This route is called directly by the inline JS in the editor page, called by the client-side OO editor component */
public rename = async (req: Request<{}, {}, RenameRequestBody, RequestQuery>, res: Response, next: NextFunction): Promise<void> => {
try {
const { token } = req.query;
const officeTokenPayload = jwt.verify(token, CREDENTIALS_SECRET) as OfficeToken;
const { company_id, drive_file_id } = officeTokenPayload;
const { name } = req.body;

if (!drive_file_id) throw new Error('OO Rename request missing drive_file_id');
if (!name) throw new Error('OO Rename request missing name');

const result = await driveService.update({ company_id, drive_file_id, changes: { name } });
res.send(result);
} catch (error) {
logger.error(`OO Rename request root error`, { error });
next(error || 'error');
}
};
}

export default OnlyOfficeController;
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export type EditConfigInitResult = {
onlyoffice_server: string;
color: string;
company_id: string;
file_id: string;
drive_file_id: string;
file_version_id: string;
filename: string;
file_type: string;
Expand All @@ -33,6 +33,7 @@ export interface IEditorService {
user: UserType,
preview: boolean,
file_id: string,
drive_file_id: string,
) => Promise<EditConfigInitResult>;
}

Expand Down
10 changes: 9 additions & 1 deletion tdrive/connectors/onlyoffice-connector/src/routes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,15 @@ export function mountRoutes(app: Application) {
export const makeURLTo = {
rootAbsolute: () => Utils.joinURL([SERVER_ORIGIN, SERVER_PREFIX]),
assets: () => Utils.joinURL([SERVER_PREFIX, 'assets']),
editorAbsolute(params: { token: string; file_id: string; editing_session_key: string; company_id: string; preview: string; office_token: string }) {
editorAbsolute(params: {
token: string;
drive_file_id: string;
file_id: string;
editing_session_key: string;
company_id: string;
preview: string;
office_token: string;
}) {
return Utils.joinURL([SERVER_ORIGIN ?? '', SERVER_PREFIX, 'editor'], params);
},
};
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@ export const OnlyOfficeRoutes = {
const controller = new OnlyOfficeController();
router.get(`/:mode/read`, requirementsMiddleware, controller.read.bind(controller));
router.post(`/:mode/callback`, requirementsMiddleware, controller.ooCallback.bind(controller));
router.post(`/:mode/rename`, requirementsMiddleware, controller.rename.bind(controller));
},
};
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,28 @@ class DriveService implements IDriveService {
}
};

public update = async (params: {
company_id: string;
drive_file_id: string;
changes: Partial<DriveFileType['item']>;
completeResult?: boolean;
}): Promise<(typeof params)['changes']> => {
try {
const { company_id, drive_file_id } = params;
const resource = await apiService.post<(typeof params)['changes'], ReturnType<DriveService['update']>>({
url: makeNonEditingSessionItemUrl(company_id, drive_file_id),
payload: params.changes,
});
if (params.completeResult) return resource;
const result = {};
Object.keys(params.changes).forEach(k => (result[k] = resource[k]));
return result;
} catch (error) {
logger.error('Failed to update file metadata: ', error.stack);
return Promise.reject();
}
};

public createVersion = async (params: {
company_id: string;
drive_file_id: string;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ class EditorService implements IEditorService {
file_version_id: string,
user: UserType,
preview: boolean,
file_id: string,
drive_file_id: string,
): Promise<EditConfigInitResult> => {
const { color, mode: fileMode } = this.getFileMode(file_name);
let [, extension] = Utils.splitFilename(file_name);

extension = extension.toLocaleLowerCase();
return {
color,
file_id,
drive_file_id,
file_version_id,
file_type: extension,
filename: file_name,
Expand Down
33 changes: 29 additions & 4 deletions tdrive/connectors/onlyoffice-connector/src/views/index.eta
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.0/jquery.min.js"></script>
<script type="text/javascript" src="<%= it.onlyoffice_server %>web-apps/apps/api/documents/api.js"></script>
<meta name="viewport" content="width=device-width, initial-scale=1.0,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no,shrink-to-fit=no" />
<title>Twake Drive</title>
</head>

<body style="height: 100%">
Expand All @@ -22,29 +23,53 @@

$('#onlyoffice_container').html("<div id='onlyoffice_container_instance'></div>");

const callbackQueryString = '?drive_file_id=<%= it.drive_file_id %>&company_id=<%= it.company_id %>&token=<%= it.token %>';
let doc = {
title: "<%= it.filename %>",
url: `${window.baseURL}read?file_id=<%= it.file_id %>&company_id=<%= it.company_id %>&token=<%= it.token %>`,
url: `${window.baseURL}read${callbackQueryString}`,
fileType: "<%= it.file_type %>",
key: "<%= it.docId %>",
token: "<%= it.file_id %>",
token: "<%= it.drive_file_id %>",
permissions: {
download: true,
edit: <%= it.editable %>,
preview: <%= it.preview %>,
}
}
function updateTitle(title) {
const el = document.querySelector('head title');
if (el) el.innerText = title + ' β€” Twake Drive';
}
updateTitle(doc.title);

window.docEditor = new DocsAPI.DocEditor('onlyoffice_container_instance', {
scrollSensitivity: window.mode === 'text' ? 100 : 40,
width: '100%',
height: '100%',
documentType: window.mode,
document: doc,
token: "<%= it.file_id %>",
token: "<%= it.drive_file_id %>",
type: screen.width < 600 ? 'mobile' : 'desktop',
events: {
onRequestRename: function (event) {
let name = event.data;
const prevExtension = /\.[^.]+$/.exec(doc.title);
if (prevExtension) name += prevExtension[0];
$.
post(`${window.baseURL}rename${callbackQueryString}`, { name }).
done((changed) => {
if (changed.name != name)
window.docEditor.showMessage(`βœ… Renamed to: ${changed.name}`);
}).
fail(() => window.docEditor.showMessage('🚨 Error renaming the file'));
},
onMetaChange: function (event) {
const { title } = event.data;
if (title) updateTitle(title);
},
},
editorConfig: {
callbackUrl: `${window.baseURL}callback?file_id=<%= it.file_id %>&company_id=<%= it.company_id %>&token=<%= it.token %>`,
callbackUrl: `${window.baseURL}callback${callbackQueryString}`,
lang: window.user.language,
user: {
id: window.user.id,
Expand Down
Loading

0 comments on commit f453ebc

Please sign in to comment.