perf: Make fetching user details lazy
Signed-off-by: Julius Härtl <jus@bitgrid.net>
This commit is contained in:
@@ -144,8 +144,8 @@ class AssignmentMapper extends QBMapper implements IPermissionMapper {
|
|||||||
|
|
||||||
private function getOrigin(Assignment $assignment) {
|
private function getOrigin(Assignment $assignment) {
|
||||||
if ($assignment->getType() === Assignment::TYPE_USER) {
|
if ($assignment->getType() === Assignment::TYPE_USER) {
|
||||||
$origin = $this->userManager->get($assignment->getParticipant());
|
$origin = $this->userManager->userExists($assignment->getParticipant());
|
||||||
return $origin ? new User($origin) : null;
|
return $origin ? new User($assignment->getParticipant(), $this->userManager) : null;
|
||||||
}
|
}
|
||||||
if ($assignment->getType() === Assignment::TYPE_GROUP) {
|
if ($assignment->getType() === Assignment::TYPE_GROUP) {
|
||||||
$origin = $this->groupManager->get($assignment->getParticipant());
|
$origin = $this->groupManager->get($assignment->getParticipant());
|
||||||
|
|||||||
@@ -455,13 +455,11 @@ class BoardMapper extends QBMapper implements IPermissionMapper {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public function mapAcl(Acl &$acl) {
|
public function mapAcl(Acl &$acl) {
|
||||||
$userManager = $this->userManager;
|
|
||||||
$groupManager = $this->groupManager;
|
$groupManager = $this->groupManager;
|
||||||
$acl->resolveRelation('participant', function ($participant) use (&$acl, &$userManager, &$groupManager) {
|
$acl->resolveRelation('participant', function ($participant) use (&$acl, &$userManager, &$groupManager) {
|
||||||
if ($acl->getType() === Acl::PERMISSION_TYPE_USER) {
|
if ($acl->getType() === Acl::PERMISSION_TYPE_USER) {
|
||||||
$user = $userManager->get($participant);
|
if ($this->userManager->userExists($acl->getParticipant())) {
|
||||||
if ($user !== null) {
|
return new User($acl->getParticipant(), $this->userManager);
|
||||||
return new User($user);
|
|
||||||
}
|
}
|
||||||
$this->logger->debug('User ' . $acl->getId() . ' not found when mapping acl ' . $acl->getParticipant());
|
$this->logger->debug('User ' . $acl->getId() . ' not found when mapping acl ' . $acl->getParticipant());
|
||||||
return null;
|
return null;
|
||||||
@@ -499,9 +497,8 @@ class BoardMapper extends QBMapper implements IPermissionMapper {
|
|||||||
public function mapOwner(Board &$board) {
|
public function mapOwner(Board &$board) {
|
||||||
$userManager = $this->userManager;
|
$userManager = $this->userManager;
|
||||||
$board->resolveRelation('owner', function ($owner) use (&$userManager) {
|
$board->resolveRelation('owner', function ($owner) use (&$userManager) {
|
||||||
$user = $userManager->get($owner);
|
if ($this->userManager->userExists($owner)) {
|
||||||
if ($user !== null) {
|
return new User($owner, $userManager);
|
||||||
return new User($user);
|
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -607,9 +607,8 @@ class CardMapper extends QBMapper implements IPermissionMapper {
|
|||||||
public function mapOwner(Card &$card) {
|
public function mapOwner(Card &$card) {
|
||||||
$userManager = $this->userManager;
|
$userManager = $this->userManager;
|
||||||
$card->resolveRelation('owner', function ($owner) use (&$userManager) {
|
$card->resolveRelation('owner', function ($owner) use (&$userManager) {
|
||||||
$user = $userManager->get($owner);
|
if ($userManager->userExists($owner)) {
|
||||||
if ($user !== null) {
|
return new User($owner, $this->userManager);
|
||||||
return new User($user);
|
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -33,7 +33,7 @@ class RelationalObject implements JsonSerializable {
|
|||||||
* RelationalObject constructor.
|
* RelationalObject constructor.
|
||||||
*
|
*
|
||||||
* @param $primaryKey string
|
* @param $primaryKey string
|
||||||
* @param $object
|
* @param callable|mixed $object
|
||||||
*/
|
*/
|
||||||
public function __construct($primaryKey, $object) {
|
public function __construct($primaryKey, $object) {
|
||||||
$this->primaryKey = $primaryKey;
|
$this->primaryKey = $primaryKey;
|
||||||
@@ -47,16 +47,24 @@ class RelationalObject implements JsonSerializable {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function getObject() {
|
||||||
|
if (is_callable($this->object)) {
|
||||||
|
$this->object = call_user_func($this->object, $this);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this->object;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This method should be overwritten if object doesn't implement \JsonSerializable
|
* This method should be overwritten if object doesn't implement \JsonSerializable
|
||||||
*
|
*
|
||||||
* @throws \Exception
|
* @throws \Exception
|
||||||
*/
|
*/
|
||||||
public function getObjectSerialization() {
|
public function getObjectSerialization() {
|
||||||
if ($this->object instanceof JsonSerializable) {
|
if ($this->getObject() instanceof JsonSerializable) {
|
||||||
return $this->object->jsonSerialize();
|
return $this->getObject()->jsonSerialize();
|
||||||
} else {
|
} else {
|
||||||
throw new \Exception('jsonSerialize is not implemented on ' . get_class($this));
|
throw new \Exception('jsonSerialize is not implemented on ' . get_class($this->getObject()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -24,26 +24,31 @@
|
|||||||
namespace OCA\Deck\Db;
|
namespace OCA\Deck\Db;
|
||||||
|
|
||||||
use OCP\IUser;
|
use OCP\IUser;
|
||||||
|
use OCP\IUserManager;
|
||||||
|
|
||||||
class User extends RelationalObject {
|
class User extends RelationalObject {
|
||||||
public function __construct(IUser $user) {
|
|
||||||
$primaryKey = $user->getUID();
|
private IUserManager $userManager;
|
||||||
parent::__construct($primaryKey, $user);
|
public function __construct($uid, IUserManager $userManager) {
|
||||||
|
$this->userManager = $userManager;
|
||||||
|
parent::__construct($uid, function ($object) {
|
||||||
|
return $this->userManager->get($object->getPrimaryKey());
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getObjectSerialization() {
|
public function getObjectSerialization() {
|
||||||
return [
|
return [
|
||||||
'uid' => $this->object->getUID(),
|
'uid' => $this->getObject()->getUID(),
|
||||||
'displayname' => $this->object->getDisplayName(),
|
'displayname' => $this->getObject()->getDisplayName(),
|
||||||
'type' => 0
|
'type' => Acl::PERMISSION_TYPE_USER
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getUID() {
|
public function getUID() {
|
||||||
return $this->object->getUID();
|
return $this->getPrimaryKey();
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getDisplayName() {
|
public function getDisplayName() {
|
||||||
return $this->object->getDisplayName();
|
return $this->userManager->getDisplayName($this->getPrimaryKey());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -119,7 +119,7 @@ class CardService {
|
|||||||
public function enrichCards($cards) {
|
public function enrichCards($cards) {
|
||||||
$user = $this->userManager->get($this->currentUser);
|
$user = $this->userManager->get($this->currentUser);
|
||||||
|
|
||||||
$cardIds = array_map(function (Card $card) {
|
$cardIds = array_map(function (Card $card) use ($user) {
|
||||||
// Everything done in here might be heavy as it is executed for every card
|
// Everything done in here might be heavy as it is executed for every card
|
||||||
$cardId = $card->getId();
|
$cardId = $card->getId();
|
||||||
$this->cardMapper->mapOwner($card);
|
$this->cardMapper->mapOwner($card);
|
||||||
|
|||||||
@@ -260,22 +260,20 @@ class PermissionService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
$users = [];
|
$users = [];
|
||||||
$owner = $this->userManager->get($board->getOwner());
|
if (!$this->userManager->userExists($board->getOwner())) {
|
||||||
if ($owner === null) {
|
|
||||||
$this->logger->info('No owner found for board ' . $board->getId());
|
$this->logger->info('No owner found for board ' . $board->getId());
|
||||||
} else {
|
} else {
|
||||||
$users[$owner->getUID()] = new User($owner);
|
$users[$board->getOwner()] = new User($board->getOwner(), $this->userManager);
|
||||||
}
|
}
|
||||||
$acls = $this->aclMapper->findAll($boardId);
|
$acls = $this->aclMapper->findAll($boardId);
|
||||||
/** @var Acl $acl */
|
/** @var Acl $acl */
|
||||||
foreach ($acls as $acl) {
|
foreach ($acls as $acl) {
|
||||||
if ($acl->getType() === Acl::PERMISSION_TYPE_USER) {
|
if ($acl->getType() === Acl::PERMISSION_TYPE_USER) {
|
||||||
$user = $this->userManager->get($acl->getParticipant());
|
if (!$this->userManager->userExists($acl->getParticipant())) {
|
||||||
if ($user === null) {
|
|
||||||
$this->logger->info('No user found for acl rule ' . $acl->getId());
|
$this->logger->info('No user found for acl rule ' . $acl->getId());
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
$users[$user->getUID()] = new User($user);
|
$users[$acl->getParticipant()] = new User($acl->getParticipant(), $this->userManager);
|
||||||
}
|
}
|
||||||
if ($acl->getType() === Acl::PERMISSION_TYPE_GROUP) {
|
if ($acl->getType() === Acl::PERMISSION_TYPE_GROUP) {
|
||||||
$group = $this->groupManager->get($acl->getParticipant());
|
$group = $this->groupManager->get($acl->getParticipant());
|
||||||
@@ -284,7 +282,7 @@ class PermissionService {
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
foreach ($group->getUsers() as $user) {
|
foreach ($group->getUsers() as $user) {
|
||||||
$users[$user->getUID()] = new User($user);
|
$users[$user->getUID()] = new User($user->getUID(), $this->userManager);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -305,7 +303,7 @@ class PermissionService {
|
|||||||
if ($user === null) {
|
if ($user === null) {
|
||||||
$this->logger->info('No user found for circle member ' . $member->getUserId());
|
$this->logger->info('No user found for circle member ' . $member->getUserId());
|
||||||
} else {
|
} else {
|
||||||
$users[$member->getUserId()] = new User($user);
|
$users[$member->getUserId()] = new User($member->getUserId(), $this->userManager);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (\Exception $e) {
|
} catch (\Exception $e) {
|
||||||
|
|||||||
Reference in New Issue
Block a user