@@ -56,6 +56,10 @@ class WP_Collaboration_Table_Storage {
5656 public function add_update ( string $ room , $ update ): bool {
5757 global $ wpdb ;
5858
59+ if ( '' === $ room || empty ( $ update ['type ' ] ) || empty ( $ update ['client_id ' ] ) ) {
60+ return false ;
61+ }
62+
5963 $ result = $ wpdb ->insert (
6064 $ wpdb ->collaboration ,
6165 array (
@@ -64,8 +68,9 @@ public function add_update( string $room, $update ): bool {
6468 'client_id ' => $ update ['client_id ' ] ?? '' ,
6569 'data ' => wp_json_encode ( $ update ),
6670 'date_gmt ' => gmdate ( 'Y-m-d H:i:s ' ),
71+ 'user_id ' => get_current_user_id (),
6772 ),
68- array ( '%s ' , '%s ' , '%s ' , '%s ' , '%s ' )
73+ array ( '%s ' , '%s ' , '%s ' , '%s ' , '%s ' , ' %d ' )
6974 );
7075
7176 return false !== $ result ;
@@ -286,27 +291,42 @@ public function remove_updates_through_cursor( string $room, int $cursor ): bool
286291 *
287292 * @global wpdb $wpdb WordPress database abstraction object.
288293 *
289- * @param string $room Room identifier.
294+ * @param string $room Room identifier.
290295 * @param string $client_id Client identifier.
291- * @param array<string, mixed> $state Serializable awareness state for this client.
292- * @param int $user_id WordPress user ID that owns this client.
296+ * @param array<string, mixed> $state Serializable awareness state for this client.
297+ * @param int $user_id WordPress user ID that owns this client.
293298 * @return bool True on success, false on failure.
294299 */
295300 public function set_awareness_state ( string $ room , string $ client_id , array $ state , int $ user_id ): bool {
296301 global $ wpdb ;
297302
303+ if ( '' === $ room || '' === $ client_id ) {
304+ return false ;
305+ }
306+
298307 $ data = wp_json_encode ( $ state );
299- $ now = gmdate ( 'Y-m-d H:i:s ' );
308+
309+ /*
310+ * Bucket the timestamp to 5-second intervals so most polls
311+ * short-circuit without a database write. Ceil is used instead
312+ * of floor to prevent the awareness timeout from being hit early.
313+ */
314+ $ now = gmdate ( 'Y-m-d H:i:s ' , (int ) ceil ( time () / 5 ) * 5 );
300315
301316 /* Check if a row already exists. */
302- $ exists = $ wpdb ->get_var (
317+ $ exists = $ wpdb ->get_row (
303318 $ wpdb ->prepare (
304- "SELECT id FROM {$ wpdb ->collaboration } WHERE room = %s AND type = 'awareness' AND client_id = %s LIMIT 1 " ,
319+ "SELECT id, date_gmt FROM {$ wpdb ->collaboration } WHERE room = %s AND type = 'awareness' AND client_id = %s LIMIT 1 " ,
305320 $ room ,
306321 $ client_id
307322 )
308323 );
309324
325+ if ( $ exists && $ exists ->date_gmt === $ now ) {
326+ // Row already has the current date, consider update a success.
327+ return true ;
328+ }
329+
310330 if ( $ exists ) {
311331 $ result = $ wpdb ->update (
312332 $ wpdb ->collaboration ,
@@ -315,7 +335,7 @@ public function set_awareness_state( string $room, string $client_id, array $sta
315335 'data ' => $ data ,
316336 'date_gmt ' => $ now ,
317337 ),
318- array ( 'id ' => $ exists )
338+ array ( 'id ' => $ exists-> id )
319339 );
320340 } else {
321341 $ result = $ wpdb ->insert (
0 commit comments