diff --git a/src/Controller/ArticleController.php b/src/Controller/ArticleController.php index 72f445c..daccc0e 100644 --- a/src/Controller/ArticleController.php +++ b/src/Controller/ArticleController.php @@ -30,13 +30,27 @@ class ArticleController extends AbstractController public function info( Request $request, TranslatorInterface $translator, - UserService $userService + UserService $userService, + ActivityService $activityService ): Response { // Init user - $user = $userService->init( - $request->getClientIp() - ); + if (!$user = $userService->findUserByAddress($request->getClientIp())) + { + $user = $userService->addUser( + $request->getClientIp(), + time(), + $this->getParameter('app.locale'), + explode('|', $this->getParameter('app.locales')), + $this->getParameter('app.theme') + ); + + // Add user join event + $activityService->addEventUserAdd( + $user->getId(), + time() + ); + } return $this->render('default/article/info.html.twig', [ 'title' => 'test' @@ -57,13 +71,27 @@ class ArticleController extends AbstractController TranslatorInterface $translator, UserService $userService, ArticleService $articleService, - ArticleService $torrentService + ArticleService $torrentService, + ActivityService $activityService ): Response { // Init user - $user = $userService->init( - $request->getClientIp() - ); + if (!$user = $userService->findUserByAddress($request->getClientIp())) + { + $user = $userService->addUser( + $request->getClientIp(), + time(), + $this->getParameter('app.locale'), + explode('|', $this->getParameter('app.locales')), + $this->getParameter('app.theme') + ); + + // Add user join event + $activityService->addEventUserAdd( + $user->getId(), + time() + ); + } if (!$user->isStatus()) { diff --git a/src/Controller/SearchController.php b/src/Controller/SearchController.php index 03e30da..f244e74 100644 --- a/src/Controller/SearchController.php +++ b/src/Controller/SearchController.php @@ -11,6 +11,7 @@ use Symfony\Component\HttpFoundation\Request; use App\Service\UserService; use App\Service\ArticleService; use App\Service\TorrentService; +use App\Service\ActivityService; class SearchController extends AbstractController { @@ -26,13 +27,27 @@ class SearchController extends AbstractController Request $request, UserService $userService, ArticleService $articleService, - TorrentService $torrentService + TorrentService $torrentService, + ActivityService $activityService ): Response { // Init user - $user = $userService->init( - $request->getClientIp() - ); + if (!$user = $userService->findUserByAddress($request->getClientIp())) + { + $user = $userService->addUser( + $request->getClientIp(), + time(), + $this->getParameter('app.locale'), + explode('|', $this->getParameter('app.locales')), + $this->getParameter('app.theme') + ); + + // Add user join event + $activityService->addEventUserAdd( + $user->getId(), + time() + ); + } $article = $request->query->get('article') ? (int) $request->query->get('article') : 1; diff --git a/src/Controller/TorrentController.php b/src/Controller/TorrentController.php index 8b262bc..c34ad1a 100644 --- a/src/Controller/TorrentController.php +++ b/src/Controller/TorrentController.php @@ -33,12 +33,26 @@ class TorrentController extends AbstractController TranslatorInterface $translator, UserService $userService, TorrentService $torrentService, + ActivityService $activityService ): Response { // Init user - $user = $userService->init( - $request->getClientIp() - ); + if (!$user = $userService->findUserByAddress($request->getClientIp())) + { + $user = $userService->addUser( + $request->getClientIp(), + time(), + $this->getParameter('app.locale'), + explode('|', $this->getParameter('app.locales')), + $this->getParameter('app.theme') + ); + + // Add user join event + $activityService->addEventUserAdd( + $user->getId(), + time() + ); + } // Init torrent if (!$torrent = $torrentService->getTorrent($request->get('torrentId'))) @@ -184,9 +198,22 @@ class TorrentController extends AbstractController ): Response { // Init user - $user = $userService->init( - $request->getClientIp() - ); + if (!$user = $userService->findUserByAddress($request->getClientIp())) + { + $user = $userService->addUser( + $request->getClientIp(), + time(), + $this->getParameter('app.locale'), + explode('|', $this->getParameter('app.locales')), + $this->getParameter('app.theme') + ); + + // Add user join event + $activityService->addEventUserAdd( + $user->getId(), + time() + ); + } if (!$user->isStatus()) { @@ -345,9 +372,22 @@ class TorrentController extends AbstractController ): Response { // Init user - $user = $userService->init( - $request->getClientIp() - ); + if (!$user = $userService->findUserByAddress($request->getClientIp())) + { + $user = $userService->addUser( + $request->getClientIp(), + time(), + $this->getParameter('app.locale'), + explode('|', $this->getParameter('app.locales')), + $this->getParameter('app.theme') + ); + + // Add user join event + $activityService->addEventUserAdd( + $user->getId(), + time() + ); + } if (!$user->isStatus()) { @@ -534,13 +574,26 @@ class TorrentController extends AbstractController TranslatorInterface $translator, UserService $userService, TorrentService $torrentService, - ActivityService $activityService, + ActivityService $activityService ): Response { // Init user - $user = $userService->init( - $request->getClientIp() - ); + if (!$user = $userService->findUserByAddress($request->getClientIp())) + { + $user = $userService->addUser( + $request->getClientIp(), + time(), + $this->getParameter('app.locale'), + explode('|', $this->getParameter('app.locales')), + $this->getParameter('app.theme') + ); + + // Add user join event + $activityService->addEventUserAdd( + $user->getId(), + time() + ); + } // Init torrent if (!$torrent = $torrentService->getTorrent($request->get('torrentId'))) @@ -622,9 +675,22 @@ class TorrentController extends AbstractController ): Response { // Init user - $user = $userService->init( - $request->getClientIp() - ); + if (!$user = $userService->findUserByAddress($request->getClientIp())) + { + $user = $userService->addUser( + $request->getClientIp(), + time(), + $this->getParameter('app.locale'), + explode('|', $this->getParameter('app.locales')), + $this->getParameter('app.theme') + ); + + // Add user join event + $activityService->addEventUserAdd( + $user->getId(), + time() + ); + } // Init torrent if (!$torrent = $torrentService->getTorrent($request->get('torrentId'))) @@ -699,9 +765,22 @@ class TorrentController extends AbstractController ): Response { // Init user - $user = $userService->init( - $request->getClientIp() - ); + if (!$user = $userService->findUserByAddress($request->getClientIp())) + { + $user = $userService->addUser( + $request->getClientIp(), + time(), + $this->getParameter('app.locale'), + explode('|', $this->getParameter('app.locales')), + $this->getParameter('app.theme') + ); + + // Add user join event + $activityService->addEventUserAdd( + $user->getId(), + time() + ); + } if (!$user->isStatus()) { @@ -862,9 +941,22 @@ class TorrentController extends AbstractController ): Response { // Init user - $user = $userService->init( - $request->getClientIp() - ); + if (!$user = $userService->findUserByAddress($request->getClientIp())) + { + $user = $userService->addUser( + $request->getClientIp(), + time(), + $this->getParameter('app.locale'), + explode('|', $this->getParameter('app.locales')), + $this->getParameter('app.theme') + ); + + // Add user join event + $activityService->addEventUserAdd( + $user->getId(), + time() + ); + } // Init torrent if (!$torrent = $torrentService->getTorrent($request->get('torrentId'))) @@ -946,9 +1038,22 @@ class TorrentController extends AbstractController ): Response { // Init user - $user = $userService->init( - $request->getClientIp() - ); + if (!$user = $userService->findUserByAddress($request->getClientIp())) + { + $user = $userService->addUser( + $request->getClientIp(), + time(), + $this->getParameter('app.locale'), + explode('|', $this->getParameter('app.locales')), + $this->getParameter('app.theme') + ); + + // Add user join event + $activityService->addEventUserAdd( + $user->getId(), + time() + ); + } // Init torrent if (!$torrent = $torrentService->getTorrent($request->get('torrentId'))) @@ -1017,9 +1122,22 @@ class TorrentController extends AbstractController ): Response { // Init user - $user = $userService->init( - $request->getClientIp() - ); + if (!$user = $userService->findUserByAddress($request->getClientIp())) + { + $user = $userService->addUser( + $request->getClientIp(), + time(), + $this->getParameter('app.locale'), + explode('|', $this->getParameter('app.locales')), + $this->getParameter('app.theme') + ); + + // Add user join event + $activityService->addEventUserAdd( + $user->getId(), + time() + ); + } if (!$user->isStatus()) { @@ -1093,9 +1211,22 @@ class TorrentController extends AbstractController ): Response { // Init user - $user = $userService->init( - $request->getClientIp() - ); + if (!$user = $userService->findUserByAddress($request->getClientIp())) + { + $user = $userService->addUser( + $request->getClientIp(), + time(), + $this->getParameter('app.locale'), + explode('|', $this->getParameter('app.locales')), + $this->getParameter('app.theme') + ); + + // Add user join event + $activityService->addEventUserAdd( + $user->getId(), + time() + ); + } if (!$user->isStatus()) { @@ -1196,9 +1327,22 @@ class TorrentController extends AbstractController ): Response { // Init user - $user = $userService->init( - $request->getClientIp() - ); + if (!$user = $userService->findUserByAddress($request->getClientIp())) + { + $user = $userService->addUser( + $request->getClientIp(), + time(), + $this->getParameter('app.locale'), + explode('|', $this->getParameter('app.locales')), + $this->getParameter('app.theme') + ); + + // Add user join event + $activityService->addEventUserAdd( + $user->getId(), + time() + ); + } if (!$user->isStatus()) { diff --git a/src/Controller/UserController.php b/src/Controller/UserController.php index 061dec5..c45c890 100644 --- a/src/Controller/UserController.php +++ b/src/Controller/UserController.php @@ -19,12 +19,27 @@ class UserController extends AbstractController #[Route('/')] public function root( Request $request, - UserService $userService + UserService $userService, + ActivityService $activityService ): Response { - $user = $userService->init( - $request->getClientIp() - ); + // Init user + if (!$user = $userService->findUserByAddress($request->getClientIp())) + { + $user = $userService->addUser( + $request->getClientIp(), + time(), + $this->getParameter('app.locale'), + explode('|', $this->getParameter('app.locales')), + $this->getParameter('app.theme') + ); + + // Add user join event + $activityService->addEventUserAdd( + $user->getId(), + time() + ); + } return $this->redirectToRoute( 'user_dashboard', @@ -64,13 +79,27 @@ class UserController extends AbstractController )] public function profile( Request $request, - UserService $userService + UserService $userService, + ActivityService $activityService ): Response { // Init user - $user = $userService->init( - $request->getClientIp() - ); + if (!$user = $userService->findUserByAddress($request->getClientIp())) + { + $user = $userService->addUser( + $request->getClientIp(), + time(), + $this->getParameter('app.locale'), + explode('|', $this->getParameter('app.locales')), + $this->getParameter('app.theme') + ); + + // Add user join event + $activityService->addEventUserAdd( + $user->getId(), + time() + ); + } // Process post request if ($request->isMethod('post')) @@ -164,12 +193,27 @@ class UserController extends AbstractController public function info( Request $request, TranslatorInterface $translator, - UserService $userService): Response + UserService $userService, + ActivityService $activityService + ): Response { // Init user - $user = $userService->init( - $request->getClientIp() - ); + if (!$user = $userService->findUserByAddress($request->getClientIp())) + { + $user = $userService->addUser( + $request->getClientIp(), + time(), + $this->getParameter('app.locale'), + explode('|', $this->getParameter('app.locales')), + $this->getParameter('app.theme') + ); + + // Add user join event + $activityService->addEventUserAdd( + $user->getId(), + time() + ); + } if (!$user->isStatus()) { @@ -236,13 +280,26 @@ class UserController extends AbstractController Request $request, TranslatorInterface $translator, UserService $userService, - ActivityService $activityService, + ActivityService $activityService ): Response { // Init user - $user = $userService->init( - $request->getClientIp() - ); + if (!$user = $userService->findUserByAddress($request->getClientIp())) + { + $user = $userService->addUser( + $request->getClientIp(), + time(), + $this->getParameter('app.locale'), + explode('|', $this->getParameter('app.locales')), + $this->getParameter('app.theme') + ); + + // Add user join event + $activityService->addEventUserAdd( + $user->getId(), + time() + ); + } if (!$user->isStatus()) { @@ -314,9 +371,22 @@ class UserController extends AbstractController ): Response { // Init user - $user = $userService->init( - $request->getClientIp() - ); + if (!$user = $userService->findUserByAddress($request->getClientIp())) + { + $user = $userService->addUser( + $request->getClientIp(), + time(), + $this->getParameter('app.locale'), + explode('|', $this->getParameter('app.locales')), + $this->getParameter('app.theme') + ); + + // Add user join event + $activityService->addEventUserAdd( + $user->getId(), + time() + ); + } if (!$user->isModerator()) { @@ -386,9 +456,22 @@ class UserController extends AbstractController ): Response { // Init user - $user = $userService->init( - $request->getClientIp() - ); + if (!$user = $userService->findUserByAddress($request->getClientIp())) + { + $user = $userService->addUser( + $request->getClientIp(), + time(), + $this->getParameter('app.locale'), + explode('|', $this->getParameter('app.locales')), + $this->getParameter('app.theme') + ); + + // Add user join event + $activityService->addEventUserAdd( + $user->getId(), + time() + ); + } if (!$user->isModerator()) { @@ -460,9 +543,22 @@ class UserController extends AbstractController ): Response { // Init user - $user = $userService->init( - $request->getClientIp() - ); + if (!$user = $userService->findUserByAddress($request->getClientIp())) + { + $user = $userService->addUser( + $request->getClientIp(), + time(), + $this->getParameter('app.locale'), + explode('|', $this->getParameter('app.locales')), + $this->getParameter('app.theme') + ); + + // Add user join event + $activityService->addEventUserAdd( + $user->getId(), + time() + ); + } if (!$user->isModerator()) { diff --git a/src/Service/UserService.php b/src/Service/UserService.php index e845717..179f358 100644 --- a/src/Service/UserService.php +++ b/src/Service/UserService.php @@ -23,20 +23,18 @@ class UserService $this->parameterBagInterface = $parameterBagInterface; } - public function init(string $address): User + public function addUser( + string $address, + string $added, + string $locale, + array $locales, + string $theme, + bool $sensitive = true, + bool $approved = false, + bool $moderator = false, + bool $status = true + ): ?User { - // Return existing user - if ($result = $this->entityManagerInterface - ->getRepository(User::class) - ->findOneBy( - [ - 'address' => $address - ] - )) - { - return $result; - } - // Create new user $user = new User(); @@ -45,38 +43,39 @@ class UserService ); $user->setAdded( - time() + $added ); $user->setApproved( - false + $approved ); $user->setModerator( - false + $moderator ); $user->setStatus( - true + $status ); $user->setLocale( - $this->parameterBagInterface->get('app.locale') + $locale ); $user->setLocales( - explode('|', $this->parameterBagInterface->get('app.locales')) + $locales ); $user->setTheme( - $this->parameterBagInterface->get('app.theme') + $theme ); $user->setSensitive( - $this->parameterBagInterface->get('app.sensitive') + $sensitive ); - $this->save($user); + $this->entityManagerInterface->persist($user); + $this->entityManagerInterface->flush(); // Set initial user as approved & moderator if (1 === $user->getId()) @@ -85,7 +84,8 @@ class UserService $user->setModerator(true); $user->setSensitive(false); - $this->save($user); + $this->entityManagerInterface->persist($user); + $this->entityManagerInterface->flush(); } // Return user data @@ -99,6 +99,17 @@ class UserService ->find($userId); } + public function findUserByAddress(string $address): ?User + { + return $this->entityManagerInterface + ->getRepository(User::class) + ->findOneBy( + [ + 'address' => $address + ] + ); + } + public function identicon( mixed $value, int $size = 16,