ConversationMute.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <?php
  2. declare(strict_types = 1);
  3. // {{{ License
  4. // This file is part of GNU social - https://www.gnu.org/software/social
  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 published by
  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 License
  17. // along with GNU social. If not, see <http://www.gnu.org/licenses/>.
  18. // }}}
  19. namespace Component\Conversation\Entity;
  20. use App\Core\Cache;
  21. use App\Core\DB\DB;
  22. use App\Core\Entity;
  23. use App\Entity\Activity;
  24. use App\Entity\Actor;
  25. use App\Entity\LocalUser;
  26. use App\Entity\Note;
  27. use DateTimeInterface;
  28. /**
  29. * Entity class for Conversations Mutes
  30. *
  31. * @category DB
  32. * @package GNUsocial
  33. *
  34. * @author Hugo Sales <hugo@hsal.es>
  35. * @copyright 2022 Free Software Foundation, Inc http://www.fsf.org
  36. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  37. */
  38. class ConversationMute extends Entity
  39. {
  40. // {{{ Autocode
  41. // @codeCoverageIgnoreStart
  42. private int $conversation_id;
  43. private int $actor_id;
  44. private DateTimeInterface $created;
  45. public function setConversationId(int $conversation_id): self
  46. {
  47. $this->conversation_id = $conversation_id;
  48. return $this;
  49. }
  50. public function getConversationId(): int
  51. {
  52. return $this->conversation_id;
  53. }
  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 setCreated(DateTimeInterface $created): self
  64. {
  65. $this->created = $created;
  66. return $this;
  67. }
  68. public function getCreated(): DateTimeInterface
  69. {
  70. return $this->created;
  71. }
  72. // @codeCoverageIgnoreEnd
  73. // }}} Autocode
  74. public static function cacheKeys(int $conversation_id, int $actor_id): array
  75. {
  76. return [
  77. 'mute' => "conversation-mute-{$conversation_id}-{$actor_id}",
  78. ];
  79. }
  80. /**
  81. * Check if a conversation referenced by $object is muted form $actor
  82. */
  83. public static function isMuted(Activity|Note|int $object, Actor|LocalUser $actor): bool
  84. {
  85. $conversation_id = null;
  86. if (\is_int($object)) {
  87. $conversation_id = $object;
  88. } elseif ($object instanceof Note) {
  89. $conversation_id = $object->getConversationId();
  90. } elseif ($object instanceof Activity) {
  91. $conversation_id = Note::getById($object->getObjectId())->getConversationId();
  92. }
  93. return Cache::get(
  94. self::cacheKeys($conversation_id, $actor->getId())['mute'],
  95. fn () => (bool) DB::count('conversation_mute', ['conversation_id' => $conversation_id, 'actor_id' => $actor->getId()]),
  96. );
  97. }
  98. public static function schemaDef(): array
  99. {
  100. return [
  101. 'name' => 'conversation_mute',
  102. 'fields' => [
  103. 'conversation_id' => ['type' => 'int', 'foreign key' => true, 'target' => 'Conversation.id', 'multiplicity' => 'one to one', 'not null' => true, 'description' => 'The conversation being blocked'],
  104. 'actor_id' => ['type' => 'int', 'foreign key' => true, 'target' => 'Actor.id', 'multiplicity' => 'one to one', 'not null' => true, 'description' => 'Who blocked the conversation'],
  105. 'created' => ['type' => 'datetime', 'not null' => true, 'default' => 'CURRENT_TIMESTAMP', 'description' => 'date this record was created'],
  106. ],
  107. 'primary key' => ['conversation_id', 'actor_id'],
  108. 'foreign keys' => [
  109. 'conversation_id_to_id_fkey' => ['conversation', ['conversation_id' => 'id']],
  110. 'actor_id_to_id_fkey' => ['actor', ['actor_id' => 'id']],
  111. ],
  112. ];
  113. }
  114. }