-
Notifications
You must be signed in to change notification settings - Fork 259
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: SebastianKrupinski <[email protected]>
- Loading branch information
1 parent
027a910
commit ece67ba
Showing
10 changed files
with
1,074 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -83,6 +83,29 @@ public function findByUserId(string $userId): array { | |
return $this->findEntities($query); | ||
} | ||
|
||
/** | ||
* Finds a mail account(s) by user id and mail address | ||
* | ||
* @since 2024.05.25 | ||
* | ||
* @param string $userId system user id | ||
* @param string $address mail address (e.g. [email protected]) | ||
* | ||
* @return MailAccount[] | ||
* | ||
* @throws DoesNotExistException | ||
*/ | ||
public function findByUserIdAndAddress(string $userId, string $address): array { | ||
$qb = $this->db->getQueryBuilder(); | ||
$query = $qb | ||
->select('*') | ||
->from($this->getTableName()) | ||
->where($qb->expr()->eq('user_id', $qb->createNamedParameter($userId))) | ||
->andWhere($qb->expr()->eq('email', $qb->createNamedParameter($address))); | ||
|
||
return $this->findEntities($query); | ||
} | ||
|
||
/** | ||
* @throws DoesNotExistException | ||
* @throws MultipleObjectsReturnedException | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors | ||
* SPDX-License-Identifier: AGPL-3.0-only | ||
*/ | ||
namespace OCA\Mail\Provider\Command; | ||
|
||
use OCA\Mail\Db\LocalMessage; | ||
use OCA\Mail\Service\AccountService; | ||
use OCA\Mail\Service\Attachment\AttachmentService; | ||
use OCA\Mail\Service\OutboxService; | ||
use OCA\Mail\Service\SmimeService; | ||
use OCP\IConfig; | ||
use OCP\Mail\Provider\IMessage; | ||
|
||
class MessageSend { | ||
|
||
public function __construct( | ||
protected IConfig $config, | ||
protected AccountService $accountService, | ||
protected OutboxService $outboxService, | ||
protected AttachmentService $attachmentService, | ||
protected SmimeService $smimeService | ||
) { | ||
} | ||
|
||
public function perform(string $userId, string $serviceId, IMessage $message, array $option = []): void { | ||
Check failure on line 30 in lib/Provider/Command/MessageSend.php GitHub Actions / static-psalm-analysis dev-masterUndefinedClass
|
||
// find user mail account details | ||
$account = $this->accountService->find($userId, (int) $serviceId); | ||
// convert mail provider message to local message | ||
$lm = new LocalMessage(); | ||
$lm->setType($lm::TYPE_OUTGOING); | ||
$lm->setAccountId($account->getId()); | ||
$lm->setSubject($message->getSubject()); | ||
$lm->setBody($message->getBody()); | ||
$lm->setHtml(true); | ||
$lm->setSendAt(time()); | ||
|
||
// convert all mail provider attachments to local attachments | ||
$attachments = []; | ||
if (count($message->getAttachments()) > 0) { | ||
// iterate attachments and save them | ||
foreach ($message->getAttachments() as $entry) { | ||
$attachments[] = $this->attachmentService->addFileFromString( | ||
$userId, | ||
$entry->getName(), | ||
$entry->getType(), | ||
$entry->getContents() | ||
)->jsonSerialize(); | ||
} | ||
} | ||
// convert recipiant addresses | ||
$to = $this->convertAddressArray($message->getTo()); | ||
$cc = $this->convertAddressArray($message->getCc()); | ||
$bcc = $this->convertAddressArray($message->getBcc()); | ||
// save message for sending | ||
$lm = $this->outboxService->saveMessage( | ||
$account, | ||
$lm, | ||
$to, | ||
$cc, | ||
$bcc, | ||
$attachments | ||
); | ||
|
||
// evaluate if job scheduler is NOT cron, send message right away otherwise let cron job handle it | ||
if ($this->config->getAppValue('core', 'backgroundjobs_mode', 'ajax') !== 'cron') { | ||
$lm = $this->outboxService->sendMessage($lm, $account); | ||
} | ||
|
||
} | ||
|
||
protected function convertAddressArray(array|null $in) { | ||
// construct place holder | ||
$out = []; | ||
// convert format | ||
foreach ($in as $entry) { | ||
$out[] = (!empty($entry->getLabel())) ? ['email' => $entry->getAddress(), 'label' => $entry->getLabel()] : ['email' => $entry->getAddress()]; | ||
} | ||
// return converted addressess | ||
return $out; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,227 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors | ||
* SPDX-License-Identifier: AGPL-3.0-only | ||
*/ | ||
namespace OCA\Mail\Provider; | ||
|
||
use OCA\Mail\Account; | ||
use OCA\Mail\Service\AccountService; | ||
|
||
use OCP\Mail\Provider\Address as MailAddress; | ||
use OCP\Mail\Provider\IProvider; | ||
use OCP\Mail\Provider\IService; | ||
|
||
use Psr\Container\ContainerInterface; | ||
|
||
class MailProvider implements IProvider { | ||
Check failure on line 20 in lib/Provider/MailProvider.php GitHub Actions / static-psalm-analysis dev-masterUndefinedClass
|
||
|
||
private ContainerInterface $container; | ||
private AccountService $AccountService; | ||
private ?array $ServiceCollection = []; | ||
|
||
public function __construct( | ||
ContainerInterface $container, | ||
AccountService $AccountService | ||
) { | ||
|
||
$this->container = $container; | ||
$this->AccountService = $AccountService; | ||
|
||
} | ||
|
||
/** | ||
* An arbitrary unique text string identifying this provider | ||
* | ||
* @since 2024.05.25 | ||
* | ||
* @return string id of this provider (e.g. UUID or 'IMAP/SMTP' or anything else) | ||
*/ | ||
public function id(): string { | ||
|
||
return 'mail-application'; | ||
|
||
} | ||
|
||
/** | ||
* The localized human frendly name of this provider | ||
* | ||
* @since 2024.05.25 | ||
* | ||
* @return string label/name of this provider (e.g. Plain Old IMAP/SMTP) | ||
*/ | ||
public function label(): string { | ||
|
||
return 'Mail Application'; | ||
|
||
} | ||
|
||
/** | ||
* Determain if any services are configured for a specific user | ||
* | ||
* @since 2024.05.25 | ||
* | ||
* @return bool true if any services are configure for the user | ||
*/ | ||
public function hasServices(string $uid): bool { | ||
|
||
return (count($this->listServices($uid)) > 0); | ||
|
||
} | ||
|
||
/** | ||
* retrieve collection of services for a specific user | ||
* | ||
* @since 2024.05.25 | ||
* | ||
* @return array<string,IService> collection of service objects | ||
*/ | ||
public function listServices(string $uid): array { | ||
|
||
try { | ||
// retrieve service(s) details from data store | ||
$accounts = $this->AccountService->findByUserId($uid); | ||
} catch (\Throwable $th) { | ||
return []; | ||
} | ||
// construct temporary collection | ||
$services = []; | ||
// add services to collection | ||
foreach ($accounts as $entry) { | ||
// extract values | ||
$id = (string) $entry->getId(); | ||
$label = $entry->getName(); | ||
$address = new MailAddress($entry->getEmail(), $entry->getName()); | ||
$identity = new MailServiceIdentity(); | ||
$location = new MailServiceLocation(); | ||
// add service to collection | ||
$services[$id] = new MailService($this->container, $uid, $id, $label, $address, $identity, $location); | ||
} | ||
// return list of services for user | ||
return $services; | ||
|
||
} | ||
|
||
/** | ||
* Retrieve a service with a specific id | ||
* | ||
* @since 2024.05.25 | ||
* | ||
* @param string $uid user id | ||
* @param string $id service id | ||
* | ||
* @return IService|null returns service object or null if non found | ||
*/ | ||
public function findServiceById(string $uid, string $id): IService | null { | ||
|
||
// evaluate if id is a number | ||
if (is_numeric($id)) { | ||
try { | ||
// retrieve service details from data store | ||
$account = $this->AccountService->find($uid, (int) $id); | ||
} catch(\Throwable $th) { | ||
return null; | ||
} | ||
} | ||
// evaliate if service details where found | ||
if ($account instanceof Account) { | ||
// extract values | ||
$id = (string) $account->getId(); | ||
$label = $account->getName(); | ||
$address = new MailAddress($account->getEmail(), $account->getName()); | ||
$identity = new MailServiceIdentity(); | ||
$location = new MailServiceLocation(); | ||
// return mail service instance | ||
return (new MailService($this->container, $uid, $id, $label, $address, $identity, $location)); | ||
} | ||
|
||
return null; | ||
|
||
} | ||
|
||
/** | ||
* Retrieve a service for a specific mail address | ||
* | ||
* @since 2024.05.25 | ||
* | ||
* @param string $uid user id | ||
* @param string $address mail address (e.g. [email protected]) | ||
* | ||
* @return IService returns service object or null if non found | ||
*/ | ||
public function findServiceByAddress(string $uid, string $address): IService | null { | ||
|
||
try { | ||
// retrieve service details from data store | ||
$accounts = $this->AccountService->findByUserIdAndAddress($uid, $address); | ||
} catch(\Throwable $th) { | ||
return null; | ||
} | ||
// evaliate if service details where found | ||
if (is_array($accounts) && count($accounts) > 0 && $accounts[0] instanceof Account) { | ||
// extract values | ||
$id = (string) $accounts[0]->getId(); | ||
$label = $accounts[0]->getName(); | ||
$address = new MailAddress($accounts[0]->getEmail(), $accounts[0]->getName()); | ||
$identity = new MailServiceIdentity(); | ||
$location = new MailServiceLocation(); | ||
// return mail service instance | ||
return (new MailService($this->container, $uid, $id, $label, $address, $identity, $location)); | ||
} | ||
|
||
return null; | ||
|
||
} | ||
|
||
/** | ||
* create a service configuration for a specific user | ||
* | ||
* @since 2024.05.25 | ||
* | ||
* @param string $uid user id of user to configure service for | ||
* @param IService $service service configuration object | ||
* | ||
* @return string id of created service | ||
*/ | ||
public function createService(string $uid, IService $service): string { | ||
|
||
return ''; | ||
|
||
} | ||
|
||
/** | ||
* modify a service configuration for a specific user | ||
* | ||
* @since 2024.05.25 | ||
* | ||
* @param string $uid user id of user to configure service for | ||
* @param IService $service service configuration object | ||
* | ||
* @return string id of modifided service | ||
*/ | ||
public function modifyService(string $uid, IService $service): string { | ||
|
||
return ''; | ||
|
||
} | ||
|
||
/** | ||
* delete a service configuration for a specific user | ||
* | ||
* @since 2024.05.25 | ||
* | ||
* @param string $uid user id of user to delete service for | ||
* @param IService $service service configuration object | ||
* | ||
* @return bool status of delete action | ||
*/ | ||
public function deleteService(string $uid, IService $service): bool { | ||
|
||
return false; | ||
|
||
} | ||
|
||
} |
Oops, something went wrong.