From 5495a3c5ec6a547b084c581644fc54e5f26f7095 Mon Sep 17 00:00:00 2001 From: Diogo Peralta Cordeiro Date: Sun, 27 Feb 2022 02:04:48 +0000 Subject: [PATCH] [ENTITY][Note] NoteType now becomes a varchar as predicted --- components/Posting/Posting.php | 3 +-- plugins/ActivityPub/Util/Model/Note.php | 10 +++++----- src/Entity/Note.php | 26 ++++++++++++------------- 3 files changed, 18 insertions(+), 21 deletions(-) diff --git a/components/Posting/Posting.php b/components/Posting/Posting.php index 3f60f57872..6baad56b27 100644 --- a/components/Posting/Posting.php +++ b/components/Posting/Posting.php @@ -36,7 +36,6 @@ use App\Core\VisibilityScope; use App\Entity\Activity; use App\Entity\Actor; use App\Entity\Note; -use App\Entity\NoteType; use App\Util\Common; use App\Util\Exception\BugFoundException; use App\Util\Exception\ClientException; @@ -247,7 +246,7 @@ class Posting extends Component rendered: $rendered, source: $source, ); - $note->setType(NoteType::PAGE); + $note->setType('page'); $note->setTitle($title); if ($flush_and_notify) { diff --git a/plugins/ActivityPub/Util/Model/Note.php b/plugins/ActivityPub/Util/Model/Note.php index e65f9e1e14..ecfa25baf3 100644 --- a/plugins/ActivityPub/Util/Model/Note.php +++ b/plugins/ActivityPub/Util/Model/Note.php @@ -44,7 +44,6 @@ use App\Core\Log; use App\Core\Router\Router; use App\Core\VisibilityScope; use App\Entity\Note as GSNote; -use App\Entity\NoteType; use App\Util\Common; use App\Util\Exception\ClientException; use App\Util\Exception\DuplicateFoundException; @@ -155,8 +154,8 @@ class Note extends Model 'reply_to' => $reply_to = $handleInReplyTo($type_note), 'modified' => new DateTime(), 'type' => match ($type_note->get('type')) { - 'Page' => NoteType::PAGE, - default => NoteType::NOTE + 'Page' => 'page', + default => 'note' }, 'source' => $source, ]; @@ -354,8 +353,9 @@ class Note extends Model $attr = [ '@context' => ActivityPub::$activity_streams_two_context, 'type' => $object->getScope() === VisibilityScope::MESSAGE ? 'ChatMessage' : (match ($object->getType()) { - NoteType::NOTE => 'Note', - NoteType::PAGE => 'Page' + 'note' => 'Note', + 'page' => 'Page', + default => throw new \Exception('Unsupported note type.') }), 'id' => $object->getUrl(), 'published' => $object->getCreated()->format(DateTimeInterface::RFC3339), diff --git a/src/Entity/Note.php b/src/Entity/Note.php index 6eb8d530dc..bc35fc6278 100644 --- a/src/Entity/Note.php +++ b/src/Entity/Note.php @@ -43,20 +43,18 @@ use DateTimeInterface; use function mb_substr; use const PREG_SPLIT_NO_EMPTY; -// The domain of this enum are Notes -enum NoteType : int // having an int is just convenient -{ - case NOTE = 1; // Is an element of microblogging, a direct message, or a reply to another note or page - case PAGE = 2; // Larger content note, beginning of a thread, or an email message -} - /** - * Entity for notices + * Entity for notes + * + * Notes can be of different types, such as: + * - Note: Is an element of microblogging, a direct message, or a reply to another note or page + * - Page: Larger content note, beginning of a thread, or an email message * * @category DB * @package GNUsocial * * @author Hugo Sales + * @author Diogo Peralta Cordeiro <@diogo.site> * @copyright 2020-2021 Free Software Foundation, Inc http://www.fsf.org * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later */ @@ -76,7 +74,7 @@ class Note extends Entity private int $scope = 1; //VisibilityScope::EVERYWHERE->value; private ?string $url = null; private ?int $language_id = null; - private int $type = 1; //NoteType::NOTE->value; + private string $type = 'note'; private ?string $title = null; private DateTimeInterface $created; private DateTimeInterface $modified; @@ -213,15 +211,15 @@ class Note extends Entity return $this->language_id; } - public function setType(NoteType|int $type): self + public function setType(string $type): self { - $this->type = \is_int($type) ? $type : $type->value; + $this->type = mb_substr($type, 0, 15); return $this; } - public function getType(): NoteType + public function getType(): string { - return NoteType::from($this->type); + return $this->type; } public function setTitle(?string $title): self @@ -652,7 +650,7 @@ class Note extends Entity 'scope' => ['type' => 'int', 'not null' => true, 'default' => VisibilityScope::EVERYWHERE->value, 'description' => 'bit map for distribution scope; 1 = everywhere; 2 = this server only; 4 = addressees; 8 = groups; 16 = collection; 32 = messages'], 'url' => ['type' => 'text', 'description' => 'Permalink to Note'], 'language_id' => ['type' => 'int', 'foreign key' => true, 'target' => 'Language.id', 'multiplicity' => 'one to many', 'description' => 'The language for this note'], - 'type' => ['type' => 'int', 'not null' => true, 'default' => NoteType::NOTE->value, 'description' => 'bit map for note type; 1 = Note; 2 = Page'], + 'type' => ['type' => 'varchar', 'length' => 15, 'not null' => true, 'default' => 'note', 'description' => 'Such as note and page'], 'title' => ['type' => 'varchar', 'not null' => false, 'default' => null, 'length' => 129, 'description' => 'Title of a page or a note'], 'created' => ['type' => 'datetime', 'not null' => true, 'default' => 'CURRENT_TIMESTAMP', 'description' => 'date this record was created'], 'modified' => ['type' => 'timestamp', 'not null' => true, 'default' => 'CURRENT_TIMESTAMP', 'description' => 'date this record was modified'],