From dcc37b055d6e59ffa866b03031e1e34305c79ef6 Mon Sep 17 00:00:00 2001 From: Diogo Peralta Cordeiro Date: Fri, 10 Dec 2021 02:38:47 +0000 Subject: [PATCH] [COMPONENT][Link] Remove relation to note when note is removed Moved entity NoteToLink to the component --- .../Link}/Entity/NoteToLink.php | 49 ++++++++++++++++++- components/Link/Link.php | 8 ++- 2 files changed, 55 insertions(+), 2 deletions(-) rename {src => components/Link}/Entity/NoteToLink.php (74%) diff --git a/src/Entity/NoteToLink.php b/components/Link/Entity/NoteToLink.php similarity index 74% rename from src/Entity/NoteToLink.php rename to components/Link/Entity/NoteToLink.php index 6555da4950..a8a6e961fe 100644 --- a/src/Entity/NoteToLink.php +++ b/components/Link/Entity/NoteToLink.php @@ -17,7 +17,7 @@ // along with GNU social. If not, see . // }}} -namespace App\Entity; +namespace Component\Link\Entity; use App\Core\DB\DB; use App\Core\Entity; @@ -93,6 +93,53 @@ class NoteToLink extends Entity return parent::create($args, $obj); } + /** + * @param int $note_id + * @return mixed + */ + public static function removeWhereNoteId(int $note_id): mixed + { + return DB::dql( + <<<'EOF' + DELETE FROM note_to_link ntl + WHERE ntl.note_id = :note_id + EOF, + ['note_id' => $note_id], + ); + } + + /** + * @param int $link_id + * @param int $note_id + * @return mixed + */ + public static function removeWhere(int $link_id, int $note_id): mixed + { + return DB::dql( + <<<'EOF' + DELETE FROM note_to_link ntl + WHERE (ntl.link_id = :link_id + OR ntl.note_id = :note_id) + EOF, + ['link_id' => $link_id, 'note_id' => $note_id], + ); + } + + /** + * @param int $link_id + * @return mixed + */ + public static function removeWhereLinkId(int $link_id): mixed + { + return DB::dql( + <<<'EOF' + DELETE FROM note_to_link ntl + WHERE ntl.link_id = :link_id + EOF, + ['link_id' => $link_id], + ); + } + public static function schemaDef(): array { return [ diff --git a/components/Link/Link.php b/components/Link/Link.php index cc547ac39f..8607dce5c3 100644 --- a/components/Link/Link.php +++ b/components/Link/Link.php @@ -27,9 +27,9 @@ use App\Core\DB\DB; use App\Core\Event; use App\Core\Modules\Component; use App\Entity\Note; -use App\Entity\NoteToLink; use App\Util\Common; use App\Util\HTML; +use Component\Link\Entity\NoteToLink; use InvalidArgumentException; class Link extends Component @@ -258,4 +258,10 @@ class Link extends Component return HTML::html(['a' => ['attrs' => $attrs, $url]], options: ['indent' => false]); } + + public function onNoteDeleteRelated(Note &$note): bool + { + NoteToLink::removeWhereNoteId($note->getId()); + return Event::next; + } }