|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/** |
| 6 | + * SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors |
| 7 | + * SPDX-License-Identifier: AGPL-3.0-or-later |
| 8 | + */ |
| 9 | + |
| 10 | +namespace OCA\Mail\BackgroundJob; |
| 11 | + |
| 12 | +use OCP\AppFramework\Utility\ITimeFactory; |
| 13 | +use OCP\BackgroundJob\IJobList; |
| 14 | +use OCP\BackgroundJob\TimedJob; |
| 15 | +use OCP\DB\QueryBuilder\IQueryBuilder; |
| 16 | +use OCP\IDBConnection; |
| 17 | + |
| 18 | +class RepairRecipients extends TimedJob { |
| 19 | + |
| 20 | + public function __construct( |
| 21 | + protected ITimeFactory $time, |
| 22 | + private IDBConnection $db, |
| 23 | + private IJobList $jobService, |
| 24 | + ) { |
| 25 | + parent::__construct($time); |
| 26 | + $this->setInterval(300); |
| 27 | + } |
| 28 | + |
| 29 | + protected function run($argument): void { |
| 30 | + // fetch all quoted emails |
| 31 | + $select = $this->db->getQueryBuilder(); |
| 32 | + $select->select('id', 'email') |
| 33 | + ->from('mail_recipients') |
| 34 | + ->where( |
| 35 | + $select->expr()->like('email', $select->createNamedParameter('\'%\'', IQueryBuilder::PARAM_STR)) |
| 36 | + ) |
| 37 | + ->setMaxResults(1000); |
| 38 | + $recipients = $select->executeQuery()->fetchAll(); |
| 39 | + // update emails |
| 40 | + $update = $this->db->getQueryBuilder(); |
| 41 | + $update->update('mail_recipients') |
| 42 | + ->set('email', $update->createParameter('email')) |
| 43 | + ->where($update->expr()->in('id', $update->createParameter('id'), IQueryBuilder::PARAM_STR)); |
| 44 | + foreach ($recipients as $recipient) { |
| 45 | + $id = $recipient['id']; |
| 46 | + $email = $recipient['email']; |
| 47 | + $email = trim(str_replace('\'', '', (string)$email)); |
| 48 | + $update->setParameter('id', $id, IQueryBuilder::PARAM_STR); |
| 49 | + $update->setParameter('email', $email, IQueryBuilder::PARAM_STR); |
| 50 | + $update->executeStatement(); |
| 51 | + } |
| 52 | + // remove job depending on the result |
| 53 | + if ($recipients === []) { |
| 54 | + $this->jobService->remove(RepairRecipients::class); |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | +} |
0 commit comments