[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:
parent
f42e91d2bc
commit
d5080890ac
@ -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(
|
||||
|
@ -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,22 +38,39 @@ 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];
|
||||
$opts = ['note_id' => $note_id, 'actor_id' => $actor_id];
|
||||
$note_already_favoured = DB::find('note_favourite', $opts);
|
||||
$activity = null;
|
||||
$activity = null;
|
||||
if (\is_null($note_already_favoured)) {
|
||||
DB::persist(FavouriteEntity::create($opts));
|
||||
$activity = Activity::create([
|
||||
'actor_id' => $actor_id,
|
||||
'verb' => 'favourite',
|
||||
'actor_id' => $actor_id,
|
||||
'verb' => 'favourite',
|
||||
'object_type' => 'note',
|
||||
'object_id' => $note_id,
|
||||
'source' => $source,
|
||||
'object_id' => $note_id,
|
||||
'source' => $source,
|
||||
]);
|
||||
DB::persist($activity);
|
||||
|
||||
@ -63,23 +79,38 @@ 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;
|
||||
$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];
|
||||
$activity = Activity::create([
|
||||
'actor_id' => $actor_id,
|
||||
'verb' => 'undo', // 'undo_favourite',
|
||||
$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',
|
||||
'object_type' => 'activity', // 'note',
|
||||
'object_id' => $favourite_activity->getId(), // $note_id,
|
||||
'source' => $source,
|
||||
'object_id' => $favourite_activity->getId(), // $note_id,
|
||||
'source' => $source,
|
||||
]);
|
||||
DB::persist($activity);
|
||||
|
||||
Event::handle('NewNotification', [$actor = Actor::getById($actor_id), $activity, [], _m('{nickname} unfavoured note {note_id}.', ['nickname' => $actor->getNickname(), 'note_id' => $activity->getObjectId()])]);
|
||||
Event::handle('NewNotification', [$actor = Actor::getById($actor_id), $activity, [], _m('{nickname} unfavoured note {note_id}.', ['nickname' => $actor->getNickname(), 'note_id' => $activity->getObjectId()])]);
|
||||
}
|
||||
return $activity;
|
||||
}
|
||||
@ -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
|
||||
{
|
||||
@ -97,12 +135,12 @@ class Favourite extends NoteHandlerPlugin
|
||||
}
|
||||
|
||||
// If note is favourite, "is_favourite" is 1
|
||||
$opts = ['note_id' => $note->getId(), 'actor_id' => $user->getId()];
|
||||
$opts = ['note_id' => $note->getId(), 'actor_id' => $user->getId()];
|
||||
$is_favourite = DB::find('note_favourite', $opts) !== null;
|
||||
|
||||
// Generating URL for favourite action route
|
||||
$args = ['id' => $note->getId()];
|
||||
$type = Router::ABSOLUTE_PATH;
|
||||
$args = ['id' => $note->getId()];
|
||||
$type = Router::ABSOLUTE_PATH;
|
||||
$favourite_action_url = $is_favourite
|
||||
? Router::url('favourite_remove', $args, $type)
|
||||
: Router::url('favourite_add', $args, $type);
|
||||
@ -111,12 +149,12 @@ class Favourite extends NoteHandlerPlugin
|
||||
// Concatenating get parameter to redirect the user to where he came from
|
||||
$favourite_action_url .= '?from=' . urlencode($request->getRequestUri());
|
||||
|
||||
$extra_classes = $is_favourite ? 'note-actions-set' : 'note-actions-unset';
|
||||
$extra_classes = $is_favourite ? 'note-actions-set' : 'note-actions-unset';
|
||||
$favourite_action = [
|
||||
'url' => $favourite_action_url,
|
||||
'title' => $is_favourite ? 'Remove this note from favourites' : 'Favourite this note!',
|
||||
'url' => $favourite_action_url,
|
||||
'title' => $is_favourite ? 'Remove this note from favourites' : 'Favourite this note!',
|
||||
'classes' => "button-container favourite-button-container {$extra_classes}",
|
||||
'id' => 'favourite-button-container-' . $note->getId(),
|
||||
'id' => 'favourite-button-container-' . $note->getId(),
|
||||
];
|
||||
|
||||
$actions[] = $favourite_action;
|
||||
@ -130,11 +168,11 @@ 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)
|
||||
$favourite_actors = FavouriteEntity::getNoteFavouriteActors($note);
|
||||
$favourite_actors = FavouriteEntity::getNoteFavouriteActors($note);
|
||||
|
||||
if (\count($favourite_actors) < 1) {
|
||||
return Event::next;
|
||||
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user