[COMPONENT][Link] Remove relation to note when note is removed

Moved entity NoteToLink to the component
This commit is contained in:
Diogo Peralta Cordeiro 2021-12-10 02:38:47 +00:00
parent dcc867dad7
commit dcc37b055d
Signed by: diogo
GPG Key ID: 18D2D35001FBFAB0
2 changed files with 55 additions and 2 deletions

View File

@ -17,7 +17,7 @@
// along with GNU social. If not, see <http://www.gnu.org/licenses/>.
// }}}
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 [

View File

@ -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;
}
}