Skip to content

Commit 384132d

Browse files
committed
fix(sync): only accept sync protocol and return sync step 2
Signed-off-by: Max <[email protected]>
1 parent 05b7f67 commit 384132d

File tree

2 files changed

+27
-32
lines changed

2 files changed

+27
-32
lines changed

cypress/e2e/api/SessionApi.spec.js

Lines changed: 21 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -73,34 +73,33 @@ describe('The session Api', function () {
7373
cy.closeConnection(connection)
7474
})
7575

76-
// Echoes all message types but queries
77-
Object.entries(messages)
78-
.filter(([key, _value]) => key !== 'query')
79-
.forEach(([type, sample]) => {
80-
it(`echos ${type} messages`, function () {
81-
const steps = [sample]
82-
const version = 0
83-
cy.pushSteps({ connection, steps, version })
84-
.its('version')
85-
.should('eql', 0)
86-
cy.syncSteps(connection)
87-
.its('steps[0].data')
88-
.should('eql', steps)
89-
})
76+
// Echoes updates and responses
77+
;['update', 'response'].forEach((type) => {
78+
it(`echos ${type} messages`, function () {
79+
const steps = [messages[type]]
80+
const version = 0
81+
cy.pushSteps({ connection, steps, version })
82+
.its('version')
83+
.should('eql', 0)
84+
cy.syncSteps(connection).its('steps[0].data').should('eql', steps)
9085
})
86+
})
9187

92-
it('responds to queries', function () {
88+
it('responds to queries with updates and responses', function () {
9389
const version = 0
9490
Object.entries(messages).forEach(([type, sample]) => {
9591
cy.pushSteps({ connection, steps: [sample], version })
9692
})
9793
cy.pushSteps({ connection, steps: [messages.query], version }).then(
9894
(response) => {
9995
cy.wrap(response).its('version').should('eql', 0)
100-
cy.wrap(response).its('steps.length').should('eql', 1)
96+
cy.wrap(response).its('steps.length').should('eql', 2)
10197
cy.wrap(response)
10298
.its('steps[0].data')
10399
.should('eql', [messages.update])
100+
cy.wrap(response)
101+
.its('steps[1].data')
102+
.should('eql', [messages.response])
104103
},
105104
)
106105
})
@@ -111,7 +110,6 @@ describe('The session Api', function () {
111110
let connection
112111
let fileId
113112
let filePath
114-
let joining
115113

116114
beforeEach(function () {
117115
cy.testName().then((name) => {
@@ -155,13 +153,10 @@ describe('The session Api', function () {
155153
manualSave: true,
156154
})
157155
cy.openConnection({ fileId, filePath })
158-
.then(({ connection: con, data }) => {
159-
joining = con
160-
return data
161-
})
162-
.its('documentState')
156+
.as('joining')
157+
.its('data.documentState')
163158
.should('eql', documentState)
164-
cy.closeConnection(joining)
159+
cy.get('@joining').its('connection').then(cy.closeConnection)
165160
})
166161

167162
afterEach(function () {
@@ -174,7 +169,6 @@ describe('The session Api', function () {
174169
let connection
175170
let filePath
176171
let shareToken
177-
let joining
178172

179173
beforeEach(function () {
180174
cy.testName().then((name) => {
@@ -230,13 +224,10 @@ describe('The session Api', function () {
230224
manualSave: true,
231225
})
232226
cy.openConnection({ filePath: '', token: shareToken })
233-
.then(({ connection: con, data }) => {
234-
joining = con
235-
return data
236-
})
237-
.its('documentState')
227+
.as('joining')
228+
.its('data.documentState')
238229
.should('eql', documentState)
239-
cy.closeConnection(joining)
230+
cy.get('@joining').its('connection').then(cy.closeConnection)
240231
})
241232
})
242233

lib/Service/DocumentService.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -216,8 +216,12 @@ public function addStep(Document $document, Session $session, array $steps, int
216216
if ($readOnly && $message->isUpdate()) {
217217
continue;
218218
}
219+
// Only accept sync protocol
220+
if ($message->getYjsMessageType() !== YjsMessage::YJS_MESSAGE_SYNC) {
221+
continue;
222+
}
219223
// Filter out query steps as they would just trigger clients to send their steps again
220-
if ($message->getYjsMessageType() === YjsMessage::YJS_MESSAGE_SYNC && $message->getYjsSyncType() === YjsMessage::YJS_MESSAGE_SYNC_STEP1) {
224+
if ($message->getYjsSyncType() === YjsMessage::YJS_MESSAGE_SYNC_STEP1) {
221225
$stepsIncludeQuery = true;
222226
} else {
223227
$stepsToInsert[] = $step;
@@ -257,7 +261,7 @@ public function addStep(Document $document, Session $session, array $steps, int
257261
$stepsToReturn = [];
258262
foreach ($allSteps as $step) {
259263
$message = YjsMessage::fromBase64($step->getData());
260-
if ($message->getYjsMessageType() === YjsMessage::YJS_MESSAGE_SYNC && $message->getYjsSyncType() === YjsMessage::YJS_MESSAGE_SYNC_UPDATE) {
264+
if ($message->isUpdate()) {
261265
$stepsToReturn[] = $step;
262266
}
263267
}

0 commit comments

Comments
 (0)