| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | declare(strict_types=1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * @copyright Copyright (c) 2020, Georg Ehrke | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * @author Georg Ehrke <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * @license AGPL-3.0 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  * This code is free software: you can redistribute it and/or modify | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  * it under the terms of the GNU Affero General Public License, version 3, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  * as published by the Free Software Foundation. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  * This program is distributed in the hope that it will be useful, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  * but WITHOUT ANY WARRANTY; without even the implied warranty of | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  * GNU Affero General Public License for more details. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  * You should have received a copy of the GNU Affero General Public License, version 3, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  * along with this program. If not, see <http://www.gnu.org/licenses/> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  | namespace OCA\UserStatus\Controller; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  | use OCP\AppFramework\Controller; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  | use OCP\AppFramework\Http; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  | use OCP\AppFramework\Http\JSONResponse; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  | use OCP\AppFramework\Utility\ITimeFactory; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  | use OCP\EventDispatcher\IEventDispatcher; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  | use OCP\IRequest; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  | use OCP\IUserSession; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  | use OCP\User\Events\UserLiveStatusEvent; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  | class HeartbeatController extends Controller { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  | 	/** @var IEventDispatcher */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  | 	private $eventDispatcher; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  | 	/** @var IUserSession */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  | 	private $userSession; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  | 	/** @var ITimeFactory */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  | 	private $timeFactory; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  | 	 * HeartbeatController constructor. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  | 	 * @param string $appName | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  | 	 * @param IRequest $request | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  | 	 * @param IEventDispatcher $eventDispatcher | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  | 	public function __construct(string $appName, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  | 								IRequest $request, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  | 								IEventDispatcher $eventDispatcher, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  | 								IUserSession $userSession, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  | 								ITimeFactory $timeFactory) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  | 		parent::__construct($appName, $request); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  | 		$this->eventDispatcher = $eventDispatcher; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  | 		$this->userSession = $userSession; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  | 		$this->timeFactory = $timeFactory; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  | 	 * @NoAdminRequired | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  | 	 * @param string $status | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  | 	 * @return JSONResponse | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 71 |  |  | 	 */ | 
            
                                                                        
                            
            
                                    
            
            
                | 72 |  |  | 	public function heartbeat(string $status): JSONResponse { | 
            
                                                                        
                            
            
                                    
            
            
                | 73 |  |  | 		if (!\in_array($status, ['online', 'away'])) { | 
            
                                                                        
                            
            
                                    
            
            
                | 74 |  |  | 			return new JSONResponse([], Http::STATUS_BAD_REQUEST); | 
            
                                                                        
                            
            
                                    
            
            
                | 75 |  |  | 		} | 
            
                                                                        
                            
            
                                    
            
            
                | 76 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 77 |  |  | 		$user = $this->userSession->getUser(); | 
            
                                                                        
                            
            
                                    
            
            
                | 78 |  |  | 		if ($user === null) { | 
            
                                                                        
                            
            
                                    
            
            
                | 79 |  |  | 			return new JSONResponse([], Http::STATUS_INTERNAL_SERVER_ERROR); | 
            
                                                                        
                            
            
                                    
            
            
                | 80 |  |  | 		} | 
            
                                                                        
                            
            
                                    
            
            
                | 81 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 82 |  |  | 		$this->eventDispatcher->dispatchTyped( | 
            
                                                                        
                            
            
                                    
            
            
                | 83 |  |  | 			new UserLiveStatusEvent( | 
            
                                                                        
                            
            
                                    
            
            
                | 84 |  |  | 				$user, | 
            
                                                                        
                            
            
                                    
            
            
                | 85 |  |  | 				$status, | 
            
                                                                        
                            
            
                                    
            
            
                | 86 |  |  | 				$this->timeFactory->getTime() | 
            
                                                                        
                            
            
                                    
            
            
                | 87 |  |  | 			) | 
            
                                                                        
                            
            
                                    
            
            
                | 88 |  |  | 		); | 
            
                                                                        
                            
            
                                    
            
            
                | 89 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 90 |  |  | 		return new JSONResponse([], Http::STATUS_NO_CONTENT); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  | 	} | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 92 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 93 |  |  |  |