1
0

PredefinedStatusService.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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. /**
  30. * DefaultStatusService constructor.
  31. *
  32. * @param IL10N $l10n
  33. */
  34. public function __construct(
  35. private IL10N $l10n,
  36. ) {
  37. }
  38. /**
  39. * @return array
  40. */
  41. public function getDefaultStatuses(): array {
  42. return [
  43. [
  44. 'id' => self::MEETING,
  45. 'icon' => '📅',
  46. 'message' => $this->getTranslatedStatusForId(self::MEETING),
  47. 'clearAt' => [
  48. 'type' => 'period',
  49. 'time' => 3600,
  50. ],
  51. ],
  52. [
  53. 'id' => self::COMMUTING,
  54. 'icon' => '🚌',
  55. 'message' => $this->getTranslatedStatusForId(self::COMMUTING),
  56. 'clearAt' => [
  57. 'type' => 'period',
  58. 'time' => 1800,
  59. ],
  60. ],
  61. [
  62. 'id' => self::REMOTE_WORK,
  63. 'icon' => '🏡',
  64. 'message' => $this->getTranslatedStatusForId(self::REMOTE_WORK),
  65. 'clearAt' => [
  66. 'type' => 'end-of',
  67. 'time' => 'day',
  68. ],
  69. ],
  70. [
  71. 'id' => self::SICK_LEAVE,
  72. 'icon' => '🤒',
  73. 'message' => $this->getTranslatedStatusForId(self::SICK_LEAVE),
  74. 'clearAt' => [
  75. 'type' => 'end-of',
  76. 'time' => 'day',
  77. ],
  78. ],
  79. [
  80. 'id' => self::VACATIONING,
  81. 'icon' => '🌴',
  82. 'message' => $this->getTranslatedStatusForId(self::VACATIONING),
  83. 'clearAt' => null,
  84. ],
  85. [
  86. 'id' => self::CALL,
  87. 'icon' => '💬',
  88. 'message' => $this->getTranslatedStatusForId(self::CALL),
  89. 'clearAt' => null,
  90. 'visible' => false,
  91. ],
  92. [
  93. 'id' => self::OUT_OF_OFFICE,
  94. 'icon' => '🛑',
  95. 'message' => $this->getTranslatedStatusForId(self::OUT_OF_OFFICE),
  96. 'clearAt' => null,
  97. 'visible' => false,
  98. ],
  99. ];
  100. }
  101. /**
  102. * @param string $id
  103. * @return array|null
  104. */
  105. public function getDefaultStatusById(string $id): ?array {
  106. foreach ($this->getDefaultStatuses() as $status) {
  107. if ($status['id'] === $id) {
  108. return $status;
  109. }
  110. }
  111. return null;
  112. }
  113. /**
  114. * @param string $id
  115. * @return string|null
  116. */
  117. public function getIconForId(string $id): ?string {
  118. switch ($id) {
  119. case self::MEETING:
  120. return '📅';
  121. case self::COMMUTING:
  122. return '🚌';
  123. case self::SICK_LEAVE:
  124. return '🤒';
  125. case self::VACATIONING:
  126. return '🌴';
  127. case self::OUT_OF_OFFICE:
  128. return '🛑';
  129. case self::REMOTE_WORK:
  130. return '🏡';
  131. case self::CALL:
  132. return '💬';
  133. default:
  134. return null;
  135. }
  136. }
  137. /**
  138. * @param string $lang
  139. * @param string $id
  140. * @return string|null
  141. */
  142. public function getTranslatedStatusForId(string $id): ?string {
  143. switch ($id) {
  144. case self::MEETING:
  145. return $this->l10n->t('In a meeting');
  146. case self::COMMUTING:
  147. return $this->l10n->t('Commuting');
  148. case self::SICK_LEAVE:
  149. return $this->l10n->t('Out sick');
  150. case self::VACATIONING:
  151. return $this->l10n->t('Vacationing');
  152. case self::OUT_OF_OFFICE:
  153. return $this->l10n->t('Out of office');
  154. case self::REMOTE_WORK:
  155. return $this->l10n->t('Working remotely');
  156. case self::CALL:
  157. return $this->l10n->t('In a call');
  158. default:
  159. return null;
  160. }
  161. }
  162. /**
  163. * @param string $id
  164. * @return bool
  165. */
  166. public function isValidId(string $id): bool {
  167. return \in_array($id, [
  168. self::MEETING,
  169. self::COMMUTING,
  170. self::SICK_LEAVE,
  171. self::VACATIONING,
  172. self::OUT_OF_OFFICE,
  173. self::REMOTE_WORK,
  174. IUserStatus::MESSAGE_CALL,
  175. IUserStatus::MESSAGE_AVAILABILITY,
  176. IUserStatus::MESSAGE_VACATION,
  177. IUserStatus::MESSAGE_CALENDAR_BUSY,
  178. IUserStatus::MESSAGE_CALENDAR_BUSY_TENTATIVE,
  179. ], true);
  180. }
  181. }