Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(contactsinteraction): Read, update or insert in DB transaction #37933

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,12 @@
use OCA\ContactsInteraction\Db\CardSearchDao;
use OCA\ContactsInteraction\Db\RecentContact;
use OCA\ContactsInteraction\Db\RecentContactMapper;
use OCP\AppFramework\Db\TTransactional;
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\Contacts\Events\ContactInteractedWithEvent;
use OCP\EventDispatcher\Event;
use OCP\EventDispatcher\IEventListener;
use OCP\IDBConnection;
use OCP\IL10N;
use OCP\IUserManager;
use Psr\Log\LoggerInterface;
Expand All @@ -41,22 +43,28 @@
use Throwable;

class ContactInteractionListener implements IEventListener {

use TTransactional;

private RecentContactMapper $mapper;
private CardSearchDao $cardSearchDao;
private IUserManager $userManager;
private IDBConnection $dbConnection;
private ITimeFactory $timeFactory;
private IL10N $l10n;
private LoggerInterface $logger;

public function __construct(RecentContactMapper $mapper,
CardSearchDao $cardSearchDao,
IUserManager $userManager,
IDBConnection $connection,
ITimeFactory $timeFactory,
IL10N $l10nFactory,
LoggerInterface $logger) {
$this->mapper = $mapper;
$this->cardSearchDao = $cardSearchDao;
$this->userManager = $userManager;
$this->dbConnection = $connection;
$this->timeFactory = $timeFactory;
$this->l10n = $l10nFactory;
$this->logger = $logger;
Expand All @@ -77,11 +85,15 @@ public function handle(Event $event): void {
return;
}

$this->atomic(function () use ($event) {
$uid = $event->getUid();
$email = $event->getEmail();
$federatedCloudId = $event->getFederatedCloudId();
$existing = $this->mapper->findMatch(
$event->getActor(),
$event->getUid(),
$event->getEmail(),
$event->getFederatedCloudId()
$uid,
$email,
$federatedCloudId
);
if (!empty($existing)) {
$now = $this->timeFactory->getTime();

Check notice

Code scanning / Psalm

DeprecatedMethod

The method OCP\AppFramework\Utility\ITimeFactory::getTime has been marked as deprecated
Expand All @@ -95,22 +107,22 @@ public function handle(Event $event): void {

$contact = new RecentContact();
$contact->setActorUid($event->getActor()->getUID());
if ($event->getUid() !== null) {
$contact->setUid($event->getUid());
if ($uid !== null) {
$contact->setUid($uid);
}
if ($event->getEmail() !== null) {
$contact->setEmail($event->getEmail());
if ($email !== null) {
$contact->setEmail($email);
}
if ($event->getFederatedCloudId() !== null) {
$contact->setFederatedCloudId($event->getFederatedCloudId());
if ($federatedCloudId !== null) {
$contact->setFederatedCloudId($federatedCloudId);
}
$contact->setLastContact($this->timeFactory->getTime());

Check notice

Code scanning / Psalm

DeprecatedMethod

The method OCP\AppFramework\Utility\ITimeFactory::getTime has been marked as deprecated

$copy = $this->cardSearchDao->findExisting(
$event->getActor(),
$event->getUid(),
$event->getEmail(),
$event->getFederatedCloudId()
$uid,
$email,
$federatedCloudId
);
if ($copy !== null) {
try {
Expand All @@ -129,6 +141,7 @@ public function handle(Event $event): void {
$contact->setCard($this->generateCard($contact));
}
$this->mapper->insert($contact);
}, $this->dbConnection);
}

private function getDisplayName(?string $uid): ?string {
Expand Down