chore: implement serice interface changes
Signed-off-by: Sebastian Krupinski <krupinski01@gmail.com>
This commit is contained in:
@@ -11,8 +11,6 @@ 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;
|
||||
@@ -25,17 +23,16 @@ use JmapClient\Requests\Mail\MailQuery;
|
||||
use JmapClient\Requests\Mail\MailQueryChanges;
|
||||
use JmapClient\Requests\Mail\MailSet;
|
||||
use JmapClient\Requests\Mail\MailSubmissionSet;
|
||||
use JmapClient\Requests\RequestBundle;
|
||||
use JmapClient\Responses\Mail\MailboxParameters as MailboxParametersResponse;
|
||||
use JmapClient\Responses\Mail\MailPart;
|
||||
use JmapClient\Responses\Mail\MailParameters as MailParametersResponse;
|
||||
use JmapClient\Responses\ResponseException;
|
||||
use KTXF\Resource\BinaryResource;
|
||||
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;
|
||||
@@ -496,6 +493,103 @@ class RemoteMailService {
|
||||
return new RangeTally();
|
||||
}
|
||||
|
||||
/**
|
||||
* 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);
|
||||
// transceive
|
||||
$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;
|
||||
}
|
||||
|
||||
public function entityDownload(string $identifier, string|null $blobId = null): BinaryResource {
|
||||
|
||||
// construct request
|
||||
$r0 = new MailGet($this->dataAccount, null, $this->resourceNamespace, $this->resourceEntityLabel);
|
||||
$r0->target($identifier);
|
||||
// select properties to return
|
||||
$r0->property(...['id', 'blobId', 'bodyStructure']);
|
||||
// transceive
|
||||
$bundle = $this->dataStore->perform([$r0]);
|
||||
// extract response
|
||||
$response = $bundle->response(0);
|
||||
|
||||
$parameters = $response->object(0);
|
||||
if (!$parameters instanceof MailParametersResponse) {
|
||||
throw new Exception('Unexpected response type received from server.', 1);
|
||||
}
|
||||
|
||||
if ($blobId === null) {
|
||||
$blobId = $parameters->blob();
|
||||
$filename = 'message.eml';
|
||||
$mimeType = 'text/plain';
|
||||
} else {
|
||||
$walk = function (?MailPart $part) use (&$walk, $blobId): ?MailPart {
|
||||
if ($part === null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if ($part->blob() === $blobId) {
|
||||
return $part;
|
||||
}
|
||||
|
||||
foreach ($part->parts() ?? [] as $subPart) {
|
||||
$subPartResult = $walk($subPart);
|
||||
if ($subPartResult !== null) {
|
||||
return $subPartResult;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
};
|
||||
|
||||
$part = $walk($parameters->bodyPartStructure());
|
||||
$filename = $part->name() ?? 'file.bin';
|
||||
$mimeType = $part->type() ?? 'application/octet-stream';
|
||||
}
|
||||
|
||||
$streamResource = $this->dataStore->downloadStream($this->dataAccount, $blobId, $mimeType, $filename);
|
||||
|
||||
$stream = (function () use ($streamResource): \Generator {
|
||||
try {
|
||||
while (!$streamResource->eof()) {
|
||||
$chunk = $streamResource->read(8192);
|
||||
if ($chunk === '' && !$streamResource->eof()) {
|
||||
throw new Exception('Unable to read from download stream.', 1);
|
||||
}
|
||||
if ($chunk !== '') {
|
||||
yield $chunk;
|
||||
}
|
||||
}
|
||||
} finally {
|
||||
$streamResource->close();
|
||||
}
|
||||
})();
|
||||
|
||||
return new BinaryResource($filename, $mimeType, $stream);
|
||||
}
|
||||
|
||||
/**
|
||||
* check existence of entities in remote storage
|
||||
*
|
||||
@@ -624,34 +718,8 @@ class RemoteMailService {
|
||||
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);
|
||||
// transceive
|
||||
$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;
|
||||
public function entityFresh(): MailParametersRequest {
|
||||
return new MailParametersRequest();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -724,6 +792,37 @@ class RemoteMailService {
|
||||
return null;
|
||||
}
|
||||
|
||||
public function entityPatch(MailParametersRequest $properties, string ...$identifiers): ?array {
|
||||
// construct request
|
||||
$r0 = new MailSet($this->dataAccount);
|
||||
foreach ($identifiers as $id) {
|
||||
$r0->patch($id, $properties);
|
||||
}
|
||||
// transceive
|
||||
$bundle = $this->dataStore->perform([$r0]);
|
||||
// extract response
|
||||
$response = $bundle->first();
|
||||
// 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);
|
||||
}
|
||||
}
|
||||
$results = [];
|
||||
// check for success
|
||||
foreach ($response->updateSuccesses() as $id => $data) {
|
||||
$results[$id] = true;
|
||||
}
|
||||
// check for failure
|
||||
foreach ($response->updateFailures() as $id => $data) {
|
||||
$results[$id] = $data['type'] ?? 'unknownError';
|
||||
}
|
||||
|
||||
return $results;
|
||||
}
|
||||
|
||||
/**
|
||||
* delete entities from remote storage
|
||||
*
|
||||
@@ -761,16 +860,6 @@ class RemoteMailService {
|
||||
return $results;
|
||||
}
|
||||
|
||||
/**
|
||||
* copy entity in remote storage
|
||||
*
|
||||
* @since Release 1.0.0
|
||||
*
|
||||
*/
|
||||
public function entityCopy(string $target, string ...$identifiers): array {
|
||||
return [];
|
||||
}
|
||||
|
||||
/**
|
||||
* move entity in remote storage
|
||||
*
|
||||
@@ -808,6 +897,16 @@ class RemoteMailService {
|
||||
return $results;
|
||||
}
|
||||
|
||||
/**
|
||||
* copy entity in remote storage
|
||||
*
|
||||
* @since Release 1.0.0
|
||||
*
|
||||
*/
|
||||
public function entityCopy(string $target, string ...$identifiers): array {
|
||||
return [];
|
||||
}
|
||||
|
||||
/**
|
||||
* send entity
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user