fixup! Register an address book with recent contacts
Signed-off-by: Christoph Wurst <christoph@winzerhof-wurst.at>
This commit is contained in:
parent
da433080bb
commit
bdf783b7cd
7 changed files with 206 additions and 40 deletions
|
@ -10,6 +10,7 @@ return array(
|
|||
'OCA\\ContactsInteraction\\AddressBookProvider' => $baseDir . '/../lib/AddressBookProvider.php',
|
||||
'OCA\\ContactsInteraction\\AppInfo\\Application' => $baseDir . '/../lib/AppInfo/Application.php',
|
||||
'OCA\\ContactsInteraction\\Card' => $baseDir . '/../lib/Card.php',
|
||||
'OCA\\ContactsInteraction\\Db\\CardSearchDao' => $baseDir . '/../lib/Db/CardSearchDao.php',
|
||||
'OCA\\ContactsInteraction\\Db\\RecentContact' => $baseDir . '/../lib/Db/RecentContact.php',
|
||||
'OCA\\ContactsInteraction\\Db\\RecentContactMapper' => $baseDir . '/../lib/Db/RecentContactMapper.php',
|
||||
'OCA\\ContactsInteraction\\Listeners\\ContactInteractionListener' => $baseDir . '/../lib/Listeners/ContactInteractionListener.php',
|
||||
|
|
|
@ -25,6 +25,7 @@ class ComposerStaticInitContactsInteraction
|
|||
'OCA\\ContactsInteraction\\AddressBookProvider' => __DIR__ . '/..' . '/../lib/AddressBookProvider.php',
|
||||
'OCA\\ContactsInteraction\\AppInfo\\Application' => __DIR__ . '/..' . '/../lib/AppInfo/Application.php',
|
||||
'OCA\\ContactsInteraction\\Card' => __DIR__ . '/..' . '/../lib/Card.php',
|
||||
'OCA\\ContactsInteraction\\Db\\CardSearchDao' => __DIR__ . '/..' . '/../lib/Db/CardSearchDao.php',
|
||||
'OCA\\ContactsInteraction\\Db\\RecentContact' => __DIR__ . '/..' . '/../lib/Db/RecentContact.php',
|
||||
'OCA\\ContactsInteraction\\Db\\RecentContactMapper' => __DIR__ . '/..' . '/../lib/Db/RecentContactMapper.php',
|
||||
'OCA\\ContactsInteraction\\Listeners\\ContactInteractionListener' => __DIR__ . '/..' . '/../lib/Listeners/ContactInteractionListener.php',
|
||||
|
|
92
apps/contactsinteraction/lib/Db/CardSearchDao.php
Normal file
92
apps/contactsinteraction/lib/Db/CardSearchDao.php
Normal file
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
|
@ -26,9 +26,6 @@ declare(strict_types=1);
|
|||
namespace OCA\ContactsInteraction\Db;
|
||||
|
||||
use OCP\AppFramework\Db\Entity;
|
||||
use OCP\Contacts\Events\ContactInteractedWithEvent;
|
||||
use Sabre\VObject\Component\VCard;
|
||||
use Sabre\VObject\UUIDUtil;
|
||||
|
||||
/**
|
||||
* @method void setActorUid(string $uid)
|
||||
|
@ -73,37 +70,4 @@ class RecentContact extends Entity {
|
|||
$this->addType('lastContact', 'int');
|
||||
}
|
||||
|
||||
public static function fromEvent(ContactInteractedWithEvent $event, int $now): self {
|
||||
$contact = new self();
|
||||
$contact->setActorUid($event->getActor()->getUID());
|
||||
if ($event->getUid() !== null) {
|
||||
$contact->setUid($event->getUid());
|
||||
}
|
||||
if ($event->getEmail() !== null) {
|
||||
$contact->setEmail($event->getEmail());
|
||||
}
|
||||
if ($event->getFederatedCloudId() !== null) {
|
||||
$contact->setFederatedCloudId($event->getFederatedCloudId());
|
||||
}
|
||||
$contact->setLastContact($now);
|
||||
$contact->setCard($contact->generateCard());
|
||||
return $contact;
|
||||
}
|
||||
|
||||
private function generateCard(): string {
|
||||
$props = [
|
||||
'URI' => UUIDUtil::getUUID(),
|
||||
'FN' => $this->getEmail() ?? $this->getUid() ?? $this->getFederatedCloudId(),
|
||||
];
|
||||
|
||||
if ($this->getEmail() !== null) {
|
||||
$props['EMAIL'] = $this->getEmail();
|
||||
}
|
||||
if ($this->getFederatedCloudId() !== null) {
|
||||
$props['CLOUD'] = $this->getFederatedCloudId();
|
||||
}
|
||||
|
||||
return (new VCard($props))->serialize();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -28,6 +28,7 @@ namespace OCA\ContactsInteraction\Db;
|
|||
use OCP\AppFramework\Db\DoesNotExistException;
|
||||
use OCP\AppFramework\Db\QBMapper;
|
||||
use OCP\IDBConnection;
|
||||
use OCP\IUser;
|
||||
|
||||
class RecentContactMapper extends QBMapper {
|
||||
|
||||
|
@ -70,4 +71,38 @@ class RecentContactMapper extends QBMapper {
|
|||
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('uid', $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);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -25,6 +25,7 @@ declare(strict_types=1);
|
|||
|
||||
namespace OCA\ContactsInteraction\Listeners;
|
||||
|
||||
use OCA\ContactsInteraction\Db\CardSearchDao;
|
||||
use OCA\ContactsInteraction\Db\RecentContact;
|
||||
use OCA\ContactsInteraction\Db\RecentContactMapper;
|
||||
use OCP\AppFramework\Utility\ITimeFactory;
|
||||
|
@ -32,12 +33,17 @@ use OCP\Contacts\Events\ContactInteractedWithEvent;
|
|||
use OCP\EventDispatcher\Event;
|
||||
use OCP\EventDispatcher\IEventListener;
|
||||
use OCP\ILogger;
|
||||
use Sabre\VObject\Component\VCard;
|
||||
use Sabre\VObject\UUIDUtil;
|
||||
|
||||
class ContactInteractionListener implements IEventListener {
|
||||
|
||||
/** @var RecentContactMapper */
|
||||
private $mapper;
|
||||
|
||||
/** @var CardSearchDao */
|
||||
private $cardSearchDao;
|
||||
|
||||
/** @var ITimeFactory */
|
||||
private $timeFactory;
|
||||
|
||||
|
@ -45,9 +51,11 @@ class ContactInteractionListener implements IEventListener {
|
|||
private $logger;
|
||||
|
||||
public function __construct(RecentContactMapper $mapper,
|
||||
CardSearchDao $cardSearchDao,
|
||||
ITimeFactory $timeFactory,
|
||||
ILogger $logger) {
|
||||
$this->mapper = $mapper;
|
||||
$this->cardSearchDao = $cardSearchDao;
|
||||
$this->timeFactory = $timeFactory;
|
||||
$this->logger = $logger;
|
||||
}
|
||||
|
@ -62,9 +70,66 @@ class ContactInteractionListener implements IEventListener {
|
|||
return;
|
||||
}
|
||||
|
||||
// TODO: check if the there is an existing contact, then just copy the card and only
|
||||
// create a new card when this data is new
|
||||
$this->mapper->insert(RecentContact::fromEvent($event, $this->timeFactory->getTime()));
|
||||
$existing = $this->mapper->findMatch(
|
||||
$event->getActor(),
|
||||
$event->getUid(),
|
||||
$event->getEmail(),
|
||||
$event->getFederatedCloudId()
|
||||
);
|
||||
if (!empty($existing)) {
|
||||
$now = $this->timeFactory->getTime();
|
||||
foreach($existing as $c) {
|
||||
$c->setLastContact($now);
|
||||
$this->mapper->update($c);
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
$contact = new RecentContact();
|
||||
$contact->setActorUid($event->getActor()->getUID());
|
||||
if ($event->getUid() !== null) {
|
||||
$contact->setUid($event->getUid());
|
||||
}
|
||||
if ($event->getEmail() !== null) {
|
||||
$contact->setEmail($event->getEmail());
|
||||
}
|
||||
if ($event->getFederatedCloudId() !== null) {
|
||||
$contact->setFederatedCloudId($event->getFederatedCloudId());
|
||||
}
|
||||
$contact->setLastContact($this->timeFactory->getTime());
|
||||
|
||||
$copy = $this->cardSearchDao->findExisting(
|
||||
$event->getActor(),
|
||||
$event->getUid(),
|
||||
$event->getEmail(),
|
||||
$event->getFederatedCloudId()
|
||||
);
|
||||
if ($copy !== null) {
|
||||
$contact->setCard($copy);
|
||||
} else {
|
||||
$contact->setCard($this->generateCard($contact));
|
||||
}
|
||||
$this->mapper->insert($contact);
|
||||
}
|
||||
|
||||
private function generateCard(RecentContact $contact): string {
|
||||
$props = [
|
||||
'URI' => UUIDUtil::getUUID(),
|
||||
'FN' => $contact->getEmail() ?? $contact->getUid() ?? $contact->getFederatedCloudId(),
|
||||
];
|
||||
|
||||
if ($contact->getUid() !== null) {
|
||||
$props['X-NEXTCLOUD-UID'] = $contact->getUid();
|
||||
}
|
||||
if ($contact->getEmail() !== null) {
|
||||
$props['EMAIL'] = $contact->getEmail();
|
||||
}
|
||||
if ($contact->getFederatedCloudId() !== null) {
|
||||
$props['CLOUD'] = $contact->getFederatedCloudId();
|
||||
}
|
||||
|
||||
return (new VCard($props))->serialize();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -27,6 +27,8 @@ namespace OC\Core\Command\App;
|
|||
|
||||
use OC\Core\Command\Base;
|
||||
use OCP\App\IAppManager;
|
||||
use OCP\Contacts\Events\ContactInteractedWithEvent;
|
||||
use OCP\EventDispatcher\IEventDispatcher;
|
||||
use Stecman\Component\Symfony\Console\BashCompletion\CompletionContext;
|
||||
use Symfony\Component\Console\Input\InputInterface;
|
||||
use Symfony\Component\Console\Input\InputOption;
|
||||
|
@ -66,7 +68,7 @@ class ListApps extends Base {
|
|||
} else {
|
||||
$shippedFilter = null;
|
||||
}
|
||||
|
||||
|
||||
$apps = \OC_App::getAllApps();
|
||||
$enabledApps = $disabledApps = [];
|
||||
$versions = \OC_App::getAppVersions();
|
||||
|
@ -96,6 +98,12 @@ class ListApps extends Base {
|
|||
}
|
||||
|
||||
$this->writeAppList($input, $output, $apps);
|
||||
|
||||
/** @var IEventDispatcher $d */
|
||||
$d = \OC::$server->query(IEventDispatcher::class);
|
||||
$e = new ContactInteractedWithEvent(\OC::$server->getUserManager()->get('admin'));
|
||||
$e->setEmail('test@user.domain');
|
||||
$d->dispatchTyped($e);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
Loading…
Reference in a new issue