Jake Vanderwerf
2026-07-05 fff721dd185f5b97f7ae7a6e64189e55887ff590
inc/rest/routes/NotificationsRoutes.php
@@ -3,6 +3,7 @@
use JVBase\managers\Cache;
use JVBase\managers\CustomTable;
use JVBase\registrar\Registrar;
use JVBase\rest\Rest;
use JVBase\rest\Route;
use WP_REST_Request;
@@ -133,7 +134,7 @@
   public function init(): void
   {
      $this->manager = JVB()->notification();
      $this->notification_types = $this->manager->getNotificationTypes();
      $this->notification_types = $this->manager->getNotificationTypes(true);
   }
   /**
@@ -145,64 +146,64 @@
      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();
   }
   // =========================================================================
@@ -214,16 +215,15 @@
    */
   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) {
@@ -258,12 +258,11 @@
    */
   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');
@@ -548,7 +547,7 @@
            $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';
@@ -640,12 +639,11 @@
    */
   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) {
@@ -688,12 +686,11 @@
    */
   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'];
@@ -723,12 +720,11 @@
    */
   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) {
@@ -772,12 +768,11 @@
    */
   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) {
@@ -1178,7 +1173,7 @@
   /**
    * 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 = [];
@@ -1311,7 +1306,7 @@
         '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']