| 
									
										
										
										
											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; | 
					
						
							| 
									
										
										
										
											2021-05-22 21:05:28 +08:00
										 |  |  | use Illuminate\Support\Collection; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class RecentlyViewed extends EntityQuery | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     public function run(int $count, int $page): Collection | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $user = user(); | 
					
						
							| 
									
										
										
										
											2023-09-16 20:18:35 +08:00
										 |  |  |         if ($user === null || $user->isGuest()) { | 
					
						
							| 
									
										
										
										
											2021-05-22 21:05:28 +08:00
										 |  |  |             return collect(); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-17 02:54:25 +08:00
										 |  |  |         $query = $this->permissionService()->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); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $query->with('viewable') | 
					
						
							|  |  |  |             ->skip(($page - 1) * $count) | 
					
						
							|  |  |  |             ->take($count) | 
					
						
							|  |  |  |             ->get() | 
					
						
							|  |  |  |             ->pluck('viewable') | 
					
						
							|  |  |  |             ->filter(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } |