Skip to content

Commit 7e3d5d6

Browse files
Merge pull request #37933 from nextcloud/fix/contactsinteraction/transactional-read-update-insert
fix(contactsinteraction): Read, update or insert in DB transaction
2 parents 3aa3d7f + 48edc27 commit 7e3d5d6

1 file changed

Lines changed: 62 additions & 49 deletions

File tree

apps/contactsinteraction/lib/Listeners/ContactInteractionListener.php

Lines changed: 62 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,12 @@
2828
use OCA\ContactsInteraction\Db\CardSearchDao;
2929
use OCA\ContactsInteraction\Db\RecentContact;
3030
use OCA\ContactsInteraction\Db\RecentContactMapper;
31+
use OCP\AppFramework\Db\TTransactional;
3132
use OCP\AppFramework\Utility\ITimeFactory;
3233
use OCP\Contacts\Events\ContactInteractedWithEvent;
3334
use OCP\EventDispatcher\Event;
3435
use OCP\EventDispatcher\IEventListener;
36+
use OCP\IDBConnection;
3537
use OCP\IL10N;
3638
use OCP\IUserManager;
3739
use Psr\Log\LoggerInterface;
@@ -41,22 +43,28 @@
4143
use Throwable;
4244

4345
class ContactInteractionListener implements IEventListener {
46+
47+
use TTransactional;
48+
4449
private RecentContactMapper $mapper;
4550
private CardSearchDao $cardSearchDao;
4651
private IUserManager $userManager;
52+
private IDBConnection $dbConnection;
4753
private ITimeFactory $timeFactory;
4854
private IL10N $l10n;
4955
private LoggerInterface $logger;
5056

5157
public function __construct(RecentContactMapper $mapper,
5258
CardSearchDao $cardSearchDao,
5359
IUserManager $userManager,
60+
IDBConnection $connection,
5461
ITimeFactory $timeFactory,
5562
IL10N $l10nFactory,
5663
LoggerInterface $logger) {
5764
$this->mapper = $mapper;
5865
$this->cardSearchDao = $cardSearchDao;
5966
$this->userManager = $userManager;
67+
$this->dbConnection = $connection;
6068
$this->timeFactory = $timeFactory;
6169
$this->l10n = $l10nFactory;
6270
$this->logger = $logger;
@@ -77,58 +85,63 @@ public function handle(Event $event): void {
7785
return;
7886
}
7987

80-
$existing = $this->mapper->findMatch(
81-
$event->getActor(),
82-
$event->getUid(),
83-
$event->getEmail(),
84-
$event->getFederatedCloudId()
85-
);
86-
if (!empty($existing)) {
87-
$now = $this->timeFactory->getTime();
88-
foreach ($existing as $c) {
89-
$c->setLastContact($now);
90-
$this->mapper->update($c);
88+
$this->atomic(function () use ($event) {
89+
$uid = $event->getUid();
90+
$email = $event->getEmail();
91+
$federatedCloudId = $event->getFederatedCloudId();
92+
$existing = $this->mapper->findMatch(
93+
$event->getActor(),
94+
$uid,
95+
$email,
96+
$federatedCloudId
97+
);
98+
if (!empty($existing)) {
99+
$now = $this->timeFactory->getTime();
100+
foreach ($existing as $c) {
101+
$c->setLastContact($now);
102+
$this->mapper->update($c);
103+
}
104+
105+
return;
91106
}
92107

93-
return;
94-
}
95-
96-
$contact = new RecentContact();
97-
$contact->setActorUid($event->getActor()->getUID());
98-
if ($event->getUid() !== null) {
99-
$contact->setUid($event->getUid());
100-
}
101-
if ($event->getEmail() !== null) {
102-
$contact->setEmail($event->getEmail());
103-
}
104-
if ($event->getFederatedCloudId() !== null) {
105-
$contact->setFederatedCloudId($event->getFederatedCloudId());
106-
}
107-
$contact->setLastContact($this->timeFactory->getTime());
108-
109-
$copy = $this->cardSearchDao->findExisting(
110-
$event->getActor(),
111-
$event->getUid(),
112-
$event->getEmail(),
113-
$event->getFederatedCloudId()
114-
);
115-
if ($copy !== null) {
116-
try {
117-
$parsed = Reader::read($copy, Reader::OPTION_FORGIVING);
118-
$parsed->CATEGORIES = $this->l10n->t('Recently contacted');
119-
$contact->setCard($parsed->serialize());
120-
} catch (Throwable $e) {
121-
$this->logger->warning(
122-
'Could not parse card to add recent category: ' . $e->getMessage(),
123-
[
124-
'exception' => $e,
125-
]);
126-
$contact->setCard($copy);
108+
$contact = new RecentContact();
109+
$contact->setActorUid($event->getActor()->getUID());
110+
if ($uid !== null) {
111+
$contact->setUid($uid);
127112
}
128-
} else {
129-
$contact->setCard($this->generateCard($contact));
130-
}
131-
$this->mapper->insert($contact);
113+
if ($email !== null) {
114+
$contact->setEmail($email);
115+
}
116+
if ($federatedCloudId !== null) {
117+
$contact->setFederatedCloudId($federatedCloudId);
118+
}
119+
$contact->setLastContact($this->timeFactory->getTime());
120+
121+
$copy = $this->cardSearchDao->findExisting(
122+
$event->getActor(),
123+
$uid,
124+
$email,
125+
$federatedCloudId
126+
);
127+
if ($copy !== null) {
128+
try {
129+
$parsed = Reader::read($copy, Reader::OPTION_FORGIVING);
130+
$parsed->CATEGORIES = $this->l10n->t('Recently contacted');
131+
$contact->setCard($parsed->serialize());
132+
} catch (Throwable $e) {
133+
$this->logger->warning(
134+
'Could not parse card to add recent category: ' . $e->getMessage(),
135+
[
136+
'exception' => $e,
137+
]);
138+
$contact->setCard($copy);
139+
}
140+
} else {
141+
$contact->setCard($this->generateCard($contact));
142+
}
143+
$this->mapper->insert($contact);
144+
}, $this->dbConnection);
132145
}
133146

134147
private function getDisplayName(?string $uid): ?string {

0 commit comments

Comments
 (0)