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) {
|
||||
if ($assignment->getType() === Assignment::TYPE_USER) {
|
||||
$origin = $this->userManager->get($assignment->getParticipant());
|
||||
return $origin ? new User($origin) : null;
|
||||
$origin = $this->userManager->userExists($assignment->getParticipant());
|
||||
return $origin ? new User($assignment->getParticipant(), $this->userManager) : null;
|
||||
}
|
||||
if ($assignment->getType() === Assignment::TYPE_GROUP) {
|
||||
$origin = $this->groupManager->get($assignment->getParticipant());
|
||||
|
||||
@@ -455,13 +455,11 @@ class BoardMapper extends QBMapper implements IPermissionMapper {
|
||||
}
|
||||
|
||||
public function mapAcl(Acl &$acl) {
|
||||
$userManager = $this->userManager;
|
||||
$groupManager = $this->groupManager;
|
||||
$acl->resolveRelation('participant', function ($participant) use (&$acl, &$userManager, &$groupManager) {
|
||||
if ($acl->getType() === Acl::PERMISSION_TYPE_USER) {
|
||||
$user = $userManager->get($participant);
|
||||
if ($user !== null) {
|
||||
return new User($user);
|
||||
if ($this->userManager->userExists($acl->getParticipant())) {
|
||||
return new User($acl->getParticipant(), $this->userManager);
|
||||
}
|
||||
$this->logger->debug('User ' . $acl->getId() . ' not found when mapping acl ' . $acl->getParticipant());
|
||||
return null;
|
||||
@@ -499,9 +497,8 @@ class BoardMapper extends QBMapper implements IPermissionMapper {
|
||||
public function mapOwner(Board &$board) {
|
||||
$userManager = $this->userManager;
|
||||
$board->resolveRelation('owner', function ($owner) use (&$userManager) {
|
||||
$user = $userManager->get($owner);
|
||||
if ($user !== null) {
|
||||
return new User($user);
|
||||
if ($this->userManager->userExists($owner)) {
|
||||
return new User($owner, $userManager);
|
||||
}
|
||||
return null;
|
||||
});
|
||||
|
||||
@@ -607,9 +607,8 @@ class CardMapper extends QBMapper implements IPermissionMapper {
|
||||
public function mapOwner(Card &$card) {
|
||||
$userManager = $this->userManager;
|
||||
$card->resolveRelation('owner', function ($owner) use (&$userManager) {
|
||||
$user = $userManager->get($owner);
|
||||
if ($user !== null) {
|
||||
return new User($user);
|
||||
if ($userManager->userExists($owner)) {
|
||||
return new User($owner, $this->userManager);
|
||||
}
|
||||
return null;
|
||||
});
|
||||
|
||||
@@ -33,7 +33,7 @@ class RelationalObject implements JsonSerializable {
|
||||
* RelationalObject constructor.
|
||||
*
|
||||
* @param $primaryKey string
|
||||
* @param $object
|
||||
* @param callable|mixed $object
|
||||
*/
|
||||
public function __construct($primaryKey, $object) {
|
||||
$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
|
||||
*
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function getObjectSerialization() {
|
||||
if ($this->object instanceof JsonSerializable) {
|
||||
return $this->object->jsonSerialize();
|
||||
if ($this->getObject() instanceof JsonSerializable) {
|
||||
return $this->getObject()->jsonSerialize();
|
||||
} 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;
|
||||
|
||||
use OCP\IUser;
|
||||
use OCP\IUserManager;
|
||||
|
||||
class User extends RelationalObject {
|
||||
public function __construct(IUser $user) {
|
||||
$primaryKey = $user->getUID();
|
||||
parent::__construct($primaryKey, $user);
|
||||
|
||||
private IUserManager $userManager;
|
||||
public function __construct($uid, IUserManager $userManager) {
|
||||
$this->userManager = $userManager;
|
||||
parent::__construct($uid, function ($object) {
|
||||
return $this->userManager->get($object->getPrimaryKey());
|
||||
});
|
||||
}
|
||||
|
||||
public function getObjectSerialization() {
|
||||
return [
|
||||
'uid' => $this->object->getUID(),
|
||||
'displayname' => $this->object->getDisplayName(),
|
||||
'type' => 0
|
||||
'uid' => $this->getObject()->getUID(),
|
||||
'displayname' => $this->getObject()->getDisplayName(),
|
||||
'type' => Acl::PERMISSION_TYPE_USER
|
||||
];
|
||||
}
|
||||
|
||||
public function getUID() {
|
||||
return $this->object->getUID();
|
||||
return $this->getPrimaryKey();
|
||||
}
|
||||
|
||||
public function getDisplayName() {
|
||||
return $this->object->getDisplayName();
|
||||
return $this->userManager->getDisplayName($this->getPrimaryKey());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -119,7 +119,7 @@ class CardService {
|
||||
public function enrichCards($cards) {
|
||||
$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
|
||||
$cardId = $card->getId();
|
||||
$this->cardMapper->mapOwner($card);
|
||||
|
||||
@@ -260,22 +260,20 @@ class PermissionService {
|
||||
}
|
||||
|
||||
$users = [];
|
||||
$owner = $this->userManager->get($board->getOwner());
|
||||
if ($owner === null) {
|
||||
if (!$this->userManager->userExists($board->getOwner())) {
|
||||
$this->logger->info('No owner found for board ' . $board->getId());
|
||||
} else {
|
||||
$users[$owner->getUID()] = new User($owner);
|
||||
$users[$board->getOwner()] = new User($board->getOwner(), $this->userManager);
|
||||
}
|
||||
$acls = $this->aclMapper->findAll($boardId);
|
||||
/** @var Acl $acl */
|
||||
foreach ($acls as $acl) {
|
||||
if ($acl->getType() === Acl::PERMISSION_TYPE_USER) {
|
||||
$user = $this->userManager->get($acl->getParticipant());
|
||||
if ($user === null) {
|
||||
if (!$this->userManager->userExists($acl->getParticipant())) {
|
||||
$this->logger->info('No user found for acl rule ' . $acl->getId());
|
||||
continue;
|
||||
}
|
||||
$users[$user->getUID()] = new User($user);
|
||||
$users[$acl->getParticipant()] = new User($acl->getParticipant(), $this->userManager);
|
||||
}
|
||||
if ($acl->getType() === Acl::PERMISSION_TYPE_GROUP) {
|
||||
$group = $this->groupManager->get($acl->getParticipant());
|
||||
@@ -284,7 +282,7 @@ class PermissionService {
|
||||
continue;
|
||||
}
|
||||
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) {
|
||||
$this->logger->info('No user found for circle member ' . $member->getUserId());
|
||||
} else {
|
||||
$users[$member->getUserId()] = new User($user);
|
||||
$users[$member->getUserId()] = new User($member->getUserId(), $this->userManager);
|
||||
}
|
||||
}
|
||||
} catch (\Exception $e) {
|
||||
|
||||
Reference in New Issue
Block a user