|
|
@@ -1,4 +1,5 @@
|
|
|
import { GlobalSocketEventName } from '@growi/core/dist/interfaces';
|
|
|
+import mongoose from 'mongoose';
|
|
|
import { Server } from 'socket.io';
|
|
|
|
|
|
import { SocketEventName } from '~/interfaces/websocket';
|
|
|
@@ -169,14 +170,31 @@ class SocketIoService {
|
|
|
|
|
|
setupYjsConnection() {
|
|
|
const yjsConnectionManager = getYjsConnectionManager();
|
|
|
+ const Page = mongoose.model('Page');
|
|
|
this.io.on('connection', (socket) => {
|
|
|
|
|
|
yjsConnectionManager.ysocketioInstance.on('awareness-update', async(update) => {
|
|
|
const pageId = extractPageIdFromYdocId(update.name);
|
|
|
const awarenessStateSize = update.awareness.states.size;
|
|
|
+
|
|
|
this.io
|
|
|
.in(getRoomNameWithId(RoomPrefix.PAGE, pageId))
|
|
|
.emit(SocketEventName.YjsAwarenessStateUpdated, awarenessStateSize);
|
|
|
+
|
|
|
+ // Executed when the last user leaves the Editor
|
|
|
+ if (awarenessStateSize === 0) {
|
|
|
+ const page = await Page.findOne({ _id: pageId });
|
|
|
+ if (page != null) {
|
|
|
+ const populatedPage = await page.populateDataToShowRevision();
|
|
|
+ const revisionBody = populatedPage.revision.body;
|
|
|
+ const currentYdoc = yjsConnectionManager.getCurrentYdoc(pageId);
|
|
|
+ const yjsDraft = currentYdoc?.getText('codemirror').toString();
|
|
|
+ const hasRevisionBodyDiff = revisionBody != null && yjsDraft != null && yjsDraft !== revisionBody;
|
|
|
+ this.io
|
|
|
+ .in(getRoomNameWithId(RoomPrefix.PAGE, pageId))
|
|
|
+ .emit(SocketEventName.YjsHasRevisionBodyDiffUpdated, hasRevisionBodyDiff);
|
|
|
+ }
|
|
|
+ }
|
|
|
});
|
|
|
|
|
|
socket.on(GlobalSocketEventName.YDocSync, async({ pageId, initialValue }) => {
|