Skip to content

Commit f01b033

Browse files
committed
Minor optimizations for saving user personal information
* Remove double hook: the OC_User::changeUser triggers an OC\AccountManager::userUpdated and the app is already listening to this signal in its Application definition * Make createCard not check if an card exists if we already checked previously. We also don't try to get the card if the user is disabled as we don't use the card in this case * Don't make AccountManager::deleteUser, delete the user twice We this change we go from 100 DB requests to 80 DB requests when saving an user email address. Signed-off-by: Carl Schwan <carl@carlschwan.eu> (cherry picked from commit c6fd482)
1 parent a145edd commit f01b033

5 files changed

Lines changed: 28 additions & 38 deletions

File tree

apps/dav/lib/CardDAV/CardDavBackend.php

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -650,21 +650,23 @@ public function getMultipleCards($addressBookId, array $uris) {
650650
* @param string $cardData
651651
* @return string
652652
*/
653-
public function createCard($addressBookId, $cardUri, $cardData) {
653+
public function createCard($addressBookId, $cardUri, $cardData, $checkAlreadyExists = true) {
654654
$etag = md5($cardData);
655655
$uid = $this->getUID($cardData);
656656

657-
$q = $this->db->getQueryBuilder();
658-
$q->select('uid')
659-
->from($this->dbCardsTable)
660-
->where($q->expr()->eq('addressbookid', $q->createNamedParameter($addressBookId)))
661-
->andWhere($q->expr()->eq('uid', $q->createNamedParameter($uid)))
662-
->setMaxResults(1);
663-
$result = $q->execute();
664-
$count = (bool)$result->fetchOne();
665-
$result->closeCursor();
666-
if ($count) {
667-
throw new \Sabre\DAV\Exception\BadRequest('VCard object with uid already exists in this addressbook collection.');
657+
if ($checkAlreadyExists) {
658+
$q = $this->db->getQueryBuilder();
659+
$q->select('uid')
660+
->from($this->dbCardsTable)
661+
->where($q->expr()->eq('addressbookid', $q->createNamedParameter($addressBookId)))
662+
->andWhere($q->expr()->eq('uid', $q->createNamedParameter($uid)))
663+
->setMaxResults(1);
664+
$result = $q->execute();
665+
$count = (bool)$result->fetchOne();
666+
$result->closeCursor();
667+
if ($count) {
668+
throw new \Sabre\DAV\Exception\BadRequest('VCard object with uid already exists in this addressbook collection.');
669+
}
668670
}
669671

670672
$query = $this->db->getQueryBuilder();

apps/dav/lib/CardDAV/SyncService.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -268,12 +268,12 @@ public function updateUser(IUser $user) {
268268
$userId = $user->getUID();
269269

270270
$cardId = "$name:$userId.vcf";
271-
$card = $this->backend->getCard($addressBookId, $cardId);
272271
if ($user->isEnabled()) {
272+
$card = $this->backend->getCard($addressBookId, $cardId);
273273
if ($card === false) {
274274
$vCard = $this->converter->createCardFromUser($user);
275275
if ($vCard !== null) {
276-
$this->backend->createCard($addressBookId, $cardId, $vCard->serialize());
276+
$this->backend->createCard($addressBookId, $cardId, $vCard->serialize(), false);
277277
}
278278
} else {
279279
$vCard = $this->converter->createCardFromUser($user);

apps/dav/lib/HookManager.php

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -104,10 +104,6 @@ public function setup() {
104104
$this->postDeleteUser(['uid' => $uid]);
105105
});
106106
\OC::$server->getUserManager()->listen('\OC\User', 'postUnassignedUserId', [$this, 'postUnassignedUserId']);
107-
Util::connectHook('OC_User',
108-
'changeUser',
109-
$this,
110-
'changeUser');
111107
}
112108

113109
public function postCreateUser($params) {
@@ -161,11 +157,6 @@ public function postUnassignedUserId($uid) {
161157
}
162158
}
163159

164-
public function changeUser($params) {
165-
$user = $params['user'];
166-
$this->syncService->updateUser($user);
167-
}
168-
169160
public function firstLogin(IUser $user = null) {
170161
if (!is_null($user)) {
171162
$principal = 'principals/users/' . $user->getUID();

lib/private/Accounts/AccountManager.php

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -263,12 +263,15 @@ protected function sanitizeWebsite(IAccountProperty $property, bool $throwOnData
263263
}
264264
}
265265

266-
protected function updateUser(IUser $user, array $data, bool $throwOnData = false): array {
267-
$oldUserData = $this->getUser($user, false);
266+
protected function updateUser(IUser $user, array $data, ?array $oldUserData, bool $throwOnData = false): array {
267+
if ($oldUserData === null) {
268+
$oldUserData = $this->getUser($user, false);
269+
}
270+
268271
$updated = true;
269272

270273
if ($oldUserData !== $data) {
271-
$this->updateExistingUser($user, $data);
274+
$this->updateExistingUser($user, $data, $oldUserData);
272275
} else {
273276
// nothing needs to be done if new and old data set are the same
274277
$updated = false;
@@ -595,12 +598,9 @@ protected function importFromJson(string $json, string $userId): ?array {
595598
}
596599

597600
/**
598-
* update existing user in accounts table
599-
*
600-
* @param IUser $user
601-
* @param array $data
601+
* Update existing user in accounts table
602602
*/
603-
protected function updateExistingUser(IUser $user, array $data): void {
603+
protected function updateExistingUser(IUser $user, array $data, array $oldData): void {
604604
$uid = $user->getUID();
605605
$jsonEncodedData = $this->prepareJson($data);
606606
$query = $this->connection->getQueryBuilder();
@@ -809,6 +809,6 @@ public function updateAccount(IAccount $account): void {
809809
];
810810
}
811811

812-
$this->updateUser($account->getUser(), $data, true);
812+
$this->updateUser($account->getUser(), $data, $oldData, true);
813813
}
814814
}

tests/lib/Accounts/AccountManagerTest.php

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -424,7 +424,7 @@ protected function populateOrUpdate(): void {
424424
],
425425
];
426426
foreach ($users as $userInfo) {
427-
$this->invokePrivate($this->accountManager, 'updateUser', [$userInfo['user'], $userInfo['data'], false]);
427+
$this->invokePrivate($this->accountManager, 'updateUser', [$userInfo['user'], $userInfo['data'], null, false]);
428428
}
429429
}
430430

@@ -466,9 +466,6 @@ public function testUpdateUser($newData, $oldData, $insertNew, $updateExisting)
466466
/** @var IUser $user */
467467
$user = $this->createMock(IUser::class);
468468

469-
// FIXME: should be an integration test instead of this abomination
470-
$accountManager->expects($this->once())->method('getUser')->with($user)->willReturn($oldData);
471-
472469
if ($updateExisting) {
473470
$accountManager->expects($this->once())->method('updateExistingUser')
474471
->with($user, $newData);
@@ -497,10 +494,10 @@ function ($eventName, $event) use ($user, $newData) {
497494
);
498495
}
499496

500-
$this->invokePrivate($accountManager, 'updateUser', [$user, $newData]);
497+
$this->invokePrivate($accountManager, 'updateUser', [$user, $newData, $oldData]);
501498
}
502499

503-
public function dataTrueFalse() {
500+
public function dataTrueFalse(): array {
504501
return [
505502
#$newData | $oldData | $insertNew | $updateExisting
506503
[['myProperty' => ['value' => 'newData']], ['myProperty' => ['value' => 'oldData']], false, true],

0 commit comments

Comments
 (0)