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 @@ -30,14 +30,28 @@ 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())
{

21
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;

190
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,10 +198,23 @@ class TorrentController extends AbstractController @@ -184,10 +198,23 @@ 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())
{
// @TODO
@ -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,14 +574,27 @@ class TorrentController extends AbstractController @@ -534,14 +574,27 @@ 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,10 +765,23 @@ class TorrentController extends AbstractController @@ -699,10 +765,23 @@ 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())
{
// @TODO
@ -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,10 +1122,23 @@ class TorrentController extends AbstractController @@ -1017,10 +1122,23 @@ 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())
{
// @TODO
@ -1093,10 +1211,23 @@ class TorrentController extends AbstractController @@ -1093,10 +1211,23 @@ 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())
{
// @TODO
@ -1196,10 +1327,23 @@ class TorrentController extends AbstractController @@ -1196,10 +1327,23 @@ 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())
{
// @TODO

132
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,14 +79,28 @@ class UserController extends AbstractController @@ -64,14 +79,28 @@ 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,13 +193,28 @@ class UserController extends AbstractController @@ -164,13 +193,28 @@ 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())
{
// @TODO
@ -236,14 +280,27 @@ class UserController extends AbstractController @@ -236,14 +280,27 @@ 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())
{
// @TODO
@ -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,10 +543,23 @@ class UserController extends AbstractController @@ -460,10 +543,23 @@ 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())
{
// @TODO

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
{
// Return existing user
if ($result = $this->entityManagerInterface
->getRepository(User::class)
->findOneBy(
[
'address' => $address
]
))
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 $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