Initial commit
This commit is contained in:
289
lib/Service/Discovery.php
Normal file
289
lib/Service/Discovery.php
Normal file
@@ -0,0 +1,289 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* SPDX-FileCopyrightText: Sebastian Krupinski <krupinski01@gmail.com>
|
||||
* SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
*/
|
||||
|
||||
namespace KTXM\ProviderJmapc\Service;
|
||||
|
||||
use KTXM\ProviderJmapc\Providers\ServiceLocation;
|
||||
|
||||
/**
|
||||
* JMAP Service Discovery
|
||||
*
|
||||
* Implements RFC 8620 service discovery via:
|
||||
* 1. DNS SRV records (_jmap._tcp.<domain>)
|
||||
* 2. Well-known URI (https://<host>/.well-known/jmap)
|
||||
*/
|
||||
class Discovery
|
||||
{
|
||||
private const WELL_KNOWN_PATH = '/.well-known/jmap';
|
||||
private const DEFAULT_PORT_HTTPS = 443;
|
||||
private const DEFAULT_PORT_HTTP = 80;
|
||||
private const CONNECTION_TIMEOUT = 10;
|
||||
private const MAX_REDIRECTS = 3;
|
||||
|
||||
/**
|
||||
* Discover JMAP service location from email address or domain
|
||||
*
|
||||
* @param string $identity Email address or domain
|
||||
* @param string|null $location Optional hostname to test directly (bypasses DNS SRV)
|
||||
* @param string|null $secret Optional password/token to validate the service
|
||||
* @param bool $verifySSL Whether to verify SSL certificates
|
||||
* @return ServiceLocation|null Discovered service location or null if not found
|
||||
*/
|
||||
public function discover(
|
||||
string $identity,
|
||||
?string $location = null,
|
||||
?string $secret = null,
|
||||
bool $verifySSL = true
|
||||
): ?ServiceLocation {
|
||||
// If location is provided, test it directly
|
||||
if ($location !== null && $location !== '') {
|
||||
$host = $this->extractDomain($location);
|
||||
if ($host !== null) {
|
||||
$result = $this->testWellKnownUri($host, self::DEFAULT_PORT_HTTPS, $verifySSL, 'https', $identity, $secret);
|
||||
if ($result !== null) {
|
||||
return $result;
|
||||
}
|
||||
|
||||
// Try HTTP if HTTPS failed
|
||||
$result = $this->testWellKnownUri($host, self::DEFAULT_PORT_HTTP, $verifySSL, 'http', $identity, $secret);
|
||||
if ($result !== null) {
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
// Extract domain from email address if needed
|
||||
$domain = $this->extractDomain($identity);
|
||||
if ($domain === null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// Try DNS SRV lookup first (RFC 8620 recommended method)
|
||||
$srvResult = $this->discoverViaSRV($domain);
|
||||
if ($srvResult !== null) {
|
||||
$result = $this->testWellKnownUri(
|
||||
$srvResult['host'],
|
||||
$srvResult['port'],
|
||||
$verifySSL,
|
||||
'https',
|
||||
$identity,
|
||||
$secret
|
||||
);
|
||||
if ($result !== null) {
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
|
||||
// Fallback: Try well-known URI directly on domain with HTTPS
|
||||
$result = $this->testWellKnownUri($domain, self::DEFAULT_PORT_HTTPS, $verifySSL, 'https', $identity, $secret);
|
||||
if ($result !== null) {
|
||||
return $result;
|
||||
}
|
||||
|
||||
// Last resort: Try HTTP (not recommended, but some servers may use it)
|
||||
$result = $this->testWellKnownUri($domain, self::DEFAULT_PORT_HTTP, $verifySSL, 'http', $identity, $secret);
|
||||
if ($result !== null) {
|
||||
return $result;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Extract domain from email address or return as-is if already a domain
|
||||
*/
|
||||
private function extractDomain(string $identity): ?string
|
||||
{
|
||||
$identity = trim($identity);
|
||||
|
||||
// If it contains @, extract domain part
|
||||
if (str_contains($identity, '@')) {
|
||||
$parts = explode('@', $identity);
|
||||
return strtolower(trim($parts[1] ?? ''));
|
||||
}
|
||||
|
||||
// Otherwise treat as domain
|
||||
$domain = strtolower($identity);
|
||||
|
||||
// Remove protocol if present
|
||||
$domain = preg_replace('#^https?://#i', '', $domain);
|
||||
|
||||
// Remove path if present
|
||||
$domain = explode('/', $domain)[0];
|
||||
|
||||
return $domain !== '' ? $domain : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Discover JMAP service via DNS SRV record
|
||||
*
|
||||
* Queries for _jmap._tcp.<domain> SRV record
|
||||
*
|
||||
* @return array{host: string, port: int}|null
|
||||
*/
|
||||
private function discoverViaSRV(string $domain): ?array
|
||||
{
|
||||
$srvRecord = "_jmap._tcp.{$domain}";
|
||||
|
||||
try {
|
||||
$records = @dns_get_record($srvRecord, DNS_SRV);
|
||||
|
||||
if ($records === false || empty($records)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// Use first record (they can be prioritized, but we'll keep it simple)
|
||||
$record = $records[0];
|
||||
|
||||
if (isset($record['target']) && isset($record['port'])) {
|
||||
return [
|
||||
'host' => rtrim($record['target'], '.'),
|
||||
'port' => (int)$record['port'],
|
||||
];
|
||||
}
|
||||
} catch (\Exception $e) {
|
||||
// DNS lookup failed, silently continue to fallback methods
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Test well-known JMAP URI and validate response
|
||||
*
|
||||
* Optionally validates with credentials if secret is provided
|
||||
*
|
||||
* @return ServiceLocation|null
|
||||
*/
|
||||
private function testWellKnownUri(
|
||||
string $host,
|
||||
int $port,
|
||||
bool $verifySSL,
|
||||
string $scheme = 'https',
|
||||
?string $identity = null,
|
||||
?string $secret = null
|
||||
): ?ServiceLocation {
|
||||
$url = $this->buildWellKnownUrl($host, $port, $scheme);
|
||||
|
||||
try {
|
||||
$ch = curl_init($url);
|
||||
if ($ch === false) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$curlOptions = [
|
||||
CURLOPT_RETURNTRANSFER => true,
|
||||
CURLOPT_FOLLOWLOCATION => true,
|
||||
CURLOPT_MAXREDIRS => self::MAX_REDIRECTS,
|
||||
CURLOPT_TIMEOUT => self::CONNECTION_TIMEOUT,
|
||||
CURLOPT_SSL_VERIFYPEER => $verifySSL,
|
||||
CURLOPT_SSL_VERIFYHOST => $verifySSL ? 2 : 0,
|
||||
CURLOPT_HTTPHEADER => [
|
||||
'Accept: application/json',
|
||||
],
|
||||
];
|
||||
|
||||
// Add basic auth if credentials provided
|
||||
if ($identity !== null && $secret !== null) {
|
||||
$curlOptions[CURLOPT_HTTPAUTH] = CURLAUTH_BASIC;
|
||||
$curlOptions[CURLOPT_USERPWD] = "{$identity}:{$secret}";
|
||||
}
|
||||
|
||||
curl_setopt_array($ch, $curlOptions);
|
||||
|
||||
$response = curl_exec($ch);
|
||||
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
||||
curl_close($ch);
|
||||
|
||||
// Must be 200 OK (or 401 if we didn't provide auth - still proves service exists)
|
||||
if ($httpCode === 401 && ($identity === null || $secret === null)) {
|
||||
// Service exists but requires auth - that's fine for discovery
|
||||
return new ServiceLocation(
|
||||
host: $host,
|
||||
port: $port,
|
||||
scheme: $scheme,
|
||||
path: self::WELL_KNOWN_PATH,
|
||||
verifyPeer: $verifySSL,
|
||||
verifyHost: $verifySSL,
|
||||
);
|
||||
}
|
||||
|
||||
if ($httpCode !== 200 || $response === false) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// Parse and validate JMAP session response
|
||||
$data = json_decode($response, true);
|
||||
if (!$this->isValidJmapSession($data)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// Create ServiceLocation with discovered settings
|
||||
return new ServiceLocation(
|
||||
host: $host,
|
||||
port: $port,
|
||||
scheme: $scheme,
|
||||
path: self::WELL_KNOWN_PATH,
|
||||
verifyPeer: $verifySSL,
|
||||
verifyHost: $verifySSL,
|
||||
);
|
||||
|
||||
} catch (\Exception $e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Build well-known JMAP URL
|
||||
*/
|
||||
private function buildWellKnownUrl(string $host, int $port, string $scheme): string
|
||||
{
|
||||
$url = "{$scheme}://{$host}";
|
||||
|
||||
// Add port if non-standard
|
||||
if (($scheme === 'https' && $port !== self::DEFAULT_PORT_HTTPS) ||
|
||||
($scheme === 'http' && $port !== self::DEFAULT_PORT_HTTP)) {
|
||||
$url .= ":{$port}";
|
||||
}
|
||||
|
||||
$url .= self::WELL_KNOWN_PATH;
|
||||
|
||||
return $url;
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate that response is a proper JMAP session object
|
||||
*
|
||||
* According to RFC 8620, session must contain at minimum:
|
||||
* - apiUrl: The URL to use for JMAP API requests
|
||||
* - capabilities: Object describing server capabilities
|
||||
*/
|
||||
private function isValidJmapSession(mixed $data): bool
|
||||
{
|
||||
if (!is_array($data)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Must have apiUrl
|
||||
if (!isset($data['apiUrl']) || !is_string($data['apiUrl'])) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Must have capabilities object
|
||||
if (!isset($data['capabilities']) || !is_array($data['capabilities'])) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Should have mail capability for our use case
|
||||
// But we'll be lenient and just check the basics above
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
187
lib/Service/MailService.php
Normal file
187
lib/Service/MailService.php
Normal file
@@ -0,0 +1,187 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* SPDX-FileCopyrightText: Sebastian Krupinski <krupinski01@gmail.com>
|
||||
* SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
*/
|
||||
|
||||
namespace KTXM\ProviderJmapc\Service;
|
||||
|
||||
use JmapClient\Client;
|
||||
use KTXF\Resource\Range\IRange;
|
||||
use KTXM\ProviderJmapc\Service\Remote\RemoteMailService;
|
||||
use KTXM\ProviderJmapc\Service\Remote\RemoteService;
|
||||
|
||||
class MailService {
|
||||
protected Client $dataStore;
|
||||
protected RemoteMailService $remoteMailService;
|
||||
protected $localMetaStore;
|
||||
protected $localBlobStore;
|
||||
protected string $servicePrimaryAccount = '';
|
||||
protected string $serviceSelectedAccount = '';
|
||||
protected array $serviceAvailableAccounts = [];
|
||||
protected string $servicePrimaryIdentity = '';
|
||||
protected string $serviceSelectedIdentity = '';
|
||||
protected array $serviceAvailableIdentities = [];
|
||||
protected array $serviceCollectionRoles = [];
|
||||
|
||||
public function __construct(
|
||||
) { }
|
||||
|
||||
public function initialize(Client $dataStore): void {
|
||||
|
||||
$this->dataStore = $dataStore;
|
||||
// evaluate if client is connected
|
||||
if (!$this->dataStore->sessionStatus()) {
|
||||
$this->dataStore->connect();
|
||||
}
|
||||
// initialize remote service
|
||||
$this->remoteMailService = RemoteService::mailService($dataStore);
|
||||
// initialize internal settings
|
||||
$this->initializeSession();
|
||||
$this->initializeCollectionRoles();
|
||||
|
||||
}
|
||||
|
||||
protected function initializeSession() {
|
||||
|
||||
// retrieve default account
|
||||
$this->servicePrimaryAccount = $this->dataStore->sessionAccountDefault('mail');
|
||||
$this->serviceSelectedAccount = $this->servicePrimaryAccount;
|
||||
// retrieve accounts
|
||||
$this->serviceAvailableAccounts = $this->dataStore->sessionAccounts();
|
||||
// retrieve identities
|
||||
$collection = $this->remoteMailService->identityFetch($this->servicePrimaryAccount);
|
||||
foreach ($collection as $entry) {
|
||||
$this->serviceAvailableIdentities[$entry->address()] = $entry;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
protected function initializeCollectionRoles() {
|
||||
|
||||
// retrieve collections
|
||||
$collectionList = $this->collectionList('', '');
|
||||
// find collection with roles
|
||||
foreach ($collectionList as $entry) {
|
||||
$this->serviceCollectionRoles[$entry->getRole()] = $entry->id();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public function collectionList(string $location, string $scope, array $options = []): array {
|
||||
|
||||
return $this->remoteMailService->collectionList($this->serviceSelectedAccount, $location, $scope);
|
||||
|
||||
}
|
||||
|
||||
public function collectionFetch(string $location, string $id, array $options = []): object {
|
||||
|
||||
return $this->remoteMailService->collectionFetch($this->serviceSelectedAccount, $location, $id);
|
||||
|
||||
}
|
||||
|
||||
public function collectionCreate(string $location, string $label, array $options = []): string {
|
||||
|
||||
return $this->remoteMailService->collectionCreate($this->serviceSelectedAccount, $location, $label);
|
||||
|
||||
}
|
||||
|
||||
public function collectionUpdate(string $location, string $id, string $label, array $options = []): string {
|
||||
|
||||
return $this->remoteMailService->collectionUpdate($this->serviceSelectedAccount, $location, $id, $label);
|
||||
|
||||
}
|
||||
|
||||
public function collectionDelete(string $location, string $id, array $options = []): string {
|
||||
|
||||
return $this->remoteMailService->collectionDelete($this->serviceSelectedAccount, $location, $id);
|
||||
|
||||
}
|
||||
|
||||
public function collectionMove(string $sourceLocation, string $id, string $destinationLocation, array $options = []): string {
|
||||
|
||||
return $this->remoteMailService->collectionMove($this->serviceSelectedAccount, $sourceLocation, $id, $destinationLocation);
|
||||
|
||||
}
|
||||
|
||||
public function entityList(string $location, ?IRange $range = null, ?string $sort = null, string $particulars = 'D', array $options = []): array {
|
||||
|
||||
return $this->remoteMailService->entityList($this->serviceSelectedAccount, $location, $range, $sort, $particulars);
|
||||
|
||||
}
|
||||
|
||||
public function entityFetch(string $location, string $id, string $particulars = 'D', array $options = []): object {
|
||||
|
||||
return $this->remoteMailService->entityFetch($this->serviceSelectedAccount, $location, $id, $particulars);
|
||||
|
||||
}
|
||||
|
||||
public function entityCreate(string $location, IMessage $message, array $options = []): string {
|
||||
|
||||
return $this->remoteMailService->entityCreate($this->serviceSelectedAccount, $location, $message);
|
||||
|
||||
}
|
||||
|
||||
public function entityUpdate(string $location, string $id, IMessage $message, array $options = []): string {
|
||||
|
||||
return $this->remoteMailService->entityUpdate($this->serviceSelectedAccount, $location, $id, $message);
|
||||
|
||||
}
|
||||
|
||||
public function entityDelete(string $location, string $id, array $options = []): string {
|
||||
|
||||
return $this->remoteMailService->entityDelete($this->serviceSelectedAccount, $location, $id);
|
||||
|
||||
}
|
||||
|
||||
public function entityCopy(string $sourceLocation, string $id, string $destinationLocation, array $options = []): string {
|
||||
|
||||
// perform action
|
||||
return $this->remoteMailService->entityCopy($this->serviceSelectedAccount, $sourceLocation, $id, $destinationLocation);
|
||||
|
||||
}
|
||||
|
||||
public function entityMove(string $sourceLocation, string $id, string $destinationLocation, array $options = []): string {
|
||||
|
||||
// perform action
|
||||
return $this->remoteMailService->entityMove($this->serviceSelectedAccount, $sourceLocation, $id, $destinationLocation);
|
||||
|
||||
}
|
||||
|
||||
public function entityForward(string $location, string $id, IMessage $message, array $options = []): string {
|
||||
|
||||
// perform action
|
||||
return $this->remoteMailService->entityForward($this->serviceSelectedAccount, $location, $id, $message);
|
||||
|
||||
}
|
||||
|
||||
public function entityReply(string $location, string $id, IMessage $message, array $options = []): string {
|
||||
|
||||
// perform action
|
||||
return $this->remoteMailService->entityReply($this->serviceSelectedAccount, $location, $id, $message);
|
||||
|
||||
}
|
||||
|
||||
public function entitySend(IMessage $message, array $options = []): string {
|
||||
|
||||
// extract from address
|
||||
$from = $message->getFrom();
|
||||
// determine if identity exists for this from address
|
||||
if (isset($this->serviceAvailableIdentities[$from->getAddress()])) {
|
||||
$selectedIdentity = $this->serviceAvailableIdentities[$from->getAddress()]->id();
|
||||
}
|
||||
// perform action
|
||||
return $this->remoteMailService->entitySend($selectedIdentity, $message, $this->serviceCollectionRoles['drafts'], $this->serviceCollectionRoles['sent']);
|
||||
|
||||
}
|
||||
|
||||
public function blobFetch(string $id): object {
|
||||
|
||||
return $this->remoteMailService->blobFetch($this->serviceSelectedAccount, $id);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
343
lib/Service/Remote/FM/RemoteContactsServiceFM.php
Normal file
343
lib/Service/Remote/FM/RemoteContactsServiceFM.php
Normal file
@@ -0,0 +1,343 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* @copyright Copyright (c) 2023 Sebastian Krupinski <krupinski01@gmail.com>
|
||||
*
|
||||
* @author Sebastian Krupinski <krupinski01@gmail.com>
|
||||
*
|
||||
* @license AGPL-3.0-or-later
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
namespace KTXM\ProviderJmapc\Service\Remote\FM;
|
||||
|
||||
use DateTimeImmutable;
|
||||
use JmapClient\Client;
|
||||
use OCA\JMAPC\Jmap\FM\Request\Contacts\ContactParameters as ContactParametersRequest;
|
||||
use OCA\JMAPC\Objects\Contact\ContactAnniversaryObject;
|
||||
use OCA\JMAPC\Objects\Contact\ContactAnniversaryTypes;
|
||||
use OCA\JMAPC\Objects\Contact\ContactEmailObject;
|
||||
use OCA\JMAPC\Objects\Contact\ContactNoteObject;
|
||||
use OCA\JMAPC\Objects\Contact\ContactObject as ContactObject;
|
||||
use OCA\JMAPC\Objects\Contact\ContactOrganizationObject;
|
||||
use OCA\JMAPC\Objects\Contact\ContactPhoneObject;
|
||||
use OCA\JMAPC\Objects\Contact\ContactPhysicalLocationObject;
|
||||
use OCA\JMAPC\Objects\Contact\ContactTitleObject;
|
||||
use OCA\JMAPC\Objects\Contact\ContactTitleTypes;
|
||||
use OCA\JMAPC\Objects\Contact\ContactVirtualLocationObject;
|
||||
use OCA\JMAPC\Objects\OriginTypes;
|
||||
use OCA\JMAPC\Service\Remote\RemoteContactsService;
|
||||
|
||||
class RemoteContactsServiceFM extends RemoteContactsService {
|
||||
private const DATE_ANNIVERSARY = 'Y-m-d';
|
||||
|
||||
public function __construct() {
|
||||
}
|
||||
|
||||
public function initialize(Client $dataStore, ?string $dataAccount = null) {
|
||||
|
||||
parent::initialize($dataStore, $dataAccount);
|
||||
|
||||
$this->resourceNamespace = 'https://www.fastmail.com/dev/contacts';
|
||||
$this->resourceCollectionLabel = null;
|
||||
$this->resourceEntityLabel = 'Contact';
|
||||
|
||||
$dataStore->configureRequestTypes('parameters', 'Contact.object', 'OCA\JMAPC\Jmap\FM\Request\Contacts\ContactParameters');
|
||||
|
||||
$dataStore->configureResponseTypes('command', 'Contact/get', 'JmapClient\Responses\Contacts\ContactGet');
|
||||
$dataStore->configureResponseTypes('command', 'Contact/set', 'JmapClient\Responses\Contacts\ContactSet');
|
||||
$dataStore->configureResponseTypes('command', 'Contact/changes', 'JmapClient\Responses\Contacts\ContactChanges');
|
||||
$dataStore->configureResponseTypes('command', 'Contact/query', 'JmapClient\Responses\Contacts\ContactQuery');
|
||||
$dataStore->configureResponseTypes('command', 'Contact/queryChanges', 'JmapClient\Responses\Contacts\ContactQueryChanges');
|
||||
$dataStore->configureResponseTypes('parameters', 'Contact', 'OCA\JMAPC\Jmap\FM\Response\ContactParameters');
|
||||
$dataStore->configureResponseTypes('parameters', 'Contact', 'OCA\JMAPC\Jmap\FM\Response\Contacts\ContactParameters');
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* convert jmap object to contact object
|
||||
*
|
||||
* @since Release 1.0.0
|
||||
*
|
||||
*/
|
||||
public function toContactObject($so): ContactObject {
|
||||
|
||||
// create object
|
||||
$do = new ContactObject();
|
||||
// source origin
|
||||
$do->Origin = OriginTypes::External;
|
||||
// id
|
||||
if ($so->id()) {
|
||||
$do->ID = $so->id();
|
||||
}
|
||||
// universal id
|
||||
if ($so->uid()) {
|
||||
$do->UUID = $so->uid();
|
||||
}
|
||||
// name - last
|
||||
if ($so->nameLast()) {
|
||||
$do->Name->Last = $so->nameLast();
|
||||
}
|
||||
// name - first
|
||||
if ($so->nameFirst()) {
|
||||
$do->Name->First = $so->nameFirst();
|
||||
}
|
||||
// name - prefix
|
||||
if ($so->namePrefix()) {
|
||||
$do->Name->Prefix = $so->namePrefix();
|
||||
}
|
||||
// name - suffix
|
||||
if ($so->nameSuffix()) {
|
||||
$do->Name->Suffix = $so->nameSuffix();
|
||||
}
|
||||
// anniversary - birth day
|
||||
if ($so->birthday() && $so->birthday() !== '0000-00-00') {
|
||||
$when = new DateTimeImmutable($so->birthday());
|
||||
if ($when) {
|
||||
$anniversary = new ContactAnniversaryObject();
|
||||
$anniversary->Type = ContactAnniversaryTypes::Birth;
|
||||
$anniversary->When = $when;
|
||||
$do->Anniversaries[] = $anniversary;
|
||||
}
|
||||
}
|
||||
// anniversary - nuptial day
|
||||
if ($so->nuptialDay() && $so->nuptialDay() !== '0000-00-00') {
|
||||
$when = new DateTimeImmutable($so->nuptialDay());
|
||||
if ($when) {
|
||||
$anniversary = new ContactAnniversaryObject();
|
||||
$anniversary->Type = ContactAnniversaryTypes::Nuptial;
|
||||
$anniversary->When = $when;
|
||||
$do->Anniversaries[] = $anniversary;
|
||||
}
|
||||
}
|
||||
// physical location(s)
|
||||
foreach ($so->location() as $id => $entry) {
|
||||
$location = new ContactPhysicalLocationObject();
|
||||
$location->Context = $entry->type();
|
||||
$location->Label = $entry->label();
|
||||
$location->Street = $entry->street();
|
||||
$location->Locality = $entry->locality();
|
||||
$location->Region = $entry->region();
|
||||
$location->Code = $entry->code();
|
||||
$location->Country = $entry->country();
|
||||
$location->Id = (string)$id;
|
||||
$location->Index = $id;
|
||||
$do->PhysicalLocations[$id] = $location;
|
||||
}
|
||||
// phone(s)
|
||||
foreach ($so->phone() as $id => $entry) {
|
||||
$phone = new ContactPhoneObject();
|
||||
$phone->Context = $entry->type();
|
||||
$phone->Number = $entry->value();
|
||||
$phone->Label = $entry->label();
|
||||
$phone->Id = (string)$id;
|
||||
$phone->Index = $id;
|
||||
if ($entry->default()) {
|
||||
$phone->Priority = 1;
|
||||
}
|
||||
$do->Phone[$id] = $phone;
|
||||
}
|
||||
// email(s)
|
||||
foreach ($so->email() as $id => $entry) {
|
||||
$email = new ContactEmailObject();
|
||||
$email->Context = $entry->type();
|
||||
$email->Address = $entry->value();
|
||||
$email->Id = (string)$id;
|
||||
$email->Index = $id;
|
||||
$do->Email[$id] = $email;
|
||||
}
|
||||
// organization - name
|
||||
if ($so->organizationName()) {
|
||||
$organization = new ContactOrganizationObject();
|
||||
$organization->Label = $so->organizationName();
|
||||
$organization->Id = '0';
|
||||
$organization->Index = 0;
|
||||
$organization->Priority = 1;
|
||||
$do->Organizations[] = $organization;
|
||||
}
|
||||
// title
|
||||
if ($so->title()) {
|
||||
$title = new ContactTitleObject();
|
||||
$title->Kind = ContactTitleTypes::Title;
|
||||
$title->Label = $so->title();
|
||||
$title->Id = '0';
|
||||
$title->Index = 0;
|
||||
$title->Priority = 1;
|
||||
$do->Titles[] = $title;
|
||||
}
|
||||
// notes
|
||||
if ($so->notes()) {
|
||||
$note = new ContactNoteObject();
|
||||
$note->Content = $so->notes();
|
||||
$note->Id = '0';
|
||||
$note->Index = 0;
|
||||
$note->Priority = 1;
|
||||
$do->Notes[] = $note;
|
||||
}
|
||||
// virtual locations
|
||||
if ($so->online()) {
|
||||
foreach ($so->online() as $id => $entry) {
|
||||
$entity = new ContactVirtualLocationObject();
|
||||
$entity->Location = $entry->value();
|
||||
$entity->Context = $entry->type();
|
||||
$entity->Label = $entry->label();
|
||||
$email->Id = (string)$id;
|
||||
$email->Index = $id;
|
||||
$do->VirtualLocations[$id] = $entity;
|
||||
}
|
||||
}
|
||||
|
||||
return $do;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* convert contact object to jmap object
|
||||
*
|
||||
* @since Release 1.0.0
|
||||
*
|
||||
*/
|
||||
public function fromContactObject(ContactObject $so): mixed {
|
||||
|
||||
// create object
|
||||
$do = new ContactParametersRequest();
|
||||
// universal id
|
||||
if ($so->UUID) {
|
||||
$do->uid($so->UUID);
|
||||
}
|
||||
// name - last
|
||||
if ($so->Name->Last) {
|
||||
$do->nameLast($so->Name->Last);
|
||||
}
|
||||
// name - first
|
||||
if ($so->Name->First) {
|
||||
$do->nameFirst($so->Name->First);
|
||||
}
|
||||
// name - prefix
|
||||
if ($so->Name->Prefix) {
|
||||
$do->namePrefix($so->Name->Prefix);
|
||||
}
|
||||
// name - suffix
|
||||
if ($so->Name->Suffix) {
|
||||
$do->nameSuffix($so->Name->Suffix);
|
||||
}
|
||||
// aliases
|
||||
// only one aliases is supported
|
||||
if ($so->Name->Aliases->count() > 0) {
|
||||
$priority = $so->Name->Aliases->highestPriority();
|
||||
$do->organizationName($so->Name->Aliases[$priority]->Label);
|
||||
}
|
||||
// anniversaries
|
||||
$delta = [ContactAnniversaryTypes::Birth->name => true, ContactAnniversaryTypes::Nuptial->name => true];
|
||||
foreach ($so->Anniversaries as $id => $entry) {
|
||||
if ($entry->When === null) {
|
||||
continue;
|
||||
}
|
||||
if ($entry->Type === ContactAnniversaryTypes::Birth) {
|
||||
$do->birthday($entry->When->format(self::DATE_ANNIVERSARY));
|
||||
unset($delta[ContactAnniversaryTypes::Birth->name]);
|
||||
}
|
||||
if ($entry->Type === ContactAnniversaryTypes::Nuptial) {
|
||||
$do->nuptialDay($entry->When->format(self::DATE_ANNIVERSARY));
|
||||
unset($delta[ContactAnniversaryTypes::Nuptial->name]);
|
||||
}
|
||||
}
|
||||
foreach ($delta as $key => $value) {
|
||||
if ($key === ContactAnniversaryTypes::Birth->name) {
|
||||
$do->birthday('0000-00-00');
|
||||
}
|
||||
if ($key === ContactAnniversaryTypes::Nuptial->name) {
|
||||
$do->nuptialDay('0000-00-00');
|
||||
}
|
||||
}
|
||||
// phone(s)
|
||||
foreach ($so->Phone as $id => $entry) {
|
||||
$entity = $do->phone($id);
|
||||
$entity->value((string)$entry->Number);
|
||||
$context = strtolower($entry->Context);
|
||||
if (in_array($context, ['home', 'work', 'mobile', 'fax', 'page', 'other'], true)) {
|
||||
$entity->type($entry->Context);
|
||||
} else {
|
||||
$entity->type('other');
|
||||
$entity->label($entry->Context);
|
||||
}
|
||||
if ($entry->Priority === 1) {
|
||||
$entity->default(true);
|
||||
}
|
||||
}
|
||||
// email(s)
|
||||
foreach ($so->Email as $id => $entry) {
|
||||
$entity = $do->email($id);
|
||||
$entity->value((string)$entry->Address);
|
||||
$context = strtolower($entry->Context);
|
||||
if (in_array($context, ['personal', 'work', 'other'], true)) {
|
||||
$entity->type($entry->Context);
|
||||
} else {
|
||||
$entity->type('other');
|
||||
$entity->label($entry->Context);
|
||||
}
|
||||
if ($entry->Priority === 1) {
|
||||
$entity->default(true);
|
||||
}
|
||||
}
|
||||
// physical location(s)
|
||||
foreach ($so->PhysicalLocations as $id => $entry) {
|
||||
$entity = $do->location($id);
|
||||
$entity->type((string)$entry->Context);
|
||||
$entity->label((string)$entry->Label);
|
||||
$entity->street((string)$entry->Street);
|
||||
$entity->locality((string)$entry->Locality);
|
||||
$entity->region((string)$entry->Region);
|
||||
$entity->code((string)$entry->Code);
|
||||
$entity->country((string)$entry->Country);
|
||||
if ($entry->Priority === 1) {
|
||||
$entity->default(true);
|
||||
}
|
||||
}
|
||||
// organization - name
|
||||
// only one organization is supported
|
||||
if ($so->Organizations->count() > 0) {
|
||||
$priority = $so->Organizations->highestPriority();
|
||||
$do->organizationName($so->Organizations[$priority]->Label);
|
||||
}
|
||||
// titles
|
||||
// only one title is supported
|
||||
if ($so->Titles->count() > 0) {
|
||||
$priority = $so->Titles->highestPriority(ContactTitleTypes::Title);
|
||||
if ($priority !== null) {
|
||||
$do->title($so->Titles[$priority]->Label);
|
||||
}
|
||||
}
|
||||
// notes
|
||||
// only one note is supported
|
||||
if ($so->Notes->count() > 0) {
|
||||
$do->notes($so->Notes[0]->Content);
|
||||
}
|
||||
// virtual locations
|
||||
foreach ($so->VirtualLocations as $id => $entry) {
|
||||
$entity = $do->online($id);
|
||||
$entity->type((string)$entry->Context);
|
||||
$entity->value((string)$entry->Location);
|
||||
$entity->label((string)$entry->Label);
|
||||
}
|
||||
|
||||
return $do;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
109
lib/Service/Remote/FM/RemoteEventsServiceFM.php
Normal file
109
lib/Service/Remote/FM/RemoteEventsServiceFM.php
Normal file
@@ -0,0 +1,109 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* @copyright Copyright (c) 2023 Sebastian Krupinski <krupinski01@gmail.com>
|
||||
*
|
||||
* @author Sebastian Krupinski <krupinski01@gmail.com>
|
||||
*
|
||||
* @license AGPL-3.0-or-later
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
namespace KTXM\ProviderJmapc\Service\Remote\FM;
|
||||
|
||||
use Exception;
|
||||
use JmapClient\Client;
|
||||
use JmapClient\Requests\Calendar\EventChanges;
|
||||
use JmapClient\Requests\Calendar\EventGet;
|
||||
use JmapClient\Responses\ResponseException;
|
||||
use OCA\JMAPC\Exceptions\JmapUnknownMethod;
|
||||
use OCA\JMAPC\Objects\BaseStringCollection;
|
||||
use OCA\JMAPC\Objects\DeltaObject;
|
||||
use OCA\JMAPC\Service\Remote\RemoteEventsService;
|
||||
|
||||
class RemoteEventsServiceFM extends RemoteEventsService {
|
||||
|
||||
public function initialize(Client $dataStore, ?string $dataAccount = null) {
|
||||
|
||||
parent::initialize($dataStore, $dataAccount);
|
||||
|
||||
$dataStore->configureRequestTypes('parameters', 'CalendarEvent.filter', 'OCA\JMAPC\Jmap\FM\Request\Events\EventFilter');
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* delta of changes for specific collection in remote storage
|
||||
*
|
||||
* @since Release 1.0.0
|
||||
*
|
||||
*/
|
||||
public function entityDeltaSpecific(?string $location, string $state, string $granularity = 'D'): DeltaObject {
|
||||
// construct set request
|
||||
$r0 = new EventChanges($this->dataAccount, null, $this->resourceNamespace, $this->resourceEntityLabel);
|
||||
// set state constraint
|
||||
if (!empty($state)) {
|
||||
$r0->state($state);
|
||||
} else {
|
||||
$r0->state('0');
|
||||
}
|
||||
// construct get for created
|
||||
$r1 = new EventGet($this->dataAccount, null, $this->resourceNamespace, $this->resourceEntityLabel);
|
||||
$r1->targetFromRequest($r0, '/created');
|
||||
$r1->property('calendarIds', 'id', 'created', 'updated');
|
||||
// construct get for updated
|
||||
$r2 = new EventGet($this->dataAccount, null, $this->resourceNamespace, $this->resourceEntityLabel);
|
||||
$r2->targetFromRequest($r0, '/updated');
|
||||
$r2->property('calendarIds', 'id', 'created', 'updated');
|
||||
// transceive
|
||||
$bundle = $this->dataStore->perform([$r0, $r1, $r2]);
|
||||
// extract response
|
||||
$response0 = $bundle->response(0);
|
||||
$response1 = $bundle->response(1);
|
||||
$response2 = $bundle->response(2);
|
||||
// determine if command errored
|
||||
if ($response0 instanceof ResponseException) {
|
||||
if ($response0->type() === 'unknownMethod') {
|
||||
throw new JmapUnknownMethod($response0->description(), 1);
|
||||
} else {
|
||||
throw new Exception($response0->type() . ': ' . $response0->description(), 1);
|
||||
}
|
||||
}
|
||||
// convert jmap object to delta object
|
||||
$delta = new DeltaObject();
|
||||
$delta->signature = $response0->stateNew();
|
||||
$delta->additions = new BaseStringCollection();
|
||||
foreach ($response1->objects() as $entry) {
|
||||
if (in_array($location, $entry->in())) {
|
||||
$delta->additions[] = $entry->id();
|
||||
}
|
||||
}
|
||||
$delta->modifications = new BaseStringCollection();
|
||||
foreach ($response2->objects() as $entry) {
|
||||
if (in_array($location, $entry->in())) {
|
||||
$delta->modifications[] = $entry->id();
|
||||
}
|
||||
}
|
||||
$delta->deletions = new BaseStringCollection();
|
||||
foreach ($response0->deleted() as $entry) {
|
||||
$delta->deletions[] = $entry;
|
||||
}
|
||||
|
||||
return $delta;
|
||||
}
|
||||
|
||||
}
|
||||
1229
lib/Service/Remote/RemoteContactsService.php
Normal file
1229
lib/Service/Remote/RemoteContactsService.php
Normal file
File diff suppressed because it is too large
Load Diff
219
lib/Service/Remote/RemoteCoreService.php
Normal file
219
lib/Service/Remote/RemoteCoreService.php
Normal file
@@ -0,0 +1,219 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* @copyright Copyright (c) 2023 Sebastian Krupinski <krupinski01@gmail.com>
|
||||
*
|
||||
* @author Sebastian Krupinski <krupinski01@gmail.com>
|
||||
*
|
||||
* @license AGPL-3.0-or-later
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
namespace KTXM\ProviderJmapc\Service\Remote;
|
||||
|
||||
use Exception;
|
||||
|
||||
use JmapClient\Client;
|
||||
use JmapClient\Requests\Blob\BlobGet;
|
||||
use JmapClient\Requests\Core\SubscriptionGet;
|
||||
use JmapClient\Requests\Core\SubscriptionParameters as SubscriptionParametersRequest;
|
||||
use JmapClient\Requests\Core\SubscriptionSet;
|
||||
use JmapClient\Responses\Core\SubscriptionParameters as SubscriptionParametersResponse;
|
||||
use JmapClient\Responses\ResponseException;
|
||||
use OCA\JMAPC\Exceptions\JmapUnknownMethod;
|
||||
|
||||
class RemoteCoreService {
|
||||
protected Client $dataStore;
|
||||
protected string $dataAccount;
|
||||
|
||||
protected ?string $resourceNamespace = null;
|
||||
protected ?string $resourceCollectionLabel = null;
|
||||
protected ?string $resourceEntityLabel = null;
|
||||
|
||||
public function __construct() {
|
||||
}
|
||||
|
||||
public function initialize(Client $dataStore, ?string $dataAccount = null) {
|
||||
|
||||
$this->dataStore = $dataStore;
|
||||
// evaluate if client is connected
|
||||
if (!$this->dataStore->sessionStatus()) {
|
||||
$this->dataStore->connect();
|
||||
}
|
||||
// determine account
|
||||
if ($dataAccount === null) {
|
||||
if ($this->resourceNamespace !== null) {
|
||||
$account = $dataStore->sessionAccountDefault($this->resourceNamespace, false);
|
||||
} else {
|
||||
$account = $dataStore->sessionAccountDefault('contacts');
|
||||
}
|
||||
$this->dataAccount = $account !== null ? $account->id() : '';
|
||||
} else {
|
||||
$this->dataAccount = $dataAccount;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* list of subscriptions in remote storage
|
||||
*
|
||||
* @since Release 1.0.0
|
||||
*
|
||||
* @return array<string,SubscriptionParametersResponse>
|
||||
*/
|
||||
public function subscriptionList(): array {
|
||||
// construct request
|
||||
$r0 = new SubscriptionGet($this->dataAccount, null, $this->resourceNamespace, $this->resourceCollectionLabel);
|
||||
// transceive
|
||||
$bundle = $this->dataStore->perform([$r0]);
|
||||
// extract response
|
||||
$response = $bundle->response(0);
|
||||
// determine if command errored
|
||||
if ($response instanceof ResponseException) {
|
||||
if ($response->type() === 'unknownMethod') {
|
||||
throw new JmapUnknownMethod($response->description(), 1);
|
||||
} else {
|
||||
throw new Exception($response->type() . ': ' . $response->description(), 1);
|
||||
}
|
||||
}
|
||||
// convert jmap objects to collection objects
|
||||
$list = [];
|
||||
foreach ($response->objects() as $so) {
|
||||
if (!$so instanceof SubscriptionParametersResponse) {
|
||||
continue;
|
||||
}
|
||||
$list[] = $so;
|
||||
}
|
||||
// return collection of collections
|
||||
return $list;
|
||||
}
|
||||
|
||||
/**
|
||||
* retrieve subscription for specific collection
|
||||
*
|
||||
* @since Release 1.0.0
|
||||
*/
|
||||
public function subscriptionFetch(string $id): ?SubscriptionParametersResponse {
|
||||
// construct request
|
||||
$r0 = new SubscriptionGet($this->dataAccount, null, $this->resourceNamespace, $this->resourceCollectionLabel);
|
||||
if (!empty($id)) {
|
||||
$r0->target($id);
|
||||
}
|
||||
// transceive
|
||||
$bundle = $this->dataStore->perform([$r0]);
|
||||
// extract response
|
||||
$response = $bundle->response(0);
|
||||
// convert jmap object to collection object
|
||||
$so = $response->object(0);
|
||||
$to = null;
|
||||
if ($so instanceof SubscriptionParametersResponse) {
|
||||
$to = $so;
|
||||
}
|
||||
return $to;
|
||||
}
|
||||
|
||||
/**
|
||||
* create subscription in remote storage
|
||||
*
|
||||
* @since Release 1.0.0
|
||||
*/
|
||||
public function subscriptionCreate(SubscriptionParametersRequest $so): string {
|
||||
// construct request
|
||||
$r0 = new SubscriptionSet($this->dataAccount, null, $this->resourceNamespace, $this->resourceCollectionLabel);
|
||||
$r0->create('1', $so);
|
||||
// transceive
|
||||
$bundle = $this->dataStore->perform([$r0]);
|
||||
// extract response
|
||||
$response = $bundle->response(0);
|
||||
// return collection id
|
||||
return (string)$response->created()['1']['id'];
|
||||
}
|
||||
|
||||
/**
|
||||
* modify collection in remote storage
|
||||
*
|
||||
* @since Release 1.0.0
|
||||
*/
|
||||
public function subscriptionModify(string $id, SubscriptionParametersRequest $so): string {
|
||||
// construct request
|
||||
$r0 = new SubscriptionSet($this->dataAccount, null, $this->resourceNamespace, $this->resourceCollectionLabel);
|
||||
$r0->update($id, $so);
|
||||
// transceive
|
||||
$bundle = $this->dataStore->perform([$r0]);
|
||||
// extract response
|
||||
$response = $bundle->response(0);
|
||||
// return collection id
|
||||
return array_key_exists($id, $response->updated()) ? (string)$id : '';
|
||||
}
|
||||
|
||||
/**
|
||||
* retrieve blob from remote storage
|
||||
*
|
||||
* @since Release 1.0.0
|
||||
*
|
||||
*/
|
||||
public function blobFetch(string $account, string $id): Object {
|
||||
|
||||
// TODO: testing remove later
|
||||
//$data = '';
|
||||
//$this->dataStore->download($account, $id, $data);
|
||||
//return null;
|
||||
|
||||
// construct get request
|
||||
$r0 = new BlobGet($this->dataAccount, null, $this->resourceNamespace, $this->resourceEntityLabel);
|
||||
// construct object
|
||||
$r0->target($id);
|
||||
// transmit request and receive response
|
||||
$bundle = $this->dataStore->perform([$r0]);
|
||||
// extract response
|
||||
$response = $bundle->response(0);
|
||||
// convert json object to message object and return
|
||||
return $response->object(0);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* deposit bolb to remote storage
|
||||
*
|
||||
* @since Release 1.0.0
|
||||
*
|
||||
*/
|
||||
public function blobDeposit(string $account, string $type, &$data): array {
|
||||
|
||||
// TODO: testing remove later
|
||||
$response = $this->dataStore->upload($account, $type, $data);
|
||||
// convert response to object
|
||||
$response = json_decode($response, true);
|
||||
|
||||
return $response;
|
||||
|
||||
/*
|
||||
// construct set request
|
||||
$r0 = new BlobSet($this->dataAccount, null, $this->resourceNamespace, $this->resourceEntityLabel)
|
||||
// construct object
|
||||
$r0->target($id);
|
||||
// transmit request and receive response
|
||||
$bundle = $this->dataStore->perform([$r0]);
|
||||
// extract response
|
||||
$response = $bundle->response(0);
|
||||
// convert json object to message object and return
|
||||
return $response->object(0);
|
||||
*/
|
||||
|
||||
}
|
||||
}
|
||||
1429
lib/Service/Remote/RemoteEventsService.php
Normal file
1429
lib/Service/Remote/RemoteEventsService.php
Normal file
File diff suppressed because it is too large
Load Diff
897
lib/Service/Remote/RemoteMailService.php
Normal file
897
lib/Service/Remote/RemoteMailService.php
Normal file
@@ -0,0 +1,897 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* SPDX-FileCopyrightText: Sebastian Krupinski <krupinski01@gmail.com>
|
||||
* SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
*/
|
||||
|
||||
namespace KTXM\ProviderJmapc\Service\Remote;
|
||||
|
||||
use Exception;
|
||||
use JmapClient\Client;
|
||||
use JmapClient\Requests\Blob\BlobGet;
|
||||
use JmapClient\Requests\Blob\BlobSet;
|
||||
use JmapClient\Requests\Mail\MailboxGet;
|
||||
use JmapClient\Requests\Mail\MailboxParameters as MailboxParametersRequest;
|
||||
use JmapClient\Requests\Mail\MailboxQuery;
|
||||
use JmapClient\Requests\Mail\MailboxSet;
|
||||
use JmapClient\Requests\Mail\MailChanges;
|
||||
use JmapClient\Requests\Mail\MailGet;
|
||||
use JmapClient\Requests\Mail\MailIdentityGet;
|
||||
use JmapClient\Requests\Mail\MailParameters as MailParametersRequest;
|
||||
use JmapClient\Requests\Mail\MailQuery;
|
||||
use JmapClient\Requests\Mail\MailQueryChanges;
|
||||
use JmapClient\Requests\Mail\MailSet;
|
||||
use JmapClient\Requests\Mail\MailSubmissionSet;
|
||||
use JmapClient\Responses\Mail\MailboxParameters as MailboxParametersResponse;
|
||||
use JmapClient\Responses\Mail\MailParameters as MailParametersResponse;
|
||||
use JmapClient\Responses\ResponseException;
|
||||
use KTXF\Resource\Delta\Delta;
|
||||
use KTXF\Resource\Delta\DeltaCollection;
|
||||
use KTXF\Resource\Filter\Filter;
|
||||
use KTXF\Resource\Filter\IFilter;
|
||||
use KTXF\Resource\Range\IRange;
|
||||
use KTXF\Resource\Range\IRangeTally;
|
||||
use KTXF\Resource\Range\Range;
|
||||
use KTXF\Resource\Range\RangeAnchorType;
|
||||
use KTXF\Resource\Range\RangeTally;
|
||||
use KTXF\Resource\Sort\ISort;
|
||||
use KTXF\Resource\Sort\Sort;
|
||||
use KTXM\ProviderJmapc\Exception\JmapUnknownMethod;
|
||||
use KTXM\ProviderJmapc\Objects\Mail\Collection as MailCollectionObject;
|
||||
|
||||
class RemoteMailService {
|
||||
protected Client $dataStore;
|
||||
protected string $dataAccount;
|
||||
|
||||
protected ?string $resourceNamespace = null;
|
||||
protected ?string $resourceCollectionLabel = null;
|
||||
protected ?string $resourceEntityLabel = null;
|
||||
|
||||
protected array $defaultMailProperties = [
|
||||
'id', 'blobId', 'threadId', 'mailboxIds', 'keywords', 'size',
|
||||
'receivedAt', 'messageId', 'inReplyTo', 'references', 'sender', 'from',
|
||||
'to', 'cc', 'bcc', 'replyTo', 'subject', 'sentAt', 'hasAttachment',
|
||||
'attachments', 'preview', 'bodyStructure', 'bodyValues'
|
||||
];
|
||||
|
||||
public function __construct() {
|
||||
}
|
||||
|
||||
public function initialize(Client $dataStore, ?string $dataAccount = null) {
|
||||
|
||||
$this->dataStore = $dataStore;
|
||||
// evaluate if client is connected
|
||||
if (!$this->dataStore->sessionStatus()) {
|
||||
$this->dataStore->connect();
|
||||
}
|
||||
// determine account
|
||||
if ($dataAccount === null) {
|
||||
if ($this->resourceNamespace !== null) {
|
||||
$account = $dataStore->sessionAccountDefault($this->resourceNamespace, false);
|
||||
} else {
|
||||
$account = $dataStore->sessionAccountDefault('mail');
|
||||
}
|
||||
$this->dataAccount = $account !== null ? $account->id() : '';
|
||||
} else {
|
||||
$this->dataAccount = $dataAccount;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* list of collections in remote storage
|
||||
*
|
||||
* @since Release 1.0.0
|
||||
*/
|
||||
public function collectionList(?string $location = null, IFilter|null $filter = null, ISort|null $sort = null, IRange|null $range = null): array {
|
||||
// construct request
|
||||
$r0 = new MailboxQuery($this->dataAccount, null, $this->resourceNamespace, $this->resourceEntityLabel);
|
||||
// define location
|
||||
if (!empty($location)) {
|
||||
$r0->filter()->in($location);
|
||||
}
|
||||
// define filter
|
||||
if ($filter !== null) {
|
||||
foreach ($filter->conditions() as $condition) {
|
||||
$value = $condition['value'];
|
||||
match($condition['attribute']) {
|
||||
'in' => $r0->filter()->in($value),
|
||||
'name' => $r0->filter()->name($value),
|
||||
'role' => $r0->filter()->role($value),
|
||||
'hasRoles' => $r0->filter()->hasRoles($value),
|
||||
'subscribed' => $r0->filter()->isSubscribed($value),
|
||||
default => null
|
||||
};
|
||||
}
|
||||
}
|
||||
// define order
|
||||
if ($sort !== null) {
|
||||
foreach ($sort->conditions() as $condition) {
|
||||
$direction = $condition['direction'];
|
||||
match($condition['attribute']) {
|
||||
'name' => $r0->sort()->name($direction),
|
||||
'order' => $r0->sort()->order($direction),
|
||||
default => null
|
||||
};
|
||||
}
|
||||
}
|
||||
// construct request
|
||||
$r1 = new MailboxGet($this->dataAccount, null, $this->resourceNamespace, $this->resourceEntityLabel);
|
||||
// define target
|
||||
$r1->targetFromRequest($r0, '/ids');
|
||||
// transceive
|
||||
$bundle = $this->dataStore->perform([$r0, $r1]);
|
||||
// extract response
|
||||
$response = $bundle->response(1);
|
||||
// check for command error
|
||||
if ($response instanceof ResponseException) {
|
||||
if ($response->type() === 'unknownMethod') {
|
||||
throw new JmapUnknownMethod($response->description(), 1);
|
||||
} else {
|
||||
throw new Exception($response->type() . ': ' . $response->description(), 1);
|
||||
}
|
||||
}
|
||||
// convert jmap objects to collection objects
|
||||
$list = [];
|
||||
foreach ($response->objects() as $so) {
|
||||
if (!$so instanceof MailboxParametersResponse) {
|
||||
continue;
|
||||
}
|
||||
$id = $so->id();
|
||||
$list[$id] = $so->parametersRaw();
|
||||
$list[$id]['signature'] = $response->state();
|
||||
}
|
||||
// return collection of collections
|
||||
return $list;
|
||||
}
|
||||
|
||||
/**
|
||||
* fresh instance of collection filter object
|
||||
*
|
||||
* @since Release 1.0.0
|
||||
*/
|
||||
public function collectionListFilter(): Filter {
|
||||
return new Filter(['in', 'name', 'role', 'hasRoles', 'subscribed']);
|
||||
}
|
||||
|
||||
/**
|
||||
* fresh instance of collection sort object
|
||||
*
|
||||
* @since Release 1.0.0
|
||||
*/
|
||||
public function collectionListSort(): Sort {
|
||||
return new Sort(['name', 'order']);
|
||||
}
|
||||
|
||||
/**
|
||||
* check existence of collections in remote storage
|
||||
*
|
||||
* @since Release 1.0.0
|
||||
*/
|
||||
public function collectionExtant(string ...$identifiers): array {
|
||||
$extant = [];
|
||||
// construct request
|
||||
$r0 = new MailboxGet($this->dataAccount, null, $this->resourceNamespace, $this->resourceEntityLabel);
|
||||
$r0->target(...$identifiers);
|
||||
$r0->property('id');
|
||||
// transceive
|
||||
$bundle = $this->dataStore->perform([$r0]);
|
||||
// extract response
|
||||
$response = $bundle->response(0);
|
||||
// check for command error
|
||||
if ($response instanceof ResponseException) {
|
||||
if ($response->type() === 'unknownMethod') {
|
||||
throw new JmapUnknownMethod($response->description(), 1);
|
||||
} else {
|
||||
throw new Exception($response->type() . ': ' . $response->description(), 1);
|
||||
}
|
||||
}
|
||||
// convert jmap objects to collection objects
|
||||
foreach ($response->objects() as $so) {
|
||||
if (!$so instanceof MailboxParametersResponse) {
|
||||
continue;
|
||||
}
|
||||
$extant[$so->id()] = true;
|
||||
}
|
||||
return $extant;
|
||||
}
|
||||
|
||||
/**
|
||||
* retrieve properties for specific collection
|
||||
*
|
||||
* @since Release 1.0.0
|
||||
*/
|
||||
public function collectionFetch(string $identifier): ?array {
|
||||
// construct request
|
||||
$r0 = new MailboxGet($this->dataAccount, null, $this->resourceNamespace, $this->resourceEntityLabel);
|
||||
$r0->target($identifier);
|
||||
// transceive
|
||||
$bundle = $this->dataStore->perform([$r0]);
|
||||
// extract response
|
||||
$response = $bundle->response(0);
|
||||
// check for command error
|
||||
if ($response instanceof ResponseException) {
|
||||
if ($response->type() === 'unknownMethod') {
|
||||
throw new JmapUnknownMethod($response->description(), 1);
|
||||
} else {
|
||||
throw new Exception($response->type() . ': ' . $response->description(), 1);
|
||||
}
|
||||
}
|
||||
// convert jmap object to collection object
|
||||
$so = $response->object(0);
|
||||
$to = null;
|
||||
if ($so instanceof MailboxParametersResponse) {
|
||||
$to = $so->parametersRaw();
|
||||
$to['signature'] = $response->state();
|
||||
}
|
||||
return $to;
|
||||
}
|
||||
|
||||
/**
|
||||
* create collection in remote storage
|
||||
*
|
||||
* @since Release 1.0.0
|
||||
*/
|
||||
public function collectionCreate(string|null $location, array $so): ?array {
|
||||
// convert entity
|
||||
$to = new MailboxParametersRequest();
|
||||
$to->parametersRaw($so);
|
||||
// define location
|
||||
if (!empty($location)) {
|
||||
$to->in($location);
|
||||
}
|
||||
$id = uniqid();
|
||||
// construct request
|
||||
$r0 = new MailboxSet($this->dataAccount, null, $this->resourceNamespace, $this->resourceEntityLabel);
|
||||
$r0->create($id, $to);
|
||||
// transceive
|
||||
$bundle = $this->dataStore->perform([$r0]);
|
||||
// extract response
|
||||
$response = $bundle->response(0);
|
||||
// check for command error
|
||||
if ($response instanceof ResponseException) {
|
||||
if ($response->type() === 'unknownMethod') {
|
||||
throw new JmapUnknownMethod($response->description(), 1);
|
||||
} else {
|
||||
throw new Exception($response->type() . ': ' . $response->description(), 1);
|
||||
}
|
||||
}
|
||||
// check for success
|
||||
$result = $response->createSuccess($id);
|
||||
if ($result !== null) {
|
||||
return array_merge($so, $result);
|
||||
}
|
||||
// check for failure
|
||||
$result = $response->createFailure($id);
|
||||
if ($result !== null) {
|
||||
$type = $result['type'] ?? 'unknownError';
|
||||
$description = $result['description'] ?? 'An unknown error occurred during collection creation.';
|
||||
throw new Exception("$type: $description", 1);
|
||||
}
|
||||
// return null if creation failed without failure reason
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* modify collection in remote storage
|
||||
*
|
||||
* @since Release 1.0.0
|
||||
*
|
||||
*/
|
||||
public function collectionModify(string $identifier, array $so): ?array {
|
||||
// convert entity
|
||||
$to = new MailboxParametersRequest();
|
||||
$to->parametersRaw($so);
|
||||
// construct request
|
||||
$r0 = new MailboxSet($this->dataAccount, null, $this->resourceNamespace, $this->resourceEntityLabel);
|
||||
$r0->update($identifier, $to);
|
||||
// transceive
|
||||
$bundle = $this->dataStore->perform([$r0]);
|
||||
// extract response
|
||||
$response = $bundle->response(0);
|
||||
// check for command error
|
||||
if ($response instanceof ResponseException) {
|
||||
if ($response->type() === 'unknownMethod') {
|
||||
throw new JmapUnknownMethod($response->description(), 1);
|
||||
} else {
|
||||
throw new Exception($response->type() . ': ' . $response->description(), 1);
|
||||
}
|
||||
}
|
||||
// check for success
|
||||
$result = $response->updateSuccess($identifier);
|
||||
if ($result !== null) {
|
||||
return array_merge($so, $result);
|
||||
}
|
||||
// check for failure
|
||||
$result = $response->updateFailure($identifier);
|
||||
if ($result !== null) {
|
||||
$type = $result['type'] ?? 'unknownError';
|
||||
$description = $result['description'] ?? 'An unknown error occurred during collection modification.';
|
||||
throw new Exception("$type: $description", 1);
|
||||
}
|
||||
// return null if modification failed without failure reason
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* delete collection in remote storage
|
||||
*
|
||||
* @since Release 1.0.0
|
||||
*
|
||||
*/
|
||||
public function collectionDestroy(string $identifier, bool $force = false, bool $recursive = false): ?string {
|
||||
// construct request
|
||||
$r0 = new MailboxSet($this->dataAccount, null, $this->resourceNamespace, $this->resourceEntityLabel);
|
||||
$r0->delete($identifier);
|
||||
if ($force) {
|
||||
$r0->destroyContents(true);
|
||||
}
|
||||
// transceive
|
||||
$bundle = $this->dataStore->perform([$r0]);
|
||||
// extract response
|
||||
$response = $bundle->response(0);
|
||||
// check for command error
|
||||
if ($response instanceof ResponseException) {
|
||||
if ($response->type() === 'unknownMethod') {
|
||||
throw new JmapUnknownMethod($response->description(), 1);
|
||||
} else {
|
||||
throw new Exception($response->type() . ': ' . $response->description(), 1);
|
||||
}
|
||||
}
|
||||
// check for success
|
||||
$result = $response->deleteSuccess($identifier);
|
||||
if ($result !== null) {
|
||||
return (string)$result['id'];
|
||||
}
|
||||
// check for failure
|
||||
$result = $response->deleteFailure($identifier);
|
||||
if ($result !== null) {
|
||||
$type = $result['type'] ?? 'unknownError';
|
||||
$description = $result['description'] ?? 'An unknown error occurred during collection deletion.';
|
||||
throw new Exception("$type: $description", 1);
|
||||
}
|
||||
// return null if deletion failed without failure reason
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* retrieve entities from remote storage
|
||||
*
|
||||
* @since Release 1.0.0
|
||||
*/
|
||||
public function entityList(?string $location = null, IFilter|null $filter = null, ISort|null $sort = null, IRange|null $range = null, string|null $granularity = null): array {
|
||||
// construct request
|
||||
$r0 = new MailQuery($this->dataAccount, null, $this->resourceNamespace, $this->resourceEntityLabel);
|
||||
// define location
|
||||
if (!empty($location)) {
|
||||
$r0->filter()->in($location);
|
||||
}
|
||||
// define filter
|
||||
if ($filter !== null) {
|
||||
foreach ($filter->conditions() as $condition) {
|
||||
$value = $condition['value'];
|
||||
match($condition['attribute']) {
|
||||
'*' => $r0->filter()->text($value),
|
||||
'in' => $r0->filter()->in($value),
|
||||
'inOmit' => $r0->filter()->inOmit($value),
|
||||
'from' => $r0->filter()->from($value),
|
||||
'to' => $r0->filter()->to($value),
|
||||
'cc' => $r0->filter()->cc($value),
|
||||
'bcc' => $r0->filter()->bcc($value),
|
||||
'subject' => $r0->filter()->subject($value),
|
||||
'body' => $r0->filter()->body($value),
|
||||
'attachmentPresent' => $r0->filter()->hasAttachment($value),
|
||||
'tagPresent' => $r0->filter()->keywordPresent($value),
|
||||
'tagAbsent' => $r0->filter()->keywordAbsent($value),
|
||||
'before' => $r0->filter()->receivedBefore($value),
|
||||
'after' => $r0->filter()->receivedAfter($value),
|
||||
'min' => $r0->filter()->sizeMin((int)$value),
|
||||
'max' => $r0->filter()->sizeMax((int)$value),
|
||||
default => null
|
||||
};
|
||||
}
|
||||
}
|
||||
// define order
|
||||
if ($sort !== null) {
|
||||
foreach ($sort->conditions() as $condition) {
|
||||
$direction = $condition['direction'];
|
||||
match($condition['attribute']) {
|
||||
'from' => $r0->sort()->from($direction),
|
||||
'to' => $r0->sort()->to($direction),
|
||||
'subject' => $r0->sort()->subject($direction),
|
||||
'received' => $r0->sort()->received($direction),
|
||||
'sent' => $r0->sort()->sent($direction),
|
||||
'size' => $r0->sort()->size($direction),
|
||||
'tag' => $r0->sort()->keyword($direction),
|
||||
default => null
|
||||
};
|
||||
}
|
||||
}
|
||||
// define range
|
||||
if ($range !== null) {
|
||||
if ($range instanceof RangeTally && $range->getAnchor() === RangeAnchorType::ABSOLUTE) {
|
||||
$r0->limitAbsolute($range->getPosition(), $range->getTally());
|
||||
}
|
||||
if ($range instanceof RangeTally && $range->getAnchor() === RangeAnchorType::RELATIVE) {
|
||||
$r0->limitRelative($range->getPosition(), $range->getTally());
|
||||
}
|
||||
}
|
||||
// construct get request
|
||||
$r1 = new MailGet($this->dataAccount, null, $this->resourceNamespace, $this->resourceEntityLabel);
|
||||
// set target to query request
|
||||
$r1->targetFromRequest($r0, '/ids');
|
||||
// select properties to return
|
||||
$r1->property(...$this->defaultMailProperties);
|
||||
$r1->bodyAll(true);
|
||||
// transmit request and receive response
|
||||
$bundle = $this->dataStore->perform([$r0, $r1]);
|
||||
// extract response
|
||||
$response = $bundle->response(1);
|
||||
// convert json objects to message objects
|
||||
$state = $response->state();
|
||||
$list = $response->objects();
|
||||
foreach ($list as $id => $entry) {
|
||||
if (!$entry instanceof MailParametersResponse) {
|
||||
continue;
|
||||
}
|
||||
$list[$id] = $entry->parametersRaw();
|
||||
}
|
||||
// return message collection
|
||||
return ['list' => $list, 'state' => $state];
|
||||
}
|
||||
|
||||
/**
|
||||
* fresh instance of object filter
|
||||
*
|
||||
* @since Release 1.0.0
|
||||
*/
|
||||
public function entityListFilter(): Filter {
|
||||
return new Filter([
|
||||
'in',
|
||||
'inOmit',
|
||||
'text',
|
||||
'from',
|
||||
'to',
|
||||
'cc',
|
||||
'bcc',
|
||||
'subject',
|
||||
'body',
|
||||
'attachmentPresent',
|
||||
'tagPresent',
|
||||
'tagAbsent',
|
||||
'receivedBefore',
|
||||
'receivedAfter',
|
||||
'sizeMin',
|
||||
'sizeMax'
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* fresh instance of object sort
|
||||
*
|
||||
* @since Release 1.0.0
|
||||
*/
|
||||
public function entityListSort(): Sort {
|
||||
return new Sort([
|
||||
'received',
|
||||
'sent',
|
||||
'from',
|
||||
'to',
|
||||
'subject',
|
||||
'size',
|
||||
'tag'
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* fresh instance of object range
|
||||
*
|
||||
* @since Release 1.0.0
|
||||
*/
|
||||
public function entityListRange(): RangeTally {
|
||||
return new RangeTally();
|
||||
}
|
||||
|
||||
/**
|
||||
* check existence of entities in remote storage
|
||||
*
|
||||
* @since Release 1.0.0
|
||||
*/
|
||||
public function entityExtant(string ...$identifiers): array {
|
||||
$extant = [];
|
||||
// construct request
|
||||
$r0 = new MailGet($this->dataAccount, null, $this->resourceNamespace, $this->resourceEntityLabel);
|
||||
$r0->target(...$identifiers);
|
||||
$r0->property('id');
|
||||
// transmit request and receive response
|
||||
$bundle = $this->dataStore->perform([$r0]);
|
||||
// extract response
|
||||
$response = $bundle->response(0);
|
||||
// convert json objects to message objects
|
||||
foreach ($response->objects() as $so) {
|
||||
if (!$so instanceof MailParametersResponse) {
|
||||
continue;
|
||||
}
|
||||
$extant[$so->id()] = true;
|
||||
}
|
||||
return $extant;
|
||||
}
|
||||
|
||||
/**
|
||||
* delta for entities in remote storage
|
||||
*
|
||||
* @since Release 1.0.0
|
||||
*
|
||||
* @return Delta
|
||||
*/
|
||||
public function entityDelta(?string $location, string $state, string $granularity = 'D'): Delta {
|
||||
|
||||
if (empty($state)) {
|
||||
$results = $this->entityList($location, null, null, null, 'B');
|
||||
$delta = new Delta();
|
||||
$delta->signature = $results['state'];
|
||||
foreach ($results['list'] as $entry) {
|
||||
$delta->additions[] = $entry['id'];
|
||||
}
|
||||
return $delta;
|
||||
}
|
||||
if (empty($location)) {
|
||||
return $this->entityDeltaDefault($state, $granularity);
|
||||
} else {
|
||||
return $this->entityDeltaSpecific($location, $state, $granularity);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* delta of changes for specific collection in remote storage
|
||||
*
|
||||
* @since Release 1.0.0
|
||||
*
|
||||
*/
|
||||
public function entityDeltaSpecific(?string $location, string $state, string $granularity = 'D'): Delta {
|
||||
// construct set request
|
||||
$r0 = new MailQueryChanges($this->dataAccount, null, $this->resourceNamespace, $this->resourceEntityLabel);
|
||||
// set location constraint
|
||||
if (!empty($location)) {
|
||||
$r0->filter()->in($location);
|
||||
}
|
||||
// set state constraint
|
||||
if (!empty($state)) {
|
||||
$r0->state($state);
|
||||
} else {
|
||||
$r0->state('0');
|
||||
}
|
||||
// transceive
|
||||
$bundle = $this->dataStore->perform([$r0]);
|
||||
// extract response
|
||||
$response = $bundle->response(0);
|
||||
// check for command error
|
||||
if ($response instanceof ResponseException) {
|
||||
if ($response->type() === 'unknownMethod') {
|
||||
throw new JmapUnknownMethod($response->description(), 1);
|
||||
} else {
|
||||
throw new Exception($response->type() . ': ' . $response->description(), 1);
|
||||
}
|
||||
}
|
||||
// convert jmap object to delta object
|
||||
$delta = new Delta();
|
||||
$delta->signature = $response->stateNew();
|
||||
$delta->additions = new DeltaCollection(array_column($response->added(), 'id'));
|
||||
$delta->modifications = new DeltaCollection([]);
|
||||
$delta->deletions = new DeltaCollection(array_column($response->removed(), 'id'));
|
||||
|
||||
return $delta;
|
||||
}
|
||||
|
||||
/**
|
||||
* delta of changes in remote storage
|
||||
*
|
||||
* @since Release 1.0.0
|
||||
*
|
||||
*/
|
||||
public function entityDeltaDefault(string $state, string $granularity = 'D'): Delta {
|
||||
// construct set request
|
||||
$r0 = new MailChanges($this->dataAccount, null, $this->resourceNamespace, $this->resourceEntityLabel);
|
||||
// set state constraint
|
||||
if (!empty($state)) {
|
||||
$r0->state($state);
|
||||
} else {
|
||||
$r0->state('');
|
||||
}
|
||||
// transceive
|
||||
$bundle = $this->dataStore->perform([$r0]);
|
||||
// extract response
|
||||
$response = $bundle->response(0);
|
||||
// check for command error
|
||||
if ($response instanceof ResponseException) {
|
||||
if ($response->type() === 'unknownMethod') {
|
||||
throw new JmapUnknownMethod($response->description(), 1);
|
||||
} else {
|
||||
throw new Exception($response->type() . ': ' . $response->description(), 1);
|
||||
}
|
||||
}
|
||||
// convert jmap object to delta object
|
||||
$delta = new Delta();
|
||||
$delta->signature = $response->stateNew();
|
||||
$delta->additions = new DeltaCollection(array_column($response->added(), 'id'));
|
||||
$delta->modifications = new DeltaCollection([]);
|
||||
$delta->deletions = new DeltaCollection(array_column($response->removed(), 'id'));
|
||||
|
||||
return $delta;
|
||||
}
|
||||
|
||||
/**
|
||||
* retrieve entity from remote storage
|
||||
*
|
||||
* @since Release 1.0.0
|
||||
*/
|
||||
public function entityFetch(string ...$identifiers): ?array {
|
||||
// construct request
|
||||
$r0 = new MailGet($this->dataAccount, null, $this->resourceNamespace, $this->resourceEntityLabel);
|
||||
$r0->target(...$identifiers);
|
||||
// select properties to return
|
||||
$r0->property(...$this->defaultMailProperties);
|
||||
$r0->bodyAll(true);
|
||||
// transmit request and receive response
|
||||
$bundle = $this->dataStore->perform([$r0]);
|
||||
// extract response
|
||||
$response = $bundle->response(0);
|
||||
// convert json objects to message objects
|
||||
$list = [];
|
||||
foreach ($response->objects() as $so) {
|
||||
if (!$so instanceof MailParametersResponse) {
|
||||
continue;
|
||||
}
|
||||
$id = $so->id();
|
||||
$list[$id] = $so->parametersRaw();
|
||||
$list[$id]['signature'] = $response->state();
|
||||
}
|
||||
// return message collection
|
||||
return $list;
|
||||
}
|
||||
|
||||
/**
|
||||
* create entity in remote storage
|
||||
*
|
||||
* @since Release 1.0.0
|
||||
*/
|
||||
public function entityCreate(string $location, array $so): ?array {
|
||||
// convert entity
|
||||
$to = new MailParametersRequest();
|
||||
$to->parametersRaw($so);
|
||||
$to->in($location);
|
||||
$id = uniqid();
|
||||
// construct request
|
||||
$r0 = new MailSet($this->dataAccount, null, $this->resourceNamespace, $this->resourceEntityLabel);
|
||||
$r0->create($id, $to);
|
||||
// transceive
|
||||
$bundle = $this->dataStore->perform([$r0]);
|
||||
// extract response
|
||||
$response = $bundle->response(0);
|
||||
// check for command error
|
||||
if ($response instanceof ResponseException) {
|
||||
if ($response->type() === 'unknownMethod') {
|
||||
throw new JmapUnknownMethod($response->description(), 1);
|
||||
} else {
|
||||
throw new Exception($response->type() . ': ' . $response->description(), 1);
|
||||
}
|
||||
}
|
||||
// check for success
|
||||
$result = $response->createSuccess($id);
|
||||
if ($result !== null) {
|
||||
return array_merge($so, $result);
|
||||
}
|
||||
// check for failure
|
||||
$result = $response->createFailure($id);
|
||||
if ($result !== null) {
|
||||
$type = $result['type'] ?? 'unknownError';
|
||||
$description = $result['description'] ?? 'An unknown error occurred during collection creation.';
|
||||
throw new Exception("$type: $description", 1);
|
||||
}
|
||||
// return null if creation failed without failure reason
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* update entity in remote storage
|
||||
*
|
||||
* @since Release 1.0.0
|
||||
*/
|
||||
public function entityModify(array $so): ?array {
|
||||
// extract entity id
|
||||
$id = $so['id'];
|
||||
// convert entity
|
||||
$to = new MailParametersRequest();
|
||||
$to->parametersRaw($so);
|
||||
// construct request
|
||||
$r0 = new MailSet($this->dataAccount, null, $this->resourceNamespace, $this->resourceEntityLabel);
|
||||
$r0->update($id, $to);
|
||||
// transmit request and receive response
|
||||
$bundle = $this->dataStore->perform([$r0]);
|
||||
// extract response
|
||||
$response = $bundle->response(0);
|
||||
// determine if command succeeded
|
||||
if (array_key_exists($id, $response->updated())) {
|
||||
// update entity
|
||||
$ro = $response->updated()[$id];
|
||||
$so = array_merge($so, $ro);
|
||||
return $so;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* delete entity from remote storage
|
||||
*
|
||||
* @since Release 1.0.0
|
||||
*/
|
||||
public function entityDelete(string $id): ?string {
|
||||
// construct set request
|
||||
$r0 = new MailSet($this->dataAccount, null, $this->resourceNamespace, $this->resourceEntityLabel);
|
||||
// construct object
|
||||
$r0->delete($id);
|
||||
// transmit request and receive response
|
||||
$bundle = $this->dataStore->perform([$r0]);
|
||||
// extract response
|
||||
$response = $bundle->response(0);
|
||||
// determine if command succeeded
|
||||
if (array_search($id, $response->deleted()) !== false) {
|
||||
return $response->stateNew();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* copy entity in remote storage
|
||||
*
|
||||
* @since Release 1.0.0
|
||||
*
|
||||
*/
|
||||
public function entityCopy(string $location, MailMessageObject $so): ?MailMessageObject {
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* move entity in remote storage
|
||||
*
|
||||
* @since Release 1.0.0
|
||||
*
|
||||
*/
|
||||
public function entityMove(string $location, array $so): ?array {
|
||||
// extract entity id
|
||||
$id = $so['id'];
|
||||
// construct request
|
||||
$r0 = new MailSet($this->dataAccount, null, $this->resourceNamespace, $this->resourceEntityLabel);
|
||||
$r0->update($id)->in($location);
|
||||
// transmit request and receive response
|
||||
$bundle = $this->dataStore->perform([$r0]);
|
||||
// extract response
|
||||
$response = $bundle->response(0);
|
||||
// determine if command succeeded
|
||||
if (array_key_exists($id, $response->updated())) {
|
||||
$so = array_merge($so, ['mailboxIds' => [$location => true]]);
|
||||
return $so;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* send entity
|
||||
*
|
||||
* @since Release 1.0.0
|
||||
*
|
||||
*/
|
||||
public function entitySend(string $identity, MailMessageObject $message, ?string $presendLocation = null, ?string $postsendLocation = null): string {
|
||||
// determine if pre-send location is present
|
||||
if ($presendLocation === null || empty($presendLocation)) {
|
||||
throw new Exception('Pre-Send Location is missing', 1);
|
||||
}
|
||||
// determine if post-send location is present
|
||||
if ($postsendLocation === null || empty($postsendLocation)) {
|
||||
throw new Exception('Post-Send Location is missing', 1);
|
||||
}
|
||||
// determine if we have the basic required data and fail otherwise
|
||||
if (empty($message->getFrom())) {
|
||||
throw new Exception('Missing Requirements: Message MUST have a From address', 1);
|
||||
}
|
||||
if (empty($message->getTo())) {
|
||||
throw new Exception('Missing Requirements: Message MUST have a To address(es)', 1);
|
||||
}
|
||||
// determine if message has attachments
|
||||
if (count($message->getAttachments()) > 0) {
|
||||
// process attachments first
|
||||
$message = $this->depositAttachmentsFromMessage($message);
|
||||
}
|
||||
// convert from address object to string
|
||||
$from = $message->getFrom()->getAddress();
|
||||
// convert to, cc and bcc address object arrays to single strings array
|
||||
$to = array_map(
|
||||
function ($entry) { return $entry->getAddress(); },
|
||||
array_merge($message->getTo(), $message->getCc(), $message->getBcc())
|
||||
);
|
||||
unset($cc, $bcc);
|
||||
// construct set request
|
||||
$r0 = new MailSet($this->dataAccount, null, $this->resourceNamespace, $this->resourceEntityLabel);
|
||||
$r0->create('1', $message)->in($presendLocation);
|
||||
// construct set request
|
||||
$r1 = new MailSubmissionSet($this->dataAccount, null, $this->resourceNamespace, $this->resourceEntityLabel);
|
||||
// construct envelope
|
||||
$e1 = $r1->create('2');
|
||||
$e1->identity($identity);
|
||||
$e1->message('#1');
|
||||
$e1->from($from);
|
||||
$e1->to($to);
|
||||
// transmit request and receive response
|
||||
$bundle = $this->dataStore->perform([$r0, $r1]);
|
||||
// extract response
|
||||
$response = $bundle->response(1);
|
||||
// return collection information
|
||||
return (string)$response->created()['2']['id'];
|
||||
}
|
||||
|
||||
/**
|
||||
* retrieve collection entity attachment from remote storage
|
||||
*
|
||||
* @since Release 1.0.0
|
||||
*
|
||||
*/
|
||||
public function depositAttachmentsFromMessage(MailMessageObject $message): MailMessageObject {
|
||||
|
||||
$parameters = $message->toJmap();
|
||||
$attachments = $message->getAttachments();
|
||||
$matches = [];
|
||||
|
||||
$this->findAttachmentParts($parameters['bodyStructure'], $matches);
|
||||
|
||||
foreach ($attachments as $attachment) {
|
||||
$part = $attachment->toJmap();
|
||||
if (isset($matches[$part->getId()])) {
|
||||
// deposit attachment in data store
|
||||
$response = $this->blobDeposit($account, $part->getType(), $attachment->getContents());
|
||||
// transfer blobId and size to mail part
|
||||
$matches[$part->getId()]->blobId = $response['blobId'];
|
||||
$matches[$part->getId()]->size = $response['size'];
|
||||
unset($matches[$part->getId()]->partId);
|
||||
}
|
||||
}
|
||||
|
||||
return (new MailMessageObject())->fromJmap($parameters);
|
||||
|
||||
}
|
||||
|
||||
protected function findAttachmentParts(object &$part, array &$matches) {
|
||||
|
||||
if ($part->disposition === 'attachment' || $part->disposition === 'inline') {
|
||||
$matches[$part->partId] = $part;
|
||||
}
|
||||
|
||||
foreach ($part->subParts as $entry) {
|
||||
$this->findAttachmentParts($entry, $matches);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* retrieve identity from remote storage
|
||||
*
|
||||
* @since Release 1.0.0
|
||||
*
|
||||
*/
|
||||
public function identityFetch(?string $account = null): array {
|
||||
if ($account === null) {
|
||||
$account = $this->dataAccount;
|
||||
}
|
||||
// construct set request
|
||||
$r0 = new MailIdentityGet($this->dataAccount, null, $this->resourceNamespace, $this->resourceEntityLabel);
|
||||
// transmit request and receive response
|
||||
$bundle = $this->dataStore->perform([$r0]);
|
||||
// extract response
|
||||
$response = $bundle->response(0);
|
||||
// convert json object to message object and return
|
||||
return $response->objects();
|
||||
}
|
||||
|
||||
}
|
||||
213
lib/Service/Remote/RemoteService.php
Normal file
213
lib/Service/Remote/RemoteService.php
Normal file
@@ -0,0 +1,213 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* SPDX-FileCopyrightText: Sebastian Krupinski <krupinski01@gmail.com>
|
||||
* SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
*/
|
||||
|
||||
namespace KTXM\ProviderJmapc\Service\Remote;
|
||||
|
||||
use JmapClient\Authentication\Basic;
|
||||
use JmapClient\Authentication\Bearer;
|
||||
use JmapClient\Authentication\JsonBasic;
|
||||
use JmapClient\Authentication\JsonBasicCookie;
|
||||
use JmapClient\Client as JmapClient;
|
||||
use KTXF\Resource\Provider\ResourceServiceBaseInterface;
|
||||
use KTXF\Resource\Provider\ResourceServiceIdentityBasic;
|
||||
use KTXF\Resource\Provider\ResourceServiceIdentityBearer;
|
||||
use KTXF\Resource\Provider\ResourceServiceIdentityOAuth;
|
||||
use KTXF\Resource\Provider\ResourceServiceLocationUri;
|
||||
use KTXM\ProviderJmapc\Providers\Mail\Service;
|
||||
use KTXM\ProviderJmapc\Service\Remote\FM\RemoteContactsServiceFM;
|
||||
use KTXM\ProviderJmapc\Service\Remote\FM\RemoteCoreServiceFM;
|
||||
use KTXM\ProviderJmapc\Service\Remote\FM\RemoteEventsServiceFM;
|
||||
|
||||
class RemoteService {
|
||||
static string $clientTransportAgent = 'KtrixJMAP/1.0 (1.0; x64)';
|
||||
//public static string $clientTransportAgent = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:130.0) Gecko/20100101 Firefox/130.0';
|
||||
|
||||
/**
|
||||
* Initialize remote data store client
|
||||
*
|
||||
* @since Release 1.0.0
|
||||
*/
|
||||
public static function freshClient(Service $service): JmapClient {
|
||||
|
||||
// defaults
|
||||
$client = new JmapClient();
|
||||
$client->setTransportAgent(self::$clientTransportAgent);
|
||||
$location = $service->getLocation();
|
||||
$identity = $service->getIdentity();
|
||||
|
||||
// location
|
||||
if ($location instanceof ResourceServiceLocationUri === false) {
|
||||
throw new \InvalidArgumentException('Service location is not a valid URI');
|
||||
}
|
||||
$client->configureTransportMode($location->getScheme());
|
||||
$client->setHost($location->getHost() . ':' . $location->getPort());
|
||||
if (!empty($location->getPath())) {
|
||||
$client->setDiscoveryPath($location->getPath());
|
||||
}
|
||||
$client->configureTransportVerification((bool)$location->getVerifyPeer());
|
||||
// authentication
|
||||
if (($identity instanceof ResourceServiceIdentityBasic) === false) {
|
||||
throw new \InvalidArgumentException('Service identity is not a valid Basic or Bearer authentication');
|
||||
}
|
||||
|
||||
if ($identity instanceof ResourceServiceIdentityBasic) {
|
||||
$client->setAuthentication(new Basic(
|
||||
$identity->getIdentity(),
|
||||
$identity->getSecret()
|
||||
));
|
||||
}
|
||||
// debugging
|
||||
if ($service->getDebug()) {
|
||||
$client->configureTransportLogState(true);
|
||||
$client->configureTransportLogLocation(
|
||||
sys_get_temp_dir() . '/' . $location->getHost() . '-' . $identity->getIdentity() . '.log'
|
||||
);
|
||||
}
|
||||
// return
|
||||
return $client;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Destroys remote data store client (Jmap Client)
|
||||
*
|
||||
* @since Release 1.0.0
|
||||
*/
|
||||
public static function destroyClient(JmapClient $Client): void {
|
||||
|
||||
// destroy remote data store client
|
||||
$Client = null;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Appropriate Mail Service for Connection
|
||||
*
|
||||
* @since Release 1.0.0
|
||||
*/
|
||||
public static function coreService(JmapClient $Client, ?string $dataAccount = null): RemoteCoreService {
|
||||
// determine if client is connected
|
||||
if (!$Client->sessionStatus()) {
|
||||
$Client->connect();
|
||||
}
|
||||
// construct service based on capabilities
|
||||
if ($Client->sessionCapable('https://www.fastmail.com/dev/user', false)) {
|
||||
$service = new RemoteCoreServiceFM();
|
||||
} else {
|
||||
$service = new RemoteCoreService();
|
||||
}
|
||||
$service->initialize($Client, $dataAccount);
|
||||
return $service;
|
||||
}
|
||||
|
||||
/**
|
||||
* Appropriate Mail Service for Connection
|
||||
*
|
||||
* @since Release 1.0.0
|
||||
*/
|
||||
public static function mailService(JmapClient $Client, ?string $dataAccount = null): RemoteMailService {
|
||||
// determine if client is connected
|
||||
if (!$Client->sessionStatus()) {
|
||||
$Client->connect();
|
||||
}
|
||||
$service = new RemoteMailService();
|
||||
$service->initialize($Client, $dataAccount);
|
||||
return $service;
|
||||
}
|
||||
|
||||
/**
|
||||
* Appropriate Contacts Service for Connection
|
||||
*
|
||||
* @since Release 1.0.0
|
||||
*/
|
||||
public static function contactsService(JmapClient $Client, ?string $dataAccount = null): RemoteContactsService {
|
||||
// determine if client is connected
|
||||
if (!$Client->sessionStatus()) {
|
||||
$Client->connect();
|
||||
}
|
||||
// construct service based on capabilities
|
||||
if ($Client->sessionCapable('https://www.fastmail.com/dev/contacts', false)) {
|
||||
$service = new RemoteContactsServiceFM();
|
||||
} else {
|
||||
$service = new RemoteContactsService();
|
||||
}
|
||||
$service->initialize($Client, $dataAccount);
|
||||
return $service;
|
||||
}
|
||||
|
||||
/**
|
||||
* Appropriate Events Service for Connection
|
||||
*
|
||||
* @since Release 1.0.0
|
||||
*/
|
||||
public static function eventsService(JmapClient $Client, ?string $dataAccount = null): RemoteEventsService {
|
||||
// determine if client is connected
|
||||
if (!$Client->sessionStatus()) {
|
||||
$Client->connect();
|
||||
}
|
||||
// construct service based on capabilities
|
||||
if ($Client->sessionCapable('https://www.fastmail.com/dev/calendars', false)) {
|
||||
$service = new RemoteEventsServiceFM();
|
||||
} else {
|
||||
$service = new RemoteEventsService();
|
||||
}
|
||||
$service->initialize($Client, $dataAccount);
|
||||
return $service;
|
||||
}
|
||||
|
||||
/**
|
||||
* Appropriate Tasks Service for Connection
|
||||
*
|
||||
* @since Release 1.0.0
|
||||
*/
|
||||
public static function tasksService(JmapClient $Client, ?string $dataAccount = null): RemoteTasksService {
|
||||
// determine if client is connected
|
||||
if (!$Client->sessionStatus()) {
|
||||
$Client->connect();
|
||||
}
|
||||
$service = new RemoteTasksService();
|
||||
$service->initialize($Client, $dataAccount);
|
||||
return $service;
|
||||
}
|
||||
|
||||
public static function cookieStoreRetrieve(mixed $id): ?array {
|
||||
|
||||
$file = sys_get_temp_dir() . DIRECTORY_SEPARATOR . (string)$id . '.jmapc';
|
||||
|
||||
if (!file_exists($file)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$data = file_get_contents($file);
|
||||
$crypto = \OC::$server->get(\OCP\Security\ICrypto::class);
|
||||
$data = $crypto->decrypt($data);
|
||||
|
||||
if (!empty($data)) {
|
||||
return json_decode($data, true);
|
||||
}
|
||||
|
||||
return null;
|
||||
|
||||
}
|
||||
|
||||
public static function cookieStoreDeposit(mixed $id, array $value): void {
|
||||
|
||||
if (empty($value)) {
|
||||
return;
|
||||
}
|
||||
|
||||
$crypto = \OC::$server->get(\OCP\Security\ICrypto::class);
|
||||
$data = $crypto->encrypt(json_encode($value));
|
||||
|
||||
$file = sys_get_temp_dir() . DIRECTORY_SEPARATOR . (string)$id . '.jmapc';
|
||||
file_put_contents($file, $data);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
652
lib/Service/Remote/RemoteTasksService.php
Normal file
652
lib/Service/Remote/RemoteTasksService.php
Normal file
@@ -0,0 +1,652 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* @copyright Copyright (c) 2023 Sebastian Krupinski <krupinski01@gmail.com>
|
||||
*
|
||||
* @author Sebastian Krupinski <krupinski01@gmail.com>
|
||||
*
|
||||
* @license AGPL-3.0-or-later
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
namespace KTXM\ProviderJmapc\Service\Remote;
|
||||
|
||||
use DateTimeImmutable;
|
||||
use DateTimeZone;
|
||||
use Exception;
|
||||
|
||||
use JmapClient\Client;
|
||||
|
||||
use JmapClient\Requests\Tasks\TaskChanges;
|
||||
use JmapClient\Requests\Tasks\TaskGet;
|
||||
use JmapClient\Requests\Tasks\TaskListGet;
|
||||
use JmapClient\Requests\Tasks\TaskListSet;
|
||||
use JmapClient\Requests\Tasks\TaskParameters as TaskParametersRequest;
|
||||
use JmapClient\Requests\Tasks\TaskQuery;
|
||||
use JmapClient\Requests\Tasks\TaskQueryChanges;
|
||||
use JmapClient\Requests\Tasks\TaskSet;
|
||||
use JmapClient\Responses\ResponseException;
|
||||
use JmapClient\Responses\Tasks\TaskListParameters as TaskListParametersResponse;
|
||||
use JmapClient\Responses\Tasks\TaskParameters as TaskParametersResponse;
|
||||
use OCA\JMAPC\Exceptions\JmapUnknownMethod;
|
||||
use OCA\JMAPC\Objects\BaseStringCollection;
|
||||
use OCA\JMAPC\Objects\DeltaObject;
|
||||
use OCA\JMAPC\Objects\OriginTypes;
|
||||
use OCA\JMAPC\Objects\Task\TaskCollectionObject;
|
||||
use OCA\JMAPC\Objects\Task\TaskObject;
|
||||
use OCA\JMAPC\Store\Common\Filters\IFilter;
|
||||
use OCA\JMAPC\Store\Common\Range\IRangeTally;
|
||||
use OCA\JMAPC\Store\Common\Sort\ISort;
|
||||
|
||||
class RemoteTasksService {
|
||||
public ?DateTimeZone $SystemTimeZone = null;
|
||||
public ?DateTimeZone $UserTimeZone = null;
|
||||
|
||||
protected Client $dataStore;
|
||||
protected string $dataAccount;
|
||||
|
||||
protected ?string $resourceNamespace = null;
|
||||
protected ?string $resourceCollectionLabel = null;
|
||||
protected ?string $resourceEntityLabel = null;
|
||||
|
||||
protected array $collectionPropertiesDefault = [];
|
||||
protected array $collectionPropertiesBasic = [];
|
||||
protected array $entityPropertiesDefault = [];
|
||||
protected array $entityPropertiesBasic = [
|
||||
'id', 'calendarIds', 'uid', 'created', 'updated'
|
||||
];
|
||||
|
||||
public function __construct() {
|
||||
}
|
||||
|
||||
public function initialize(Client $dataStore, ?string $dataAccount = null) {
|
||||
|
||||
$this->dataStore = $dataStore;
|
||||
// evaluate if client is connected
|
||||
if (!$this->dataStore->sessionStatus()) {
|
||||
$this->dataStore->connect();
|
||||
}
|
||||
// determine account
|
||||
if ($dataAccount === null) {
|
||||
if ($this->resourceNamespace !== null) {
|
||||
$account = $dataStore->sessionAccountDefault($this->resourceNamespace, false);
|
||||
} else {
|
||||
$account = $dataStore->sessionAccountDefault('contacts');
|
||||
}
|
||||
$this->dataAccount = $account !== null ? $account->id() : '';
|
||||
} else {
|
||||
$this->dataAccount = $dataAccount;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* retrieve properties for specific collection
|
||||
*
|
||||
* @since Release 1.0.0
|
||||
*
|
||||
*/
|
||||
public function collectionFetch(string $id): ?TaskCollectionObject {
|
||||
// construct request
|
||||
$r0 = new TaskListGet($this->dataAccount, null, $this->resourceNamespace, $this->resourceCollectionLabel);
|
||||
if (!empty($id)) {
|
||||
$r0->target($id);
|
||||
}
|
||||
// transceive
|
||||
$bundle = $this->dataStore->perform([$r0]);
|
||||
// extract response
|
||||
$response = $bundle->response(0);
|
||||
// convert jmap object to collection object
|
||||
if ($response->object(0) instanceof TaskListParametersResponse) {
|
||||
$co = $response->object(0);
|
||||
$collection = new TaskCollectionObject();
|
||||
$collection->Id = $co->id();
|
||||
$collection->Label = $co->label();
|
||||
$collection->Description = $co->description();
|
||||
$collection->Priority = $co->priority();
|
||||
$collection->Visibility = $co->visible();
|
||||
$collection->Color = $co->color();
|
||||
return $collection;
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* create collection in remote storage
|
||||
*
|
||||
* @since Release 1.0.0
|
||||
*
|
||||
*/
|
||||
public function collectionCreate(TaskCollectionObject $collection): string {
|
||||
// construct request
|
||||
$r0 = new TaskListSet($this->dataAccount, null, $this->resourceNamespace, $this->resourceCollectionLabel);
|
||||
$m0 = $r0->create('1');
|
||||
if ($collection->Label) {
|
||||
$m0->label($collection->Label);
|
||||
}
|
||||
if ($collection->Description) {
|
||||
$m0->description($collection->Description);
|
||||
}
|
||||
if ($collection->Priority) {
|
||||
$m0->priority($collection->Priority);
|
||||
}
|
||||
if ($collection->Visibility) {
|
||||
$m0->visible($collection->Visibility);
|
||||
}
|
||||
if ($collection->Color) {
|
||||
$m0->color($collection->Color);
|
||||
}
|
||||
// transceive
|
||||
$bundle = $this->dataStore->perform([$r0]);
|
||||
// extract response
|
||||
$response = $bundle->response(0);
|
||||
// return collection id
|
||||
return (string)$response->created()['1']['id'];
|
||||
}
|
||||
|
||||
/**
|
||||
* update collection in remote storage
|
||||
*
|
||||
* @since Release 1.0.0
|
||||
*
|
||||
*/
|
||||
public function collectionUpdate(string $id, TaskCollectionObject $collection): string {
|
||||
// construct request
|
||||
$r0 = new TaskListSet($this->dataAccount, null, $this->resourceNamespace, $this->resourceCollectionLabel);
|
||||
$m0 = $r0->update($id);
|
||||
$m0->label($collection->Label);
|
||||
$m0->description($collection->Description);
|
||||
$m0->priority($collection->Priority);
|
||||
$m0->visible($collection->Visibility);
|
||||
$m0->color($collection->Color);
|
||||
// transceive
|
||||
$bundle = $this->dataStore->perform([$r0]);
|
||||
// extract response
|
||||
$response = $bundle->response(0);
|
||||
// return collection id
|
||||
return array_key_exists($id, $response->updated()) ? (string)$id : '';
|
||||
}
|
||||
|
||||
/**
|
||||
* delete collection in remote storage
|
||||
*
|
||||
* @since Release 1.0.0
|
||||
*
|
||||
*/
|
||||
public function collectionDelete(string $id): string {
|
||||
// construct request
|
||||
$r0 = new TaskListSet($this->dataAccount, null, $this->resourceNamespace, $this->resourceCollectionLabel);
|
||||
$r0->delete($id);
|
||||
// transceive
|
||||
$bundle = $this->dataStore->perform([$r0]);
|
||||
// extract response
|
||||
$response = $bundle->response(0);
|
||||
// return collection id
|
||||
return (string)$response->deleted()[0];
|
||||
}
|
||||
|
||||
/**
|
||||
* list of collections in remote storage
|
||||
*
|
||||
* @since Release 1.0.0
|
||||
*
|
||||
* @param string|null $location Id of parent collection
|
||||
* @param string|null $granularity Amount of detail to return
|
||||
* @param int|null $depth Depth of sub collections to return
|
||||
*
|
||||
* @return array<string,TaskCollectionObject>
|
||||
*/
|
||||
public function collectionList(?string $location = null, ?string $granularity = null, ?int $depth = null): array {
|
||||
// construct request
|
||||
$r0 = new TaskListGet($this->dataAccount, null, $this->resourceNamespace, $this->resourceCollectionLabel);
|
||||
// set target to query request
|
||||
if ($location !== null) {
|
||||
$r0->target($location);
|
||||
}
|
||||
// transceive
|
||||
$bundle = $this->dataStore->perform([$r0]);
|
||||
// extract response
|
||||
$response = $bundle->response(0);
|
||||
// determine if command errored
|
||||
if ($response instanceof ResponseException) {
|
||||
if ($response->type() === 'unknownMethod') {
|
||||
throw new JmapUnknownMethod($response->description(), 1);
|
||||
} else {
|
||||
throw new Exception($response->type() . ': ' . $response->description(), 1);
|
||||
}
|
||||
}
|
||||
// convert jmap objects to collection objects
|
||||
$list = [];
|
||||
foreach ($response->objects() as $co) {
|
||||
$collection = new TaskCollectionObject();
|
||||
$collection->Id = $co->id();
|
||||
$collection->Label = $co->label();
|
||||
$collection->Description = $co->description();
|
||||
$collection->Priority = $co->priority();
|
||||
$collection->Visibility = $co->visible();
|
||||
$collection->Color = $co->color();
|
||||
$list[] = $collection;
|
||||
}
|
||||
// return collection of collections
|
||||
return $list;
|
||||
}
|
||||
|
||||
/**
|
||||
* retrieve entity from remote storage
|
||||
*
|
||||
* @since Release 1.0.0
|
||||
*
|
||||
*/
|
||||
public function entityFetch(string $location, string $id, string $granularity = 'D'): ?TaskObject {
|
||||
// construct request
|
||||
$r0 = new TaskGet($this->dataAccount, null, $this->resourceNamespace, $this->resourceEntityLabel);
|
||||
$r0->target($id);
|
||||
// select properties to return
|
||||
if ($granularity === 'B') {
|
||||
$r0->property(...$this->entityPropertiesBasic);
|
||||
}
|
||||
// transceive
|
||||
$bundle = $this->dataStore->perform([$r0]);
|
||||
// extract response
|
||||
$response = $bundle->response(0);
|
||||
// convert jmap object to Task object
|
||||
$eo = $this->toTaskObject($response->object(0));
|
||||
$eo->Signature = $this->generateSignature($eo);
|
||||
|
||||
return $eo;
|
||||
}
|
||||
|
||||
/**
|
||||
* create entity in remote storage
|
||||
*
|
||||
* @since Release 1.0.0
|
||||
*
|
||||
*/
|
||||
public function entityCreate(string $location, TaskObject $so): ?TaskObject {
|
||||
// convert entity
|
||||
$entity = $this->fromTaskObject($so);
|
||||
// construct set request
|
||||
$r0 = new TaskSet($this->dataAccount, null, $this->resourceNamespace, $this->resourceEntityLabel);
|
||||
$r0->create('1', $entity)->in($location);
|
||||
// transceive
|
||||
$bundle = $this->dataStore->perform([$r0]);
|
||||
// extract response
|
||||
$response = $bundle->response(0);
|
||||
// return entity
|
||||
if (isset($response->created()['1']['id'])) {
|
||||
$ro = clone $so;
|
||||
$ro->Origin = OriginTypes::External;
|
||||
$ro->ID = $response->created()['1']['id'];
|
||||
$ro->CreatedOn = isset($response->created()['1']['updated']) ? new DateTimeImmutable($response->created()['1']['updated']) : null;
|
||||
$ro->ModifiedOn = $ro->CreatedOn;
|
||||
$ro->Signature = $this->generateSignature($ro);
|
||||
return $ro;
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* update entity in remote storage
|
||||
*
|
||||
* @since Release 1.0.0
|
||||
*
|
||||
*/
|
||||
public function entityModify(string $location, string $id, TaskObject $so): ?TaskObject {
|
||||
// convert entity
|
||||
$entity = $this->fromTaskObject($so);
|
||||
// construct set request
|
||||
$r0 = new TaskSet($this->dataAccount, null, $this->resourceNamespace, $this->resourceEntityLabel);
|
||||
$r0->update($id, $entity)->in($location);
|
||||
// transceive
|
||||
$bundle = $this->dataStore->perform([$r0]);
|
||||
// extract response
|
||||
$response = $bundle->response(0);
|
||||
// convert jmap object to Task object
|
||||
if (array_key_exists($id, $response->updated())) {
|
||||
$ro = clone $so;
|
||||
$ro->Origin = OriginTypes::External;
|
||||
$ro->ID = $id;
|
||||
$ro->ModifiedOn = isset($response->updated()[$id]['updated']) ? new DateTimeImmutable($response->updated()[$id]['updated']) : null;
|
||||
$ro->Signature = $this->generateSignature($ro);
|
||||
} else {
|
||||
$ro = null;
|
||||
}
|
||||
// return entity information
|
||||
return $ro;
|
||||
}
|
||||
|
||||
/**
|
||||
* delete entity from remote storage
|
||||
*
|
||||
* @since Release 1.0.0
|
||||
*
|
||||
*/
|
||||
public function entityDelete(string $location, string $id): string {
|
||||
// construct set request
|
||||
$r0 = new TaskSet($this->dataAccount, null, $this->resourceNamespace, $this->resourceEntityLabel);
|
||||
// construct object
|
||||
$r0->delete($id);
|
||||
// transceive
|
||||
$bundle = $this->dataStore->perform([$r0]);
|
||||
// extract response
|
||||
$response = $bundle->response(0);
|
||||
// return collection information
|
||||
return (string)$response->deleted()[0];
|
||||
}
|
||||
|
||||
/**
|
||||
* copy entity in remote storage
|
||||
*
|
||||
* @since Release 1.0.0
|
||||
*
|
||||
*/
|
||||
public function entityCopy(string $sourceLocation, string $id, string $destinationLocation): string {
|
||||
return '';
|
||||
}
|
||||
|
||||
/**
|
||||
* move entity in remote storage
|
||||
*
|
||||
* @since Release 1.0.0
|
||||
*
|
||||
*/
|
||||
public function entityMove(string $sourceLocation, string $id, string $destinationLocation): string {
|
||||
// construct set request
|
||||
$r0 = new TaskSet($this->dataAccount, null, $this->resourceNamespace, $this->resourceEntityLabel);
|
||||
// construct object
|
||||
$m0 = $r0->update($id);
|
||||
$m0->in($destinationLocation);
|
||||
// transceive
|
||||
$bundle = $this->dataStore->perform([$r0]);
|
||||
// extract response
|
||||
$response = $bundle->response(0);
|
||||
// return collection information
|
||||
return array_key_exists($id, $response->updated()) ? (string)$id : '';
|
||||
}
|
||||
|
||||
/**
|
||||
* retrieve entities from remote storage
|
||||
*
|
||||
* @since Release 1.0.0
|
||||
*
|
||||
* @param string|null $location Id of parent collection
|
||||
* @param string|null $granularity Amount of detail to return
|
||||
* @param IRange|null $range Range of collections to return
|
||||
* @param IFilter|null $filter Properties to filter by
|
||||
* @param ISort|null $sort Properties to sort by
|
||||
*/
|
||||
public function entityList(?string $location = null, ?string $granularity = null, ?IRangeTally $range = null, ?IFilter $filter = null, ?ISort $sort = null, ?int $depth = null): array {
|
||||
// construct request
|
||||
$r0 = new TaskQuery($this->dataAccount, null, $this->resourceNamespace, $this->resourceEntityLabel);
|
||||
// define location
|
||||
if (!empty($location)) {
|
||||
$r0->filter()->in($location);
|
||||
}
|
||||
// define filter
|
||||
if ($filter !== null) {
|
||||
foreach ($filter->conditions() as $condition) {
|
||||
[$operator, $property, $value] = $condition;
|
||||
match($property) {
|
||||
'before' => $r0->filter()->before($value),
|
||||
'after' => $r0->filter()->after($value),
|
||||
'uid' => $r0->filter()->uid($value),
|
||||
default => null
|
||||
};
|
||||
}
|
||||
}
|
||||
// define sort
|
||||
if ($sort !== null) {
|
||||
foreach ($sort->conditions() as $condition) {
|
||||
[$property, $direction] = $condition;
|
||||
match($property) {
|
||||
'created' => $r0->sort()->created($direction),
|
||||
'modified' => $r0->sort()->updated($direction),
|
||||
'start' => $r0->sort()->start($direction),
|
||||
'uid' => $r0->sort()->uid($direction),
|
||||
default => null
|
||||
};
|
||||
}
|
||||
}
|
||||
// define order
|
||||
if ($sort !== null) {
|
||||
foreach ($sort->conditions() as $condition) {
|
||||
match($condition['attribute']) {
|
||||
'created' => $r0->sort()->created($condition['direction']),
|
||||
'modified' => $r0->sort()->updated($condition['direction']),
|
||||
'start' => $r0->sort()->start($condition['direction']),
|
||||
'uid' => $r0->sort()->uid($condition['direction']),
|
||||
'recurrence' => $r0->sort()->recurrence($condition['direction']),
|
||||
default => null
|
||||
};
|
||||
}
|
||||
}
|
||||
// construct request
|
||||
$r1 = new TaskGet($this->dataAccount, null, $this->resourceNamespace, $this->resourceEntityLabel);
|
||||
// set target to query request
|
||||
$r1->targetFromRequest($r0, '/ids');
|
||||
// select properties to return
|
||||
if ($granularity === 'B') {
|
||||
$r1->property(...$this->entityPropertiesBasic);
|
||||
}
|
||||
// transceive
|
||||
$bundle = $this->dataStore->perform([$r0, $r1]);
|
||||
// extract response
|
||||
$response = $bundle->response(1);
|
||||
// convert json objects to message objects
|
||||
$state = $response->state();
|
||||
$list = $response->objects();
|
||||
foreach ($list as $id => $entry) {
|
||||
$list[$id] = $this->toTaskObject($entry);
|
||||
}
|
||||
// return message collection
|
||||
return ['list' => $list, 'state' => $state];
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* delta for entities in remote storage
|
||||
*
|
||||
* @since Release 1.0.0
|
||||
*
|
||||
* @return DeltaObject
|
||||
*/
|
||||
public function entityDelta(?string $location, string $state, string $granularity = 'D'): DeltaObject {
|
||||
|
||||
if (empty($state)) {
|
||||
$results = $this->entityList($location, 'B');
|
||||
$delta = new DeltaObject();
|
||||
$delta->signature = $results['state'];
|
||||
foreach ($results['list'] as $entry) {
|
||||
$delta->additions[] = $entry->ID;
|
||||
}
|
||||
return $delta;
|
||||
}
|
||||
if (empty($location)) {
|
||||
return $this->entityDeltaDefault($state, $granularity);
|
||||
} else {
|
||||
return $this->entityDeltaSpecific($location, $state, $granularity);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* delta of changes for specific collection in remote storage
|
||||
*
|
||||
* @since Release 1.0.0
|
||||
*
|
||||
*/
|
||||
public function entityDeltaSpecific(?string $location, string $state, string $granularity = 'D'): DeltaObject {
|
||||
// construct set request
|
||||
$r0 = new TaskQueryChanges($this->dataAccount, null, $this->resourceNamespace, $this->resourceEntityLabel);
|
||||
// set location constraint
|
||||
if (!empty($location)) {
|
||||
$r0->filter()->in($location);
|
||||
}
|
||||
// set state constraint
|
||||
if (!empty($state)) {
|
||||
$r0->state($state);
|
||||
} else {
|
||||
$r0->state('0');
|
||||
}
|
||||
// transceive
|
||||
$bundle = $this->dataStore->perform([$r0]);
|
||||
// extract response
|
||||
$response = $bundle->response(0);
|
||||
// determine if command errored
|
||||
if ($response instanceof ResponseException) {
|
||||
if ($response->type() === 'unknownMethod') {
|
||||
throw new JmapUnknownMethod($response->description(), 1);
|
||||
} else {
|
||||
throw new Exception($response->type() . ': ' . $response->description(), 1);
|
||||
}
|
||||
}
|
||||
// convert jmap object to delta object
|
||||
$delta = new DeltaObject();
|
||||
$delta->signature = $response->stateNew();
|
||||
$delta->additions = new BaseStringCollection($response->created());
|
||||
$delta->modifications = new BaseStringCollection($response->updated());
|
||||
$delta->deletions = new BaseStringCollection($response->deleted());
|
||||
|
||||
return $delta;
|
||||
}
|
||||
|
||||
/**
|
||||
* delta of changes in remote storage
|
||||
*
|
||||
* @since Release 1.0.0
|
||||
*
|
||||
*/
|
||||
public function entityDeltaDefault(string $state, string $granularity = 'D'): DeltaObject {
|
||||
// construct set request
|
||||
$r0 = new TaskChanges($this->dataAccount, null, $this->resourceNamespace, $this->resourceEntityLabel);
|
||||
// set state constraint
|
||||
if (!empty($state)) {
|
||||
$r0->state($state);
|
||||
} else {
|
||||
$r0->state('');
|
||||
}
|
||||
// transceive
|
||||
$bundle = $this->dataStore->perform([$r0]);
|
||||
// extract response
|
||||
$response = $bundle->response(0);
|
||||
// determine if command errored
|
||||
if ($response instanceof ResponseException) {
|
||||
if ($response->type() === 'unknownMethod') {
|
||||
throw new JmapUnknownMethod($response->description(), 1);
|
||||
} else {
|
||||
throw new Exception($response->type() . ': ' . $response->description(), 1);
|
||||
}
|
||||
}
|
||||
// convert jmap object to delta object
|
||||
$delta = new DeltaObject();
|
||||
$delta->signature = $response->stateNew();
|
||||
$delta->additions = new BaseStringCollection($response->created());
|
||||
$delta->modifications = new BaseStringCollection($response->updated());
|
||||
$delta->deletions = new BaseStringCollection($response->deleted());
|
||||
|
||||
return $delta;
|
||||
}
|
||||
|
||||
/**
|
||||
* convert jmap object to Task object
|
||||
*
|
||||
* @since Release 1.0.0
|
||||
*
|
||||
*/
|
||||
public function toTaskObject(TaskParametersResponse $so): TaskObject {
|
||||
// create object
|
||||
$eo = new TaskObject();
|
||||
// source origin
|
||||
$eo->Origin = OriginTypes::External;
|
||||
// id
|
||||
if ($so->id()) {
|
||||
$eo->ID = $so->id();
|
||||
}
|
||||
if ($so->in()) {
|
||||
$eo->CID = $so->in()[0];
|
||||
}
|
||||
// universal id
|
||||
if ($so->uid()) {
|
||||
$eo->UUID = $so->uid();
|
||||
}
|
||||
// creation date time
|
||||
if ($so->created()) {
|
||||
$eo->CreatedOn = $so->created();
|
||||
}
|
||||
// modification date time
|
||||
if ($so->updated()) {
|
||||
$eo->ModifiedOn = $so->updated();
|
||||
}
|
||||
|
||||
return $eo;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* convert Task object to jmap object
|
||||
*
|
||||
* @since Release 1.0.0
|
||||
*
|
||||
*/
|
||||
public function fromTaskObject(TaskObject $eo): TaskParametersRequest {
|
||||
|
||||
// create object
|
||||
$to = new TaskParametersRequest();
|
||||
// universal id
|
||||
if ($eo->UUID) {
|
||||
$to->uid($eo->UUID);
|
||||
}
|
||||
// creation date time
|
||||
if ($eo->CreatedOn) {
|
||||
$to->created($eo->CreatedOn);
|
||||
}
|
||||
// modification date time
|
||||
if ($eo->ModifiedOn) {
|
||||
$to->updated($eo->ModifiedOn);
|
||||
}
|
||||
|
||||
return $to;
|
||||
|
||||
}
|
||||
|
||||
|
||||
public function generateSignature(TaskObject $eo): string {
|
||||
|
||||
// clone self
|
||||
$o = clone $eo;
|
||||
// remove non needed values
|
||||
unset(
|
||||
$o->Origin,
|
||||
$o->ID,
|
||||
$o->CID,
|
||||
$o->Signature,
|
||||
$o->CCID,
|
||||
$o->CEID,
|
||||
$o->CESN,
|
||||
$o->UUID,
|
||||
$o->CreatedOn,
|
||||
$o->ModifiedOn
|
||||
);
|
||||
|
||||
// generate signature
|
||||
return md5(json_encode($o, JSON_PARTIAL_OUTPUT_ON_ERROR));
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user