Skip to content

Commit

Permalink
testing
Browse files Browse the repository at this point in the history
  • Loading branch information
cskrov committed Aug 19, 2024
1 parent 1479688 commit 518b1fb
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 8 deletions.
2 changes: 1 addition & 1 deletion server/src/plugins/crdt/api/fetch.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { isConnectionContext } from '@app/plugins/crdt/context';
import { getLogger } from '@app/logger';
import { getDocument } from '@app/plugins/crdt/get-document';
import { getDocument } from '@app/plugins/crdt/api/get-document';

const log = getLogger('collaboration');

Expand Down
File renamed without changes.
50 changes: 43 additions & 7 deletions server/src/plugins/crdt/collaboration-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ import { isConnectionContext } from '@app/plugins/crdt/context';
import { isDeployed } from '@app/config/env';
import { getRedisExtension } from '@app/plugins/crdt/redis';
import { isNotNull } from '@app/functions/guards';
import { getApiExtension } from '@app/plugins/crdt/api/extension';
import { getDocument } from '@app/plugins/crdt/get-document';
import { applyUpdate, encodeStateAsUpdate } from 'yjs';
import { getDocument } from '@app/plugins/crdt/api/get-document';
import { XmlText, applyUpdateV2, encodeSnapshotV2, encodeStateAsUpdateV2, snapshot } from 'yjs';
import { KABAL_API_URL } from '@app/plugins/crdt/api/url';
import { getHeaders } from '@app/plugins/crdt/api/headers';
import { yTextToSlateElement } from '@slate-yjs/core';

const log = getLogger('collaboration');

Expand Down Expand Up @@ -55,14 +57,48 @@ export const collaborationServer = Server.configure({

log.info({ msg: 'Loaded document', data: { behandlingId, dokumentId } });

const state = Buffer.from(res.data, 'base64');
const state = new Uint8Array(Buffer.from(res.data, 'base64'));

applyUpdate(document, encodeStateAsUpdate(document, state));
applyUpdateV2(document, encodeStateAsUpdateV2(document, state));

log.info({ msg: 'Loaded document applied', data: { behandlingId, dokumentId } });
},

onStoreDocument: async ({ context, document }) => {
if (!isConnectionContext(context)) {
log.error({ msg: 'Tried to store document without context' });
throw new Error('Invalid context');
}

const { behandlingId, dokumentId, req } = context;

const state = encodeSnapshotV2(snapshot(document));

// const state = Buffer.from(encodeStateAsUpdateV2(document));
const data = Buffer.from(state).toString('base64');

const sharedRoot = document.get('content', XmlText);
const nodes = yTextToSlateElement(sharedRoot);

const res = await fetch(`${KABAL_API_URL}/behandlinger/${behandlingId}/smartdokumenter/${dokumentId}`, {
method: 'PATCH',
headers: { ...getHeaders(req), 'Content-Type': 'application/json' },
body: JSON.stringify({ content: nodes.children, data }),
});

if (!res.ok) {
const msg = `Failed to save document. API responded with status code ${res.status}.`;
const text = await res.text();
log.error({ msg, data: { behandlingId, dokumentId, statusCode: res.status, response: text } });

throw new Error(`${msg} - ${text}`);
}

return document;
log.info({
msg: 'Saved document to database',
data: { behandlingId, dokumentId, content: JSON.stringify(nodes, null, 2) },
});
},

extensions: isDeployed ? [getRedisExtension(), getApiExtension()].filter(isNotNull) : [getApiExtension()],
extensions: isDeployed ? [getRedisExtension()].filter(isNotNull) : [],
});

0 comments on commit 518b1fb

Please sign in to comment.