Browse Source

add user join event #4

main
ghost 1 year ago
parent
commit
28f21d09c6
  1. 44
      src/Controller/ArticleController.php
  2. 23
      src/Controller/SearchController.php
  3. 212
      src/Controller/TorrentController.php
  4. 146
      src/Controller/UserController.php
  5. 57
      src/Service/UserService.php

44
src/Controller/ArticleController.php

@ -30,13 +30,27 @@ class ArticleController extends AbstractController @@ -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 @@ -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())
{

23
src/Controller/SearchController.php

@ -11,6 +11,7 @@ use Symfony\Component\HttpFoundation\Request; @@ -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 @@ -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;

212
src/Controller/TorrentController.php

@ -33,12 +33,26 @@ class TorrentController extends AbstractController @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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())
{

146
src/Controller/UserController.php

@ -19,12 +19,27 @@ class UserController extends AbstractController @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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())
{

57
src/Service/UserService.php

@ -23,20 +23,18 @@ class UserService @@ -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 @@ -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 @@ -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 @@ -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,

Loading…
Cancel
Save