You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

StatusService.php 9.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2020, Georg Ehrke
  5. *
  6. * @author Georg Ehrke <oc.list@georgehrke.com>
  7. *
  8. * @license AGPL-3.0
  9. *
  10. * This code is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License, version 3,
  12. * as published by the Free Software Foundation.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License, version 3,
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>
  21. *
  22. */
  23. namespace OCA\UserStatus\Service;
  24. use OCA\UserStatus\Db\UserStatus;
  25. use OCA\UserStatus\Db\UserStatusMapper;
  26. use OCA\UserStatus\Exception\InvalidClearAtException;
  27. use OCA\UserStatus\Exception\InvalidMessageIdException;
  28. use OCA\UserStatus\Exception\InvalidStatusIconException;
  29. use OCA\UserStatus\Exception\InvalidStatusTypeException;
  30. use OCA\UserStatus\Exception\StatusMessageTooLongException;
  31. use OCP\AppFramework\Db\DoesNotExistException;
  32. use OCP\AppFramework\Utility\ITimeFactory;
  33. /**
  34. * Class StatusService
  35. *
  36. * @package OCA\UserStatus\Service
  37. */
  38. class StatusService {
  39. /** @var UserStatusMapper */
  40. private $mapper;
  41. /** @var ITimeFactory */
  42. private $timeFactory;
  43. /** @var PredefinedStatusService */
  44. private $predefinedStatusService;
  45. /** @var EmojiService */
  46. private $emojiService;
  47. /** @var string[] */
  48. private $allowedStatusTypes = [
  49. 'online',
  50. 'away',
  51. 'dnd',
  52. 'invisible',
  53. 'offline'
  54. ];
  55. /** @var int */
  56. private $maximumMessageLength = 80;
  57. /**
  58. * StatusService constructor.
  59. *
  60. * @param UserStatusMapper $mapper
  61. * @param ITimeFactory $timeFactory
  62. * @param PredefinedStatusService $defaultStatusService,
  63. * @param EmojiService $emojiService
  64. */
  65. public function __construct(UserStatusMapper $mapper,
  66. ITimeFactory $timeFactory,
  67. PredefinedStatusService $defaultStatusService,
  68. EmojiService $emojiService) {
  69. $this->mapper = $mapper;
  70. $this->timeFactory = $timeFactory;
  71. $this->predefinedStatusService = $defaultStatusService;
  72. $this->emojiService = $emojiService;
  73. }
  74. /**
  75. * @param int|null $limit
  76. * @param int|null $offset
  77. * @return UserStatus[]
  78. */
  79. public function findAll(?int $limit = null, ?int $offset = null): array {
  80. return array_map(function ($status) {
  81. return $this->processStatus($status);
  82. }, $this->mapper->findAll($limit, $offset));
  83. }
  84. /**
  85. * @param string $userId
  86. * @return UserStatus
  87. * @throws DoesNotExistException
  88. */
  89. public function findByUserId(string $userId):UserStatus {
  90. return $this->processStatus($this->mapper->findByUserId($userId));
  91. }
  92. /**
  93. * @param string $userId
  94. * @param string $status
  95. * @param int|null $statusTimestamp
  96. * @param bool $isUserDefined
  97. * @return UserStatus
  98. * @throws InvalidStatusTypeException
  99. */
  100. public function setStatus(string $userId,
  101. string $status,
  102. ?int $statusTimestamp,
  103. bool $isUserDefined): UserStatus {
  104. try {
  105. $userStatus = $this->mapper->findByUserId($userId);
  106. } catch (DoesNotExistException $ex) {
  107. $userStatus = new UserStatus();
  108. $userStatus->setUserId($userId);
  109. }
  110. // Check if status-type is valid
  111. if (!\in_array($status, $this->allowedStatusTypes, true)) {
  112. throw new InvalidStatusTypeException('Status-type "' . $status . '" is not supported');
  113. }
  114. if ($statusTimestamp === null) {
  115. $statusTimestamp = $this->timeFactory->getTime();
  116. }
  117. $userStatus->setStatus($status);
  118. $userStatus->setStatusTimestamp($statusTimestamp);
  119. $userStatus->setIsUserDefined($isUserDefined);
  120. if ($userStatus->getId() === null) {
  121. return $this->mapper->insert($userStatus);
  122. }
  123. return $this->mapper->update($userStatus);
  124. }
  125. /**
  126. * @param string $userId
  127. * @param string $messageId
  128. * @param int|null $clearAt
  129. * @return UserStatus
  130. * @throws InvalidMessageIdException
  131. * @throws InvalidClearAtException
  132. */
  133. public function setPredefinedMessage(string $userId,
  134. string $messageId,
  135. ?int $clearAt): UserStatus {
  136. try {
  137. $userStatus = $this->mapper->findByUserId($userId);
  138. } catch (DoesNotExistException $ex) {
  139. $userStatus = new UserStatus();
  140. $userStatus->setUserId($userId);
  141. $userStatus->setStatus('offline');
  142. $userStatus->setStatusTimestamp(0);
  143. $userStatus->setIsUserDefined(false);
  144. }
  145. if (!$this->predefinedStatusService->isValidId($messageId)) {
  146. throw new InvalidMessageIdException('Message-Id "' . $messageId . '" is not supported');
  147. }
  148. // Check that clearAt is in the future
  149. if ($clearAt !== null && $clearAt < $this->timeFactory->getTime()) {
  150. throw new InvalidClearAtException('ClearAt is in the past');
  151. }
  152. $userStatus->setMessageId($messageId);
  153. $userStatus->setCustomIcon(null);
  154. $userStatus->setCustomMessage(null);
  155. $userStatus->setClearAt($clearAt);
  156. if ($userStatus->getId() === null) {
  157. return $this->mapper->insert($userStatus);
  158. }
  159. return $this->mapper->update($userStatus);
  160. }
  161. /**
  162. * @param string $userId
  163. * @param string|null $statusIcon
  164. * @param string|null $message
  165. * @param int|null $clearAt
  166. * @return UserStatus
  167. * @throws InvalidClearAtException
  168. * @throws InvalidStatusIconException
  169. * @throws StatusMessageTooLongException
  170. */
  171. public function setCustomMessage(string $userId,
  172. ?string $statusIcon,
  173. string $message,
  174. ?int $clearAt): UserStatus {
  175. try {
  176. $userStatus = $this->mapper->findByUserId($userId);
  177. } catch (DoesNotExistException $ex) {
  178. $userStatus = new UserStatus();
  179. $userStatus->setUserId($userId);
  180. $userStatus->setStatus('offline');
  181. $userStatus->setStatusTimestamp(0);
  182. $userStatus->setIsUserDefined(false);
  183. }
  184. // Check if statusIcon contains only one character
  185. if ($statusIcon !== null && !$this->emojiService->isValidEmoji($statusIcon)) {
  186. throw new InvalidStatusIconException('Status-Icon is longer than one character');
  187. }
  188. // Check for maximum length of custom message
  189. if (\mb_strlen($message) > $this->maximumMessageLength) {
  190. throw new StatusMessageTooLongException('Message is longer than supported length of ' . $this->maximumMessageLength . ' characters');
  191. }
  192. // Check that clearAt is in the future
  193. if ($clearAt !== null && $clearAt < $this->timeFactory->getTime()) {
  194. throw new InvalidClearAtException('ClearAt is in the past');
  195. }
  196. $userStatus->setMessageId(null);
  197. $userStatus->setCustomIcon($statusIcon);
  198. $userStatus->setCustomMessage($message);
  199. $userStatus->setClearAt($clearAt);
  200. if ($userStatus->getId() === null) {
  201. return $this->mapper->insert($userStatus);
  202. }
  203. return $this->mapper->update($userStatus);
  204. }
  205. /**
  206. * @param string $userId
  207. * @return bool
  208. */
  209. public function clearStatus(string $userId): bool {
  210. try {
  211. $userStatus = $this->mapper->findByUserId($userId);
  212. } catch (DoesNotExistException $ex) {
  213. // if there is no status to remove, just return
  214. return false;
  215. }
  216. $userStatus->setStatus('offline');
  217. $userStatus->setStatusTimestamp(0);
  218. $userStatus->setIsUserDefined(false);
  219. $this->mapper->update($userStatus);
  220. return true;
  221. }
  222. /**
  223. * @param string $userId
  224. * @return bool
  225. */
  226. public function clearMessage(string $userId): bool {
  227. try {
  228. $userStatus = $this->mapper->findByUserId($userId);
  229. } catch (DoesNotExistException $ex) {
  230. // if there is no status to remove, just return
  231. return false;
  232. }
  233. $userStatus->setMessageId(null);
  234. $userStatus->setCustomMessage(null);
  235. $userStatus->setCustomIcon(null);
  236. $userStatus->setClearAt(null);
  237. $this->mapper->update($userStatus);
  238. return true;
  239. }
  240. /**
  241. * @param string $userId
  242. * @return bool
  243. */
  244. public function removeUserStatus(string $userId): bool {
  245. try {
  246. $userStatus = $this->mapper->findByUserId($userId);
  247. } catch (DoesNotExistException $ex) {
  248. // if there is no status to remove, just return
  249. return false;
  250. }
  251. $this->mapper->delete($userStatus);
  252. return true;
  253. }
  254. /**
  255. * Processes a status to check if custom message is still
  256. * up to date and provides translated default status if needed
  257. *
  258. * @param UserStatus $status
  259. * @returns UserStatus
  260. */
  261. private function processStatus(UserStatus $status): UserStatus {
  262. $clearAt = $status->getClearAt();
  263. if ($clearAt !== null && $clearAt < $this->timeFactory->getTime()) {
  264. $this->cleanStatus($status);
  265. }
  266. if ($status->getMessageId() !== null) {
  267. $this->addDefaultMessage($status);
  268. }
  269. return $status;
  270. }
  271. /**
  272. * @param UserStatus $status
  273. */
  274. private function cleanStatus(UserStatus $status): void {
  275. $status->setMessageId(null);
  276. $status->setCustomIcon(null);
  277. $status->setCustomMessage(null);
  278. $status->setClearAt(null);
  279. $this->mapper->update($status);
  280. }
  281. /**
  282. * @param UserStatus $status
  283. */
  284. private function addDefaultMessage(UserStatus $status): void {
  285. // If the message is predefined, insert the translated message and icon
  286. $predefinedMessage = $this->predefinedStatusService->getDefaultStatusById($status->getMessageId());
  287. if ($predefinedMessage !== null) {
  288. $status->setCustomMessage($predefinedMessage['message']);
  289. $status->setCustomIcon($predefinedMessage['icon']);
  290. }
  291. }
  292. }