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());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user