[PLUGINS][Favourite] Added onNoteDeleteRelated event

All favourite entities are now removed from note_favourite table when the respective note
is deleted. Documented the favourNote and unfavourNote methods
This commit is contained in:
Eliseu Amaro 2022-01-02 15:55:10 +00:00 committed by Diogo Peralta Cordeiro
parent f42e91d2bc
commit d5080890ac
Signed by: diogo
GPG Key ID: 18D2D35001FBFAB0
2 changed files with 85 additions and 24 deletions

View File

@ -82,6 +82,11 @@ class NoteFavourite extends Entity
// @codeCoverageIgnoreEnd
// }}} Autocode
public static function getNoteFavourites(Note $note): array
{
return DB::findBy('note_favourite', ['note_id' => $note->getId()]);
}
public static function getNoteFavouriteActors(Note $note): array
{
return DB::dql(

View File

@ -25,7 +25,6 @@ namespace Plugin\Favourite;
use App\Core\DB\DB;
use App\Core\Event;
use function App\Core\I18n\_m;
use App\Core\Modules\NoteHandlerPlugin;
use App\Core\Router\RouteLoader;
use App\Core\Router\Router;
@ -39,9 +38,26 @@ use App\Util\Nickname;
use DateTime;
use Plugin\Favourite\Entity\NoteFavourite as FavouriteEntity;
use Symfony\Component\HttpFoundation\Request;
use function App\Core\I18n\_m;
class Favourite extends NoteHandlerPlugin
{
/**
* Creates a new Favourite Entity, upon the given Actor performs a Favourite
* action on the given Note object
*
* A new notification is then handled, informing all interested Actors of this action
*
* @param int $note_id
* @param int $actor_id
* @param string $source
*
* @return \App\Entity\Activity|null
* @throws \App\Util\Exception\ServerException
* @throws \Doctrine\ORM\ORMException
* @throws \Doctrine\ORM\OptimisticLockException
* @throws \Doctrine\ORM\TransactionRequiredException
*/
public static function favourNote(int $note_id, int $actor_id, string $source = 'web'): ?Activity
{
$opts = ['note_id' => $note_id, 'actor_id' => $actor_id];
@ -63,13 +79,28 @@ class Favourite extends NoteHandlerPlugin
return $activity;
}
/**
* Removes the Favourite Entity created beforehand, by the same Actor, and on the same Note
*
* Informs all interested Actors of this action, handling out the NewNotification event
*
* @param int $note_id
* @param int $actor_id
* @param string $source
*
* @return \App\Entity\Activity|null
* @throws \App\Util\Exception\ServerException
* @throws \Doctrine\ORM\ORMException
* @throws \Doctrine\ORM\OptimisticLockException
* @throws \Doctrine\ORM\TransactionRequiredException
*/
public static function unfavourNote(int $note_id, int $actor_id, string $source = 'web'): ?Activity
{
$note_already_favoured = DB::find('note_favourite', ['note_id' => $note_id, 'actor_id' => $actor_id]);
$activity = null;
if (!\is_null($note_already_favoured)) {
DB::remove($note_already_favoured);
$favourite_activity = DB::findBy('activity', ['verb' => 'favourite', 'object_type' => 'note', 'object_id' => $note_id], order_by: ['created' => 'DESC'])[0];
$favourite_activity = DB::findBy('activity', ['verb' => 'favourite', 'object_type' => 'note', 'object_id' => $note_id], order_by: ['created' => 'DESC'])[ 0 ];
$activity = Activity::create([
'actor_id' => $actor_id,
'verb' => 'undo', // 'undo_favourite',
@ -88,7 +119,14 @@ class Favourite extends NoteHandlerPlugin
* HTML rendering event that adds the favourite form as a note
* action, if a user is logged in
*
* @param \Symfony\Component\HttpFoundation\Request $request
* @param \App\Entity\Note $note
* @param array $actions
*
* @return bool Event hook
* @throws \Doctrine\ORM\ORMException
* @throws \Doctrine\ORM\OptimisticLockException
* @throws \Doctrine\ORM\TransactionRequiredException
*/
public function onAddNoteActions(Request $request, Note $note, array &$actions): bool
{
@ -130,7 +168,7 @@ class Favourite extends NoteHandlerPlugin
$check_user = !\is_null(Common::user());
// The current Note being rendered
$note = $vars['note'];
$note = $vars[ 'note' ];
// Will have actors array, and action string
// Actors are the subjects, action is the verb (in the final phrase)
@ -146,6 +184,24 @@ class Favourite extends NoteHandlerPlugin
return Event::next;
}
/**
* Deletes every favourite entity in table related to a deleted Note
*
* @param \App\Entity\Note $note
* @param \App\Entity\Actor $actor
*
* @return bool
*/
public function onNoteDeleteRelated(Note &$note, Actor $actor): bool
{
$note_favourites_list = FavouriteEntity::getNoteFavourites($note);
foreach ($note_favourites_list as $favourite_entity) {
DB::remove($favourite_entity);
}
return Event::next;
}
public function onAddRoute(RouteLoader $r): bool
{
// Add/remove note to/from favourites