| | |
| | | <?php |
| | | namespace JVBase\rest\routes; |
| | | |
| | | use JVBase\Instance; |
| | | use JVBase\managers\Cache; |
| | | use JVBase\managers\queue\Operation; |
| | | use JVBase\managers\queue\Queue; |
| | | use JVBase\rest\Rest; |
| | | use JVBase\rest\Route; |
| | | use JVBase\rest\Response; |
| | |
| | | |
| | | class QueueRoutes extends Rest |
| | | { |
| | | use Instance; |
| | | public function __construct() |
| | | { |
| | | $this->cacheName = 'queue'; |
| | | $this->cacheTtl = 300; |
| | | parent::__construct(); |
| | | $this->cache = Cache::for($this->cacheName, 'custom', $this->cacheTtl)->user(); |
| | | |
| | | if (JVB_TESTING) { |
| | | $this->cache->flush(); |
| | | } |
| | | |
| | | } |
| | | |
| | | /** |
| | |
| | | 'limit' => 'integer|default:50|min:1|max:100', |
| | | ]) |
| | | ->auth('user') |
| | | ->rateLimit(30) |
| | | ->rateLimit() |
| | | ->post([$this, 'handleAction']) |
| | | ->args([ |
| | | 'ids' => 'array|required', |
| | | 'action' => 'string|required|enum:dismiss,retry,cancel', |
| | | ]) |
| | | ->auth('user') |
| | | ->rateLimit(30); |
| | | ->rateLimit() |
| | | ->register(); |
| | | |
| | | // Poll endpoint |
| | | Route::for('queue/poll') |
| | |
| | | 'ids' => 'string', |
| | | ]) |
| | | ->auth('user') |
| | | ->rateLimit(15); |
| | | ->rateLimit() |
| | | ->register(); |
| | | |
| | | // Errors endpoint |
| | | Route::for('queue/errors') |
| | | ->get([$this, 'getOperationErrors']) |
| | | ->auth('user') |
| | | ->rateLimit(15); |
| | | ->rateLimit() |
| | | ->register(); |
| | | |
| | | // Single operation with dynamic ID |
| | | Route::for(Route::pattern('queue/{id}')) |
| | | ->get([$this, 'getOperation']) |
| | | ->arg('id', 'string|required') |
| | | ->auth('user') |
| | | ->rateLimit(15); |
| | | ->rateLimit() |
| | | ->register(); |
| | | } |
| | | |
| | | /** |
| | |
| | | public function getQueue(WP_REST_Request $request): WP_REST_Response |
| | | { |
| | | $params = $request->get_params(); |
| | | $user_id = absint($params['user']); |
| | | $status = sanitize_text_field($params['status']); |
| | | $ids = !empty($params['ids']) |
| | | ? array_map('trim', array_map('sanitize_text_field', explode(',', $params['ids']))) |
| | | : []; |
| | | $limit = absint($params['limit']); |
| | | |
| | | $cacheKey = $this->cache->generateKey(compact('user_id', 'status', 'ids', 'limit')); |
| | | if ($cached = $this->checkHeaders($request, $cacheKey)) { |
| | | return $cached; |
| | | $user_id = get_current_user_id(); |
| | | if (!$user_id) { |
| | | return $this->unauthorized(); |
| | | } |
| | | |
| | | $data = $this->cache->tag("user:{$user_id}")->remember($cacheKey, function() use ($user_id, $params) { |
| | | $filters = $this->buildFilters($params); |
| | | $operations = JVB()->queue()->getUserOperations($user_id, $filters); |
| | | $filters = $this->buildFilters($params); |
| | | $ops = Queue::getInstance()->getUserOperations($user_id, $filters); |
| | | |
| | | return [ |
| | | 'items' => array_map([$this, 'formatOperation'], $operations), |
| | | 'total' => count($operations), |
| | | 'queue_stats' => $this->getQueueStats($user_id), |
| | | 'server_time' => date('c'), |
| | | ]; |
| | | }); |
| | | |
| | | $response = Response::success($data); |
| | | return $this->addCacheHeaders($response); |
| | | return Response::success([ |
| | | 'items' => array_map([$this, 'formatOperation'], $ops), |
| | | 'total' => count($ops), |
| | | 'queue_stats' => $this->getQueueStats($user_id), |
| | | 'server_time' => date('c') |
| | | ]); |
| | | } |
| | | |
| | | private function buildFilters(array $params): array |
| | |
| | | : array_map('trim', array_map('sanitize_text_field', explode(',', $data['ids']))); |
| | | |
| | | $action = sanitize_text_field($data['action'] ?? ''); |
| | | $user_id = absint($data['user']); |
| | | $user_id = get_current_user_id(); |
| | | if (!$user_id) { |
| | | return $this->unauthorized(); |
| | | } |
| | | |
| | | $this->cache = Cache::for($user_id.'_queue'); |
| | | |
| | | // Validate input |
| | | if (empty($ids)) { |
| | |
| | | } |
| | | |
| | | // Get operations via Queue - verifies ownership |
| | | $operations = JVB()->queue()->getUserOperations($user_id, [ |
| | | $operations = Queue::getInstance()->getUserOperations($user_id, [ |
| | | 'ids' => $ids, |
| | | 'limit' => count($ids), |
| | | ]); |
| | |
| | | |
| | | public function pollQueue(WP_REST_Request $request): WP_REST_Response |
| | | { |
| | | $userId = $request->get_param('user'); |
| | | $userId = get_current_user_id(); |
| | | if (!$userId) { |
| | | return $this->unauthorized(); |
| | | } |
| | | $this->cache = Cache::for($userId.'_queue'); |
| | | $since = $request->get_param('since'); |
| | | $ids = $request->get_param('ids'); |
| | | |
| | |
| | | $filters['ids'] = array_map('trim', explode(',', $ids)); |
| | | } |
| | | |
| | | $operations = JVB()->queue()->getUserOperations($userId, $filters); |
| | | $operations = Queue::getInstance()->getUserOperations($userId, $filters); |
| | | |
| | | if ($since) { |
| | | $sinceTime = strtotime($since); |
| | |
| | | |
| | | public function getOperationErrors(WP_REST_Request $request): WP_REST_Response |
| | | { |
| | | $user_id = absint($request->get_param('user')); |
| | | $operations = JVB()->queue()->getUserOperations($user_id, [ |
| | | $user_id = get_current_user_id(); |
| | | if (!$user_id) { |
| | | return $this->unauthorized(); |
| | | } |
| | | $this->cache = Cache::for($user_id.'_queue'); |
| | | $operations = Queue::getInstance()->getUserOperations($user_id, [ |
| | | 'state' => 'completed', |
| | | 'outcome' => ['failed', 'failed_permanent', 'partial'], |
| | | 'has_errors' => true, |
| | |
| | | public function getOperation(WP_REST_Request $request): WP_REST_Response |
| | | { |
| | | $id = $request->get_param('id'); |
| | | $userId = $request->get_param('user'); |
| | | $userId = get_current_user_id(); |
| | | if (!$userId) { |
| | | return $this->unauthorized(); |
| | | } |
| | | $this->cache = Cache::for($userId.'_queue'); |
| | | |
| | | $op = JVB()->queue()->get($id); |
| | | $op = Queue::getInstance()->get($id); |
| | | |
| | | if (!$op || $op->userId !== $userId) { |
| | | return Response::notFound('Operation not found'); |
| | | } |
| | | |
| | | return Response::item($this->formatOperation($op, true), 'operation'); |
| | | return Response::item($this->formatOperation($op), 'operation'); |
| | | } |
| | | |
| | | |
| | |
| | | { |
| | | return array_merge( |
| | | ['queued' => 0, 'localProcessing' => 0, 'uploading' => 0], |
| | | JVB()->queue()->getUserStats($userId) |
| | | Queue::getInstance()->getUserStats($userId) |
| | | ); |
| | | } |
| | | |
| | |
| | | |
| | | private function processAction(string $action, array $operations, int $userId): array |
| | | { |
| | | $queue = JVB()->queue(); |
| | | $queue = Queue::getInstance(); |
| | | $processed = 0; |
| | | $processedIds = []; |
| | | |