Jake Vanderwerf
2025-11-23 d7dbe7fee362d587dfc334135d9581b6216a4295
inc/rest/routes/QueueRoutes.php
@@ -1,7 +1,9 @@
<?php
namespace JVBase\rest\routes;
use Exception;
use JVBase\JVB;
use JVBase\managers\CacheManager;
use JVBase\rest\RestRouteManager;
use WP_REST_Request;
use WP_REST_Response;
@@ -92,19 +94,12 @@
      $ids = $request->get_param('ids');
      $limit = intval($request->get_param('limit'));
      // User-specific caching (keep this)
      $user_queue_timestamp = $this->getUserQueueTimestamp($user_id);
      $if_modified_since = $request->get_header('If-Modified-Since');
      if ($if_modified_since) {
         $if_modified_timestamp = strtotime($if_modified_since);
         if ($user_queue_timestamp <= $if_modified_timestamp) {
            return new WP_REST_Response(null, 304);
      // Use base class user-specific header checking
      // This checks both 'queue' and 'user_{$user_id}' timestamps
      $cache_check = $this->checkUserHeaders($request, $user_id, 'queue');
      if ($cache_check) {
         return $cache_check; // Returns 304 Not Modified
         }
      }
      header('Last-Modified: ' . gmdate('D, d M Y H:i:s', $user_queue_timestamp) . ' GMT');
      header('Cache-Control: private, max-age=30');
      global $wpdb;
      $table = $wpdb->prefix . $this->table;
@@ -135,19 +130,22 @@
      $operations = $wpdb->get_results($wpdb->prepare($sql, $params), ARRAY_A);
      // Format operations with improved data structure
      // Format operations
      foreach ($operations as &$op) {
         $op = $this->formatOperation($op);
      }
      return new WP_REST_Response([
      $response = new WP_REST_Response([
         'items' => $operations,
         'total' => count($operations),
         'timestamp' => date('c'), // ISO format
         'timestamp' => date('c'),
         'has_more' => count($operations) === $limit,
         'queue_stats' => $this->getQueueStats($user_id),
         'server_time' => date('c') // Helpful for frontend time sync
         'server_time' => date('c')
      ]);
      // Add cache headers (ETag, Last-Modified)
      return $this->addCacheHeaders($response);
   }
@@ -230,16 +228,6 @@
   }
   /**
    * Update user's queue timestamp when any operation changes
    * This should be called whenever an operation status changes
    */
   public function updateUserQueueTimestamp(int $user_id): void
   {
      $key = "{$user_id}_queue_timestamp";
      $this->cache->set($key, time());
   }
   /**
    * Convert MySQL datetime to ISO 8601 timestamp with proper timezone
    */
   protected function formatTimestamp(?string $mysql_datetime): ?string
@@ -249,14 +237,19 @@
      }
      try {
         // Create DateTime object from MySQL datetime (assuming UTC storage)
         $date = new DateTime($mysql_datetime, new DateTimeZone('UTC'));
         // Get WordPress timezone - dates are stored in this timezone
         $wp_timezone = wp_timezone();
         // Return ISO 8601 format with UTC timezone indicator
         return $date->format('c'); // e.g., "2025-07-23T22:57:35+00:00"
         // Parse the datetime in WordPress timezone
         $date = new DateTime($mysql_datetime, $wp_timezone);
         // Convert to UTC for API consistency
         $date->setTimezone(new DateTimeZone('UTC'));
         // Return ISO 8601 format
         return $date->format('c');
      } catch (Exception $e) {
         // Fallback: return null if datetime is invalid
         return null;
      }
   }
@@ -293,24 +286,6 @@
   }
   /**
    * Get user's queue last update timestamp from cache
    */
   protected function getUserQueueTimestamp(int $user_id): int
   {
      $key = "{$user_id}_queue_timestamp";
      // Use CacheManager for consistency
      $timestamp = $this->cache->get($key);
      if ($timestamp === false) {
         $timestamp = time();
         $this->cache->set($key, $timestamp);
      }
      return $timestamp;
   }
    /**
     * Update operation status (dismiss or retry)
     *
     * @param WP_REST_Request $request
@@ -321,7 +296,9 @@
      $data = $request->get_json_params();
      $ids = $data['ids'] ?? [];
      $action = $data['action'] ?? '';
      $user_id = get_current_user_id();
      $user_id = (int)$data['user'];
      // Validate input
      if (empty($ids) || !is_array($ids)) {
@@ -362,19 +339,13 @@
      $result = $this->processQueueAction($action, $valid_operations, $user_id);
      if ($result['success']) {
         $this->invalidateUserCache($user_id);
         // Update timestamp for this user's queue
         CacheManager::updateTimestamp("user_{$user_id}");
      }
      return new WP_REST_Response($result);
   }
   protected function invalidateUserCache(int $user_id): void
   {
      $key = "{$user_id}_queue_timestamp";
      $this->cache->invalidate($key);
      $this->cache->set($key, time());
   }
   protected function processQueueAction(string $action, array $operations, int $user_id): array
   {
      global $wpdb;