1
0

PredefinedStatusService.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2020 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OCA\UserStatus\Service;
  8. use OCP\IL10N;
  9. use OCP\UserStatus\IUserStatus;
  10. /**
  11. * Class DefaultStatusService
  12. *
  13. * We are offering a set of default statuses, so we can
  14. * translate them into different languages.
  15. *
  16. * @package OCA\UserStatus\Service
  17. */
  18. class PredefinedStatusService {
  19. private const MEETING = 'meeting';
  20. private const COMMUTING = 'commuting';
  21. private const SICK_LEAVE = 'sick-leave';
  22. private const VACATIONING = 'vacationing';
  23. private const REMOTE_WORK = 'remote-work';
  24. /**
  25. * @deprecated See \OCP\UserStatus\IUserStatus::MESSAGE_CALL
  26. */
  27. public const CALL = 'call';
  28. public const OUT_OF_OFFICE = 'out-of-office';
  29. /** @var IL10N */
  30. private $l10n;
  31. /**
  32. * DefaultStatusService constructor.
  33. *
  34. * @param IL10N $l10n
  35. */
  36. public function __construct(IL10N $l10n) {
  37. $this->l10n = $l10n;
  38. }
  39. /**
  40. * @return array
  41. */
  42. public function getDefaultStatuses(): array {
  43. return [
  44. [
  45. 'id' => self::MEETING,
  46. 'icon' => '📅',
  47. 'message' => $this->getTranslatedStatusForId(self::MEETING),
  48. 'clearAt' => [
  49. 'type' => 'period',
  50. 'time' => 3600,
  51. ],
  52. ],
  53. [
  54. 'id' => self::COMMUTING,
  55. 'icon' => '🚌',
  56. 'message' => $this->getTranslatedStatusForId(self::COMMUTING),
  57. 'clearAt' => [
  58. 'type' => 'period',
  59. 'time' => 1800,
  60. ],
  61. ],
  62. [
  63. 'id' => self::REMOTE_WORK,
  64. 'icon' => '🏡',
  65. 'message' => $this->getTranslatedStatusForId(self::REMOTE_WORK),
  66. 'clearAt' => [
  67. 'type' => 'end-of',
  68. 'time' => 'day',
  69. ],
  70. ],
  71. [
  72. 'id' => self::SICK_LEAVE,
  73. 'icon' => '🤒',
  74. 'message' => $this->getTranslatedStatusForId(self::SICK_LEAVE),
  75. 'clearAt' => [
  76. 'type' => 'end-of',
  77. 'time' => 'day',
  78. ],
  79. ],
  80. [
  81. 'id' => self::VACATIONING,
  82. 'icon' => '🌴',
  83. 'message' => $this->getTranslatedStatusForId(self::VACATIONING),
  84. 'clearAt' => null,
  85. ],
  86. [
  87. 'id' => self::CALL,
  88. 'icon' => '💬',
  89. 'message' => $this->getTranslatedStatusForId(self::CALL),
  90. 'clearAt' => null,
  91. 'visible' => false,
  92. ],
  93. [
  94. 'id' => self::OUT_OF_OFFICE,
  95. 'icon' => '🛑',
  96. 'message' => $this->getTranslatedStatusForId(self::OUT_OF_OFFICE),
  97. 'clearAt' => null,
  98. 'visible' => false,
  99. ],
  100. ];
  101. }
  102. /**
  103. * @param string $id
  104. * @return array|null
  105. */
  106. public function getDefaultStatusById(string $id): ?array {
  107. foreach ($this->getDefaultStatuses() as $status) {
  108. if ($status['id'] === $id) {
  109. return $status;
  110. }
  111. }
  112. return null;
  113. }
  114. /**
  115. * @param string $id
  116. * @return string|null
  117. */
  118. public function getIconForId(string $id): ?string {
  119. switch ($id) {
  120. case self::MEETING:
  121. return '📅';
  122. case self::COMMUTING:
  123. return '🚌';
  124. case self::SICK_LEAVE:
  125. return '🤒';
  126. case self::VACATIONING:
  127. return '🌴';
  128. case self::OUT_OF_OFFICE:
  129. return '🛑';
  130. case self::REMOTE_WORK:
  131. return '🏡';
  132. case self::CALL:
  133. return '💬';
  134. default:
  135. return null;
  136. }
  137. }
  138. /**
  139. * @param string $lang
  140. * @param string $id
  141. * @return string|null
  142. */
  143. public function getTranslatedStatusForId(string $id): ?string {
  144. switch ($id) {
  145. case self::MEETING:
  146. return $this->l10n->t('In a meeting');
  147. case self::COMMUTING:
  148. return $this->l10n->t('Commuting');
  149. case self::SICK_LEAVE:
  150. return $this->l10n->t('Out sick');
  151. case self::VACATIONING:
  152. return $this->l10n->t('Vacationing');
  153. case self::OUT_OF_OFFICE:
  154. return $this->l10n->t('Out of office');
  155. case self::REMOTE_WORK:
  156. return $this->l10n->t('Working remotely');
  157. case self::CALL:
  158. return $this->l10n->t('In a call');
  159. default:
  160. return null;
  161. }
  162. }
  163. /**
  164. * @param string $id
  165. * @return bool
  166. */
  167. public function isValidId(string $id): bool {
  168. return \in_array($id, [
  169. self::MEETING,
  170. self::COMMUTING,
  171. self::SICK_LEAVE,
  172. self::VACATIONING,
  173. self::OUT_OF_OFFICE,
  174. self::REMOTE_WORK,
  175. IUserStatus::MESSAGE_CALL,
  176. IUserStatus::MESSAGE_AVAILABILITY,
  177. IUserStatus::MESSAGE_VACATION,
  178. IUserStatus::MESSAGE_CALENDAR_BUSY,
  179. IUserStatus::MESSAGE_CALENDAR_BUSY_TENTATIVE,
  180. ], true);
  181. }
  182. }