LocalGroup.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. <?php
  2. declare(strict_types = 1);
  3. // {{{ License
  4. // This file is part of GNU social - https://www.gnu.org/software/soci
  5. //
  6. // GNU social is free software: you can redistribute it and/or modify
  7. // it under the terms of the GNU Affero General Public License as publ
  8. // the Free Software Foundation, either version 3 of the License, or
  9. // (at your option) any later version.
  10. //
  11. // GNU social is distributed in the hope that it will be useful,
  12. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. // GNU Affero General Public License for more details.
  15. //
  16. // You should have received a copy of the GNU Affero General Public Li
  17. // along with GNU social. If not, see <http://www.gnu.org/licenses/>.
  18. // }}}
  19. namespace Component\Group\Entity;
  20. use App\Core\Cache;
  21. use App\Core\DB\DB;
  22. use App\Core\Entity;
  23. use App\Entity\Actor;
  24. use App\Util\Exception\NicknameEmptyException;
  25. use App\Util\Exception\NicknameException;
  26. use App\Util\Exception\NicknameInvalidException;
  27. use App\Util\Exception\NicknameNotAllowedException;
  28. use App\Util\Exception\NicknameTakenException;
  29. use App\Util\Exception\NicknameTooLongException;
  30. use App\Util\Nickname;
  31. use DateTimeInterface;
  32. /**
  33. * Entity for local groups
  34. *
  35. * @category DB
  36. * @package GNUsocial
  37. *
  38. * @author Zach Copley <zach@status.net>
  39. * @copyright 2010 StatusNet Inc.
  40. * @author Mikael Nordfeldth <mmn@hethane.se>
  41. * @copyright 2009-2014 Free Software Foundation, Inc http://www.fsf.org
  42. * @author Hugo Sales <hugo@hsal.es>
  43. * @copyright 2020-2021 Free Software Foundation, Inc http://www.fsf.org
  44. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  45. */
  46. class LocalGroup extends Entity
  47. {
  48. // {{{ Autocode
  49. // @codeCoverageIgnoreStart
  50. private int $group_id;
  51. private ?string $nickname = null;
  52. private DateTimeInterface $created;
  53. private DateTimeInterface $modified;
  54. public function setGroupId(int $group_id): self
  55. {
  56. $this->group_id = $group_id;
  57. return $this;
  58. }
  59. public function getGroupId(): int
  60. {
  61. return $this->group_id;
  62. }
  63. public function setNickname(?string $nickname): self
  64. {
  65. $this->nickname = \is_null($nickname) ? null : mb_substr($nickname, 0, 64);
  66. return $this;
  67. }
  68. public function getNickname(): ?string
  69. {
  70. return $this->nickname;
  71. }
  72. public function setCreated(DateTimeInterface $created): self
  73. {
  74. $this->created = $created;
  75. return $this;
  76. }
  77. public function getCreated(): DateTimeInterface
  78. {
  79. return $this->created;
  80. }
  81. public function setModified(DateTimeInterface $modified): self
  82. {
  83. $this->modified = $modified;
  84. return $this;
  85. }
  86. public function getModified(): DateTimeInterface
  87. {
  88. return $this->modified;
  89. }
  90. // @codeCoverageIgnoreEnd
  91. // }}} Autocode
  92. public function getActor()
  93. {
  94. return DB::find('actor', ['id' => $this->group_id]);
  95. }
  96. public static function getByNickname(string $nickname): ?self
  97. {
  98. $res = DB::findBy(self::class, ['nickname' => $nickname]);
  99. return $res === [] ? null : $res[0];
  100. }
  101. public static function getActorByNickname(string $nickname): ?Actor
  102. {
  103. $res = DB::findBy(Actor::class, ['nickname' => $nickname, 'type' => Actor::GROUP]);
  104. return $res === [] ? null : $res[0];
  105. }
  106. /**
  107. * Checks if desired nickname is allowed, and in case it is, it sets Actor's nickname cache to newly set nickname
  108. *
  109. * @param string $nickname Desired NEW nickname (do not use in local user creation)
  110. *
  111. * @throws NicknameEmptyException
  112. * @throws NicknameException
  113. * @throws NicknameInvalidException
  114. * @throws NicknameNotAllowedException
  115. * @throws NicknameTakenException
  116. * @throws NicknameTooLongException
  117. *
  118. * @return $this
  119. */
  120. public function setNicknameSanitizedAndCached(string $nickname): self
  121. {
  122. $nickname = Nickname::normalize($nickname, check_already_used: true, which: Nickname::CHECK_LOCAL_GROUP, check_is_allowed: true);
  123. $this->setNickname($nickname);
  124. $this->getActor()->setNickname($nickname);
  125. /// XXX: cache?
  126. return $this;
  127. }
  128. public static function schemaDef(): array
  129. {
  130. return [
  131. 'name' => 'local_group',
  132. 'description' => 'Record for a user group on the local site, with some additional info not in user_group',
  133. 'fields' => [
  134. 'group_id' => ['type' => 'int', 'foreign key' => true, 'target' => 'Group.id', 'multiplicity' => 'one to one', 'name' => 'local_group_group_id_fkey', 'not null' => true, 'description' => 'group represented'],
  135. 'nickname' => ['type' => 'varchar', 'length' => 64, 'description' => 'group represented'],
  136. 'created' => ['type' => 'datetime', 'not null' => true, 'default' => 'CURRENT_TIMESTAMP', 'description' => 'date this record was created'],
  137. 'modified' => ['type' => 'datetime', 'not null' => true, 'default' => 'CURRENT_TIMESTAMP', 'description' => 'date this record was modified'],
  138. ],
  139. 'primary key' => ['group_id'],
  140. 'unique keys' => [
  141. 'local_group_nickname_key' => ['nickname'],
  142. ],
  143. ];
  144. }
  145. }