LocalGroup.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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\DB\DB;
  21. use App\Core\Entity;
  22. use App\Entity\Actor;
  23. use App\Util\Exception\NicknameEmptyException;
  24. use App\Util\Exception\NicknameException;
  25. use App\Util\Exception\NicknameInvalidException;
  26. use App\Util\Exception\NicknameNotAllowedException;
  27. use App\Util\Exception\NicknameTakenException;
  28. use App\Util\Exception\NicknameTooLongException;
  29. use App\Util\Nickname;
  30. use DateTimeInterface;
  31. /**
  32. * Entity for local groups
  33. *
  34. * @category DB
  35. * @package GNUsocial
  36. *
  37. * @author Zach Copley <zach@status.net>
  38. * @copyright 2010 StatusNet Inc.
  39. * @author Mikael Nordfeldth <mmn@hethane.se>
  40. * @copyright 2009-2014 Free Software Foundation, Inc http://www.fsf.org
  41. * @author Hugo Sales <hugo@hsal.es>
  42. * @copyright 2020-2021 Free Software Foundation, Inc http://www.fsf.org
  43. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  44. */
  45. class LocalGroup extends Entity
  46. {
  47. // {{{ Autocode
  48. // @codeCoverageIgnoreStart
  49. private int $actor_id;
  50. private string $nickname;
  51. private string $type = 'group';
  52. private DateTimeInterface $created;
  53. private DateTimeInterface $modified;
  54. public function setActorId(int $actor_id): self
  55. {
  56. $this->actor_id = $actor_id;
  57. return $this;
  58. }
  59. public function getActorId(): int
  60. {
  61. return $this->actor_id;
  62. }
  63. public function setNickname(string $nickname): self
  64. {
  65. $this->nickname = mb_substr($nickname, 0, 64);
  66. return $this;
  67. }
  68. public function getNickname(): string
  69. {
  70. return $this->nickname;
  71. }
  72. public function setType(string $type): self
  73. {
  74. $this->type = mb_substr($type, 0, 64);
  75. return $this;
  76. }
  77. public function getType(): string
  78. {
  79. return $this->type;
  80. }
  81. public function setCreated(DateTimeInterface $created): self
  82. {
  83. $this->created = $created;
  84. return $this;
  85. }
  86. public function getCreated(): DateTimeInterface
  87. {
  88. return $this->created;
  89. }
  90. public function setModified(DateTimeInterface $modified): self
  91. {
  92. $this->modified = $modified;
  93. return $this;
  94. }
  95. public function getModified(): DateTimeInterface
  96. {
  97. return $this->modified;
  98. }
  99. // @codeCoverageIgnoreEnd
  100. // }}} Autocode
  101. public function getActor()
  102. {
  103. return DB::findOneBy(Actor::class, ['id' => $this->actor_id]);
  104. }
  105. public static function getByNickname(string $nickname): ?self
  106. {
  107. return DB::findOneBy(self::class, ['nickname' => $nickname]);
  108. }
  109. public static function getActorByNickname(string $nickname): ?Actor
  110. {
  111. return DB::findOneBy(Actor::class, ['nickname' => $nickname, 'type' => Actor::GROUP]);
  112. }
  113. /**
  114. * Checks if desired nickname is allowed, and in case it is, it sets Actor's nickname cache to newly set nickname
  115. *
  116. * @param string $nickname Desired NEW nickname (do not use in local user creation)
  117. *
  118. * @throws NicknameEmptyException
  119. * @throws NicknameException
  120. * @throws NicknameInvalidException
  121. * @throws NicknameNotAllowedException
  122. * @throws NicknameTakenException
  123. * @throws NicknameTooLongException
  124. *
  125. * @return $this
  126. */
  127. public function setNicknameSanitizedAndCached(string $nickname): self
  128. {
  129. $nickname = Nickname::normalize($nickname, check_already_used: true, which: Nickname::CHECK_LOCAL_GROUP, check_is_allowed: true);
  130. $this->setNickname($nickname);
  131. $this->getActor()->setNickname($nickname);
  132. /// XXX: cache?
  133. return $this;
  134. }
  135. public static function schemaDef(): array
  136. {
  137. return [
  138. 'name' => 'local_group',
  139. 'description' => 'Record for a user group on the local site, with some additional info not in user_group',
  140. 'fields' => [
  141. 'actor_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'],
  142. 'nickname' => ['type' => 'varchar', 'not null' => true, 'length' => 64, 'description' => 'group represented'],
  143. 'type' => ['type' => 'varchar', 'not null' => true, 'default' => 'group', 'length' => 64, 'description' => 'Group or Organisation'],
  144. 'created' => ['type' => 'datetime', 'not null' => true, 'default' => 'CURRENT_TIMESTAMP', 'description' => 'date this record was created'],
  145. 'modified' => ['type' => 'datetime', 'not null' => true, 'default' => 'CURRENT_TIMESTAMP', 'description' => 'date this record was modified'],
  146. ],
  147. 'primary key' => ['actor_id'],
  148. 'unique keys' => [
  149. 'local_group_nickname_key' => ['nickname'],
  150. ],
  151. ];
  152. }
  153. }