| | |
| | | |
| | | use JVBase\managers\Cache; |
| | | use JVBase\managers\CustomTable; |
| | | use JVBase\registrar\Registrar; |
| | | use JVBase\rest\Rest; |
| | | use JVBase\rest\Route; |
| | | use WP_REST_Request; |
| | |
| | | public function init(): void |
| | | { |
| | | $this->manager = JVB()->notification(); |
| | | $this->notification_types = $this->manager->getNotificationTypes(); |
| | | $this->notification_types = $this->manager->getNotificationTypes(true); |
| | | } |
| | | |
| | | /** |
| | |
| | | Route::for('notifications') |
| | | ->get([$this, 'getNotifications']) |
| | | ->args([ |
| | | 'user' => 'integer|required', |
| | | 'type' => 'string', |
| | | 'status' => 'string|enum:unread,read,actioned,dismissed', |
| | | 'limit' => 'integer|default:20|min:1|max:100', |
| | | 'offset' => 'integer|default:0', |
| | | ]) |
| | | ->auth('user') |
| | | ->rateLimit(30); |
| | | ->rateLimit(30) |
| | | ->register(); |
| | | |
| | | // Mark as read |
| | | Route::for('notifications/read') |
| | | ->post([$this, 'markRead']) |
| | | ->args([ |
| | | 'user' => 'integer|required', |
| | | 'notification_id' => 'integer|required', |
| | | ]) |
| | | ->auth('user') |
| | | ->rateLimit(30); |
| | | ->rateLimit(30) |
| | | ->register(); |
| | | |
| | | // Mark all as read |
| | | Route::for('notifications/read-all') |
| | | ->post([$this, 'markAllRead']) |
| | | ->args([ |
| | | 'user' => 'integer|required', |
| | | 'type' => 'string', |
| | | ]) |
| | | ->auth('user') |
| | | ->rateLimit(10); |
| | | ->rateLimit(10) |
| | | ->register(); |
| | | |
| | | // Mark as actioned |
| | | Route::for('notifications/action') |
| | | ->post([$this, 'markActioned']) |
| | | ->args([ |
| | | 'user' => 'integer|required', |
| | | 'notification_id' => 'integer|required', |
| | | ]) |
| | | ->auth('user') |
| | | ->rateLimit(30); |
| | | ->rateLimit(30) |
| | | ->register(); |
| | | |
| | | // Dismiss notification |
| | | Route::for('notifications/dismiss') |
| | | ->post([$this, 'markDismissed']) |
| | | ->args([ |
| | | 'user' => 'integer|required', |
| | | 'notification_id' => 'integer|required', |
| | | ]) |
| | | ->auth('user') |
| | | ->rateLimit(30); |
| | | ->rateLimit(30) |
| | | ->register(); |
| | | |
| | | // Get unread count |
| | | Route::for('notifications/count') |
| | | ->get([$this, 'getUnreadCount']) |
| | | ->args([ |
| | | 'user' => 'integer|required', |
| | | 'type' => 'string', |
| | | ]) |
| | | ->auth('user') |
| | | ->rateLimit(60); |
| | | ->rateLimit() |
| | | ->register(); |
| | | } |
| | | |
| | | // ========================================================================= |
| | |
| | | */ |
| | | public function getNotifications(WP_REST_Request $request): WP_REST_Response |
| | | { |
| | | $user_id = absint($request->get_param('user')); |
| | | $user_id = get_current_user_id(); |
| | | if (!$user_id) { |
| | | return $this->unauthorized(); |
| | | } |
| | | $type = sanitize_text_field($request->get_param('type') ?? ''); |
| | | $status = sanitize_text_field($request->get_param('status') ?? ''); |
| | | $limit = absint($request->get_param('limit')); |
| | | $offset = absint($request->get_param('offset')); |
| | | |
| | | if (!$this->checkUser($user_id)) { |
| | | return $this->unauthorized(); |
| | | } |
| | | |
| | | $cacheKey = compact('user_id', 'type', 'status', 'limit', 'offset'); |
| | | |
| | | $result = $this->cache->remember($cacheKey, function() use ($user_id, $type, $status, $limit, $offset) { |
| | |
| | | */ |
| | | public function getUnreadCount(WP_REST_Request $request): WP_REST_Response |
| | | { |
| | | $user_id = absint($request->get_param('user')); |
| | | $type = sanitize_text_field($request->get_param('type') ?? ''); |
| | | |
| | | if (!$this->checkUser($user_id)) { |
| | | $user_id = get_current_user_id(); |
| | | if (!$user_id) { |
| | | return $this->unauthorized(); |
| | | } |
| | | $type = sanitize_text_field($request->get_param('type') ?? ''); |
| | | |
| | | $cacheKey = compact('user_id', 'type'); |
| | | |
| | |
| | | $statusCondition = $wpdb->prepare("a.status = %s", $status); |
| | | } |
| | | |
| | | $approvals = jvbApprovalTypes(); |
| | | $approvals = Registrar::withFeature('approve_new'); |
| | | foreach ($approvals as $type => $config) { |
| | | $table = $wpdb->prefix . BASE . 'approval_' . $type . 'requests'; |
| | | $votes = $wpdb->prefix . BASE . 'approval_' . $type . 'votes'; |
| | |
| | | */ |
| | | public function markRead(WP_REST_Request $request): WP_REST_Response |
| | | { |
| | | $user_id = absint($request->get_param('user')); |
| | | $notification_id = absint($request->get_param('notification_id')); |
| | | |
| | | if (!$this->checkUser($user_id)) { |
| | | $user_id = get_current_user_id(); |
| | | if (!$user_id) { |
| | | return $this->unauthorized(); |
| | | } |
| | | $notification_id = absint($request->get_param('notification_id')); |
| | | |
| | | try { |
| | | $result = $this->notifications->transaction(function($table) use ($notification_id, $user_id) { |
| | |
| | | */ |
| | | public function markAllRead(WP_REST_Request $request): WP_REST_Response |
| | | { |
| | | $user_id = absint($request->get_param('user')); |
| | | $type = sanitize_text_field($request->get_param('type') ?? ''); |
| | | |
| | | if (!$this->checkUser($user_id)) { |
| | | $user_id = get_current_user_id(); |
| | | if (!$user_id) { |
| | | return $this->unauthorized(); |
| | | } |
| | | $type = sanitize_text_field($request->get_param('type') ?? ''); |
| | | |
| | | try { |
| | | $where = ['owner_id' => $user_id, 'status' => 'unread']; |
| | |
| | | */ |
| | | public function markActioned(WP_REST_Request $request): WP_REST_Response |
| | | { |
| | | $user_id = absint($request->get_param('user')); |
| | | $notification_id = absint($request->get_param('notification_id')); |
| | | |
| | | if (!$this->checkUser($user_id)) { |
| | | $user_id = get_current_user_id(); |
| | | if (!$user_id) { |
| | | return $this->unauthorized(); |
| | | } |
| | | $notification_id = absint($request->get_param('notification_id')); |
| | | |
| | | try { |
| | | $result = $this->notifications->transaction(function($table) use ($notification_id, $user_id) { |
| | |
| | | */ |
| | | public function markDismissed(WP_REST_Request $request): WP_REST_Response |
| | | { |
| | | $user_id = absint($request->get_param('user')); |
| | | $notification_id = absint($request->get_param('notification_id')); |
| | | |
| | | if (!$this->checkUser($user_id)) { |
| | | $user_id = get_current_user_id(); |
| | | if (!$user_id) { |
| | | return $this->unauthorized(); |
| | | } |
| | | $notification_id = absint($request->get_param('notification_id')); |
| | | |
| | | try { |
| | | $result = $this->notifications->transaction(function($table) use ($notification_id, $user_id) { |
| | |
| | | /** |
| | | * Get notification actions |
| | | */ |
| | | protected function getNotificationActions(string $type, array $data, object $notification = null): array |
| | | protected function getNotificationActions(string $type, array $data, ?object $notification = null): array |
| | | { |
| | | $actions = []; |
| | | |
| | |
| | | 'status' => in_array($params['status'] ?? '', ['all', 'unread', 'expired']) |
| | | ? $params['status'] |
| | | : 'unread', |
| | | 'user_id' => absint($params['user'] ?? get_current_user_id()), |
| | | 'user_id' => get_current_user_id(), |
| | | 'page' => absint($params['page'] ?? 1), |
| | | 'type' => in_array($params['type'] ?? '', array_keys($this->manager->notification_types ?? [])) |
| | | ? $params['type'] |