diff options
author | Christoph Wurst <christoph@winzerhof-wurst.at> | 2020-01-23 11:05:45 +0300 |
---|---|---|
committer | Christoph Wurst <christoph@winzerhof-wurst.at> | 2020-03-25 18:16:45 +0300 |
commit | 5a33cb0b78538e24b2753ef09f760f7b23408f29 (patch) | |
tree | 599b532e51eccb8c18315ca4913cba98aa211be6 /apps/contactsinteraction/lib/Db | |
parent | b3e194f38ecf2ec11aa5228937c4d6bdda28d5f0 (diff) |
Register an address book with recent contacts
Signed-off-by: Christoph Wurst <christoph@winzerhof-wurst.at>
Diffstat (limited to 'apps/contactsinteraction/lib/Db')
-rw-r--r-- | apps/contactsinteraction/lib/Db/CardSearchDao.php | 92 | ||||
-rw-r--r-- | apps/contactsinteraction/lib/Db/RecentContact.php | 73 | ||||
-rw-r--r-- | apps/contactsinteraction/lib/Db/RecentContactMapper.php | 118 |
3 files changed, 283 insertions, 0 deletions
diff --git a/apps/contactsinteraction/lib/Db/CardSearchDao.php b/apps/contactsinteraction/lib/Db/CardSearchDao.php new file mode 100644 index 00000000000..8370203bb9e --- /dev/null +++ b/apps/contactsinteraction/lib/Db/CardSearchDao.php @@ -0,0 +1,92 @@ +<?php + +declare(strict_types=1); + +/** + * @copyright 2020 Christoph Wurst <christoph@winzerhof-wurst.at> + * + * @author 2020 Christoph Wurst <christoph@winzerhof-wurst.at> + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +namespace OCA\ContactsInteraction\Db; + +use OCP\DB\QueryBuilder\IQueryBuilder; +use OCP\IDBConnection; +use OCP\IUser; + +class CardSearchDao { + + /** @var IDBConnection */ + private $db; + + public function __construct(IDBConnection $db) { + $this->db = $db; + } + + public function findExisting(IUser $user, + ?string $uid, + ?string $email, + ?string $cloudId): ?string { + $addressbooksQuery = $this->db->getQueryBuilder(); + $cardQuery = $this->db->getQueryBuilder(); + $propQuery = $this->db->getQueryBuilder(); + + $propOr = $propQuery->expr()->orX(); + if ($uid !== null) { + $propOr->add($propQuery->expr()->andX( + $propQuery->expr()->eq('name', $cardQuery->createNamedParameter('UID')), + $propQuery->expr()->eq('value', $cardQuery->createNamedParameter($uid)) + )); + } + if ($email !== null) { + $propOr->add($propQuery->expr()->andX( + $propQuery->expr()->eq('name', $cardQuery->createNamedParameter('EMAIL')), + $propQuery->expr()->eq('value', $cardQuery->createNamedParameter($email)) + )); + } + if ($cloudId !== null) { + $propOr->add($propQuery->expr()->andX( + $propQuery->expr()->eq('name', $cardQuery->createNamedParameter('CLOUD')), + $propQuery->expr()->eq('value', $cardQuery->createNamedParameter($cloudId)) + )); + } + $addressbooksQuery->selectDistinct('id') + ->from('addressbooks') + ->where($addressbooksQuery->expr()->eq('principaluri', $cardQuery->createNamedParameter("principals/users/" . $user->getUID()))); + $propQuery->selectDistinct('cardid') + ->from('cards_properties') + ->where($propQuery->expr()->in('addressbookid', $propQuery->createFunction($addressbooksQuery->getSQL()), IQueryBuilder::PARAM_INT_ARRAY)) + ->andWhere($propOr) + ->groupBy('cardid'); + $cardQuery->select('carddata') + ->from('cards') + ->where($cardQuery->expr()->in('id', $cardQuery->createFunction($propQuery->getSQL()), IQueryBuilder::PARAM_INT_ARRAY)) + ->andWhere($cardQuery->expr()->in('addressbookid', $cardQuery->createFunction($addressbooksQuery->getSQL()), IQueryBuilder::PARAM_INT_ARRAY)) + ->setMaxResults(1); + $result = $cardQuery->execute(); + /** @var string|false $card */ + $card = $result->fetchColumn(0); + + if ($card === false) { + return null; + } + + return $card; + } + +} diff --git a/apps/contactsinteraction/lib/Db/RecentContact.php b/apps/contactsinteraction/lib/Db/RecentContact.php new file mode 100644 index 00000000000..71b58353efb --- /dev/null +++ b/apps/contactsinteraction/lib/Db/RecentContact.php @@ -0,0 +1,73 @@ +<?php + +declare(strict_types=1); + +/** + * @copyright 2020 Christoph Wurst <christoph@winzerhof-wurst.at> + * + * @author 2020 Christoph Wurst <christoph@winzerhof-wurst.at> + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +namespace OCA\ContactsInteraction\Db; + +use OCP\AppFramework\Db\Entity; + +/** + * @method void setActorUid(string $uid) + * @method string|null getActorUid() + * @method void setUid(string $uid) + * @method string|null getUid() + * @method void setEmail(string $email) + * @method string|null getEmail() + * @method void setFederatedCloudId(string $federatedCloudId) + * @method string|null getFederatedCloudId() + * @method void setCard(string $card) + * @method string getCard() + * @method void setLastContact(int $lastContact) + * @method int getLastContact() + */ +class RecentContact extends Entity { + + /** @var string */ + protected $actorUid; + + /** @var string|null */ + protected $uid; + + /** @var string|null */ + protected $email; + + /** @var string|null */ + protected $federatedCloudId; + + /** @var string */ + protected $card; + + /** @var int */ + protected $lastContact; + + public function __construct() { + $this->addType('actorUid', 'string'); + $this->addType('uid', 'string'); + $this->addType('email', 'string'); + $this->addType('federatedCloudId', 'string'); + $this->addType('card', 'string'); + $this->addType('lastContact', 'int'); + } + +} diff --git a/apps/contactsinteraction/lib/Db/RecentContactMapper.php b/apps/contactsinteraction/lib/Db/RecentContactMapper.php new file mode 100644 index 00000000000..7fe98e6e4ec --- /dev/null +++ b/apps/contactsinteraction/lib/Db/RecentContactMapper.php @@ -0,0 +1,118 @@ +<?php + +declare(strict_types=1); + +/** + * @copyright 2020 Christoph Wurst <christoph@winzerhof-wurst.at> + * + * @author 2020 Christoph Wurst <christoph@winzerhof-wurst.at> + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +namespace OCA\ContactsInteraction\Db; + +use OCP\AppFramework\Db\DoesNotExistException; +use OCP\AppFramework\Db\QBMapper; +use OCP\IDBConnection; +use OCP\IUser; + +class RecentContactMapper extends QBMapper { + + public const TABLE_NAME = 'recent_contact'; + + public function __construct(IDBConnection $db) { + parent::__construct($db, self::TABLE_NAME); + } + + /** + * @return RecentContact[] + */ + public function findAll(string $uid): array { + $qb = $this->db->getQueryBuilder(); + + $select = $qb + ->select('*') + ->from($this->getTableName()) + ->where($qb->expr()->eq('actor_uid', $qb->createNamedParameter($uid))); + + return $this->findEntities($select); + } + + /** + * @param string $uid + * @param int $id + * + * @return RecentContact + * @throws DoesNotExistException + */ + public function find(string $uid, int $id): RecentContact { + $qb = $this->db->getQueryBuilder(); + + $select = $qb + ->select('*') + ->from($this->getTableName()) + ->where($qb->expr()->eq('id', $qb->createNamedParameter($id, $qb::PARAM_INT))) + ->andWhere($qb->expr()->eq('actor_uid', $qb->createNamedParameter($uid))); + + return $this->findEntity($select); + } + + /** + * @param IUser $user + * @param string|null $uid + * @param string|null $email + * @param string|null $cloudId + * + * @return RecentContact[] + */ + public function findMatch(IUser $user, + ?string $uid, + ?string $email, + ?string $cloudId): array { + $qb = $this->db->getQueryBuilder(); + + $or = $qb->expr()->orX(); + if ($uid !== null) { + $or->add($qb->expr()->eq('uid', $qb->createNamedParameter($uid))); + } + if ($email !== null) { + $or->add($qb->expr()->eq('email', $qb->createNamedParameter($email))); + } + if ($cloudId !== null) { + $or->add($qb->expr()->eq('federated_cloud_id', $qb->createNamedParameter($cloudId))); + } + + $select = $qb + ->select('*') + ->from($this->getTableName()) + ->where($or) + ->andWhere($qb->expr()->eq('actor_uid', $qb->createNamedParameter($user->getUID()))); + + return $this->findEntities($select); + } + + public function cleanUp(int $olderThan): void { + $qb = $this->db->getQueryBuilder(); + + $delete = $qb + ->delete($this->getTableName()) + ->where($qb->expr()->lt('last_contact', $qb->createNamedParameter($olderThan))); + + $delete->execute(); + } + +} |