@@ -41,156 +41,156 @@ |
||
41 | 41 | |
42 | 42 | class UserStatusController extends OCSController { |
43 | 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 | - if ($message !== null && $message !== '') { |
|
142 | - $status = $this->service->setCustomMessage($this->userId, $statusIcon, $message, $clearAt); |
|
143 | - } else { |
|
144 | - $this->service->clearMessage($this->userId); |
|
145 | - $status = $this->service->findByUserId($this->userId); |
|
146 | - } |
|
147 | - return new DataResponse($this->formatStatus($status)); |
|
148 | - } catch (InvalidClearAtException $ex) { |
|
149 | - $this->logger->debug('New user-status for "' . $this->userId . '" was rejected due to an invalid clearAt value "' . $clearAt . '"'); |
|
150 | - throw new OCSBadRequestException($ex->getMessage(), $ex); |
|
151 | - } catch (InvalidStatusIconException $ex) { |
|
152 | - $this->logger->debug('New user-status for "' . $this->userId . '" was rejected due to an invalid icon value "' . $statusIcon . '"'); |
|
153 | - throw new OCSBadRequestException($ex->getMessage(), $ex); |
|
154 | - } catch (StatusMessageTooLongException $ex) { |
|
155 | - $this->logger->debug('New user-status for "' . $this->userId . '" was rejected due to a too long status message.'); |
|
156 | - throw new OCSBadRequestException($ex->getMessage(), $ex); |
|
157 | - } |
|
158 | - } |
|
159 | - |
|
160 | - /** |
|
161 | - * @NoAdminRequired |
|
162 | - * |
|
163 | - * @return DataResponse |
|
164 | - */ |
|
165 | - public function clearStatus(): DataResponse { |
|
166 | - $this->service->clearStatus($this->userId); |
|
167 | - return new DataResponse([]); |
|
168 | - } |
|
169 | - |
|
170 | - /** |
|
171 | - * @NoAdminRequired |
|
172 | - * |
|
173 | - * @return DataResponse |
|
174 | - */ |
|
175 | - public function clearMessage(): DataResponse { |
|
176 | - $this->service->clearMessage($this->userId); |
|
177 | - return new DataResponse([]); |
|
178 | - } |
|
179 | - |
|
180 | - /** |
|
181 | - * @param UserStatus $status |
|
182 | - * @return array |
|
183 | - */ |
|
184 | - private function formatStatus(UserStatus $status): array { |
|
185 | - return [ |
|
186 | - 'userId' => $status->getUserId(), |
|
187 | - 'message' => $status->getCustomMessage(), |
|
188 | - 'messageId' => $status->getMessageId(), |
|
189 | - 'messageIsPredefined' => $status->getMessageId() !== null, |
|
190 | - 'icon' => $status->getCustomIcon(), |
|
191 | - 'clearAt' => $status->getClearAt(), |
|
192 | - 'status' => $status->getStatus(), |
|
193 | - 'statusIsUserDefined' => $status->getIsUserDefined(), |
|
194 | - ]; |
|
195 | - } |
|
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 | + if ($message !== null && $message !== '') { |
|
142 | + $status = $this->service->setCustomMessage($this->userId, $statusIcon, $message, $clearAt); |
|
143 | + } else { |
|
144 | + $this->service->clearMessage($this->userId); |
|
145 | + $status = $this->service->findByUserId($this->userId); |
|
146 | + } |
|
147 | + return new DataResponse($this->formatStatus($status)); |
|
148 | + } catch (InvalidClearAtException $ex) { |
|
149 | + $this->logger->debug('New user-status for "' . $this->userId . '" was rejected due to an invalid clearAt value "' . $clearAt . '"'); |
|
150 | + throw new OCSBadRequestException($ex->getMessage(), $ex); |
|
151 | + } catch (InvalidStatusIconException $ex) { |
|
152 | + $this->logger->debug('New user-status for "' . $this->userId . '" was rejected due to an invalid icon value "' . $statusIcon . '"'); |
|
153 | + throw new OCSBadRequestException($ex->getMessage(), $ex); |
|
154 | + } catch (StatusMessageTooLongException $ex) { |
|
155 | + $this->logger->debug('New user-status for "' . $this->userId . '" was rejected due to a too long status message.'); |
|
156 | + throw new OCSBadRequestException($ex->getMessage(), $ex); |
|
157 | + } |
|
158 | + } |
|
159 | + |
|
160 | + /** |
|
161 | + * @NoAdminRequired |
|
162 | + * |
|
163 | + * @return DataResponse |
|
164 | + */ |
|
165 | + public function clearStatus(): DataResponse { |
|
166 | + $this->service->clearStatus($this->userId); |
|
167 | + return new DataResponse([]); |
|
168 | + } |
|
169 | + |
|
170 | + /** |
|
171 | + * @NoAdminRequired |
|
172 | + * |
|
173 | + * @return DataResponse |
|
174 | + */ |
|
175 | + public function clearMessage(): DataResponse { |
|
176 | + $this->service->clearMessage($this->userId); |
|
177 | + return new DataResponse([]); |
|
178 | + } |
|
179 | + |
|
180 | + /** |
|
181 | + * @param UserStatus $status |
|
182 | + * @return array |
|
183 | + */ |
|
184 | + private function formatStatus(UserStatus $status): array { |
|
185 | + return [ |
|
186 | + 'userId' => $status->getUserId(), |
|
187 | + 'message' => $status->getCustomMessage(), |
|
188 | + 'messageId' => $status->getMessageId(), |
|
189 | + 'messageIsPredefined' => $status->getMessageId() !== null, |
|
190 | + 'icon' => $status->getCustomIcon(), |
|
191 | + 'clearAt' => $status->getClearAt(), |
|
192 | + 'status' => $status->getStatus(), |
|
193 | + 'statusIsUserDefined' => $status->getIsUserDefined(), |
|
194 | + ]; |
|
195 | + } |
|
196 | 196 | } |