diff --git a/server/src/plugins/crdt/api/fetch.ts b/server/src/plugins/crdt/api/fetch.ts index ed3e3c3d5..1f680f198 100644 --- a/server/src/plugins/crdt/api/fetch.ts +++ b/server/src/plugins/crdt/api/fetch.ts @@ -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'); diff --git a/server/src/plugins/crdt/get-document.ts b/server/src/plugins/crdt/api/get-document.ts similarity index 100% rename from server/src/plugins/crdt/get-document.ts rename to server/src/plugins/crdt/api/get-document.ts diff --git a/server/src/plugins/crdt/collaboration-server.ts b/server/src/plugins/crdt/collaboration-server.ts index 58dad1a7d..e36614493 100644 --- a/server/src/plugins/crdt/collaboration-server.ts +++ b/server/src/plugins/crdt/collaboration-server.ts @@ -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'); @@ -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) : [], });