From ef84fefca35a5f45de409fb90f0857fa9483cefc Mon Sep 17 00:00:00 2001 From: ghost Date: Tue, 10 Oct 2023 23:42:45 +0300 Subject: [PATCH] init activity features #4 --- src/Controller/ActivityController.php | 620 ++++++++++++++++++ src/Controller/TorrentController.php | 97 ++- src/Controller/UserController.php | 62 +- src/Entity/Activity.php | 103 ++- src/Repository/ActivityRepository.php | 24 - src/Service/ActivityService.php | 419 +++++++++++- src/Service/UserService.php | 7 +- .../activity/event/torrent/add.html.twig | 12 + .../event/torrent/locales/add.html.twig | 18 + .../torrent/locales/approve/add.html.twig | 18 + .../torrent/locales/approve/delete.html.twig | 18 + .../event/torrent/locales/delete.html.twig | 18 + .../event/torrent/sensitive/add.html.twig | 18 + .../torrent/sensitive/approve/add.html.twig | 18 + .../sensitive/approve/delete.html.twig | 18 + .../event/torrent/sensitive/delete.html.twig | 18 + .../activity/event/undefined.html.twig | 9 + .../default/activity/event/user/add.html.twig | 9 + .../activity/event/user/approve/add.html.twig | 12 + .../event/user/approve/delete.html.twig | 12 + .../event/user/moderator/add.html.twig | 12 + .../event/user/moderator/delete.html.twig | 12 + .../activity/event/user/star/add.html.twig | 12 + .../activity/event/user/star/delete.html.twig | 12 + .../activity/event/user/status/add.html.twig | 12 + .../event/user/status/delete.html.twig | 12 + templates/default/user/dashboard.html.twig | 21 +- 27 files changed, 1486 insertions(+), 137 deletions(-) create mode 100644 src/Controller/ActivityController.php create mode 100644 templates/default/activity/event/torrent/add.html.twig create mode 100644 templates/default/activity/event/torrent/locales/add.html.twig create mode 100644 templates/default/activity/event/torrent/locales/approve/add.html.twig create mode 100644 templates/default/activity/event/torrent/locales/approve/delete.html.twig create mode 100644 templates/default/activity/event/torrent/locales/delete.html.twig create mode 100644 templates/default/activity/event/torrent/sensitive/add.html.twig create mode 100644 templates/default/activity/event/torrent/sensitive/approve/add.html.twig create mode 100644 templates/default/activity/event/torrent/sensitive/approve/delete.html.twig create mode 100644 templates/default/activity/event/torrent/sensitive/delete.html.twig create mode 100644 templates/default/activity/event/undefined.html.twig create mode 100644 templates/default/activity/event/user/add.html.twig create mode 100644 templates/default/activity/event/user/approve/add.html.twig create mode 100644 templates/default/activity/event/user/approve/delete.html.twig create mode 100644 templates/default/activity/event/user/moderator/add.html.twig create mode 100644 templates/default/activity/event/user/moderator/delete.html.twig create mode 100644 templates/default/activity/event/user/star/add.html.twig create mode 100644 templates/default/activity/event/user/star/delete.html.twig create mode 100644 templates/default/activity/event/user/status/add.html.twig create mode 100644 templates/default/activity/event/user/status/delete.html.twig diff --git a/src/Controller/ActivityController.php b/src/Controller/ActivityController.php new file mode 100644 index 0000000..98c9939 --- /dev/null +++ b/src/Controller/ActivityController.php @@ -0,0 +1,620 @@ +getEvent()) + { + // User + case $activity::EVENT_USER_ADD: + + return $this->render( + 'default/activity/event/user/add.html.twig', + [ + 'added' => $activity->getAdded(), + 'user' => + [ + 'id' => $activity->getUserId(), + 'identicon' => $userService->identicon( + $userService->getUser( + $activity->getUserId() + )->getAddress() + ) + ] + ] + ); + + break; + + case $activity::EVENT_USER_APPROVE_ADD: + + return $this->render( + 'default/activity/event/user/approve/add.html.twig', + [ + 'added' => $activity->getAdded(), + 'user' => + [ + 'id' => $activity->getUserId(), + 'identicon' => $userService->identicon( + $userService->getUser( + $activity->getUserId() + )->getAddress() + ) + ], + 'by' => + [ + 'user' => + [ + 'id' => $activity->getData()['userId'], + 'identicon' => $userService->identicon( + $userService->getUser( + $activity->getData()['userId'] + )->getAddress() + ) + ] + ] + ] + ); + + break; + + case $activity::EVENT_USER_APPROVE_DELETE: + + return $this->render( + 'default/activity/event/user/approve/delete.html.twig', + [ + 'added' => $activity->getAdded(), + 'user' => + [ + 'id' => $activity->getUserId(), + 'identicon' => $userService->identicon( + $userService->getUser( + $activity->getUserId() + )->getAddress() + ) + ], + 'by' => + [ + 'user' => + [ + 'id' => $activity->getData()['userId'], + 'identicon' => $userService->identicon( + $userService->getUser( + $activity->getData()['userId'] + )->getAddress() + ) + ] + ] + ] + ); + + break; + + case $activity::EVENT_USER_MODERATOR_ADD: + + return $this->render( + 'default/activity/event/user/moderator/add.html.twig', + [ + 'added' => $activity->getAdded(), + 'user' => + [ + 'id' => $activity->getUserId(), + 'identicon' => $userService->identicon( + $userService->getUser( + $activity->getUserId() + )->getAddress() + ) + ], + 'by' => + [ + 'user' => + [ + 'id' => $activity->getData()['userId'], + 'identicon' => $userService->identicon( + $userService->getUser( + $activity->getData()['userId'] + )->getAddress() + ) + ] + ] + ] + ); + + break; + + case $activity::EVENT_USER_MODERATOR_DELETE: + + return $this->render( + 'default/activity/event/user/moderator/delete.html.twig', + [ + 'added' => $activity->getAdded(), + 'user' => + [ + 'id' => $activity->getUserId(), + 'identicon' => $userService->identicon( + $userService->getUser( + $activity->getUserId() + )->getAddress() + ) + ], + 'by' => + [ + 'user' => + [ + 'id' => $activity->getData()['userId'], + 'identicon' => $userService->identicon( + $userService->getUser( + $activity->getData()['userId'] + )->getAddress() + ) + ] + ] + ] + ); + + break; + + case $activity::EVENT_USER_STATUS_ADD: + + return $this->render( + 'default/activity/event/user/status/add.html.twig', + [ + 'added' => $activity->getAdded(), + 'user' => + [ + 'id' => $activity->getUserId(), + 'identicon' => $userService->identicon( + $userService->getUser( + $activity->getUserId() + )->getAddress() + ) + ], + 'by' => + [ + 'user' => + [ + 'id' => $activity->getData()['userId'], + 'identicon' => $userService->identicon( + $userService->getUser( + $activity->getData()['userId'] + )->getAddress() + ) + ] + ] + ] + ); + + break; + + case $activity::EVENT_USER_STATUS_DELETE: + + return $this->render( + 'default/activity/event/user/status/delete.html.twig', + [ + 'added' => $activity->getAdded(), + 'user' => + [ + 'id' => $activity->getUserId(), + 'identicon' => $userService->identicon( + $userService->getUser( + $activity->getUserId() + )->getAddress() + ) + ], + 'by' => + [ + 'user' => + [ + 'id' => $activity->getData()['userId'], + 'identicon' => $userService->identicon( + $userService->getUser( + $activity->getData()['userId'] + )->getAddress() + ) + ] + ] + ] + ); + + break; + + case $activity::EVENT_USER_STAR_ADD: + + return $this->render( + 'default/activity/event/user/star/add.html.twig', + [ + 'added' => $activity->getAdded(), + 'user' => + [ + 'id' => $activity->getUserId(), + 'identicon' => $userService->identicon( + $userService->getUser( + $activity->getUserId() + )->getAddress() + ) + ], + 'by' => + [ + 'user' => + [ + 'id' => $activity->getData()['userId'], + 'identicon' => $userService->identicon( + $userService->getUser( + $activity->getData()['userId'] + )->getAddress() + ) + ] + ] + ] + ); + + break; + + case $activity::EVENT_USER_STAR_DELETE: + + return $this->render( + 'default/activity/event/user/star/delete.html.twig', + [ + 'added' => $activity->getAdded(), + 'user' => + [ + 'id' => $activity->getUserId(), + 'identicon' => $userService->identicon( + $userService->getUser( + $activity->getUserId() + )->getAddress() + ) + ], + 'by' => + [ + 'user' => + [ + 'id' => $activity->getData()['userId'], + 'identicon' => $userService->identicon( + $userService->getUser( + $activity->getData()['userId'] + )->getAddress() + ) + ] + ] + ] + ); + + break; + + // Torrent + case $activity::EVENT_TORRENT_ADD: + + return $this->render( + 'default/activity/event/torrent/add.html.twig', + [ + 'added' => $activity->getAdded(), + 'user' => + [ + 'id' => $activity->getUserId(), + 'identicon' => $userService->identicon( + $userService->getUser( + $activity->getUserId() + )->getAddress() + ) + ], + 'torrent' => + [ + 'id' => $activity->getTorrentId(), + 'name' => $torrentService->readTorrentFileByTorrentId( + $activity->getTorrentId() + )->getName() + ] + ] + ); + + break; + + /// Torrent Locales + case $activity::EVENT_TORRENT_LOCALES_ADD: + + return $this->render( + 'default/activity/event/torrent/locales/add.html.twig', + [ + 'added' => $activity->getAdded(), + 'user' => + [ + 'id' => $activity->getUserId(), + 'identicon' => $userService->identicon( + $userService->getUser( + $activity->getUserId() + )->getAddress() + ) + ], + 'torrent' => + [ + 'id' => $activity->getTorrentId(), + 'name' => $torrentService->readTorrentFileByTorrentId( + $activity->getTorrentId() + )->getName(), + 'locales' => [ + 'id' => $activity->getData()['torrentLocalesId'], + 'exist' => $torrentService->getTorrentLocales( + $activity->getData()['torrentLocalesId'] // could be deleted by moderator, remove links + ) + ] + ] + ] + ); + + break; + + case $activity::EVENT_TORRENT_LOCALES_DELETE: + + return $this->render( + 'default/activity/event/torrent/locales/delete.html.twig', + [ + 'added' => $activity->getAdded(), + 'user' => + [ + 'id' => $activity->getUserId(), + 'identicon' => $userService->identicon( + $userService->getUser( + $activity->getUserId() + )->getAddress() + ) + ], + 'torrent' => + [ + 'id' => $activity->getTorrentId(), + 'name' => $torrentService->readTorrentFileByTorrentId( + $activity->getTorrentId() + )->getName(), + 'locales' => [ + 'id' => $activity->getData()['torrentLocalesId'], + 'exist' => $torrentService->getTorrentLocales( + $activity->getData()['torrentLocalesId'] // could be deleted by moderator, remove links + ) + ] + ] + ] + ); + + break; + + case $activity::EVENT_TORRENT_LOCALES_APPROVE_ADD: + + return $this->render( + 'default/activity/event/torrent/locales/approve/add.html.twig', + [ + 'added' => $activity->getAdded(), + 'user' => + [ + 'id' => $activity->getUserId(), + 'identicon' => $userService->identicon( + $userService->getUser( + $activity->getUserId() + )->getAddress() + ) + ], + 'torrent' => + [ + 'id' => $activity->getTorrentId(), + 'name' => $torrentService->readTorrentFileByTorrentId( + $activity->getTorrentId() + )->getName(), + 'locales' => [ + 'id' => $activity->getData()['torrentLocalesId'], + 'exist' => $torrentService->getTorrentLocales( + $activity->getData()['torrentLocalesId'] // could be deleted by moderator, remove links + ) + ] + ] + ] + ); + + break; + + case $activity::EVENT_TORRENT_LOCALES_APPROVE_DELETE: + + return $this->render( + 'default/activity/event/torrent/locales/approve/delete.html.twig', + [ + 'added' => $activity->getAdded(), + 'user' => + [ + 'id' => $activity->getUserId(), + 'identicon' => $userService->identicon( + $userService->getUser( + $activity->getUserId() + )->getAddress() + ) + ], + 'torrent' => + [ + 'id' => $activity->getTorrentId(), + 'name' => $torrentService->readTorrentFileByTorrentId( + $activity->getTorrentId() + )->getName(), + 'locales' => [ + 'id' => $activity->getData()['torrentLocalesId'], + 'exist' => $torrentService->getTorrentLocales( + $activity->getData()['torrentLocalesId'] // could be deleted by moderator, remove links + ) + ] + ] + ] + ); + + break; + + /// Torrent Sensitive + case $activity::EVENT_TORRENT_SENSITIVE_ADD: + + return $this->render( + 'default/activity/event/torrent/sensitive/add.html.twig', + [ + 'added' => $activity->getAdded(), + 'user' => + [ + 'id' => $activity->getUserId(), + 'identicon' => $userService->identicon( + $userService->getUser( + $activity->getUserId() + )->getAddress() + ) + ], + 'torrent' => + [ + 'id' => $activity->getTorrentId(), + 'name' => $torrentService->readTorrentFileByTorrentId( + $activity->getTorrentId() + )->getName(), + 'sensitive' => [ + 'id' => $activity->getData()['torrentSensitiveId'], + 'exist' => $torrentService->getTorrentSensitive( + $activity->getData()['torrentSensitiveId'] // could be deleted by moderator, remove links + ) + ] + ] + ] + ); + + break; + + case $activity::EVENT_TORRENT_SENSITIVE_DELETE: + + return $this->render( + 'default/activity/event/torrent/sensitive/delete.html.twig', + [ + 'added' => $activity->getAdded(), + 'user' => + [ + 'id' => $activity->getUserId(), + 'identicon' => $userService->identicon( + $userService->getUser( + $activity->getUserId() + )->getAddress() + ) + ], + 'torrent' => + [ + 'id' => $activity->getTorrentId(), + 'name' => $torrentService->readTorrentFileByTorrentId( + $activity->getTorrentId() + )->getName(), + 'sensitive' => [ + 'id' => $activity->getData()['torrentSensitiveId'], + 'exist' => $torrentService->getTorrentSensitive( + $activity->getData()['torrentSensitiveId'] // could be deleted by moderator, remove links + ) + ] + ] + ] + ); + + break; + + case $activity::EVENT_TORRENT_SENSITIVE_APPROVE_ADD: + + return $this->render( + 'default/activity/event/torrent/sensitive/approve/add.html.twig', + [ + 'added' => $activity->getAdded(), + 'user' => + [ + 'id' => $activity->getUserId(), + 'identicon' => $userService->identicon( + $userService->getUser( + $activity->getUserId() + )->getAddress() + ) + ], + 'torrent' => + [ + 'id' => $activity->getTorrentId(), + 'name' => $torrentService->readTorrentFileByTorrentId( + $activity->getTorrentId() + )->getName(), + 'sensitive' => [ + 'id' => $activity->getData()['torrentSensitiveId'], + 'exist' => $torrentService->getTorrentSensitive( + $activity->getData()['torrentSensitiveId'] // could be deleted by moderator, remove links + ) + ] + ] + ] + ); + + break; + + case $activity::EVENT_TORRENT_SENSITIVE_APPROVE_DELETE: + + return $this->render( + 'default/activity/event/torrent/sensitive/approve/delete.html.twig', + [ + 'added' => $activity->getAdded(), + 'user' => + [ + 'id' => $activity->getUserId(), + 'identicon' => $userService->identicon( + $userService->getUser( + $activity->getUserId() + )->getAddress() + ) + ], + 'torrent' => + [ + 'id' => $activity->getTorrentId(), + 'name' => $torrentService->readTorrentFileByTorrentId( + $activity->getTorrentId() + )->getName(), + 'sensitive' => [ + 'id' => $activity->getData()['torrentSensitiveId'], + 'exist' => $torrentService->getTorrentSensitive( + $activity->getData()['torrentSensitiveId'] // could be deleted by moderator, remove links + ) + ] + ] + ] + ); + + break; + + // Page + + default: + + return $this->render( + 'default/activity/event/undefined.html.twig', + [ + 'added' => $activity->getAdded(), + 'user' => + [ + 'id' => $activity->getUserId(), + 'identicon' => $userService->identicon( + $userService->getUser( + $activity->getUserId() + )->getAddress() + ) + ] + ] + ); + } + } +} \ No newline at end of file diff --git a/src/Controller/TorrentController.php b/src/Controller/TorrentController.php index e2b3281..02bb81f 100644 --- a/src/Controller/TorrentController.php +++ b/src/Controller/TorrentController.php @@ -11,6 +11,7 @@ use Symfony\Component\HttpFoundation\Request; use App\Service\UserService; use App\Service\TorrentService; +use App\Service\ActivityService; class TorrentController extends AbstractController { @@ -332,7 +333,8 @@ class TorrentController extends AbstractController Request $request, TranslatorInterface $translator, UserService $userService, - TorrentService $torrentService + TorrentService $torrentService, + ActivityService $activityService ): Response { // Init user @@ -463,7 +465,7 @@ class TorrentController extends AbstractController if (empty($form['locales']['error'])) { // Save data - $torrentService->addTorrentLocales( + $torrentLocales = $torrentService->addTorrentLocales( $torrent->getId(), $user->getId(), time(), @@ -471,6 +473,14 @@ class TorrentController extends AbstractController $user->isApproved() ); + // Register activity event + $activityService->addEventTorrentLocalesAdd( + $user->getId(), + $torrent->getId(), + time(), + $torrentLocales->getId() + ); + // Redirect to info article created return $this->redirectToRoute( 'torrent_info', @@ -516,7 +526,8 @@ class TorrentController extends AbstractController Request $request, TranslatorInterface $translator, UserService $userService, - TorrentService $torrentService + TorrentService $torrentService, + ActivityService $activityService, ): Response { // Init user @@ -545,6 +556,27 @@ class TorrentController extends AbstractController ); } + // Register activity event + if (!$torrentLocales->isApproved()) + { + $activityService->addEventTorrentLocalesApproveAdd( + $user->getId(), + $torrent->getId(), + time(), + $torrentLocales->getId() + ); + } + + else + { + $activityService->addEventTorrentLocalesApproveDelete( + $user->getId(), + $torrent->getId(), + time(), + $torrentLocales->getId() + ); + } + // Update approved $torrentService->toggleTorrentLocalesApproved( $torrentLocales->getId() @@ -578,7 +610,8 @@ class TorrentController extends AbstractController Request $request, TranslatorInterface $translator, UserService $userService, - TorrentService $torrentService + TorrentService $torrentService, + ActivityService $activityService ): Response { // Init user @@ -607,6 +640,14 @@ class TorrentController extends AbstractController ); } + // Add activity event + $activityService->addEventTorrentLocalesDelete( + $user->getId(), + $torrent->getId(), + time(), + $torrentLocales->getId() + ); + // Update approved $torrentService->deleteTorrentLocales( $torrentLocales->getId() @@ -646,7 +687,8 @@ class TorrentController extends AbstractController Request $request, TranslatorInterface $translator, UserService $userService, - TorrentService $torrentService + TorrentService $torrentService, + ActivityService $activityService ): Response { // Init user @@ -749,7 +791,7 @@ class TorrentController extends AbstractController if ($request->isMethod('post')) { // Save data - $torrentService->addTorrentSensitive( + $torrentSensitive = $torrentService->addTorrentSensitive( $torrent->getId(), $user->getId(), time(), @@ -757,6 +799,14 @@ class TorrentController extends AbstractController $user->isApproved() ); + // Add activity event + $activityService->addEventTorrentSensitiveAdd( + $user->getId(), + $torrent->getId(), + time(), + $torrentSensitive->getId() + ); + // Redirect to info article created return $this->redirectToRoute( 'torrent_info', @@ -800,7 +850,8 @@ class TorrentController extends AbstractController Request $request, TranslatorInterface $translator, UserService $userService, - TorrentService $torrentService + TorrentService $torrentService, + ActivityService $activityService ): Response { // Init user @@ -829,6 +880,27 @@ class TorrentController extends AbstractController ); } + // Add activity event + if (!$torrentSensitive->isApproved()) + { + $activityService->addEventTorrentSensitiveApproveAdd( + $user->getId(), + $torrent->getId(), + time(), + $torrentSensitive->getId() + ); + } + + else + { + $activityService->addEventTorrentSensitiveApproveDelete( + $user->getId(), + $torrent->getId(), + time(), + $torrentSensitive->getId() + ); + } + // Update approved $torrentService->toggleTorrentSensitiveApproved( $torrentSensitive->getId() @@ -862,7 +934,8 @@ class TorrentController extends AbstractController Request $request, TranslatorInterface $translator, UserService $userService, - TorrentService $torrentService + TorrentService $torrentService, + ActivityService $activityService ): Response { // Init user @@ -891,6 +964,14 @@ class TorrentController extends AbstractController ); } + // Add activity event + $activityService->addEventTorrentSensitiveDelete( + $user->getId(), + $torrent->getId(), + time(), + $torrentSensitive->getId() + ); + // Update approved $torrentService->deleteTorrentSensitive( $torrentSensitive->getId() diff --git a/src/Controller/UserController.php b/src/Controller/UserController.php index 425cbe5..0ce686a 100644 --- a/src/Controller/UserController.php +++ b/src/Controller/UserController.php @@ -44,46 +44,10 @@ class UserController extends AbstractController UserService $userService ): Response { - // Init user session - $user = $userService->init( - $request->getClientIp() - ); - - // Build activity history - $activities = []; - - /* - foreach ($activityService->findLast($user->isModerator()) as $activity) - { - if (!$activity->getUserId()) - { - continue; - } - - $activityUser = $userService->getUser( - $activity->getUserId() - ); - - $activities[] = - [ - 'user' => - [ - 'id' => $activityUser->getId(), - 'identicon' => $userService->identicon( - $activityUser->getAddress(), - 24 - ) - ], - 'type' => 'join', - 'added' => $activity->getAdded() - ]; - } - */ - return $this->render( 'default/user/dashboard.html.twig', [ - 'activities' => $activities + 'activities' => $activityService->findLastActivities() ] ); } @@ -271,7 +235,8 @@ class UserController extends AbstractController public function toggleStar( Request $request, TranslatorInterface $translator, - UserService $userService + UserService $userService, + ActivityService $activityService, ): Response { // Init user @@ -294,12 +259,31 @@ class UserController extends AbstractController } // Update - $userService->toggleUserStar( + $value = $userService->toggleUserStar( $user->getId(), $userTarget->getId(), time() ); + // Add activity event + if ($value) + { + $activityService->addEventUserStarAdd( + $user->getId(), + time(), + $userTarget->getId() + ); + } + + else + { + $activityService->addEventUserStarDelete( + $user->getId(), + time(), + $userTarget->getId() + ); + } + // Redirect to info article created return $this->redirectToRoute( 'user_info', diff --git a/src/Entity/Activity.php b/src/Entity/Activity.php index 2b370ab..ba9ca71 100644 --- a/src/Entity/Activity.php +++ b/src/Entity/Activity.php @@ -3,6 +3,7 @@ namespace App\Entity; use App\Repository\ActivityRepository; +use Doctrine\DBAL\Types\Types; use Doctrine\ORM\Mapping as ORM; #[ORM\Entity(repositoryClass: ActivityRepository::class)] @@ -13,18 +14,62 @@ class Activity #[ORM\Column] private ?int $id = null; - #[ORM\Column(length: 255)] - private ?string $event = null; - #[ORM\Column] - private ?int $added = null; + private ?int $event = null; - #[ORM\Column(nullable: true)] + // Event codes + + /// User + public const EVENT_USER_ADD = 10000; + + public const EVENT_USER_APPROVE_ADD = 10200; + public const EVENT_USER_APPROVE_DELETE = 10210; + + public const EVENT_USER_MODERATOR_ADD = 10300; + public const EVENT_USER_MODERATOR_DELETE = 10310; + + public const EVENT_USER_STATUS_ADD = 10400; + public const EVENT_USER_STATUS_DELETE = 10410; + + public const EVENT_USER_STAR_ADD = 10500; + public const EVENT_USER_STAR_DELETE = 10510; + + /// Torrent + public const EVENT_TORRENT_ADD = 20000; + + public const EVENT_TORRENT_LOCALES_ADD = 20100; + public const EVENT_TORRENT_LOCALES_DELETE = 20101; + public const EVENT_TORRENT_LOCALES_APPROVE_ADD = 20110; + public const EVENT_TORRENT_LOCALES_APPROVE_DELETE = 20111; + + public const EVENT_TORRENT_SENSITIVE_ADD = 20200; + public const EVENT_TORRENT_SENSITIVE_DELETE = 20201; + public const EVENT_TORRENT_SENSITIVE_APPROVE_ADD = 20210; + public const EVENT_TORRENT_SENSITIVE_APPROVE_DELETE = 20211; + + public const EVENT_TORRENT_DOWNLOAD_FILE_ADD = 20300; + + public const EVENT_TORRENT_DOWNLOAD_MAGNET_ADD = 20400; + + /// Article + public const EVENT_ARTICLE_ADD = 30000; + // ... + + #[ORM\Column] private ?int $userId = null; #[ORM\Column(nullable: true)] private ?int $articleId = null; + #[ORM\Column(nullable: true)] + private ?int $torrentId = null; + + #[ORM\Column] + private ?int $added = null; + + #[ORM\Column(type: Types::ARRAY)] + private array $data = []; + public function getId(): ?int { return $this->id; @@ -37,63 +82,75 @@ class Activity return $this; } - public function getEvent(): ?string + public function getEvent(): ?int { return $this->event; } - public function setEvent(string $event): static + public function setEvent(int $event): static { $this->event = $event; return $this; } - public function getAdded(): ?int + public function getUserId(): ?int { - return $this->added; + return $this->userId; } - public function setAdded(int $added): static + public function setUserId(?int $userId): static { - $this->added = $added; + $this->userId = $userId; return $this; } - public function setApproved(bool $approved): static + public function getArticleId(): ?int { - $this->approved = $approved; + return $this->articleId; + } + + public function setArticleId(int $articleId): static + { + $this->articleId = $articleId; return $this; } - public function getUserId(): ?int + public function getTorrentId(): ?int { - return $this->userId; + return $this->torrentId; } - public function setUserId(?int $userId): static + public function setTorrentId(?int $torrentId): static { - $this->userId = $userId; + $this->torrentId = $torrentId; return $this; } - public function getArticleId(): ?int + public function getAdded(): ?int { - return $this->articleId; + return $this->added; } - public function setArticleId(?int $articleId): static + public function setAdded(int $added): static { - $this->articleId = $articleId; + $this->added = $added; return $this; } - public function isApproved(): ?bool + public function getData(): array + { + return $this->data; + } + + public function setData(array $data): static { - return $this->approved; + $this->data = $data; + + return $this; } } diff --git a/src/Repository/ActivityRepository.php b/src/Repository/ActivityRepository.php index 6d26c74..78d420e 100644 --- a/src/Repository/ActivityRepository.php +++ b/src/Repository/ActivityRepository.php @@ -20,28 +20,4 @@ class ActivityRepository extends ServiceEntityRepository { parent::__construct($registry, Activity::class); } - - public function findLast(int $start = 0, int $limit = 10): array - { - return $this->createQueryBuilder('a') - ->orderBy('a.id', 'DESC') // same to a.added - ->setFirstResult($start) - ->setMaxResults($limit) - ->getQuery() - ->getResult() - ; - } - - public function findLastByApprovedField(bool $approved, int $start = 0, int $limit = 10): array - { - return $this->createQueryBuilder('a') - ->orderBy('a.id', 'DESC') // same to a.added - ->where('a.approved = :approved') - ->setParameter('approved', $approved) - ->setFirstResult($start) - ->setMaxResults($limit) - ->getQuery() - ->getResult() - ; - } } diff --git a/src/Service/ActivityService.php b/src/Service/ActivityService.php index 268fb0b..7cd25ee 100644 --- a/src/Service/ActivityService.php +++ b/src/Service/ActivityService.php @@ -6,49 +6,416 @@ use App\Entity\Activity; use App\Repository\ActivityRepository; use Doctrine\ORM\EntityManagerInterface; -use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface; - class ActivityService { - private EntityManagerInterface $entityManager; - private ActivityRepository $activityRepository; - private ParameterBagInterface $parameterBagInterface; + private EntityManagerInterface $entityManagerInterface; public function __construct( - EntityManagerInterface $entityManager, - ParameterBagInterface $parameterBagInterface + EntityManagerInterface $entityManagerInterface ) { - $this->entityManager = $entityManager; - $this->activityRepository = $entityManager->getRepository(Activity::class); - $this->parameterBagInterface = $parameterBagInterface; + $this->entityManagerInterface = $entityManagerInterface; + } + + public function findLastActivities(): array + { + return $this->entityManagerInterface + ->getRepository(Activity::class) + ->findBy( + [], + [ + 'id' => 'DESC' + ] + ); } - public function addEvent(int $userId, string $event, array $data): ?Activity + // User + public function addEventUserJoin( + int $userId, + int $added + ): ?Activity { $activity = new Activity(); - $activity->setEvent($event); - $activity->setUserId($userId); - $activity->setApproved($approved); - $activity->setAdded(time()); + $activity->setEvent( + Activity::EVENT_USER_ADD + ); - $this->entityManager->persist($activity); - $this->entityManager->flush(); + $activity->setUserId( + $userId + ); + + $activity->setAdded( + $added + ); + + $this->entityManagerInterface->persist($activity); + $this->entityManagerInterface->flush(); return $activity; } - public function findLast(bool $moderator): ?array + public function addEventUserStarAdd( + int $userId, + int $added, + int $userIdTarget + ): ?Activity { - if ($moderator) - { - return $this->activityRepository->findLast(); - } + $activity = new Activity(); + + $activity->setEvent( + Activity::EVENT_USER_STAR_DELETE + ); + + $activity->setUserId( + $userId + ); + + $activity->setAdded( + $added + ); + + $activity->setData( + [ + 'userId' => $userIdTarget + ] + ); + + $this->entityManagerInterface->persist($activity); + $this->entityManagerInterface->flush(); + + return $activity; + } + + public function addEventUserStarDelete( + int $userId, + int $added, + int $userIdTarget + ): ?Activity + { + $activity = new Activity(); + + $activity->setEvent( + Activity::EVENT_USER_STAR_DELETE + ); + + $activity->setUserId( + $userId + ); + + $activity->setAdded( + $added + ); + + $activity->setData( + [ + 'userId' => $userIdTarget + ] + ); + + $this->entityManagerInterface->persist($activity); + $this->entityManagerInterface->flush(); + + return $activity; + } + + // Torrent + + /// Torrent locales + public function addEventTorrentLocalesAdd( + int $userId, + int $torrentId, + int $added, + int $torrentLocalesId, + ): ?Activity + { + $activity = new Activity(); + + $activity->setEvent( + Activity::EVENT_TORRENT_LOCALES_ADD + ); - else - { - return $this->activityRepository->findLastByApprovedField(true); - } + $activity->setUserId( + $userId + ); + + $activity->setTorrentId( + $torrentId + ); + + $activity->setAdded( + $added + ); + + $activity->setData( + [ + 'torrentLocalesId' => $torrentLocalesId + ] + ); + + $this->entityManagerInterface->persist($activity); + $this->entityManagerInterface->flush(); + + return $activity; + } + + public function addEventTorrentLocalesDelete( + int $userId, + int $torrentId, + int $added, + int $torrentLocalesId, + ): ?Activity + { + $activity = new Activity(); + + $activity->setEvent( + Activity::EVENT_TORRENT_LOCALES_DELETE + ); + + $activity->setUserId( + $userId + ); + + $activity->setTorrentId( + $torrentId + ); + + $activity->setAdded( + $added + ); + + $activity->setData( + [ + 'torrentLocalesId' => $torrentLocalesId + ] + ); + + $this->entityManagerInterface->persist($activity); + $this->entityManagerInterface->flush(); + + return $activity; + } + + public function addEventTorrentLocalesApproveAdd( + int $userId, + int $torrentId, + int $added, + int $torrentLocalesId, + ): ?Activity + { + $activity = new Activity(); + + $activity->setEvent( + Activity::EVENT_TORRENT_LOCALES_APPROVE_ADD + ); + + $activity->setUserId( + $userId + ); + + $activity->setTorrentId( + $torrentId + ); + + $activity->setAdded( + $added + ); + + $activity->setData( + [ + 'torrentLocalesId' => $torrentLocalesId + ] + ); + + $this->entityManagerInterface->persist($activity); + $this->entityManagerInterface->flush(); + + return $activity; + } + + public function addEventTorrentLocalesApproveDelete( + int $userId, + int $torrentId, + int $added, + int $torrentLocalesId, + ): ?Activity + { + $activity = new Activity(); + + $activity->setEvent( + Activity::EVENT_TORRENT_LOCALES_APPROVE_DELETE + ); + + $activity->setUserId( + $userId + ); + + $activity->setTorrentId( + $torrentId + ); + + $activity->setAdded( + $added + ); + + $activity->setData( + [ + 'torrentLocalesId' => $torrentLocalesId + ] + ); + + $this->entityManagerInterface->persist($activity); + $this->entityManagerInterface->flush(); + + return $activity; + } + + /// Torrent sensitive + public function addEventTorrentSensitiveAdd( + int $userId, + int $torrentId, + int $added, + int $torrentSensitiveId, + ): ?Activity + { + $activity = new Activity(); + + $activity->setEvent( + Activity::EVENT_TORRENT_SENSITIVE_ADD + ); + + $activity->setUserId( + $userId + ); + + $activity->setTorrentId( + $torrentId + ); + + $activity->setAdded( + $added + ); + + $activity->setData( + [ + 'torrentSensitiveId' => $torrentSensitiveId + ] + ); + + $this->entityManagerInterface->persist($activity); + $this->entityManagerInterface->flush(); + + return $activity; + } + + public function addEventTorrentSensitiveDelete( + int $userId, + int $torrentId, + int $added, + int $torrentSensitiveId, + ): ?Activity + { + $activity = new Activity(); + + $activity->setEvent( + Activity::EVENT_TORRENT_SENSITIVE_DELETE + ); + + $activity->setUserId( + $userId + ); + + $activity->setTorrentId( + $torrentId + ); + + $activity->setAdded( + $added + ); + + $activity->setData( + [ + 'torrentSensitiveId' => $torrentSensitiveId + ] + ); + + $this->entityManagerInterface->persist($activity); + $this->entityManagerInterface->flush(); + + return $activity; + } + + public function addEventTorrentSensitiveApproveAdd( + int $userId, + int $torrentId, + int $added, + int $torrentSensitiveId, + ): ?Activity + { + $activity = new Activity(); + + $activity->setEvent( + Activity::EVENT_TORRENT_SENSITIVE_APPROVE_ADD + ); + + $activity->setUserId( + $userId + ); + + $activity->setTorrentId( + $torrentId + ); + + $activity->setAdded( + $added + ); + + $activity->setData( + [ + 'torrentSensitiveId' => $torrentSensitiveId + ] + ); + + $this->entityManagerInterface->persist($activity); + $this->entityManagerInterface->flush(); + + return $activity; + } + + public function addEventTorrentSensitiveApproveDelete( + int $userId, + int $torrentId, + int $added, + int $torrentSensitiveId, + ): ?Activity + { + $activity = new Activity(); + + $activity->setEvent( + Activity::EVENT_TORRENT_SENSITIVE_APPROVE_DELETE + ); + + $activity->setUserId( + $userId + ); + + $activity->setTorrentId( + $torrentId + ); + + $activity->setAdded( + $added + ); + + $activity->setData( + [ + 'torrentSensitiveId' => $torrentSensitiveId + ] + ); + + $this->entityManagerInterface->persist($activity); + $this->entityManagerInterface->flush(); + + return $activity; } } \ No newline at end of file diff --git a/src/Service/UserService.php b/src/Service/UserService.php index 3774a1c..ccbd341 100644 --- a/src/Service/UserService.php +++ b/src/Service/UserService.php @@ -84,6 +84,7 @@ class UserService $user->setApproved(true); $user->setModerator(true); $user->setSensitive(false); + $this->save($user); } @@ -151,12 +152,14 @@ class UserService int $userId, int $userIdTarget, int $added - ): void + ): bool { if ($userStar = $this->findUserStar($userId, $userIdTarget)) { $this->entityManagerInterface->remove($userStar); $this->entityManagerInterface->flush(); + + return false; } else @@ -169,6 +172,8 @@ class UserService $this->entityManagerInterface->persist($userStar); $this->entityManagerInterface->flush(); + + return true; } } diff --git a/templates/default/activity/event/torrent/add.html.twig b/templates/default/activity/event/torrent/add.html.twig new file mode 100644 index 0000000..201afff --- /dev/null +++ b/templates/default/activity/event/torrent/add.html.twig @@ -0,0 +1,12 @@ + + {{ 'identicon' | trans }} + + + {{ 'added torrent' | trans }} + + + {{ torrent.name }} + +
+ {{ added | format_ago }} +
\ No newline at end of file diff --git a/templates/default/activity/event/torrent/locales/add.html.twig b/templates/default/activity/event/torrent/locales/add.html.twig new file mode 100644 index 0000000..1403bea --- /dev/null +++ b/templates/default/activity/event/torrent/locales/add.html.twig @@ -0,0 +1,18 @@ + + {{ 'identicon' | trans }} + +{{ 'added locales edition' | trans }} +{% if torrent.locales.exist %} + + #{{ torrent.locales.id }} + +{% else %} + #{{ torrent.locales.id }} +{% endif %} +{{ 'for torrent' | trans }} + + {{ torrent.name }} + +
+ {{ added | format_ago }} +
\ No newline at end of file diff --git a/templates/default/activity/event/torrent/locales/approve/add.html.twig b/templates/default/activity/event/torrent/locales/approve/add.html.twig new file mode 100644 index 0000000..5272c62 --- /dev/null +++ b/templates/default/activity/event/torrent/locales/approve/add.html.twig @@ -0,0 +1,18 @@ + + {{ 'identicon' | trans }} + +{{ 'approved locales edition' | trans }} +{% if torrent.locales.exist %} + + #{{ torrent.locales.id }} + +{% else %} + #{{ torrent.locales.id }} +{% endif %} +{{ 'for torrent' | trans }} + + {{ torrent.name }} + +
+ {{ added | format_ago }} +
\ No newline at end of file diff --git a/templates/default/activity/event/torrent/locales/approve/delete.html.twig b/templates/default/activity/event/torrent/locales/approve/delete.html.twig new file mode 100644 index 0000000..0165dea --- /dev/null +++ b/templates/default/activity/event/torrent/locales/approve/delete.html.twig @@ -0,0 +1,18 @@ + + {{ 'identicon' | trans }} + +{{ 'disapproved locales edition' | trans }} +{% if torrent.locales.exist %} + + #{{ torrent.locales.id }} + +{% else %} + #{{ torrent.locales.id }} +{% endif %} +{{ 'for torrent' | trans }} + + {{ torrent.name }} + +
+ {{ added | format_ago }} +
\ No newline at end of file diff --git a/templates/default/activity/event/torrent/locales/delete.html.twig b/templates/default/activity/event/torrent/locales/delete.html.twig new file mode 100644 index 0000000..bd6e727 --- /dev/null +++ b/templates/default/activity/event/torrent/locales/delete.html.twig @@ -0,0 +1,18 @@ + + {{ 'identicon' | trans }} + +{{ 'deleted locales edition' | trans }} +{% if torrent.locales.exist %} + + #{{ torrent.locales.id }} + +{% else %} + #{{ torrent.locales.id }} +{% endif %} +{{ 'for torrent' | trans }} + + {{ torrent.name }} + +
+ {{ added | format_ago }} +
\ No newline at end of file diff --git a/templates/default/activity/event/torrent/sensitive/add.html.twig b/templates/default/activity/event/torrent/sensitive/add.html.twig new file mode 100644 index 0000000..cab5966 --- /dev/null +++ b/templates/default/activity/event/torrent/sensitive/add.html.twig @@ -0,0 +1,18 @@ + + {{ 'identicon' | trans }} + +{{ 'added sensitive edition' | trans }} +{% if torrent.sensitive.exist %} + + #{{ torrent.sensitive.id }} + +{% else %} + #{{ torrent.sensitive.id }} +{% endif %} +{{ 'for torrent' | trans }} + + {{ torrent.name }} + +
+ {{ added | format_ago }} +
\ No newline at end of file diff --git a/templates/default/activity/event/torrent/sensitive/approve/add.html.twig b/templates/default/activity/event/torrent/sensitive/approve/add.html.twig new file mode 100644 index 0000000..c7ac2cd --- /dev/null +++ b/templates/default/activity/event/torrent/sensitive/approve/add.html.twig @@ -0,0 +1,18 @@ + + {{ 'identicon' | trans }} + +{{ 'approved sensitive edition' | trans }} +{% if torrent.sensitive.exist %} + + #{{ torrent.sensitive.id }} + +{% else %} + #{{ torrent.sensitive.id }} +{% endif %} +{{ 'for torrent' | trans }} + + {{ torrent.name }} + +
+ {{ added | format_ago }} +
\ No newline at end of file diff --git a/templates/default/activity/event/torrent/sensitive/approve/delete.html.twig b/templates/default/activity/event/torrent/sensitive/approve/delete.html.twig new file mode 100644 index 0000000..4c9fdf9 --- /dev/null +++ b/templates/default/activity/event/torrent/sensitive/approve/delete.html.twig @@ -0,0 +1,18 @@ + + {{ 'identicon' | trans }} + +{{ 'disapproved sensitive edition' | trans }} +{% if torrent.sensitive.exist %} + + #{{ torrent.sensitive.id }} + +{% else %} + #{{ torrent.sensitive.id }} +{% endif %} +{{ 'for torrent' | trans }} + + {{ torrent.name }} + +
+ {{ added | format_ago }} +
\ No newline at end of file diff --git a/templates/default/activity/event/torrent/sensitive/delete.html.twig b/templates/default/activity/event/torrent/sensitive/delete.html.twig new file mode 100644 index 0000000..e8f8a17 --- /dev/null +++ b/templates/default/activity/event/torrent/sensitive/delete.html.twig @@ -0,0 +1,18 @@ + + {{ 'identicon' | trans }} + +{{ 'deleted sensitive edition' | trans }} +{% if torrent.sensitive.exist %} + + #{{ torrent.sensitive.id }} + +{% else %} + #{{ torrent.sensitive.id }} +{% endif %} +{{ 'for torrent' | trans }} + + {{ torrent.name }} + +
+ {{ added | format_ago }} +
\ No newline at end of file diff --git a/templates/default/activity/event/undefined.html.twig b/templates/default/activity/event/undefined.html.twig new file mode 100644 index 0000000..9278683 --- /dev/null +++ b/templates/default/activity/event/undefined.html.twig @@ -0,0 +1,9 @@ + + {{ 'identicon' | trans }} + + + {{ 'undefined event' | trans }} + +
+ {{ added | format_ago }} +
\ No newline at end of file diff --git a/templates/default/activity/event/user/add.html.twig b/templates/default/activity/event/user/add.html.twig new file mode 100644 index 0000000..133d725 --- /dev/null +++ b/templates/default/activity/event/user/add.html.twig @@ -0,0 +1,9 @@ + + {{ 'identicon' | trans }} + + + {{ 'joined' | trans }} {{ name }} + +
+ {{ added | format_ago }} +
\ No newline at end of file diff --git a/templates/default/activity/event/user/approve/add.html.twig b/templates/default/activity/event/user/approve/add.html.twig new file mode 100644 index 0000000..eaaf469 --- /dev/null +++ b/templates/default/activity/event/user/approve/add.html.twig @@ -0,0 +1,12 @@ + + {{ 'identicon' | trans }} + + + {{ 'approved by' | trans }} + + + {{ 'identicon' | trans }} + +
+ {{ added | format_ago }} +
\ No newline at end of file diff --git a/templates/default/activity/event/user/approve/delete.html.twig b/templates/default/activity/event/user/approve/delete.html.twig new file mode 100644 index 0000000..c1f3ff8 --- /dev/null +++ b/templates/default/activity/event/user/approve/delete.html.twig @@ -0,0 +1,12 @@ + + {{ 'identicon' | trans }} + + + {{ 'disapproved by' | trans }} + + + {{ 'identicon' | trans }} + +
+ {{ added | format_ago }} +
\ No newline at end of file diff --git a/templates/default/activity/event/user/moderator/add.html.twig b/templates/default/activity/event/user/moderator/add.html.twig new file mode 100644 index 0000000..f907b83 --- /dev/null +++ b/templates/default/activity/event/user/moderator/add.html.twig @@ -0,0 +1,12 @@ + + {{ 'identicon' | trans }} + + + {{ 'grant moderator permissions to' | trans }} + + + {{ 'identicon' | trans }} + +
+ {{ added | format_ago }} +
\ No newline at end of file diff --git a/templates/default/activity/event/user/moderator/delete.html.twig b/templates/default/activity/event/user/moderator/delete.html.twig new file mode 100644 index 0000000..f98c741 --- /dev/null +++ b/templates/default/activity/event/user/moderator/delete.html.twig @@ -0,0 +1,12 @@ + + {{ 'identicon' | trans }} + + + {{ 'remove moderator permissions from' | trans }} + + + {{ 'identicon' | trans }} + +
+ {{ added | format_ago }} +
\ No newline at end of file diff --git a/templates/default/activity/event/user/star/add.html.twig b/templates/default/activity/event/user/star/add.html.twig new file mode 100644 index 0000000..3834315 --- /dev/null +++ b/templates/default/activity/event/user/star/add.html.twig @@ -0,0 +1,12 @@ + + {{ 'identicon' | trans }} + + +{{ 'add star to' | trans }} + + + {{ 'identicon' | trans }} + +
+ {{ added | format_ago }} +
\ No newline at end of file diff --git a/templates/default/activity/event/user/star/delete.html.twig b/templates/default/activity/event/user/star/delete.html.twig new file mode 100644 index 0000000..8e7a96e --- /dev/null +++ b/templates/default/activity/event/user/star/delete.html.twig @@ -0,0 +1,12 @@ + + {{ 'identicon' | trans }} + + +{{ 'removed star from' | trans }} + + + {{ 'identicon' | trans }} + +
+ {{ added | format_ago }} +
\ No newline at end of file diff --git a/templates/default/activity/event/user/status/add.html.twig b/templates/default/activity/event/user/status/add.html.twig new file mode 100644 index 0000000..53c422a --- /dev/null +++ b/templates/default/activity/event/user/status/add.html.twig @@ -0,0 +1,12 @@ + + {{ 'identicon' | trans }} + + + {{ 'enable user' | trans }} + + + {{ 'identicon' | trans }} + +
+ {{ added | format_ago }} +
\ No newline at end of file diff --git a/templates/default/activity/event/user/status/delete.html.twig b/templates/default/activity/event/user/status/delete.html.twig new file mode 100644 index 0000000..60fca4d --- /dev/null +++ b/templates/default/activity/event/user/status/delete.html.twig @@ -0,0 +1,12 @@ + + {{ 'identicon' | trans }} + + + {{ 'diable user' | trans }} + + + {{ 'identicon' | trans }} + +
+ {{ added | format_ago }} +
\ No newline at end of file diff --git a/templates/default/user/dashboard.html.twig b/templates/default/user/dashboard.html.twig index 3ca31dd..189e3b1 100644 --- a/templates/default/user/dashboard.html.twig +++ b/templates/default/user/dashboard.html.twig @@ -1,23 +1,12 @@ {% extends 'default/layout.html.twig' %} -{% block title %}{{ 'Last activity'|trans }} - {{ name }}{% endblock %} +{% block title %}{{ 'Activity' | trans }} - {{ name }}{% endblock %} {% block main_content %} {% for activity in activities %}
-
-
- - {{ 'identicon'|trans }} - - {% if activity.type == 'join' %} - {{ 'joined'|trans }} {{ name }} - {% endif %} -
-
- {{ activity.added }} -
-
+ {{ render(controller( + 'App\\Controller\\ActivityController::template', + { activity : activity } + )) }}
{% endfor %} {% endblock %} \ No newline at end of file