From 5a18d4533369113ba8d0dc6bfb3953dc2248d0ea Mon Sep 17 00:00:00 2001 From: Akhil Date: Wed, 6 Sep 2023 16:05:29 +0530 Subject: [PATCH 1/3] Use pdo addressbook --- lib/Db/WebmailMapper.php | 25 +++++++++---------------- 1 file changed, 9 insertions(+), 16 deletions(-) diff --git a/lib/Db/WebmailMapper.php b/lib/Db/WebmailMapper.php index 614d13ea..a322a1e8 100644 --- a/lib/Db/WebmailMapper.php +++ b/lib/Db/WebmailMapper.php @@ -13,6 +13,8 @@ use OCP\IUserManager; use OCP\IUser; use OCA\DAV\CardDAV\CardDavBackend; use OCP\DB\QueryBuilder\IQueryBuilder; +use OCA\SnappyMail\Util\SnappyMailHelper; + use Throwable; class WebmailMapper { @@ -78,21 +80,12 @@ class WebmailMapper { return $users; } - private function getUserContacts(string $uid) : array { - $qb = $this->conn->createQueryBuilder(); - - $qb->select('p.prop_value') - ->from('rainloop_ab_contacts', 'c') - ->where('c.id_user = :uid') - ->andWhere('p.prop_value IS NOT NULL') - ->setParameter('uid', $uid) - ->leftJoin('c', 'rainloop_ab_properties', 'p', 'p.id_contact = c.id_contact AND p.prop_type = 251'); + private function getUserContacts(string $email) : array { + SnappyMailHelper::loadApp(); + $pdoAddressBook = new \RainLoop\Providers\AddressBook\PdoAddressBook(); - $result = $qb->execute(); - $contacts = []; - while ($row = $result->fetch()) { - $contacts[] = Reader::readJson($row['prop_value']); - } + $pdoAddressBook->SetEmail($email); + $contacts = $pdoAddressBook->GetContacts(0, PHP_INT_MAX); return $contacts; } @@ -133,7 +126,7 @@ class WebmailMapper { $this->cardDavBackend->createCard( $addressBookId, UUIDUtil::getUUID() . '.vcf', - $contact->serialize(), + $contact->vCard->serialize(), true ); } catch (Throwable $e) { @@ -149,7 +142,7 @@ class WebmailMapper { if ($commandOutput) { $commandOutput->writeln('Migrating user ' . $userCount . ' with email: '. $user['email']); } - $contacts = $this->getUserContacts($user['id']); + $contacts = $this->getUserContacts($user['email']); $numberOfContacts = count($contacts); if ($commandOutput) { $commandOutput->writeln('Number of contacts for ' . $user['email'] . ':' . $numberOfContacts); -- GitLab From 9e8dd677a8cb55eba61d2b984aef555744138ee7 Mon Sep 17 00:00:00 2001 From: Akhil Date: Wed, 6 Sep 2023 18:14:04 +0530 Subject: [PATCH 2/3] Handle timestamp correctly --- lib/Command/MigrateWebmailAddressbooks.php | 10 ++--- lib/Db/WebmailMapper.php | 49 ++++++++++++---------- 2 files changed, 32 insertions(+), 27 deletions(-) diff --git a/lib/Command/MigrateWebmailAddressbooks.php b/lib/Command/MigrateWebmailAddressbooks.php index cce07a14..0ce63dee 100644 --- a/lib/Command/MigrateWebmailAddressbooks.php +++ b/lib/Command/MigrateWebmailAddressbooks.php @@ -74,7 +74,6 @@ class MigrateWebmailAddressbooks extends Command { * @return void */ private function migrateUsers(int $limit = 0, int $offset = 0, array $usernames = []) : void { - $users = []; if (!empty($usernames)) { $emails = []; foreach ($usernames as $username) { @@ -87,19 +86,18 @@ class MigrateWebmailAddressbooks extends Command { $email = $user->getEMailAddress(); $emails[] = $email; } - $users = $this->webmailMapper->getUsers($limit, $offset, $emails); - if (empty($users)) { + if (empty($emails)) { return; } - $this->webmailMapper->migrateContacts($users, $this->commandOutput); + $this->webmailMapper->migrateContacts($emails, $this->commandOutput); return; } - $users = $this->webmailMapper->getUsers($limit, $offset); + $emails = $this->webmailMapper->getEmails($limit, $offset); if ($limit === 0) { $this->commandOutput->writeln('Migrating all users starting at ' . $offset); } else { $this->commandOutput->writeln('Migrating ' . $limit . ' users starting at ' . $offset); } - $this->webmailMapper->migrateContacts($users, $this->commandOutput); + $this->webmailMapper->migrateContacts($emails, $this->commandOutput); } } diff --git a/lib/Db/WebmailMapper.php b/lib/Db/WebmailMapper.php index a322a1e8..d22dca8b 100644 --- a/lib/Db/WebmailMapper.php +++ b/lib/Db/WebmailMapper.php @@ -2,18 +2,19 @@ namespace OCA\EcloudAccounts\Db; +use DateTime; use OCP\IConfig; use OCP\ILogger; use Doctrine\DBAL\DriverManager; use Doctrine\DBAL\Connection; use OCA\EcloudAccounts\Exception\DbConnectionParamsException; use Sabre\VObject\UUIDUtil; -use \Sabre\VObject\Reader; use OCP\IUserManager; use OCP\IUser; use OCA\DAV\CardDAV\CardDavBackend; use OCP\DB\QueryBuilder\IQueryBuilder; use OCA\SnappyMail\Util\SnappyMailHelper; +use RainLoop\Providers\AddressBook\PdoAddressBook; use Throwable; @@ -54,9 +55,9 @@ class WebmailMapper { } - public function getUsers(int $limit = 0, int $offset = 0, array $emails = []) : array { + public function getEmails(int $limit = 0, int $offset = 0, array $emails = []) : array { $qb = $this->conn->createQueryBuilder(); - $qb->select('rl_email, id_user') + $qb->select('rl_email') ->from(self::USERS_TABLE, 'u') ->setFirstResult($offset); if ($limit > 0) { @@ -69,24 +70,18 @@ class WebmailMapper { $result = $qb->execute(); - $users = []; + $emails = []; while ($row = $result->fetch()) { - $user = [ - 'email' => $row['rl_email'], - 'id' => $row['id_user'] - ]; - $users[] = $user; + $users[] = (string) $row['rl_email']; } - return $users; + return $emails; } private function getUserContacts(string $email) : array { SnappyMailHelper::loadApp(); - $pdoAddressBook = new \RainLoop\Providers\AddressBook\PdoAddressBook(); - + $pdoAddressBook = new PdoAddressBook(); $pdoAddressBook->SetEmail($email); - $contacts = $pdoAddressBook->GetContacts(0, PHP_INT_MAX); - return $contacts; + return $pdoAddressBook->GetContacts(0, PHP_INT_MAX); } private function createCloudAddressBook(array $contacts, string $email) { @@ -122,7 +117,19 @@ class WebmailMapper { foreach ($contacts as $contact) { try { $contact->PRODID = '-//IDN murena.io//Migrated contact//EN'; - + if (isset($contact->vCard->REV)) { + try { + $timeString = $contact->vCard->REV[0]->getJsonValue()[0]; + $timestamp = strtotime($timeString); + } + catch (Throwable $e) { + // Do nothing + } + } + if (!isset($timestamp)) { + $timestamp = time(); + } + $contact->vCard->REV = \gmdate('Ymd\\THis\\Z', $timestamp); $this->cardDavBackend->createCard( $addressBookId, UUIDUtil::getUUID() . '.vcf', @@ -135,20 +142,20 @@ class WebmailMapper { } } - public function migrateContacts(array $users, $commandOutput = null) { + public function migrateContacts(array $emails, $commandOutput = null) { $userCount = 0; - foreach ($users as $user) { + foreach ($emails as $email) { $userCount += 1; if ($commandOutput) { - $commandOutput->writeln('Migrating user ' . $userCount . ' with email: '. $user['email']); + $commandOutput->writeln('Migrating user ' . $userCount . ' with email: '. $email); } - $contacts = $this->getUserContacts($user['email']); + $contacts = $this->getUserContacts($email); $numberOfContacts = count($contacts); if ($commandOutput) { - $commandOutput->writeln('Number of contacts for ' . $user['email'] . ':' . $numberOfContacts); + $commandOutput->writeln('Number of contacts for ' . $email . ':' . $numberOfContacts); } if ($numberOfContacts > 0) { - $this->createCloudAddressBook($contacts, $user['email']); + $this->createCloudAddressBook($contacts, $email); } } } -- GitLab From e09c03f6e13cc46fc6f12836244d68008301a683 Mon Sep 17 00:00:00 2001 From: Akhil Date: Wed, 6 Sep 2023 18:15:29 +0530 Subject: [PATCH 3/3] Lint PHP --- lib/Db/WebmailMapper.php | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/lib/Db/WebmailMapper.php b/lib/Db/WebmailMapper.php index d22dca8b..13c994e3 100644 --- a/lib/Db/WebmailMapper.php +++ b/lib/Db/WebmailMapper.php @@ -2,7 +2,6 @@ namespace OCA\EcloudAccounts\Db; -use DateTime; use OCP\IConfig; use OCP\ILogger; use Doctrine\DBAL\DriverManager; @@ -121,8 +120,7 @@ class WebmailMapper { try { $timeString = $contact->vCard->REV[0]->getJsonValue()[0]; $timestamp = strtotime($timeString); - } - catch (Throwable $e) { + } catch (Throwable $e) { // Do nothing } } -- GitLab