Optimized API operations
Every operation updated user last login time, which was inefficient. Changed it to update only after logins from credentials or cookies.
This commit is contained in:
parent
a140e04ca3
commit
c33817e4ab
4 changed files with 20 additions and 8 deletions
2
TODO
2
TODO
|
@ -145,8 +145,6 @@ miscellaneous:
|
||||||
- I don't think browsing online logs is very necessary
|
- I don't think browsing online logs is very necessary
|
||||||
- add help, api documentation
|
- add help, api documentation
|
||||||
- add version on homepage that reads version from package.json and git hash
|
- add version on homepage that reads version from package.json and git hash
|
||||||
- make script faster, right now every request to backend takes 200ms
|
|
||||||
(current suspect: dependency injector and sqlite)
|
|
||||||
- add tool for migrating szurubooru database
|
- add tool for migrating szurubooru database
|
||||||
- endless pager should include information about page number
|
- endless pager should include information about page number
|
||||||
- add README - apache2 has good README:
|
- add README - apache2 has good README:
|
||||||
|
|
|
@ -44,9 +44,13 @@ App.Auth = function(_, jQuery, util, api, appState, promise) {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
function loginFromToken(token) {
|
function loginFromToken(token, isFromCookie) {
|
||||||
return promise.make(function(resolve, reject) {
|
return promise.make(function(resolve, reject) {
|
||||||
promise.wait(api.post('/login', {token: token}))
|
var fd = {
|
||||||
|
token: token,
|
||||||
|
isFromCookie: isFromCookie
|
||||||
|
};
|
||||||
|
promise.wait(api.post('/login', fd))
|
||||||
.then(function(response) {
|
.then(function(response) {
|
||||||
updateAppState(response);
|
updateAppState(response);
|
||||||
resolve(response);
|
resolve(response);
|
||||||
|
@ -89,7 +93,7 @@ App.Auth = function(_, jQuery, util, api, appState, promise) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
promise.wait(loginFromToken(authCookie))
|
promise.wait(loginFromToken(authCookie, true))
|
||||||
.then(function(response) {
|
.then(function(response) {
|
||||||
resolve();
|
resolve();
|
||||||
}).fail(function(response) {
|
}).fail(function(response) {
|
||||||
|
|
|
@ -4,6 +4,7 @@ namespace Szurubooru\Controllers;
|
||||||
final class AuthController extends AbstractController
|
final class AuthController extends AbstractController
|
||||||
{
|
{
|
||||||
private $authService;
|
private $authService;
|
||||||
|
private $userService;
|
||||||
private $tokenService;
|
private $tokenService;
|
||||||
private $privilegeService;
|
private $privilegeService;
|
||||||
private $inputReader;
|
private $inputReader;
|
||||||
|
@ -12,6 +13,7 @@ final class AuthController extends AbstractController
|
||||||
|
|
||||||
public function __construct(
|
public function __construct(
|
||||||
\Szurubooru\Services\AuthService $authService,
|
\Szurubooru\Services\AuthService $authService,
|
||||||
|
\Szurubooru\Services\UserService $userService,
|
||||||
\Szurubooru\Services\TokenService $tokenService,
|
\Szurubooru\Services\TokenService $tokenService,
|
||||||
\Szurubooru\Services\PrivilegeService $privilegeService,
|
\Szurubooru\Services\PrivilegeService $privilegeService,
|
||||||
\Szurubooru\Helpers\InputReader $inputReader,
|
\Szurubooru\Helpers\InputReader $inputReader,
|
||||||
|
@ -19,6 +21,7 @@ final class AuthController extends AbstractController
|
||||||
\Szurubooru\Controllers\ViewProxies\TokenViewProxy $tokenViewProxy)
|
\Szurubooru\Controllers\ViewProxies\TokenViewProxy $tokenViewProxy)
|
||||||
{
|
{
|
||||||
$this->authService = $authService;
|
$this->authService = $authService;
|
||||||
|
$this->userService = $userService;
|
||||||
$this->tokenService = $tokenService;
|
$this->tokenService = $tokenService;
|
||||||
$this->privilegeService = $privilegeService;
|
$this->privilegeService = $privilegeService;
|
||||||
$this->inputReader = $inputReader;
|
$this->inputReader = $inputReader;
|
||||||
|
@ -38,21 +41,30 @@ final class AuthController extends AbstractController
|
||||||
{
|
{
|
||||||
$formData = new \Szurubooru\FormData\LoginFormData($this->inputReader);
|
$formData = new \Szurubooru\FormData\LoginFormData($this->inputReader);
|
||||||
$this->authService->loginFromCredentials($formData);
|
$this->authService->loginFromCredentials($formData);
|
||||||
|
|
||||||
|
$user = $this->authService->getLoggedInUser();
|
||||||
|
$this->userService->updateUserLastLoginTime($user);
|
||||||
}
|
}
|
||||||
elseif (isset($this->inputReader->token))
|
elseif (isset($this->inputReader->token))
|
||||||
{
|
{
|
||||||
$token = $this->tokenService->getByName($this->inputReader->token);
|
$token = $this->tokenService->getByName($this->inputReader->token);
|
||||||
$this->authService->loginFromToken($token);
|
$this->authService->loginFromToken($token);
|
||||||
|
|
||||||
|
$user = $this->authService->getLoggedInUser();
|
||||||
|
$isFromCookie = boolval($this->inputReader->isFromCookie);
|
||||||
|
if ($isFromCookie)
|
||||||
|
$this->userService->updateUserLastLoginTime($user);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
$this->authService->loginAnonymous();
|
$this->authService->loginAnonymous();
|
||||||
|
$user = $this->authService->getLoggedInUser();
|
||||||
}
|
}
|
||||||
|
|
||||||
return
|
return
|
||||||
[
|
[
|
||||||
'token' => $this->tokenViewProxy->fromEntity($this->authService->getLoginToken()),
|
'token' => $this->tokenViewProxy->fromEntity($this->authService->getLoginToken()),
|
||||||
'user' => $this->userViewProxy->fromEntity($this->authService->getLoggedInUser()),
|
'user' => $this->userViewProxy->fromEntity($user),
|
||||||
'privileges' => $this->privilegeService->getCurrentPrivileges(),
|
'privileges' => $this->privilegeService->getCurrentPrivileges(),
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
|
@ -54,7 +54,6 @@ class AuthService
|
||||||
|
|
||||||
$this->loginToken = $this->createAndSaveLoginToken($user);
|
$this->loginToken = $this->createAndSaveLoginToken($user);
|
||||||
$this->loggedInUser = $user;
|
$this->loggedInUser = $user;
|
||||||
$this->userService->updateUserLastLoginTime($user);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function loginFromToken(\Szurubooru\Entities\Token $token)
|
public function loginFromToken(\Szurubooru\Entities\Token $token)
|
||||||
|
@ -67,7 +66,6 @@ class AuthService
|
||||||
|
|
||||||
$this->loginToken = $token;
|
$this->loginToken = $token;
|
||||||
$this->loggedInUser = $user;
|
$this->loggedInUser = $user;
|
||||||
$this->userService->updateUserLastLoginTime($this->loggedInUser);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getAnonymousUser()
|
public function getAnonymousUser()
|
||||||
|
|
Loading…
Reference in a new issue