Browse Source

add user join event #4

main
ghost 1 year ago
parent
commit
28f21d09c6
  1. 40
      src/Controller/ArticleController.php
  2. 21
      src/Controller/SearchController.php
  3. 190
      src/Controller/TorrentController.php
  4. 132
      src/Controller/UserController.php
  5. 57
      src/Service/UserService.php

40
src/Controller/ArticleController.php

@ -30,14 +30,28 @@ class ArticleController extends AbstractController
public function info( public function info(
Request $request, Request $request,
TranslatorInterface $translator, TranslatorInterface $translator,
UserService $userService UserService $userService,
ActivityService $activityService
): Response ): Response
{ {
// Init user // Init user
$user = $userService->init( if (!$user = $userService->findUserByAddress($request->getClientIp()))
$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', [ return $this->render('default/article/info.html.twig', [
'title' => 'test' 'title' => 'test'
]); ]);
@ -57,13 +71,27 @@ class ArticleController extends AbstractController
TranslatorInterface $translator, TranslatorInterface $translator,
UserService $userService, UserService $userService,
ArticleService $articleService, ArticleService $articleService,
ArticleService $torrentService ArticleService $torrentService,
ActivityService $activityService
): Response ): Response
{ {
// Init user // Init user
$user = $userService->init( if (!$user = $userService->findUserByAddress($request->getClientIp()))
$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()) if (!$user->isStatus())
{ {

21
src/Controller/SearchController.php

@ -11,6 +11,7 @@ use Symfony\Component\HttpFoundation\Request;
use App\Service\UserService; use App\Service\UserService;
use App\Service\ArticleService; use App\Service\ArticleService;
use App\Service\TorrentService; use App\Service\TorrentService;
use App\Service\ActivityService;
class SearchController extends AbstractController class SearchController extends AbstractController
{ {
@ -26,13 +27,27 @@ class SearchController extends AbstractController
Request $request, Request $request,
UserService $userService, UserService $userService,
ArticleService $articleService, ArticleService $articleService,
TorrentService $torrentService TorrentService $torrentService,
ActivityService $activityService
): Response ): Response
{ {
// Init user // Init user
$user = $userService->init( if (!$user = $userService->findUserByAddress($request->getClientIp()))
$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; $article = $request->query->get('article') ? (int) $request->query->get('article') : 1;

190
src/Controller/TorrentController.php

@ -33,12 +33,26 @@ class TorrentController extends AbstractController
TranslatorInterface $translator, TranslatorInterface $translator,
UserService $userService, UserService $userService,
TorrentService $torrentService, TorrentService $torrentService,
ActivityService $activityService
): Response ): Response
{ {
// Init user // Init user
$user = $userService->init( if (!$user = $userService->findUserByAddress($request->getClientIp()))
$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 // Init torrent
if (!$torrent = $torrentService->getTorrent($request->get('torrentId'))) if (!$torrent = $torrentService->getTorrent($request->get('torrentId')))
@ -184,10 +198,23 @@ class TorrentController extends AbstractController
): Response ): Response
{ {
// Init user // Init user
$user = $userService->init( if (!$user = $userService->findUserByAddress($request->getClientIp()))
$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()) if (!$user->isStatus())
{ {
// @TODO // @TODO
@ -345,9 +372,22 @@ class TorrentController extends AbstractController
): Response ): Response
{ {
// Init user // Init user
$user = $userService->init( if (!$user = $userService->findUserByAddress($request->getClientIp()))
$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()) if (!$user->isStatus())
{ {
@ -534,14 +574,27 @@ class TorrentController extends AbstractController
TranslatorInterface $translator, TranslatorInterface $translator,
UserService $userService, UserService $userService,
TorrentService $torrentService, TorrentService $torrentService,
ActivityService $activityService, ActivityService $activityService
): Response ): Response
{ {
// Init user // Init user
$user = $userService->init( if (!$user = $userService->findUserByAddress($request->getClientIp()))
$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 // Init torrent
if (!$torrent = $torrentService->getTorrent($request->get('torrentId'))) if (!$torrent = $torrentService->getTorrent($request->get('torrentId')))
{ {
@ -622,9 +675,22 @@ class TorrentController extends AbstractController
): Response ): Response
{ {
// Init user // Init user
$user = $userService->init( if (!$user = $userService->findUserByAddress($request->getClientIp()))
$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 // Init torrent
if (!$torrent = $torrentService->getTorrent($request->get('torrentId'))) if (!$torrent = $torrentService->getTorrent($request->get('torrentId')))
@ -699,10 +765,23 @@ class TorrentController extends AbstractController
): Response ): Response
{ {
// Init user // Init user
$user = $userService->init( if (!$user = $userService->findUserByAddress($request->getClientIp()))
$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()) if (!$user->isStatus())
{ {
// @TODO // @TODO
@ -862,9 +941,22 @@ class TorrentController extends AbstractController
): Response ): Response
{ {
// Init user // Init user
$user = $userService->init( if (!$user = $userService->findUserByAddress($request->getClientIp()))
$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 // Init torrent
if (!$torrent = $torrentService->getTorrent($request->get('torrentId'))) if (!$torrent = $torrentService->getTorrent($request->get('torrentId')))
@ -946,9 +1038,22 @@ class TorrentController extends AbstractController
): Response ): Response
{ {
// Init user // Init user
$user = $userService->init( if (!$user = $userService->findUserByAddress($request->getClientIp()))
$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 // Init torrent
if (!$torrent = $torrentService->getTorrent($request->get('torrentId'))) if (!$torrent = $torrentService->getTorrent($request->get('torrentId')))
@ -1017,10 +1122,23 @@ class TorrentController extends AbstractController
): Response ): Response
{ {
// Init user // Init user
$user = $userService->init( if (!$user = $userService->findUserByAddress($request->getClientIp()))
$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()) if (!$user->isStatus())
{ {
// @TODO // @TODO
@ -1093,10 +1211,23 @@ class TorrentController extends AbstractController
): Response ): Response
{ {
// Init user // Init user
$user = $userService->init( if (!$user = $userService->findUserByAddress($request->getClientIp()))
$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()) if (!$user->isStatus())
{ {
// @TODO // @TODO
@ -1196,10 +1327,23 @@ class TorrentController extends AbstractController
): Response ): Response
{ {
// Init user // Init user
$user = $userService->init( if (!$user = $userService->findUserByAddress($request->getClientIp()))
$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()) if (!$user->isStatus())
{ {
// @TODO // @TODO

132
src/Controller/UserController.php

@ -19,12 +19,27 @@ class UserController extends AbstractController
#[Route('/')] #[Route('/')]
public function root( public function root(
Request $request, Request $request,
UserService $userService UserService $userService,
ActivityService $activityService
): Response ): Response
{ {
$user = $userService->init( // Init user
$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->redirectToRoute( return $this->redirectToRoute(
'user_dashboard', 'user_dashboard',
@ -64,14 +79,28 @@ class UserController extends AbstractController
)] )]
public function profile( public function profile(
Request $request, Request $request,
UserService $userService UserService $userService,
ActivityService $activityService
): Response ): Response
{ {
// Init user // Init user
$user = $userService->init( if (!$user = $userService->findUserByAddress($request->getClientIp()))
$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 // Process post request
if ($request->isMethod('post')) if ($request->isMethod('post'))
{ {
@ -164,13 +193,28 @@ class UserController extends AbstractController
public function info( public function info(
Request $request, Request $request,
TranslatorInterface $translator, TranslatorInterface $translator,
UserService $userService): Response UserService $userService,
ActivityService $activityService
): Response
{ {
// Init user // Init user
$user = $userService->init( if (!$user = $userService->findUserByAddress($request->getClientIp()))
$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()) if (!$user->isStatus())
{ {
// @TODO // @TODO
@ -236,14 +280,27 @@ class UserController extends AbstractController
Request $request, Request $request,
TranslatorInterface $translator, TranslatorInterface $translator,
UserService $userService, UserService $userService,
ActivityService $activityService, ActivityService $activityService
): Response ): Response
{ {
// Init user // Init user
$user = $userService->init( if (!$user = $userService->findUserByAddress($request->getClientIp()))
$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()) if (!$user->isStatus())
{ {
// @TODO // @TODO
@ -314,9 +371,22 @@ class UserController extends AbstractController
): Response ): Response
{ {
// Init user // Init user
$user = $userService->init( if (!$user = $userService->findUserByAddress($request->getClientIp()))
$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()) if (!$user->isModerator())
{ {
@ -386,9 +456,22 @@ class UserController extends AbstractController
): Response ): Response
{ {
// Init user // Init user
$user = $userService->init( if (!$user = $userService->findUserByAddress($request->getClientIp()))
$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()) if (!$user->isModerator())
{ {
@ -460,10 +543,23 @@ class UserController extends AbstractController
): Response ): Response
{ {
// Init user // Init user
$user = $userService->init( if (!$user = $userService->findUserByAddress($request->getClientIp()))
$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()) if (!$user->isModerator())
{ {
// @TODO // @TODO

57
src/Service/UserService.php

@ -23,20 +23,18 @@ class UserService
$this->parameterBagInterface = $parameterBagInterface; $this->parameterBagInterface = $parameterBagInterface;
} }
public function init(string $address): User public function addUser(
{ string $address,
// Return existing user string $added,
if ($result = $this->entityManagerInterface string $locale,
->getRepository(User::class) array $locales,
->findOneBy( string $theme,
[ bool $sensitive = true,
'address' => $address bool $approved = false,
] bool $moderator = false,
)) bool $status = true
): ?User
{ {
return $result;
}
// Create new user // Create new user
$user = new User(); $user = new User();
@ -45,38 +43,39 @@ class UserService
); );
$user->setAdded( $user->setAdded(
time() $added
); );
$user->setApproved( $user->setApproved(
false $approved
); );
$user->setModerator( $user->setModerator(
false $moderator
); );
$user->setStatus( $user->setStatus(
true $status
); );
$user->setLocale( $user->setLocale(
$this->parameterBagInterface->get('app.locale') $locale
); );
$user->setLocales( $user->setLocales(
explode('|', $this->parameterBagInterface->get('app.locales')) $locales
); );
$user->setTheme( $user->setTheme(
$this->parameterBagInterface->get('app.theme') $theme
); );
$user->setSensitive( $user->setSensitive(
$this->parameterBagInterface->get('app.sensitive') $sensitive
); );
$this->save($user); $this->entityManagerInterface->persist($user);
$this->entityManagerInterface->flush();
// Set initial user as approved & moderator // Set initial user as approved & moderator
if (1 === $user->getId()) if (1 === $user->getId())
@ -85,7 +84,8 @@ class UserService
$user->setModerator(true); $user->setModerator(true);
$user->setSensitive(false); $user->setSensitive(false);
$this->save($user); $this->entityManagerInterface->persist($user);
$this->entityManagerInterface->flush();
} }
// Return user data // Return user data
@ -99,6 +99,17 @@ class UserService
->find($userId); ->find($userId);
} }
public function findUserByAddress(string $address): ?User
{
return $this->entityManagerInterface
->getRepository(User::class)
->findOneBy(
[
'address' => $address
]
);
}
public function identicon( public function identicon(
mixed $value, mixed $value,
int $size = 16, int $size = 16,

Loading…
Cancel
Save