Skip to content

Commit

Permalink
feat: mail provider backend
Browse files Browse the repository at this point in the history
Signed-off-by: SebastianKrupinski <krupinskis05@gmail.com>
  • Loading branch information
SebastianKrupinski committed Jul 13, 2024
1 parent ece67ba commit 8ac84a5
Show file tree
Hide file tree
Showing 7 changed files with 105 additions and 131 deletions.
2 changes: 1 addition & 1 deletion lib/Provider/Command/MessageSend.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

/**
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-only
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace OCA\Mail\Provider\Command;

Expand Down
109 changes: 60 additions & 49 deletions lib/Provider/MailProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

/**
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-only
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace OCA\Mail\Provider;

Expand All @@ -19,22 +19,16 @@

class MailProvider implements IProvider {

private ContainerInterface $container;
private AccountService $AccountService;
private ?array $ServiceCollection = [];

public function __construct(
ContainerInterface $container,
AccountService $AccountService
protected ContainerInterface $container,
protected AccountService $AccountService
) {

$this->container = $container;
$this->AccountService = $AccountService;

}

/**
* An arbitrary unique text string identifying this provider
* arbitrary unique text string identifying this provider
*
* @since 2024.05.25
*
Expand All @@ -47,7 +41,7 @@ public function id(): string {
}

/**
* The localized human frendly name of this provider
* localized human frendly name of this provider
*
* @since 2024.05.25
*
Expand All @@ -60,15 +54,17 @@ public function label(): string {
}

/**
* Determain if any services are configured for a specific user
* determain if any services are configured for a specific user
*
* @since 2024.05.25
*
* @param string $userId user id
*
* @return bool true if any services are configure for the user
*/
public function hasServices(string $uid): bool {
public function hasServices(string $userId): bool {

return (count($this->listServices($uid)) > 0);
return (count($this->listServices($userId)) > 0);

}

Expand All @@ -77,13 +73,15 @@ public function hasServices(string $uid): bool {
*
* @since 2024.05.25
*
* @return array<string,IService> collection of service objects
* @param string $userId user id
*
* @return array<string,IService> collection of service id and object ['1' => IServiceObject]
*/
public function listServices(string $uid): array {
public function listServices(string $userId): array {

try {
// retrieve service(s) details from data store
$accounts = $this->AccountService->findByUserId($uid);
$accounts = $this->AccountService->findByUserId($userId);
} catch (\Throwable $th) {
return [];
}
Expand All @@ -92,101 +90,114 @@ public function listServices(string $uid): array {
// add services to collection
foreach ($accounts as $entry) {
// extract values
$id = (string) $entry->getId();
$serviceId = (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);
$services[$serviceId] = new MailService($this->container, $userId, $serviceId, $label, $address, $identity, $location);
}
// return list of services for user
return $services;

}

/**
* Retrieve a service with a specific id
* retrieve a service with a specific id
*
* @since 2024.05.25
*
* @param string $uid user id
* @param string $id service id
* @param string $userId user id
* @param string $serviceId service id
*
* @return IService|null returns service object or null if non found
* @return IService|null returns service object or null if none found
*/
public function findServiceById(string $uid, string $id): IService | null {
public function findServiceById(string $userId, string $serviceId): IService | null {

// evaluate if id is a number
if (is_numeric($id)) {
if (is_numeric($serviceId)) {
try {
// retrieve service details from data store
$account = $this->AccountService->find($uid, (int) $id);
$account = $this->AccountService->find($userId, (int) $serviceId);
} catch(\Throwable $th) {
return null;
}
}
// evaliate if service details where found
if ($account instanceof Account) {
// extract values
$id = (string) $account->getId();
$serviceId = (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 mail service object
return (new MailService($this->container, $userId, $serviceId, $label, $address, $identity, $location));
}

return null;

}

/**
* Retrieve a service for a specific mail address
* retrieve a service for a specific mail address
*
* @since 2024.05.25
*
* @param string $uid user id
* @param string $userId user id
* @param string $address mail address (e.g. test@example.com)
*
* @return IService returns service object or null if non found
* @return IService returns service object or null if none found
*/
public function findServiceByAddress(string $uid, string $address): IService | null {
public function findServiceByAddress(string $userId, string $address): IService | null {

try {
// retrieve service details from data store
$accounts = $this->AccountService->findByUserIdAndAddress($uid, $address);
$accounts = $this->AccountService->findByUserIdAndAddress($userId, $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();
$serviceId = (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 mail service object
return (new MailService($this->container, $userId, $serviceId, $label, $address, $identity, $location));
}

return null;

}

/**
* construct a new empty service object
*
* @since 30.0.0
*
* @return IService blank service object
*/
public function initiateService(): IService {

return (new MailService($this->container, $userId, '', '', ''));

}

/**
* 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
* @param string $userId user id
* @param IService $service service object
*
* @return string id of created service
* @return string id of created service
*/
public function createService(string $uid, IService $service): string {
public function createService(string $userId, IService $service): string {

return '';

Expand All @@ -197,12 +208,12 @@ public function createService(string $uid, IService $service): string {
*
* @since 2024.05.25
*
* @param string $uid user id of user to configure service for
* @param IService $service service configuration object
* @param string $userId user id
* @param IService $service service object
*
* @return string id of modifided service
* @return string id of modifided service
*/
public function modifyService(string $uid, IService $service): string {
public function modifyService(string $userId, IService $service): string {

return '';

Expand All @@ -213,12 +224,12 @@ public function modifyService(string $uid, IService $service): string {
*
* @since 2024.05.25
*
* @param string $uid user id of user to delete service for
* @param IService $service service configuration object
* @param string $userId user id
* @param IService $service service object
*
* @return bool status of delete action
* @return bool status of delete action
*/
public function deleteService(string $uid, IService $service): bool {
public function deleteService(string $userId, IService $service): bool {

return false;

Expand Down
63 changes: 31 additions & 32 deletions lib/Provider/MailService.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

/**
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-only
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace OCA\Mail\Provider;

Expand All @@ -14,41 +14,27 @@
use OCP\Mail\Provider\IService;
use OCP\Mail\Provider\IServiceIdentity;
use OCP\Mail\Provider\IServiceLocation;
use OCP\Mail\Provider\Message;

use Psr\Container\ContainerInterface;

class MailService implements IService, IMessageSend {

private string $userId;
private string $serviceId;
private string $serviceLabel;
private IAddress $servicePrimaryAddress;
private ?array $serviceSecondaryAddress;
private ?MailServiceIdentity $serviceIdentity;
private ?MailServiceLocation $serviceLocation;
protected array $serviceSecondaryAddresses = [];

public function __construct(
ContainerInterface $container,
string $uid,
string $sid,
string $label,
IAddress $primaryAddress,
?MailServiceIdentity $identity = null,
?MailServiceLocation $location = null
protected ContainerInterface $container,
protected string $userId,
protected string $serviceId,
protected string $serviceLabel,
protected IAddress $servicePrimaryAddress,
protected ?MailServiceIdentity $serviceIdentity = null,
protected ?MailServiceLocation $serviceLocation = null
) {

$this->container = $container;
$this->userId = $uid;
$this->serviceId = $sid;
$this->serviceLabel = $label;
$this->servicePrimaryAddress = $primaryAddress;
$this->serviceIdentity = $identity;
$this->serviceLocation = $location;

}

/**
* An arbitrary unique text string identifying this service
* arbitrary unique text string identifying this service
*
* @since 2024.05.25
*
Expand Down Expand Up @@ -205,12 +191,12 @@ public function setPrimaryAddress(IAddress $value): self {
*
* @since 2024.05.25
*
* @return array<int, IAddress> collection of mail address objects
* @return array<int, IAddress> collection of mail address object [IAddress, IAddress]
*/
public function getSecondaryAddress(): array | null {
public function getSecondaryAddresses(): array {

// retrieve and return secondary service addressess (aliases) collection
return $this->serviceSecondaryAddress;
return $this->serviceSecondaryAddresses;

}

Expand All @@ -219,19 +205,32 @@ public function getSecondaryAddress(): array | null {
*
* @since 2024.05.25
*
* @param IAddress ...$value collection of or one or more mail address objects
* @param IAddress ...$value collection of one or more mail address object
*
* @return self return this object for command chaining
*/
public function setSecondaryAddress(IAddress ...$value): self {
public function setSecondaryAddresses(IAddress ...$value): self {

$this->serviceSecondaryAddress = $value;
$this->serviceSecondaryAddresses = $value;
return $this;

}

/**
* Sends an outbound message
* construct a new empty message object
*
* @since 30.0.0
*
* @return IMessage blank message object
*/
public function initiateMessage(): IMessage {

return (new Message());

}

/**
* sends an outbound message
*
* @since 2024.05.25
*
Expand Down
4 changes: 2 additions & 2 deletions lib/Provider/MailServiceIdentity.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

/**
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-only
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace OCA\Mail\Provider;

Expand All @@ -13,7 +13,7 @@
class MailServiceIdentity implements IServiceIdentity {

/**
* An arbitrary unique text string identifying this credential type
* arbitrary unique text string identifying this credential type
*
* @since 2024.05.25
*
Expand Down
Loading

0 comments on commit 8ac84a5

Please sign in to comment.