[PLUGIN][Favourite] Log changes into Activity

This commit is contained in:
2021-12-08 22:28:35 +00:00
parent 480a42cca5
commit 5025901c86
2 changed files with 85 additions and 54 deletions

View File

@@ -26,7 +26,6 @@ namespace Plugin\Favourite\Controller;
use App\Core\Controller\FeedController;
use App\Core\DB\DB;
use App\Core\Form;
use function App\Core\I18n\_m;
use App\Core\Log;
use App\Core\Router\Router;
use App\Util\Common;
@@ -35,14 +34,16 @@ use App\Util\Exception\InvalidFormException;
use App\Util\Exception\NoLoggedInUser;
use App\Util\Exception\NoSuchNoteException;
use App\Util\Exception\RedirectException;
use Plugin\Favourite\Entity\Favourite as FavouriteEntity;
use App\Util\Exception\ServerException;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\HttpFoundation\Request;
use function App\Core\I18n\_m;
use function is_null;
class Favourite extends FeedController
{
/**
* @throws \App\Util\Exception\ServerException
* @throws ServerException
* @throws InvalidFormException
* @throws NoLoggedInUser
* @throws NoSuchNoteException
@@ -50,11 +51,11 @@ class Favourite extends FeedController
*/
public function favouriteAddNote(Request $request, int $id): bool|array
{
$user = Common::ensureLoggedIn();
$actor_id = $user->getId();
$opts = ['id' => $id];
$user = Common::ensureLoggedIn();
$actor_id = $user->getId();
$opts = ['id' => $id];
$add_favourite_note = DB::find('note', $opts);
if (\is_null($add_favourite_note)) {
if (is_null($add_favourite_note)) {
throw new NoSuchNoteException();
}
@@ -62,7 +63,7 @@ class Favourite extends FeedController
['add_favourite', SubmitType::class,
[
'label' => _m('Favourite note!'),
'attr' => [
'attr' => [
'title' => _m('Favourite this note!'),
],
],
@@ -72,18 +73,12 @@ class Favourite extends FeedController
$form_add_to_favourite->handleRequest($request);
if ($form_add_to_favourite->isSubmitted()) {
$opts = ['note_id' => $id, 'actor_id' => $user->getId()];
$note_already_favourited = DB::find('favourite', $opts);
if (\is_null($note_already_favourited)) {
$opts = ['note_id' => $id, 'actor_id' => $user->getId()];
DB::persist(FavouriteEntity::create($opts));
DB::flush();
}
\Plugin\Favourite\Favourite::favourNote(note_id: $id, actor_id: $actor_id);
DB::flush();
// Redirect user to where they came from
// Prevent open redirect
if (!\is_null($from = $this->string('from'))) {
if (!is_null($from = $this->string('from'))) {
if (Router::isAbsolute($from)) {
Log::warning("Actor {$actor_id} attempted to reply to a note and then get redirected to another host, or the URL was invalid ({$from})");
throw new ClientException(_m('Can not redirect to outside the website from here'), 400); // 400 Bad request (deceptive)
@@ -98,14 +93,14 @@ class Favourite extends FeedController
}
return [
'_template' => 'favourite/add_to_favourites.html.twig',
'note' => $add_favourite_note,
'_template' => 'favourite/add_to_favourites.html.twig',
'note' => $add_favourite_note,
'add_favourite' => $form_add_to_favourite->createView(),
];
}
/**
* @throws \App\Util\Exception\ServerException
* @throws ServerException
* @throws InvalidFormException
* @throws NoLoggedInUser
* @throws NoSuchNoteException
@@ -113,11 +108,11 @@ class Favourite extends FeedController
*/
public function favouriteRemoveNote(Request $request, int $id): array
{
$user = Common::ensureLoggedIn();
$actor_id = $user->getId();
$opts = ['note_id' => $id, 'actor_id' => $user->getId()];
$remove_favourite_note = DB::find('favourite', $opts);
if (\is_null($remove_favourite_note)) {
$user = Common::ensureLoggedIn();
$actor_id = $user->getId();
$opts = ['id' => $id];
$remove_favourite_note = DB::find('note', $opts);
if (is_null($remove_favourite_note)) {
throw new NoSuchNoteException();
}
@@ -125,7 +120,7 @@ class Favourite extends FeedController
['remove_favourite', SubmitType::class,
[
'label' => _m('Remove favourite'),
'attr' => [
'attr' => [
'title' => _m('Remove note from favourites.'),
],
],
@@ -134,14 +129,12 @@ class Favourite extends FeedController
$form_remove_favourite->handleRequest($request);
if ($form_remove_favourite->isSubmitted()) {
if ($remove_favourite_note) {
DB::remove($remove_favourite_note);
DB::flush();
}
\Plugin\Favourite\Favourite::unfavourNote(note_id: $id, actor_id: $actor_id);
DB::flush();
// Redirect user to where they came from
// Prevent open redirect
if (!\is_null($from = $this->string('from'))) {
if (!is_null($from = $this->string('from'))) {
if (Router::isAbsolute($from)) {
Log::warning("Actor {$actor_id} attempted to reply to a note and then get redirected to another host, or the URL was invalid ({$from})");
throw new ClientException(_m('Can not redirect to outside the website from here'), 400); // 400 Bad request (deceptive)
@@ -157,8 +150,8 @@ class Favourite extends FeedController
$note = DB::find('note', ['id' => $id]);
return [
'_template' => 'favourite/remove_from_favourites.html.twig',
'note' => $note,
'_template' => 'favourite/remove_from_favourites.html.twig',
'note' => $note,
'remove_favourite' => $form_remove_favourite->createView(),
];
}
@@ -191,9 +184,9 @@ class Favourite extends FeedController
/**
* Reverse favourites stream
*
* @return array template
* @throws NoLoggedInUser user not logged in
*
* @return array template
*/
public function reverseFavouritesByActorId(Request $request, int $id): array
{