forked from WordPress/wordpress-develop
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclass-wp-http-polling-sync-server.php
More file actions
505 lines (449 loc) · 14 KB
/
class-wp-http-polling-sync-server.php
File metadata and controls
505 lines (449 loc) · 14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
<?php
/**
* WP_HTTP_Polling_Sync_Server class
*
* @package WordPress
*/
/**
* Core class that contains an HTTP server used for collaborative editing.
*
* @since 7.0.0
* @access private
*/
class WP_HTTP_Polling_Sync_Server {
/**
* REST API namespace.
*
* @since 7.0.0
* @var string
*/
const REST_NAMESPACE = 'wp-sync/v1';
/**
* Awareness timeout in seconds. Clients that haven't updated
* their awareness state within this time are considered disconnected.
*
* @since 7.0.0
* @var int
*/
const AWARENESS_TIMEOUT = 30;
/**
* Threshold used to signal clients to send a compaction update.
*
* @since 7.0.0
* @var int
*/
const COMPACTION_THRESHOLD = 50;
/**
* Sync update type: compaction.
*
* @since 7.0.0
* @var string
*/
const UPDATE_TYPE_COMPACTION = 'compaction';
/**
* Sync update type: sync step 1.
*
* @since 7.0.0
* @var string
*/
const UPDATE_TYPE_SYNC_STEP1 = 'sync_step1';
/**
* Sync update type: sync step 2.
*
* @since 7.0.0
* @var string
*/
const UPDATE_TYPE_SYNC_STEP2 = 'sync_step2';
/**
* Sync update type: regular update.
*
* @since 7.0.0
* @var string
*/
const UPDATE_TYPE_UPDATE = 'update';
/**
* Storage backend for sync updates.
*
* @since 7.0.0
*/
private WP_Sync_Storage $storage;
/**
* Constructor.
*
* @since 7.0.0
*
* @param WP_Sync_Storage $storage Storage backend for sync updates.
*/
public function __construct( WP_Sync_Storage $storage ) {
$this->storage = $storage;
}
/**
* Registers REST API routes.
*
* @since 7.0.0
*/
public function register_routes(): void {
$typed_update_args = array(
'properties' => array(
'data' => array(
'type' => 'string',
'required' => true,
),
'type' => array(
'type' => 'string',
'required' => true,
'enum' => array(
self::UPDATE_TYPE_COMPACTION,
self::UPDATE_TYPE_SYNC_STEP1,
self::UPDATE_TYPE_SYNC_STEP2,
self::UPDATE_TYPE_UPDATE,
),
),
),
'required' => true,
'type' => 'object',
);
$room_args = array(
'after' => array(
'minimum' => 0,
'required' => true,
'type' => 'integer',
),
'awareness' => array(
'required' => true,
'type' => array( 'object', 'null' ),
),
'client_id' => array(
'minimum' => 1,
'required' => true,
'type' => 'integer',
),
'room' => array(
'required' => true,
'type' => 'string',
'pattern' => '^[^/]+/[^/:]+(?::\\S+)?$',
),
'updates' => array(
'items' => $typed_update_args,
'minItems' => 0,
'required' => true,
'type' => 'array',
),
);
register_rest_route(
self::REST_NAMESPACE,
'/updates',
array(
'methods' => array( WP_REST_Server::CREATABLE ),
'callback' => array( $this, 'handle_request' ),
'permission_callback' => array( $this, 'check_permissions' ),
'args' => array(
'rooms' => array(
'items' => array(
'properties' => $room_args,
'type' => 'object',
),
'required' => true,
'type' => 'array',
),
),
)
);
}
/**
* Checks if the current user has permission to access a room.
*
* @since 7.0.0
*
* @param WP_REST_Request $request The REST request.
* @return bool|WP_Error True if user has permission, otherwise WP_Error with details.
*/
public function check_permissions( WP_REST_Request $request ) {
// Minimum cap check. Is user logged in with a contributor role or higher?
if ( ! current_user_can( 'edit_posts' ) ) {
return new WP_Error(
'rest_cannot_edit',
__( 'You do not have permission to perform this action' ),
array( 'status' => rest_authorization_required_code() )
);
}
$rooms = $request['rooms'];
foreach ( $rooms as $room ) {
$room = $room['room'];
$type_parts = explode( '/', $room, 2 );
$object_parts = explode( ':', $type_parts[1] ?? '', 2 );
$entity_kind = $type_parts[0];
$entity_name = $object_parts[0];
$object_id = $object_parts[1] ?? null;
if ( ! $this->can_user_sync_entity_type( $entity_kind, $entity_name, $object_id ) ) {
return new WP_Error(
'rest_cannot_edit',
sprintf(
/* translators: %s: The room name encodes the current entity being synced. */
__( 'You do not have permission to sync this entity: %s.' ),
$room
),
array( 'status' => rest_authorization_required_code() )
);
}
}
return true;
}
/**
* Handles request: stores sync updates and awareness data, and returns
* updates the client is missing.
*
* @since 7.0.0
*
* @param WP_REST_Request $request The REST request.
* @return WP_REST_Response|WP_Error Response object or error.
*/
public function handle_request( WP_REST_Request $request ) {
$rooms = $request['rooms'];
$response = array(
'rooms' => array(),
);
foreach ( $rooms as $room_request ) {
$awareness = $room_request['awareness'];
$client_id = $room_request['client_id'];
$cursor = $room_request['after'];
$room = $room_request['room'];
// Merge awareness state.
$merged_awareness = $this->process_awareness_update( $room, $client_id, $awareness );
// The lowest client ID is nominated to perform compaction when needed.
$is_compactor = false;
if ( count( $merged_awareness ) > 0 ) {
$is_compactor = min( array_keys( $merged_awareness ) ) === $client_id;
}
// Process each update according to its type.
foreach ( $room_request['updates'] as $update ) {
$result = $this->process_sync_update( $room, $client_id, $cursor, $update );
if ( is_wp_error( $result ) ) {
return $result;
}
}
// Get updates for this client.
$room_response = $this->get_updates( $room, $client_id, $cursor, $is_compactor );
$room_response['awareness'] = $merged_awareness;
$response['rooms'][] = $room_response;
}
return new WP_REST_Response( $response, 200 );
}
/**
* Checks if the current user can sync a specific entity type.
*
* @since 7.0.0
*
* @param string $entity_kind The entity kind, e.g. 'postType', 'taxonomy', 'root'.
* @param string $entity_name The entity name, e.g. 'post', 'category', 'site'.
* @param string|null $object_id The object ID / entity key for single entities, null for collections.
* @return bool True if user has permission, otherwise false.
*/
private function can_user_sync_entity_type( string $entity_kind, string $entity_name, ?string $object_id ): bool {
// Handle single post type entities with a defined object ID.
if ( 'postType' === $entity_kind && is_numeric( $object_id ) ) {
return current_user_can( 'edit_post', (int) $object_id );
}
// Handle single taxonomy term entities with a defined object ID.
if ( 'taxonomy' === $entity_kind && is_numeric( $object_id ) ) {
$taxonomy = get_taxonomy( $entity_name );
return isset( $taxonomy->cap->assign_terms ) && current_user_can( $taxonomy->cap->assign_terms );
}
// All the remaining checks are for collections. If an object ID is provided,
// reject the request.
if ( null !== $object_id ) {
return false;
}
// For postType collections, check if the user can edit posts of this type.
if ( 'postType' === $entity_kind ) {
$post_type_object = get_post_type_object( $entity_name );
if ( ! isset( $post_type_object->cap->edit_posts ) ) {
return false;
}
return current_user_can( $post_type_object->cap->edit_posts );
}
// Collection syncing does not exchange entity data. It only signals if
// another user has updated an entity in the collection. Therefore, we only
// compare against an allow list of collection types.
$allowed_collection_entity_kinds = array(
'postType',
'root',
'taxonomy',
);
return in_array( $entity_kind, $allowed_collection_entity_kinds, true );
}
/**
* Processes and stores an awareness update from a client.
*
* @since 7.0.0
*
* @param string $room Room identifier.
* @param int $client_id Client identifier.
* @param array<string, mixed>|null $awareness_update Awareness state sent by the client.
* @return array<int, array<string, mixed>> Map of client ID to awareness state.
*/
private function process_awareness_update( string $room, int $client_id, ?array $awareness_update ): array {
$existing_awareness = $this->storage->get_awareness_state( $room );
$updated_awareness = array();
$current_time = time();
foreach ( $existing_awareness as $entry ) {
// Remove this client's entry (it will be updated below).
if ( $client_id === $entry['client_id'] ) {
continue;
}
// Remove entries that have expired.
if ( $current_time - $entry['updated_at'] >= self::AWARENESS_TIMEOUT ) {
continue;
}
$updated_awareness[] = $entry;
}
// Add this client's awareness state.
if ( null !== $awareness_update ) {
$updated_awareness[] = array(
'client_id' => $client_id,
'state' => $awareness_update,
'updated_at' => $current_time,
);
}
// This action can fail, but it shouldn't fail the entire request.
$this->storage->set_awareness_state( $room, $updated_awareness );
// Convert to client_id => state map for response.
$response = array();
foreach ( $updated_awareness as $entry ) {
$response[ $entry['client_id'] ] = $entry['state'];
}
return $response;
}
/**
* Processes a sync update based on its type.
*
* @since 7.0.0
*
* @param string $room Room identifier.
* @param int $client_id Client identifier.
* @param int $cursor Client cursor (marker of last seen update).
* @param array{data: string, type: string} $update Sync update.
* @return true|WP_Error True on success, WP_Error on storage failure.
*/
private function process_sync_update( string $room, int $client_id, int $cursor, array $update ) {
$data = $update['data'];
$type = $update['type'];
switch ( $type ) {
case self::UPDATE_TYPE_COMPACTION:
/*
* Compaction replaces updates the client has already seen. Only remove
* updates with markers before the client's cursor to preserve updates
* that arrived since the client's last sync.
*
* Check for a newer compaction update first. If one exists, skip this
* compaction to avoid overwriting it.
*/
$updates_after_cursor = $this->storage->get_updates_after_cursor( $room, $cursor );
$has_newer_compaction = false;
foreach ( $updates_after_cursor as $existing ) {
if ( self::UPDATE_TYPE_COMPACTION === $existing['type'] ) {
$has_newer_compaction = true;
break;
}
}
if ( ! $has_newer_compaction ) {
if ( ! $this->storage->remove_updates_before_cursor( $room, $cursor ) ) {
return new WP_Error(
'rest_sync_storage_error',
__( 'Failed to remove updates during compaction.' ),
array( 'status' => 500 )
);
}
return $this->add_update( $room, $client_id, $type, $data );
}
break;
case self::UPDATE_TYPE_SYNC_STEP1:
case self::UPDATE_TYPE_SYNC_STEP2:
case self::UPDATE_TYPE_UPDATE:
/*
* Sync step 1 announces a client's state vector. Other clients need
* to see it so they can respond with sync_step2 containing missing
* updates. The cursor-based filtering prevents re-delivery.
*
* Sync step 2 contains updates for a specific client.
*
* All updates are stored persistently.
*/
return $this->add_update( $room, $client_id, $type, $data );
}
return new WP_Error(
'rest_invalid_update_type',
__( 'Invalid sync update type.' ),
array( 'status' => 400 )
);
}
/**
* Adds an update to a room's update list via storage.
*
* @since 7.0.0
*
* @param string $room Room identifier.
* @param int $client_id Client identifier.
* @param string $type Update type (sync_step1, sync_step2, update, compaction).
* @param string $data Base64-encoded update data.
* @return true|WP_Error True on success, WP_Error on storage failure.
*/
private function add_update( string $room, int $client_id, string $type, string $data ) {
$update = array(
'client_id' => $client_id,
'data' => $data,
'type' => $type,
);
if ( ! $this->storage->add_update( $room, $update ) ) {
return new WP_Error(
'rest_sync_storage_error',
__( 'Failed to store sync update.' ),
array( 'status' => 500 )
);
}
return true;
}
/**
* Gets sync updates for a specific client from a room after a given cursor.
*
* Delegates cursor-based retrieval to the storage layer, then applies
* client-specific filtering and compaction logic.
*
* @since 7.0.0
*
* @param string $room Room identifier.
* @param int $client_id Client identifier.
* @param int $cursor Return updates after this cursor.
* @param bool $is_compactor True if this client is nominated to perform compaction.
* @return array{
* end_cursor: int,
* should_compact: bool,
* room: string,
* total_updates: int,
* updates: array<int, array{data: string, type: string}>,
* } Response data for this room.
*/
private function get_updates( string $room, int $client_id, int $cursor, bool $is_compactor ): array {
$updates_after_cursor = $this->storage->get_updates_after_cursor( $room, $cursor );
$total_updates = $this->storage->get_update_count( $room );
// Filter out this client's updates, except compaction updates.
$typed_updates = array();
foreach ( $updates_after_cursor as $update ) {
if ( $client_id === $update['client_id'] && self::UPDATE_TYPE_COMPACTION !== $update['type'] ) {
continue;
}
$typed_updates[] = array(
'data' => $update['data'],
'type' => $update['type'],
);
}
$should_compact = $is_compactor && $total_updates > self::COMPACTION_THRESHOLD;
return array(
'end_cursor' => $this->storage->get_cursor( $room ),
'room' => $room,
'should_compact' => $should_compact,
'total_updates' => $total_updates,
'updates' => $typed_updates,
);
}
}