| 
									
										
										
										
											2021-06-26 23:23:15 +08:00
										 |  |  | <?php | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | namespace BookStack\Entities\Queries; | 
					
						
							| 
									
										
										
										
											2021-05-22 21:05:28 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-18 00:56:55 +08:00
										 |  |  | use BookStack\Activity\Models\View; | 
					
						
							| 
									
										
										
										
											2024-02-09 00:39:59 +08:00
										 |  |  | use BookStack\Entities\Tools\MixedEntityListLoader; | 
					
						
							|  |  |  | use BookStack\Permissions\PermissionApplicator; | 
					
						
							| 
									
										
										
										
											2021-05-22 21:05:28 +08:00
										 |  |  | use Illuminate\Support\Collection; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-09 00:39:59 +08:00
										 |  |  | class QueryRecentlyViewed | 
					
						
							| 
									
										
										
										
											2021-05-22 21:05:28 +08:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2024-02-09 00:39:59 +08:00
										 |  |  |     public function __construct( | 
					
						
							|  |  |  |         protected PermissionApplicator $permissions, | 
					
						
							|  |  |  |         protected MixedEntityListLoader $listLoader, | 
					
						
							|  |  |  |     ) { | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-22 21:05:28 +08:00
										 |  |  |     public function run(int $count, int $page): Collection | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $user = user(); | 
					
						
							| 
									
										
										
										
											2024-02-04 22:39:01 +08:00
										 |  |  |         if ($user->isGuest()) { | 
					
						
							| 
									
										
										
										
											2021-05-22 21:05:28 +08:00
										 |  |  |             return collect(); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-09 00:39:59 +08:00
										 |  |  |         $query = $this->permissions->restrictEntityRelationQuery( | 
					
						
							| 
									
										
										
										
											2021-05-22 21:05:28 +08:00
										 |  |  |             View::query(), | 
					
						
							|  |  |  |             'views', | 
					
						
							|  |  |  |             'viewable_id', | 
					
						
							| 
									
										
										
										
											2022-07-16 20:17:08 +08:00
										 |  |  |             'viewable_type' | 
					
						
							| 
									
										
										
										
											2021-05-22 21:05:28 +08:00
										 |  |  |         ) | 
					
						
							|  |  |  |             ->orderBy('views.updated_at', 'desc') | 
					
						
							|  |  |  |             ->where('user_id', '=', user()->id); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-04 22:39:01 +08:00
										 |  |  |         $views = $query | 
					
						
							| 
									
										
										
										
											2021-05-22 21:05:28 +08:00
										 |  |  |             ->skip(($page - 1) * $count) | 
					
						
							|  |  |  |             ->take($count) | 
					
						
							| 
									
										
										
										
											2024-02-04 22:39:01 +08:00
										 |  |  |             ->get(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-09 00:39:59 +08:00
										 |  |  |         $this->listLoader->loadIntoRelations($views->all(), 'viewable', false); | 
					
						
							| 
									
										
										
										
											2024-02-04 22:39:01 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |         return $views->pluck('viewable')->filter(); | 
					
						
							| 
									
										
										
										
											2021-05-22 21:05:28 +08:00
										 |  |  |     } | 
					
						
							|  |  |  | } |