From 0339ee9f23d1832d313d93fa48cae0c40f0cc8c7 Mon Sep 17 00:00:00 2001 From: ghost Date: Wed, 11 Oct 2023 04:04:02 +0300 Subject: [PATCH] add user moderation events #4 --- src/Controller/ActivityController.php | 2 +- src/Controller/UserController.php | 84 +++++++++-- src/Service/ActivityService.php | 195 ++++++++++++++++++++++++++ src/Service/UserService.php | 12 +- 4 files changed, 278 insertions(+), 15 deletions(-) diff --git a/src/Controller/ActivityController.php b/src/Controller/ActivityController.php index 2b0363a..a0da1ab 100644 --- a/src/Controller/ActivityController.php +++ b/src/Controller/ActivityController.php @@ -706,7 +706,7 @@ class ActivityController extends AbstractController break; - // Page + // @TODO Page default: diff --git a/src/Controller/UserController.php b/src/Controller/UserController.php index 0ce686a..061dec5 100644 --- a/src/Controller/UserController.php +++ b/src/Controller/UserController.php @@ -309,7 +309,8 @@ class UserController extends AbstractController public function toggleModerator( Request $request, TranslatorInterface $translator, - UserService $userService + UserService $userService, + ActivityService $activityService ): Response { // Init user @@ -331,10 +332,29 @@ class UserController extends AbstractController throw $this->createNotFoundException(); } - // Update - $userService->toggleUserModerator( + // Update user moderator + $value = $userService->toggleUserModerator( $userTarget->getId() - ); + )->isModerator(); + + // Add activity event + if ($value) + { + $activityService->addEventUserModeratorAdd( + $user->getId(), + time(), + $userTarget->getId() + ); + } + + else + { + $activityService->addEventUserModeratorDelete( + $user->getId(), + time(), + $userTarget->getId() + ); + } // Redirect to info article created return $this->redirectToRoute( @@ -361,7 +381,8 @@ class UserController extends AbstractController public function toggleStatus( Request $request, TranslatorInterface $translator, - UserService $userService + UserService $userService, + ActivityService $activityService ): Response { // Init user @@ -383,10 +404,29 @@ class UserController extends AbstractController throw $this->createNotFoundException(); } - // Update - $userService->toggleUserStatus( + // Update user status + $value = $userService->toggleUserStatus( $userTarget->getId() - ); + )->isStatus(); + + // Add activity event + if ($value) + { + $activityService->addEventUserStatusAdd( + $user->getId(), + time(), + $userTarget->getId() + ); + } + + else + { + $activityService->addEventUserStatusDelete( + $user->getId(), + time(), + $userTarget->getId() + ); + } // Redirect to info article created return $this->redirectToRoute( @@ -415,7 +455,8 @@ class UserController extends AbstractController TranslatorInterface $translator, UserService $userService, ArticleService $articleService, - TorrentService $torrentService + TorrentService $torrentService, + ActivityService $activityService ): Response { // Init user @@ -454,12 +495,33 @@ class UserController extends AbstractController $userTarget->getId(), true ); + + // @TODO make event for each item } // Update user approved - $userService->toggleUserApproved( + $value = $userService->toggleUserApproved( $userTarget->getId() - ); + )->isApproved(); + + // Add activity event + if ($value) + { + $activityService->addEventUserApproveAdd( + $user->getId(), + time(), + $userTarget->getId() + ); + } + + else + { + $activityService->addEventUserApproveDelete( + $user->getId(), + time(), + $userTarget->getId() + ); + } // Redirect to info article created return $this->redirectToRoute( diff --git a/src/Service/ActivityService.php b/src/Service/ActivityService.php index c81925c..44dd9bf 100644 --- a/src/Service/ActivityService.php +++ b/src/Service/ActivityService.php @@ -55,6 +55,201 @@ class ActivityService return $activity; } + /// User approved + public function addEventUserApproveAdd( + int $userId, + int $added, + int $userIdTarget, + ): ?Activity + { + $activity = new Activity(); + + $activity->setEvent( + Activity::EVENT_USER_APPROVE_ADD + ); + + $activity->setUserId( + $userId + ); + + $activity->setAdded( + $added + ); + + $activity->setData( + [ + 'userId' => $userIdTarget + ] + ); + + $this->entityManagerInterface->persist($activity); + $this->entityManagerInterface->flush(); + + return $activity; + } + + public function addEventUserApproveDelete( + int $userId, + int $added, + int $userIdTarget, + ): ?Activity + { + $activity = new Activity(); + + $activity->setEvent( + Activity::EVENT_USER_APPROVE_DELETE + ); + + $activity->setUserId( + $userId + ); + + $activity->setAdded( + $added + ); + + $activity->setData( + [ + 'userId' => $userIdTarget + ] + ); + + $this->entityManagerInterface->persist($activity); + $this->entityManagerInterface->flush(); + + return $activity; + } + + /// User status + public function addEventUserStatusAdd( + int $userId, + int $added, + int $userIdTarget, + ): ?Activity + { + $activity = new Activity(); + + $activity->setEvent( + Activity::EVENT_USER_STATUS_ADD + ); + + $activity->setUserId( + $userId + ); + + $activity->setAdded( + $added + ); + + $activity->setData( + [ + 'userId' => $userIdTarget + ] + ); + + $this->entityManagerInterface->persist($activity); + $this->entityManagerInterface->flush(); + + return $activity; + } + + public function addEventUserStatusDelete( + int $userId, + int $added, + int $userIdTarget, + ): ?Activity + { + $activity = new Activity(); + + $activity->setEvent( + Activity::EVENT_USER_STATUS_DELETE + ); + + $activity->setUserId( + $userId + ); + + $activity->setAdded( + $added + ); + + $activity->setData( + [ + 'userId' => $userIdTarget + ] + ); + + $this->entityManagerInterface->persist($activity); + $this->entityManagerInterface->flush(); + + return $activity; + } + + /// User moderator + public function addEventUserModeratorAdd( + int $userId, + int $added, + int $userIdTarget, + ): ?Activity + { + $activity = new Activity(); + + $activity->setEvent( + Activity::EVENT_USER_MODERATOR_ADD + ); + + $activity->setUserId( + $userId + ); + + $activity->setAdded( + $added + ); + + $activity->setData( + [ + 'userId' => $userIdTarget + ] + ); + + $this->entityManagerInterface->persist($activity); + $this->entityManagerInterface->flush(); + + return $activity; + } + + public function addEventUserModeratorDelete( + int $userId, + int $added, + int $userIdTarget, + ): ?Activity + { + $activity = new Activity(); + + $activity->setEvent( + Activity::EVENT_USER_MODERATOR_DELETE + ); + + $activity->setUserId( + $userId + ); + + $activity->setAdded( + $added + ); + + $activity->setData( + [ + 'userId' => $userIdTarget + ] + ); + + $this->entityManagerInterface->persist($activity); + $this->entityManagerInterface->flush(); + + return $activity; + } + /// User star public function addEventUserStarAdd( int $userId, diff --git a/src/Service/UserService.php b/src/Service/UserService.php index ccbd341..e845717 100644 --- a/src/Service/UserService.php +++ b/src/Service/UserService.php @@ -179,7 +179,7 @@ class UserService public function toggleUserModerator( int $userId - ): void + ): ?User { if ($user = $this->getUser($userId)) { @@ -190,11 +190,13 @@ class UserService $this->entityManagerInterface->persist($user); $this->entityManagerInterface->flush(); } + + return $user; } public function toggleUserStatus( int $userId - ): void + ): ?User { if ($user = $this->getUser($userId)) { @@ -205,11 +207,13 @@ class UserService $this->entityManagerInterface->persist($user); $this->entityManagerInterface->flush(); } + + return $user; } public function toggleUserApproved( int $userId - ): void + ): ?User { if ($user = $this->getUser($userId)) { @@ -220,5 +224,7 @@ class UserService $this->entityManagerInterface->persist($user); $this->entityManagerInterface->flush(); } + + return $user; } } \ No newline at end of file