| 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 |  |  | namespace OCA\UserStatus\Controller; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  | use OCA\UserStatus\Db\UserStatus; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  | use OCA\UserStatus\Exception\InvalidClearAtException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  | use OCA\UserStatus\Exception\InvalidMessageIdException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  | use OCA\UserStatus\Exception\InvalidStatusIconException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  | use OCA\UserStatus\Exception\InvalidStatusTypeException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  | use OCA\UserStatus\Exception\StatusMessageTooLongException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  | use OCA\UserStatus\Service\StatusService; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  | use OCP\AppFramework\Db\DoesNotExistException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  | use OCP\AppFramework\Http\DataResponse; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  | use OCP\AppFramework\OCS\OCSBadRequestException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  | use OCP\AppFramework\OCS\OCSNotFoundException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  | use OCP\AppFramework\OCSController; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  | use OCP\ILogger; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  | use OCP\IRequest; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  | class UserStatusController extends OCSController { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  | 	/** @var string */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  | 	private $userId; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  | 	/** @var ILogger */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  | 	private $logger; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  | 	/** @var StatusService */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  | 	private $service; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  | 	 * StatusesController constructor. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  | 	 * @param string $appName | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  | 	 * @param IRequest $request | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  | 	 * @param string $userId | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  | 	 * @param ILogger $logger; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  | 	 * @param StatusService $service | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  | 	public function __construct(string $appName, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  | 								IRequest $request, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  | 								string $userId, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  | 								ILogger $logger, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  | 								StatusService $service) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  | 		parent::__construct($appName, $request); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  | 		$this->userId = $userId; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  | 		$this->logger = $logger; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  | 		$this->service = $service; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  | 	 * @NoAdminRequired | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  | 	 * @return DataResponse | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  | 	 * @throws OCSNotFoundException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  | 	public function getStatus(): DataResponse { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  | 		try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  | 			$userStatus = $this->service->findByUserId($this->userId); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  | 		} catch (DoesNotExistException $ex) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  | 			throw new OCSNotFoundException('No status for the current user'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  | 		return new DataResponse($this->formatStatus($userStatus)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  | 	 * @NoAdminRequired | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  | 	 * @param string $statusType | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  | 	 * @return DataResponse | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  | 	 * @throws OCSBadRequestException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  | 	public function setStatus(string $statusType): DataResponse { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  | 		try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  | 			$status = $this->service->setStatus($this->userId, $statusType, null, true); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  | 			return new DataResponse($this->formatStatus($status)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  | 		} catch (InvalidStatusTypeException $ex) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  | 			$this->logger->debug('New user-status for "' . $this->userId . '" was rejected due to an invalid status type "' . $statusType . '"'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  | 			throw new OCSBadRequestException($ex->getMessage(), $ex); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  | 	 * @NoAdminRequired | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  | 	 * @param string $messageId | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  | 	 * @param int|null $clearAt | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  | 	 * @return DataResponse | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  | 	 * @throws OCSBadRequestException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  | 	public function setPredefinedMessage(string $messageId, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  | 										 ?int $clearAt): DataResponse { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  | 		try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  | 			$status = $this->service->setPredefinedMessage($this->userId, $messageId, $clearAt); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  | 			return new DataResponse($this->formatStatus($status)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  | 		} catch (InvalidClearAtException $ex) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  | 			$this->logger->debug('New user-status for "' . $this->userId . '" was rejected due to an invalid clearAt value "' . $clearAt . '"'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  | 			throw new OCSBadRequestException($ex->getMessage(), $ex); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  | 		} catch (InvalidMessageIdException $ex) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  | 			$this->logger->debug('New user-status for "' . $this->userId . '" was rejected due to an invalid message-id "' . $messageId . '"'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  | 			throw new OCSBadRequestException($ex->getMessage(), $ex); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  | 	 * @NoAdminRequired | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  | 	 * @param string|null $statusIcon | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  | 	 * @param string $message | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  | 	 * @param int|null $clearAt | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  | 	 * @return DataResponse | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  | 	 * @throws OCSBadRequestException | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 136 |  |  | 	 */ | 
            
                                                                        
                            
            
                                    
            
            
                | 137 |  |  | 	public function setCustomMessage(?string $statusIcon, | 
            
                                                                        
                            
            
                                    
            
            
                | 138 |  |  | 									 string $message, | 
            
                                                                        
                            
            
                                    
            
            
                | 139 |  |  | 									 ?int $clearAt): DataResponse { | 
            
                                                                        
                            
            
                                    
            
            
                | 140 |  |  | 		try { | 
            
                                                                        
                            
            
                                    
            
            
                | 141 |  |  | 			$status = $this->service->setCustomMessage($this->userId, $statusIcon, $message, $clearAt); | 
            
                                                                        
                            
            
                                    
            
            
                | 142 |  |  | 			return new DataResponse($this->formatStatus($status)); | 
            
                                                                        
                            
            
                                    
            
            
                | 143 |  |  | 		} catch (InvalidClearAtException $ex) { | 
            
                                                                        
                            
            
                                    
            
            
                | 144 |  |  | 			$this->logger->debug('New user-status for "' . $this->userId . '" was rejected due to an invalid clearAt value "' . $clearAt . '"'); | 
            
                                                                        
                            
            
                                    
            
            
                | 145 |  |  | 			throw new OCSBadRequestException($ex->getMessage(), $ex); | 
            
                                                                        
                            
            
                                    
            
            
                | 146 |  |  | 		} catch (InvalidStatusIconException $ex) { | 
            
                                                                        
                            
            
                                    
            
            
                | 147 |  |  | 			$this->logger->debug('New user-status for "' . $this->userId . '" was rejected due to an invalid icon value "' . $statusIcon . '"'); | 
            
                                                                        
                            
            
                                    
            
            
                | 148 |  |  | 			throw new OCSBadRequestException($ex->getMessage(), $ex); | 
            
                                                                        
                            
            
                                    
            
            
                | 149 |  |  | 		} catch (StatusMessageTooLongException $ex) { | 
            
                                                                        
                            
            
                                    
            
            
                | 150 |  |  | 			$this->logger->debug('New user-status for "' . $this->userId . '" was rejected due to a too long status message.'); | 
            
                                                                        
                            
            
                                    
            
            
                | 151 |  |  | 			throw new OCSBadRequestException($ex->getMessage(), $ex); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  | 	 * @NoAdminRequired | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  | 	 * @return DataResponse | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  | 	public function clearStatus(): DataResponse { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  | 		$this->service->clearStatus($this->userId); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  | 		return new DataResponse([]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  | 	 * @NoAdminRequired | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 |  |  | 	 * @return DataResponse | 
            
                                                                                                            
                            
            
                                    
            
            
                | 169 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 170 |  |  | 	public function clearMessage(): DataResponse { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 171 |  |  | 		$this->service->clearMessage($this->userId); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 172 |  |  | 		return new DataResponse([]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 173 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 174 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 175 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 176 |  |  | 	 * @param UserStatus $status | 
            
                                                                                                            
                            
            
                                    
            
            
                | 177 |  |  | 	 * @return array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 178 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 179 |  |  | 	private function formatStatus(UserStatus $status): array { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 180 |  |  | 		return [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 181 |  |  | 			'userId' => $status->getUserId(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 182 |  |  | 			'message' => $status->getCustomMessage(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 183 |  |  | 			'messageId' => $status->getMessageId(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 184 |  |  | 			'messageIsPredefined' => $status->getMessageId() !== null, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 185 |  |  | 			'icon' => $status->getCustomIcon(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 186 |  |  | 			'clearAt' => $status->getClearAt(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 187 |  |  | 			'status' => $status->getStatus(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 188 |  |  | 			'statusIsUserDefined' => $status->getIsUserDefined(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 189 |  |  | 		]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 190 |  |  | 	} | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 191 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 192 |  |  |  |