generated from Nodarx/template
Merge pull request 'refactor: use new mail interface desing' (#17) from refactor/use-new-interface-design into main
All checks were successful
Renovate / renovate (push) Successful in 5m23s
All checks were successful
Renovate / renovate (push) Successful in 5m23s
Reviewed-on: #17
This commit was merged in pull request #17.
This commit is contained in:
@@ -15,42 +15,21 @@ final class FetchOptions
|
|||||||
|
|
||||||
public static function default(): self
|
public static function default(): self
|
||||||
{
|
{
|
||||||
return self::message();
|
return (new self([]))
|
||||||
}
|
->withUid()
|
||||||
|
->withFlags()
|
||||||
public static function summary(): self
|
->withInternalDate()
|
||||||
{
|
->withSize();
|
||||||
return new self([
|
|
||||||
'UID',
|
|
||||||
'FLAGS',
|
|
||||||
'INTERNALDATE',
|
|
||||||
'RFC822.SIZE',
|
|
||||||
]);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function message(): self
|
public static function message(): self
|
||||||
{
|
{
|
||||||
return self::summary()
|
return self::default()
|
||||||
|
->withHeaders()
|
||||||
->withEnvelope()
|
->withEnvelope()
|
||||||
->withBodyStructure();
|
->withBodyStructure();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function fullMessage(): self
|
|
||||||
{
|
|
||||||
return self::message()->withBodyText();
|
|
||||||
}
|
|
||||||
|
|
||||||
public function withBodySection(string $section): self
|
|
||||||
{
|
|
||||||
$section = strtoupper(trim($section));
|
|
||||||
|
|
||||||
if ($section === '') {
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
|
|
||||||
return $this->with(sprintf('BODY[%s]', $section));
|
|
||||||
}
|
|
||||||
|
|
||||||
public static function of(string ...$items): self
|
public static function of(string ...$items): self
|
||||||
{
|
{
|
||||||
return new self(self::normalize($items));
|
return new self(self::normalize($items));
|
||||||
@@ -76,6 +55,25 @@ final class FetchOptions
|
|||||||
return $this->with('RFC822.SIZE');
|
return $this->with('RFC822.SIZE');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function withHeaders(): self
|
||||||
|
{
|
||||||
|
return $this->with('BODY[HEADER]');
|
||||||
|
}
|
||||||
|
|
||||||
|
public function withHeader(string ...$fields): self
|
||||||
|
{
|
||||||
|
$fields = array_values(array_filter(array_map(
|
||||||
|
static fn (string $field): string => strtoupper(trim($field)),
|
||||||
|
$fields,
|
||||||
|
), static fn (string $field): bool => $field !== ''));
|
||||||
|
|
||||||
|
if ($fields === []) {
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this->with(sprintf('BODY.PEEK[HEADER.FIELDS (%s)]', implode(' ', $fields)));
|
||||||
|
}
|
||||||
|
|
||||||
public function withEnvelope(): self
|
public function withEnvelope(): self
|
||||||
{
|
{
|
||||||
return $this->with('ENVELOPE');
|
return $this->with('ENVELOPE');
|
||||||
@@ -88,21 +86,18 @@ final class FetchOptions
|
|||||||
|
|
||||||
public function withBodyText(): self
|
public function withBodyText(): self
|
||||||
{
|
{
|
||||||
return $this->withBodySection('TEXT');
|
return $this->with('BODY[TEXT]');
|
||||||
}
|
}
|
||||||
|
|
||||||
public function withHeaderFields(string ...$fields): self
|
public function withBodySection(string $section): self
|
||||||
{
|
{
|
||||||
$fields = array_values(array_filter(array_map(
|
$section = strtoupper(trim($section));
|
||||||
static fn (string $field): string => strtoupper(trim($field)),
|
|
||||||
$fields,
|
|
||||||
), static fn (string $field): bool => $field !== ''));
|
|
||||||
|
|
||||||
if ($fields === []) {
|
if ($section === '') {
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
return $this->with(sprintf('BODY.PEEK[HEADER.FIELDS (%s)]', implode(' ', $fields)));
|
return $this->with(sprintf('BODY[%s]', $section));
|
||||||
}
|
}
|
||||||
|
|
||||||
public function with(string $item): self
|
public function with(string $item): self
|
||||||
|
|||||||
@@ -21,6 +21,7 @@ final class Message
|
|||||||
private readonly int $uid,
|
private readonly int $uid,
|
||||||
private readonly int $size,
|
private readonly int $size,
|
||||||
private readonly ?string $internalDate,
|
private readonly ?string $internalDate,
|
||||||
|
private readonly ?string $receivedAt,
|
||||||
private readonly array $flags,
|
private readonly array $flags,
|
||||||
private readonly ?string $subject,
|
private readonly ?string $subject,
|
||||||
private readonly ?string $sentAt,
|
private readonly ?string $sentAt,
|
||||||
@@ -56,6 +57,11 @@ final class Message
|
|||||||
return $this->internalDate;
|
return $this->internalDate;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function receivedAt(): ?string
|
||||||
|
{
|
||||||
|
return $this->receivedAt;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return list<string>
|
* @return list<string>
|
||||||
*/
|
*/
|
||||||
@@ -139,7 +145,7 @@ final class Message
|
|||||||
|
|
||||||
public function bodyText(): ?string
|
public function bodyText(): ?string
|
||||||
{
|
{
|
||||||
return $this->bodyText;
|
return $this->bodySections['TEXT'] ?? null;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -160,6 +166,7 @@ final class Message
|
|||||||
$this->uid,
|
$this->uid,
|
||||||
$this->size,
|
$this->size,
|
||||||
$this->internalDate,
|
$this->internalDate,
|
||||||
|
$this->receivedAt,
|
||||||
$this->flags,
|
$this->flags,
|
||||||
$this->subject,
|
$this->subject,
|
||||||
$this->sentAt,
|
$this->sentAt,
|
||||||
|
|||||||
@@ -4,6 +4,8 @@ declare(strict_types=1);
|
|||||||
|
|
||||||
namespace KTXM\ProviderImap\Client;
|
namespace KTXM\ProviderImap\Client;
|
||||||
|
|
||||||
|
use DateTimeInterface;
|
||||||
|
|
||||||
final class MessageParser
|
final class MessageParser
|
||||||
{
|
{
|
||||||
public static function isFetchMessage(string $payload): bool
|
public static function isFetchMessage(string $payload): bool
|
||||||
@@ -23,12 +25,14 @@ final class MessageParser
|
|||||||
$envelope = is_array($attributes['ENVELOPE'] ?? null) ? $attributes['ENVELOPE'] : null;
|
$envelope = is_array($attributes['ENVELOPE'] ?? null) ? $attributes['ENVELOPE'] : null;
|
||||||
$bodyStructure = isset($attributes['BODYSTRUCTURE']) ? self::parseBodyPart($attributes['BODYSTRUCTURE'], '') : null;
|
$bodyStructure = isset($attributes['BODYSTRUCTURE']) ? self::parseBodyPart($attributes['BODYSTRUCTURE'], '') : null;
|
||||||
$bodySections = self::parseBodySections($attributes, $bodyStructure);
|
$bodySections = self::parseBodySections($attributes, $bodyStructure);
|
||||||
|
$headers = self::parseFetchedHeaders($attributes);
|
||||||
|
|
||||||
return new Message(
|
return new Message(
|
||||||
$sequence,
|
$sequence,
|
||||||
$uid,
|
$uid,
|
||||||
self::toOptionalInt($attributes['RFC822.SIZE'] ?? null) ?? 0,
|
self::toOptionalInt($attributes['RFC822.SIZE'] ?? null) ?? 0,
|
||||||
self::toNullableString($attributes['INTERNALDATE'] ?? null),
|
self::toNullableString($attributes['INTERNALDATE'] ?? null),
|
||||||
|
self::extractReceivedAt($headers),
|
||||||
self::parseFlags($attributes['FLAGS'] ?? null),
|
self::parseFlags($attributes['FLAGS'] ?? null),
|
||||||
self::decodeMimeHeader(self::envelopeString($envelope, 1)),
|
self::decodeMimeHeader(self::envelopeString($envelope, 1)),
|
||||||
self::envelopeString($envelope, 0),
|
self::envelopeString($envelope, 0),
|
||||||
@@ -258,6 +262,127 @@ final class MessageParser
|
|||||||
return is_string($value) && $value !== '' ? $value : null;
|
return is_string($value) && $value !== '' ? $value : null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param array<string, mixed> $attributes
|
||||||
|
* @return array<string, list<string>>
|
||||||
|
*/
|
||||||
|
private static function parseFetchedHeaders(array $attributes): array
|
||||||
|
{
|
||||||
|
$headers = [];
|
||||||
|
|
||||||
|
foreach ($attributes as $name => $value) {
|
||||||
|
if (!is_string($value)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!preg_match('/^BODY(?:\.PEEK)?\[(.+)\]$/i', $name, $matches)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
$section = strtoupper(trim($matches[1]));
|
||||||
|
if (!str_starts_with($section, 'HEADER')) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach (self::parseHeaderBlock($value) as $headerName => $headerValues) {
|
||||||
|
$normalized = strtolower($headerName);
|
||||||
|
$headers[$normalized] ??= [];
|
||||||
|
array_push($headers[$normalized], ...$headerValues);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return $headers;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return array<string, list<string>>
|
||||||
|
*/
|
||||||
|
private static function parseHeaderBlock(string $headers): array
|
||||||
|
{
|
||||||
|
$parsed = [];
|
||||||
|
$currentName = null;
|
||||||
|
$currentValue = '';
|
||||||
|
|
||||||
|
foreach (preg_split("/\r\n|\n|\r/", $headers) ?: [] as $line) {
|
||||||
|
if ($line === '') {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (($line[0] === ' ' || $line[0] === "\t") && $currentName !== null) {
|
||||||
|
$currentValue .= ' ' . trim($line);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($currentName !== null) {
|
||||||
|
$parsed[$currentName] ??= [];
|
||||||
|
$parsed[$currentName][] = trim($currentValue);
|
||||||
|
}
|
||||||
|
|
||||||
|
$separator = strpos($line, ':');
|
||||||
|
if ($separator === false) {
|
||||||
|
$currentName = null;
|
||||||
|
$currentValue = '';
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
$currentName = substr($line, 0, $separator);
|
||||||
|
$currentValue = substr($line, $separator + 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($currentName !== null) {
|
||||||
|
$parsed[$currentName] ??= [];
|
||||||
|
$parsed[$currentName][] = trim($currentValue);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $parsed;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param array<string, list<string>> $headers
|
||||||
|
*/
|
||||||
|
private static function extractReceivedAt(array $headers): ?string
|
||||||
|
{
|
||||||
|
foreach ($headers['delivery-date'] ?? [] as $value) {
|
||||||
|
$date = self::parseHeaderDate($value);
|
||||||
|
if ($date !== null) {
|
||||||
|
return $date;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach ($headers['received'] ?? [] as $value) {
|
||||||
|
$date = self::parseHeaderDateFromReceived($value);
|
||||||
|
if ($date !== null) {
|
||||||
|
return $date;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static function parseHeaderDate(?string $value): ?string
|
||||||
|
{
|
||||||
|
$value = self::toNullableString($value);
|
||||||
|
if ($value === null) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
return (new \DateTimeImmutable($value))->format(DateTimeInterface::ATOM);
|
||||||
|
} catch (\Exception) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static function parseHeaderDateFromReceived(string $value): ?string
|
||||||
|
{
|
||||||
|
$separator = strrpos($value, ';');
|
||||||
|
if ($separator === false) {
|
||||||
|
return self::parseHeaderDate($value);
|
||||||
|
}
|
||||||
|
|
||||||
|
return self::parseHeaderDate(substr($value, $separator + 1));
|
||||||
|
}
|
||||||
|
|
||||||
private static function envelopeString(?array $envelope, int $index): ?string
|
private static function envelopeString(?array $envelope, int $index): ?string
|
||||||
{
|
{
|
||||||
if ($envelope === null) {
|
if ($envelope === null) {
|
||||||
|
|||||||
@@ -91,12 +91,4 @@ class CollectionResource extends CollectionMutableAbstract
|
|||||||
return $this->properties;
|
return $this->properties;
|
||||||
}
|
}
|
||||||
|
|
||||||
// ── JSON ─────────────────────────────────────────────────────────────────
|
|
||||||
|
|
||||||
public function jsonSerialize(): array
|
|
||||||
{
|
|
||||||
$data = $this->data;
|
|
||||||
$data['properties'] = $this->getProperties()->jsonSerialize();
|
|
||||||
return $data;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,7 +9,6 @@ declare(strict_types=1);
|
|||||||
|
|
||||||
namespace KTXM\ProviderImap\Providers;
|
namespace KTXM\ProviderImap\Providers;
|
||||||
|
|
||||||
use DateTimeInterface;
|
|
||||||
use KTXM\ProviderImap\Client\Message;
|
use KTXM\ProviderImap\Client\Message;
|
||||||
use KTXF\Mail\Entity\EntityMutableAbstract;
|
use KTXF\Mail\Entity\EntityMutableAbstract;
|
||||||
|
|
||||||
@@ -43,27 +42,6 @@ class EntityResource extends EntityMutableAbstract {
|
|||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Convert mail entity object to store array
|
|
||||||
*/
|
|
||||||
public function toStore(): array {
|
|
||||||
return array_merge(
|
|
||||||
$this->data,
|
|
||||||
['properties' => $this->getProperties()->toStore()]
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Hydrate mail entity object from store array
|
|
||||||
*/
|
|
||||||
public function fromStore(array $data): static {
|
|
||||||
$properties = $data['properties'] ?? [];
|
|
||||||
unset($data['properties']);
|
|
||||||
$this->data = $data;
|
|
||||||
$this->getProperties()->fromStore($properties);
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @inheritDoc
|
* @inheritDoc
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -25,71 +25,74 @@ class MessageProperties extends MessagePropertiesMutableAbstract {
|
|||||||
*/
|
*/
|
||||||
public function fromImap(Message $message): static
|
public function fromImap(Message $message): static
|
||||||
{
|
{
|
||||||
$this->data['size'] = $message->size();
|
$this->data[static::PROPERTY_SIZE] = $message->size();
|
||||||
|
|
||||||
$this->data['flags'] = [];
|
|
||||||
foreach ($message->flags() as $flag) {
|
|
||||||
$flag = ltrim($flag, '\\');
|
|
||||||
$normalized = match (strtolower($flag)) {
|
|
||||||
'seen' => 'read',
|
|
||||||
'flagged' => 'flagged',
|
|
||||||
'answered' => 'answered',
|
|
||||||
'draft' => 'draft',
|
|
||||||
'deleted' => 'deleted',
|
|
||||||
default => strtolower($flag),
|
|
||||||
};
|
|
||||||
$this->data['flags'][$normalized] = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
if ($message->messageId() !== null) {
|
if ($message->messageId() !== null) {
|
||||||
$this->data['urid'] = $message->messageId();
|
$this->data[static::PROPERTY_URID] = $message->messageId();
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($message->subject() !== null) {
|
if ($message->inReplyTo() !== null) {
|
||||||
$this->data['subject'] = $message->subject();
|
$this->data[static::PROPERTY_IN_REPLY_TO] = $message->inReplyTo();
|
||||||
|
}
|
||||||
|
|
||||||
|
//if ($message->references() !== []) {
|
||||||
|
// $this->data[static::PROPERTY_REFERENCES] = $message->references();
|
||||||
|
//}
|
||||||
|
|
||||||
|
$receivedAt = $message->receivedAt() ?? $message->internalDate();
|
||||||
|
if ($receivedAt !== null) {
|
||||||
|
$date = new DateTimeImmutable($receivedAt);
|
||||||
|
$this->data[static::PROPERTY_RECEIVED] = $date->format(DateTimeInterface::ATOM);
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($message->sentAt() !== null) {
|
if ($message->sentAt() !== null) {
|
||||||
$date = new DateTimeImmutable($message->sentAt());
|
$date = new DateTimeImmutable($message->sentAt());
|
||||||
$this->data['date'] = $date->format(DateTimeInterface::ATOM);
|
$this->data[static::PROPERTY_SENT] = $date->format(DateTimeInterface::ATOM);
|
||||||
}
|
|
||||||
|
|
||||||
if ($message->inReplyTo() !== null) {
|
|
||||||
$this->data['inReplyTo'] = $message->inReplyTo();
|
|
||||||
}
|
|
||||||
|
|
||||||
if ($message->from() !== []) {
|
|
||||||
$this->data['from'] = $message->from()[0]->toArray();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($message->sender() !== []) {
|
if ($message->sender() !== []) {
|
||||||
$this->data['sender'] = $message->sender()[0]->toArray();
|
$this->data[static::PROPERTY_SENDER] = $message->sender()[0]->toArray();
|
||||||
}
|
}
|
||||||
|
|
||||||
foreach (['to', 'cc', 'bcc', 'replyTo'] as $field) {
|
if ($message->from() !== []) {
|
||||||
|
$this->data[static::PROPERTY_FROM] = $message->from()[0]->toArray();
|
||||||
|
}
|
||||||
|
|
||||||
|
$addressProperties = [
|
||||||
|
'to' => static::PROPERTY_TO,
|
||||||
|
'cc' => static::PROPERTY_CC,
|
||||||
|
'bcc' => static::PROPERTY_BCC,
|
||||||
|
'replyTo' => static::PROPERTY_REPLY_TO,
|
||||||
|
];
|
||||||
|
|
||||||
|
foreach ($addressProperties as $field => $property) {
|
||||||
$addresses = $message->{$field}();
|
$addresses = $message->{$field}();
|
||||||
if ($addresses === []) {
|
if ($addresses === []) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->data[$field] = array_map(
|
$this->data[$property] = array_map(
|
||||||
static fn ($address): array => $address->toArray(),
|
static fn ($address): array => $address->toArray(),
|
||||||
$addresses,
|
$addresses,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if ($message->subject() !== null) {
|
||||||
|
$this->data[static::PROPERTY_SUBJECT] = $message->subject();
|
||||||
|
}
|
||||||
|
|
||||||
if ($message->bodyStructure() !== null) {
|
if ($message->bodyStructure() !== null) {
|
||||||
$this->data['body'] = $message->bodyStructure()->toArray();
|
$this->data[static::PROPERTY_BODY] = $message->bodyStructure()->toArray();
|
||||||
|
|
||||||
$attachments = [];
|
$attachments = [];
|
||||||
self::collectAttachments($message->bodyStructure(), $attachments);
|
$this->collectAttachments($message->bodyStructure(), $attachments);
|
||||||
if ($attachments !== []) {
|
if ($attachments !== []) {
|
||||||
$this->data['attachments'] = $attachments;
|
$this->data[static::PROPERTY_ATTACHMENTS] = $attachments;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($message->bodyStructure() !== null) {
|
if ($message->bodyStructure() !== null) {
|
||||||
$this->data['body'] = $message->bodyStructure()->toArray();
|
$this->data[static::PROPERTY_BODY] = $message->bodyStructure()->toArray();
|
||||||
// Recursively add content from bodyValues to matching parts
|
// Recursively add content from bodyValues to matching parts
|
||||||
if (is_array($message->bodySections())) {
|
if (is_array($message->bodySections())) {
|
||||||
$addContentToParts = function(&$structure, $bodyValues) use (&$addContentToParts) {
|
$addContentToParts = function(&$structure, $bodyValues) use (&$addContentToParts) {
|
||||||
@@ -105,77 +108,36 @@ class MessageProperties extends MessagePropertiesMutableAbstract {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
$addContentToParts($this->data['body'], $message->bodySections());
|
$addContentToParts($this->data[static::PROPERTY_BODY], $message->bodySections());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$this->data[static::PROPERTY_FLAGS] = [];
|
||||||
|
foreach ($message->flags() as $flag) {
|
||||||
|
$flag = ltrim($flag, '\\');
|
||||||
|
$normalized = match (strtolower($flag)) {
|
||||||
|
'seen' => 'read',
|
||||||
|
'flagged' => 'flagged',
|
||||||
|
'answered' => 'answered',
|
||||||
|
'draft' => 'draft',
|
||||||
|
'deleted' => 'deleted',
|
||||||
|
default => strtolower($flag),
|
||||||
|
};
|
||||||
|
$this->data[static::PROPERTY_FLAGS][$normalized] = true;
|
||||||
|
}
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function toImap(): array
|
|
||||||
{
|
|
||||||
$message = [];
|
|
||||||
|
|
||||||
if (isset($this->data['flags'])) {
|
|
||||||
$message['flags'] = $this->data['flags'];
|
|
||||||
}
|
|
||||||
|
|
||||||
return $message;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Hydrate from store array
|
|
||||||
*/
|
|
||||||
public function fromStore(array $data): static {
|
|
||||||
$this->data = $data;
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Serialize to store array
|
|
||||||
*/
|
|
||||||
public function toStore(): array {
|
|
||||||
return $this->data;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Convert a string to UTF-8 from the given charset.
|
|
||||||
*
|
|
||||||
* Tries mb_convert_encoding first; falls back to iconv when mbstring does
|
|
||||||
* not recognise the charset name (e.g. "windows-1250").
|
|
||||||
*/
|
|
||||||
public static function toUtf8(string $content, string $charset): string
|
|
||||||
{
|
|
||||||
if ($charset === '' || in_array(strtolower($charset), ['utf-8', 'utf8'], true)) {
|
|
||||||
// Content claims to be UTF-8 but may still have invalid sequences; scrub to be safe.
|
|
||||||
return mb_convert_encoding($content, 'UTF-8', 'UTF-8');
|
|
||||||
}
|
|
||||||
// Try mbstring first
|
|
||||||
try {
|
|
||||||
$converted = mb_convert_encoding($content, 'UTF-8', $charset);
|
|
||||||
if ($converted !== false) {
|
|
||||||
return $converted;
|
|
||||||
}
|
|
||||||
} catch (\ValueError) {
|
|
||||||
// charset not recognised by mbstring — fall through to iconv
|
|
||||||
}
|
|
||||||
// iconv fallback (handles Windows-125x, ISO-8859-*, etc.)
|
|
||||||
$converted = @iconv($charset, 'UTF-8//TRANSLIT//IGNORE', $content);
|
|
||||||
$content = ($converted !== false) ? $converted : $content;
|
|
||||||
// Final scrub: strip any residual invalid UTF-8 bytes so json_encode never fails.
|
|
||||||
return mb_convert_encoding($content, 'UTF-8', 'UTF-8');
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Recursively collect attachment parts from body structure
|
* Recursively collect attachment parts from body structure
|
||||||
*/
|
*/
|
||||||
private static function collectAttachments(ClientMessagePart $part, array &$attachments): void
|
private function collectAttachments(ClientMessagePart $part, array &$attachments): void
|
||||||
{
|
{
|
||||||
$children = $part->parts();
|
$children = $part->parts();
|
||||||
if ($children !== []) {
|
if ($children !== []) {
|
||||||
foreach ($children as $childPart) {
|
foreach ($children as $childPart) {
|
||||||
self::collectAttachments($childPart, $attachments);
|
$this->collectAttachments($childPart, $attachments);
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -14,6 +14,7 @@ use KTXF\Mail\Provider\ProviderServiceDiscoverInterface;
|
|||||||
use KTXF\Mail\Provider\ProviderServiceMutateInterface;
|
use KTXF\Mail\Provider\ProviderServiceMutateInterface;
|
||||||
use KTXF\Mail\Provider\ProviderServiceTestInterface;
|
use KTXF\Mail\Provider\ProviderServiceTestInterface;
|
||||||
use KTXF\Mail\Service\ServiceBaseInterface;
|
use KTXF\Mail\Service\ServiceBaseInterface;
|
||||||
|
use KTXF\Mail\Service\ServiceMutableInterface;
|
||||||
use KTXF\Resource\Provider\ResourceServiceLocationInterface;
|
use KTXF\Resource\Provider\ResourceServiceLocationInterface;
|
||||||
use KTXF\Resource\Provider\ResourceServiceMutateInterface;
|
use KTXF\Resource\Provider\ResourceServiceMutateInterface;
|
||||||
use KTXM\ProviderImap\Service\Discovery;
|
use KTXM\ProviderImap\Service\Discovery;
|
||||||
@@ -23,7 +24,7 @@ use KTXM\ProviderImap\Stores\ServiceStore;
|
|||||||
/**
|
/**
|
||||||
* IMAP Mail Provider
|
* IMAP Mail Provider
|
||||||
*/
|
*/
|
||||||
class Provider implements ProviderServiceMutateInterface, ProviderServiceDiscoverInterface, ProviderServiceTestInterface
|
class Provider implements ProviderBaseInterface, ProviderServiceMutateInterface, ProviderServiceDiscoverInterface, ProviderServiceTestInterface
|
||||||
{
|
{
|
||||||
|
|
||||||
public const JSON_TYPE = ProviderBaseInterface::JSON_TYPE;
|
public const JSON_TYPE = ProviderBaseInterface::JSON_TYPE;
|
||||||
@@ -180,7 +181,7 @@ class Provider implements ProviderServiceMutateInterface, ProviderServiceDiscove
|
|||||||
return $discovery->discover($identity, $location, $secret, $verifySSL);
|
return $discovery->discover($identity, $location, $secret, $verifySSL);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function serviceTest(ServiceBaseInterface $service, array $options = []): array
|
public function serviceTest(ServiceBaseInterface|ServiceMutableInterface $service, array $options = []): array
|
||||||
{
|
{
|
||||||
$startTime = microtime(true);
|
$startTime = microtime(true);
|
||||||
|
|
||||||
|
|||||||
@@ -12,8 +12,7 @@ namespace KTXM\ProviderImap\Providers;
|
|||||||
use Generator;
|
use Generator;
|
||||||
use KTXF\Mail\Collection\CollectionBaseInterface;
|
use KTXF\Mail\Collection\CollectionBaseInterface;
|
||||||
use KTXF\Mail\Collection\CollectionMutableInterface;
|
use KTXF\Mail\Collection\CollectionMutableInterface;
|
||||||
use KTXF\Mail\Entity\EntityBaseInterface;
|
use KTXF\Mail\Collection\CollectionPropertiesBaseInterface;
|
||||||
use KTXF\Mail\Entity\EntityMutableInterface;
|
|
||||||
use KTXF\Mail\Object\Address;
|
use KTXF\Mail\Object\Address;
|
||||||
use KTXF\Mail\Object\AddressInterface;
|
use KTXF\Mail\Object\AddressInterface;
|
||||||
use KTXF\Mail\Service\ServiceBaseInterface;
|
use KTXF\Mail\Service\ServiceBaseInterface;
|
||||||
@@ -41,6 +40,7 @@ use KTXM\ProviderImap\Service\Remote\RemoteService;
|
|||||||
use KTXM\ProviderImap\Providers\CollectionResource;
|
use KTXM\ProviderImap\Providers\CollectionResource;
|
||||||
use KTXF\Mail\Collection\CollectionRoles;
|
use KTXF\Mail\Collection\CollectionRoles;
|
||||||
use KTXF\Mail\Object\MessagePropertiesMutableInterface;
|
use KTXF\Mail\Object\MessagePropertiesMutableInterface;
|
||||||
|
use KTXF\Mail\Service\ServiceEntityMutableInterface;
|
||||||
use KTXM\ProviderImap\Providers\EntityResource;
|
use KTXM\ProviderImap\Providers\EntityResource;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -48,8 +48,6 @@ use KTXM\ProviderImap\Providers\EntityResource;
|
|||||||
*/
|
*/
|
||||||
class Service implements ServiceBaseInterface, ServiceMutableInterface, ServiceConfigurableInterface, ServiceCollectionMutableInterface, ServiceEntityMutableInterface
|
class Service implements ServiceBaseInterface, ServiceMutableInterface, ServiceConfigurableInterface, ServiceCollectionMutableInterface, ServiceEntityMutableInterface
|
||||||
{
|
{
|
||||||
public const JSON_TYPE = ServiceBaseInterface::JSON_TYPE;
|
|
||||||
|
|
||||||
private const PROVIDER_IDENTIFIER = 'imap';
|
private const PROVIDER_IDENTIFIER = 'imap';
|
||||||
|
|
||||||
private ?string $serviceTenantId = null;
|
private ?string $serviceTenantId = null;
|
||||||
@@ -92,6 +90,11 @@ class Service implements ServiceBaseInterface, ServiceMutableInterface, ServiceC
|
|||||||
self::CAPABILITY_ENTITY_LIST_RANGE => ['tally' => ['absolute', 'relative']],
|
self::CAPABILITY_ENTITY_LIST_RANGE => ['tally' => ['absolute', 'relative']],
|
||||||
self::CAPABILITY_ENTITY_EXTANT => true,
|
self::CAPABILITY_ENTITY_EXTANT => true,
|
||||||
self::CAPABILITY_ENTITY_FETCH => true,
|
self::CAPABILITY_ENTITY_FETCH => true,
|
||||||
|
self::CAPABILITY_ENTITY_CREATE => false,
|
||||||
|
self::CAPABILITY_ENTITY_MODIFY => false,
|
||||||
|
self::CAPABILITY_ENTITY_DELETE => true,
|
||||||
|
self::CAPABILITY_ENTITY_MOVE => true,
|
||||||
|
self::CAPABILITY_ENTITY_COPY => false,
|
||||||
];
|
];
|
||||||
|
|
||||||
private RemoteMailService $mailService;
|
private RemoteMailService $mailService;
|
||||||
@@ -383,12 +386,16 @@ class Service implements ServiceBaseInterface, ServiceMutableInterface, ServiceC
|
|||||||
{
|
{
|
||||||
$this->initialize();
|
$this->initialize();
|
||||||
|
|
||||||
$mailboxes = $this->collectionList(null);
|
$list = [];
|
||||||
$extant = [];
|
|
||||||
foreach ($identifiers as $id) {
|
foreach ($identifiers as $identifier) {
|
||||||
$extant[(string) $id] = isset($mailboxes[(string) $id]);
|
$key = (string) $identifier;
|
||||||
|
$result = $this->mailService->collectionFetch($key);
|
||||||
|
|
||||||
|
$list[$key] = $result !== false;
|
||||||
}
|
}
|
||||||
return $extant;
|
|
||||||
|
return $list;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function collectionFetch(string|int $identifier): ?CollectionBaseInterface
|
public function collectionFetch(string|int $identifier): ?CollectionBaseInterface
|
||||||
@@ -411,17 +418,23 @@ class Service implements ServiceBaseInterface, ServiceMutableInterface, ServiceC
|
|||||||
return new CollectionResource($this->provider(), $this->identifier());
|
return new CollectionResource($this->provider(), $this->identifier());
|
||||||
}
|
}
|
||||||
|
|
||||||
public function collectionCreate(string|int|null $location, CollectionMutableInterface $collection, array $options = []): CollectionBaseInterface
|
public function collectionCreate(CollectionIdentifier|null $target, CollectionPropertiesBaseInterface $properties, array $options = []): CollectionBaseInterface
|
||||||
{
|
{
|
||||||
$this->initialize();
|
$this->initialize();
|
||||||
|
|
||||||
|
if (!$properties->getLabel()) {
|
||||||
|
throw new \InvalidArgumentException('Collection label is required property');
|
||||||
|
}
|
||||||
|
$label = $properties->getLabel();
|
||||||
|
|
||||||
// Resolve the full name: if a parent location is given, prepend it
|
// Resolve the full name: if a parent location is given, prepend it
|
||||||
$label = $collection->getProperties()->getLabel() ?? '';
|
if ($target !== null) {
|
||||||
if ($location !== null && $location !== '') {
|
$path = $target->collection();
|
||||||
// Determine the hierarchy delimiter from an existing mailbox, default to '/'
|
// Determine the hierarchy delimiter from an existing mailbox, default to '/'
|
||||||
$mailboxes = iterator_to_array($this->mailService->collectionList(null, null, null, ''));
|
$mailboxes = iterator_to_array($this->mailService->collectionList(null, null, null, ''));
|
||||||
$delimiter = $mailboxes ? reset($mailboxes)->delimiter() ?? '/' : '/';
|
$rootMailbox = $mailboxes === [] ? null : reset($mailboxes);
|
||||||
$label = rtrim((string) $location, $delimiter) . $delimiter . ltrim($label, $delimiter);
|
$delimiter = $rootMailbox === false ? '/' : ($rootMailbox?->delimiter() ?? '/');
|
||||||
|
$label = rtrim((string) $path, $delimiter) . $delimiter . ltrim($label, $delimiter);
|
||||||
}
|
}
|
||||||
|
|
||||||
$mailbox = $this->mailService->collectionCreate($label);
|
$mailbox = $this->mailService->collectionCreate($label);
|
||||||
@@ -432,20 +445,26 @@ class Service implements ServiceBaseInterface, ServiceMutableInterface, ServiceC
|
|||||||
return $collection;
|
return $collection;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function collectionUpdate(string|int $identifier, CollectionMutableInterface $collection): CollectionBaseInterface
|
public function collectionUpdate(CollectionIdentifier $target, CollectionPropertiesBaseInterface $properties): CollectionBaseInterface
|
||||||
{
|
{
|
||||||
$this->initialize();
|
$this->initialize();
|
||||||
|
|
||||||
|
if (!$properties->getLabel()) {
|
||||||
|
throw new \InvalidArgumentException('Collection label is a required property');
|
||||||
|
}
|
||||||
|
$label = $properties->getLabel();
|
||||||
|
|
||||||
// In IMAP, "update" = rename to the new label
|
// In IMAP, "update" = rename to the new label
|
||||||
$newName = $collection->getProperties()->getLabel() ?? (string) $identifier;
|
$oldPath = (string) $target->collection();
|
||||||
$mailbox = $this->mailService->collectionRename((string) $identifier, $newName);
|
$newName = $properties->getLabel();
|
||||||
|
$mailbox = $this->mailService->collectionRename($oldPath, $newName);
|
||||||
|
|
||||||
$collection = $this->collectionFresh();
|
$collection = $this->collectionFresh();
|
||||||
$collection->fromImap($mailbox);
|
$collection->fromImap($mailbox);
|
||||||
return $collection;
|
return $collection;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function collectionDelete(string|int $identifier, bool $force = false): CollectionBaseInterface | true
|
public function collectionDelete(CollectionIdentifier $target, bool $force = false): CollectionBaseInterface | true
|
||||||
{
|
{
|
||||||
$this->initialize();
|
$this->initialize();
|
||||||
|
|
||||||
@@ -458,7 +477,7 @@ class Service implements ServiceBaseInterface, ServiceMutableInterface, ServiceC
|
|||||||
|
|
||||||
// Move to target collection (e.g. Trash) instead of deleting
|
// Move to target collection (e.g. Trash) instead of deleting
|
||||||
if ($deleteMode === 'soft' && $deleteTarget !== null) {
|
if ($deleteMode === 'soft' && $deleteTarget !== null) {
|
||||||
return $this->collectionMove((string) $identifier, (string) $deleteTarget);
|
return $this->collectionMove($target, new CollectionIdentifier($target->provider(), $target->service(), $deleteTarget));
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($deleteMode === 'soft' && $deleteTarget === null) {
|
if ($deleteMode === 'soft' && $deleteTarget === null) {
|
||||||
@@ -474,24 +493,24 @@ class Service implements ServiceBaseInterface, ServiceMutableInterface, ServiceC
|
|||||||
}
|
}
|
||||||
|
|
||||||
// we need to determine if the folder being deleted is already in the trash
|
// we need to determine if the folder being deleted is already in the trash
|
||||||
if (str_starts_with((string) $identifier, (string) $deleteTarget)) {
|
if (str_starts_with((string) $target->collection(), (string) $deleteTarget)) {
|
||||||
// if so, we should hard delete instead of moving to avoid duplicates in the trash
|
// if so, we should hard delete instead of moving to avoid duplicates in the trash
|
||||||
$deleteMode = 'hard';
|
$deleteMode = 'hard';
|
||||||
}
|
}
|
||||||
|
|
||||||
$result = match ($deleteMode) {
|
$result = match ($deleteMode) {
|
||||||
'soft' => $this->collectionMove((string) $identifier, (string) $deleteTarget),
|
'soft' => $this->collectionMove($target, new CollectionIdentifier($target->provider(), $target->service(), $deleteTarget)),
|
||||||
'hard' => $this->mailService->collectionDestroy((string) $identifier)
|
'hard' => $this->mailService->collectionDestroy((string) $target->collection()),
|
||||||
};
|
};
|
||||||
return $result;
|
return $result;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function collectionMove(string|int $identifier, string|int|null $target): CollectionBaseInterface
|
public function collectionMove(CollectionIdentifier $target, CollectionIdentifier $source): CollectionBaseInterface
|
||||||
{
|
{
|
||||||
$this->initialize();
|
$this->initialize();
|
||||||
|
|
||||||
$sourceMailbox = $this->mailService->collectionFetch((string) $identifier);
|
$sourceMailbox = $this->mailService->collectionFetch((string) $source->collection());
|
||||||
$targetMailbox = $this->mailService->collectionFetch((string) $target);
|
$targetMailbox = $this->mailService->collectionFetch((string) $target->collection());
|
||||||
if ($sourceMailbox === null) {
|
if ($sourceMailbox === null) {
|
||||||
throw new \RuntimeException('Source collection not found for move operation');
|
throw new \RuntimeException('Source collection not found for move operation');
|
||||||
}
|
}
|
||||||
@@ -514,7 +533,7 @@ class Service implements ServiceBaseInterface, ServiceMutableInterface, ServiceC
|
|||||||
|
|
||||||
public function entityList(string|int $collection, ?IFilter $filter = null, ?ISort $sort = null, ?IRange $range = null, ?array $properties = null): array
|
public function entityList(string|int $collection, ?IFilter $filter = null, ?ISort $sort = null, ?IRange $range = null, ?array $properties = null): array
|
||||||
{
|
{
|
||||||
return itterator_to_array($this->entityList((string) $collection, $filter, $sort, $range), true);
|
return iterator_to_array($this->entityList((string) $collection, $filter, $sort, $range), true);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function entityListStream(string|int $collection, ?IFilter $filter = null, ?ISort $sort = null, ?IRange $range = null, ?array $properties = null): Generator
|
public function entityListStream(string|int $collection, ?IFilter $filter = null, ?ISort $sort = null, ?IRange $range = null, ?array $properties = null): Generator
|
||||||
@@ -579,12 +598,12 @@ class Service implements ServiceBaseInterface, ServiceMutableInterface, ServiceC
|
|||||||
|
|
||||||
public function entityCreate(CollectionIdentifier $target, MessagePropertiesMutableInterface $properties, array $options = []): EntityResource
|
public function entityCreate(CollectionIdentifier $target, MessagePropertiesMutableInterface $properties, array $options = []): EntityResource
|
||||||
{
|
{
|
||||||
return $this->entityFresh();
|
throw new \RuntimeException('Entity creation is not supported in this service');
|
||||||
}
|
}
|
||||||
|
|
||||||
public function entityModify(EntityIdentifier $target, MessagePropertiesMutableInterface $properties): EntityResource
|
public function entityModify(EntityIdentifier $target, MessagePropertiesMutableInterface $properties): EntityResource
|
||||||
{
|
{
|
||||||
return $this->entityFresh();
|
throw new \RuntimeException('Entity modification is not supported in this service');
|
||||||
}
|
}
|
||||||
|
|
||||||
public function entityDelete(EntityIdentifier ...$targets): array
|
public function entityDelete(EntityIdentifier ...$targets): array
|
||||||
@@ -613,7 +632,12 @@ class Service implements ServiceBaseInterface, ServiceMutableInterface, ServiceC
|
|||||||
throw new \RuntimeException('No Trash collection configured or found for deletion');
|
throw new \RuntimeException('No Trash collection configured or found for deletion');
|
||||||
}
|
}
|
||||||
|
|
||||||
$deleteTargetNative = reset($mailboxes)->name();
|
$rootMailbox = reset($mailboxes);
|
||||||
|
if ($rootMailbox === false) {
|
||||||
|
throw new \RuntimeException('No Trash collection configured or found for deletion');
|
||||||
|
}
|
||||||
|
|
||||||
|
$deleteTargetNative = $rootMailbox->name();
|
||||||
$deleteTargetIdentifier = new CollectionIdentifier($this->provider(), (string) $this->identifier(), $deleteTargetNative);
|
$deleteTargetIdentifier = new CollectionIdentifier($this->provider(), (string) $this->identifier(), $deleteTargetNative);
|
||||||
} else {
|
} else {
|
||||||
$deleteTargetNative = $deleteTarget;
|
$deleteTargetNative = $deleteTarget;
|
||||||
@@ -636,7 +660,7 @@ class Service implements ServiceBaseInterface, ServiceMutableInterface, ServiceC
|
|||||||
|
|
||||||
foreach ($uids as $uid) {
|
foreach ($uids as $uid) {
|
||||||
$mutatedUid = $mutations[$uid] ?? null;
|
$mutatedUid = $mutations[$uid] ?? null;
|
||||||
$results[(string)$sourceEntities[$uid]] = [
|
$list[(string)$sourceEntities[$uid]] = [
|
||||||
'disposition' => $deleteMode === 'soft' ? 'moved' : 'deleted',
|
'disposition' => $deleteMode === 'soft' ? 'moved' : 'deleted',
|
||||||
'destination' => $deleteMode === 'soft' ? $deleteTargetIdentifier : null,
|
'destination' => $deleteMode === 'soft' ? $deleteTargetIdentifier : null,
|
||||||
'mutation' => $mutatedUid !== null ? new EntityIdentifier($this->provider(), $this->identifier(), $deleteTargetIdentifier->collection(), $mutatedUid) : null,
|
'mutation' => $mutatedUid !== null ? new EntityIdentifier($this->provider(), $this->identifier(), $deleteTargetIdentifier->collection(), $mutatedUid) : null,
|
||||||
@@ -644,7 +668,12 @@ class Service implements ServiceBaseInterface, ServiceMutableInterface, ServiceC
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return $results;
|
return $list;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function entityPatch(MessagePropertiesMutableInterface $properties, EntityIdentifier ...$targets): array
|
||||||
|
{
|
||||||
|
throw new \RuntimeException('Entity patching is not supported in this service');
|
||||||
}
|
}
|
||||||
|
|
||||||
public function entityPatch(MessagePropertiesMutableInterface $properties, EntityIdentifier ...$targets): array
|
public function entityPatch(MessagePropertiesMutableInterface $properties, EntityIdentifier ...$targets): array
|
||||||
@@ -736,6 +765,11 @@ class Service implements ServiceBaseInterface, ServiceMutableInterface, ServiceC
|
|||||||
return $list;
|
return $list;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function entityCopy(CollectionIdentifier $target, EntityIdentifier ...$sources): array
|
||||||
|
{
|
||||||
|
throw new \RuntimeException('Entity copying is not supported in this service');
|
||||||
|
}
|
||||||
|
|
||||||
private function groupEntitiesByCollection(EntityIdentifier ...$identifiers): array
|
private function groupEntitiesByCollection(EntityIdentifier ...$identifiers): array
|
||||||
{
|
{
|
||||||
$list = [];
|
$list = [];
|
||||||
|
|||||||
@@ -106,7 +106,7 @@ class RemoteMailService
|
|||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
$status = $this->client->perform(new StatusCommand($mailbox->name(), self::DEFAULT_MAILBOX_STATUS_ITEMS));
|
$status = $this->client->perform(new StatusCommand($mailbox->name(), self::DEFAULT_MAILBOX_STATUS_ITEMS));
|
||||||
$mailbox->fromStatus($status);
|
$mailbox = $mailbox->fromStatus($status);
|
||||||
} catch (ImapException) {
|
} catch (ImapException) {
|
||||||
// do nothing
|
// do nothing
|
||||||
}
|
}
|
||||||
@@ -132,7 +132,7 @@ class RemoteMailService
|
|||||||
$mailbox = reset($mailbox);
|
$mailbox = reset($mailbox);
|
||||||
// enrich with STATUS
|
// enrich with STATUS
|
||||||
$status = $this->client->perform(new StatusCommand($mailbox->name(), self::DEFAULT_MAILBOX_STATUS_ITEMS));
|
$status = $this->client->perform(new StatusCommand($mailbox->name(), self::DEFAULT_MAILBOX_STATUS_ITEMS));
|
||||||
$mailbox->fromStatus($status);
|
$mailbox = $mailbox->fromStatus($status);
|
||||||
|
|
||||||
return $mailbox;
|
return $mailbox;
|
||||||
}
|
}
|
||||||
@@ -241,14 +241,31 @@ class RemoteMailService
|
|||||||
*/
|
*/
|
||||||
public function entityList(string $collection, ?IFilter $filter = null, ?ISort $sort = null, ?IRange $range = null): Generator
|
public function entityList(string $collection, ?IFilter $filter = null, ?ISort $sort = null, ?IRange $range = null): Generator
|
||||||
{
|
{
|
||||||
|
$options = FetchOptions::message()->withBodyText();
|
||||||
|
|
||||||
|
// fast path: fetch all messages without filtering, sorting or pagination
|
||||||
|
if ($filter === null && $sort === null && $range === null) {
|
||||||
|
$mailbox = $this->client->perform(new SelectCommand($collection, true));
|
||||||
|
|
||||||
|
if ($mailbox === null) {
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
|
||||||
|
yield from $this->client->perform(new FetchManyCommand(
|
||||||
|
FetchTarget::all(),
|
||||||
|
$options,
|
||||||
|
));
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
// find all the UIDs matching the filter
|
// find all the UIDs matching the filter
|
||||||
$uids = $this->entityFind($collection, $filter, $sort, $range);
|
$uids = $this->entityFind($collection, $filter, $sort, $range);
|
||||||
|
|
||||||
if (empty($uids)) {
|
if (empty($uids)) {
|
||||||
return [];
|
return [];
|
||||||
}
|
}
|
||||||
|
|
||||||
$options = FetchOptions::default()->withBodyText();
|
|
||||||
|
|
||||||
yield from $this->entityFetch($collection, $options, ...$uids);
|
yield from $this->entityFetch($collection, $options, ...$uids);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -264,7 +281,7 @@ class RemoteMailService
|
|||||||
return [];
|
return [];
|
||||||
}
|
}
|
||||||
|
|
||||||
$options ??= FetchOptions::default();
|
$options ??= FetchOptions::message()->withBodyText();
|
||||||
$this->client->perform(new SelectCommand($collection, true));
|
$this->client->perform(new SelectCommand($collection, true));
|
||||||
|
|
||||||
$request = new FetchManyCommand(
|
$request = new FetchManyCommand(
|
||||||
@@ -599,7 +616,7 @@ class RemoteMailService
|
|||||||
*/
|
*/
|
||||||
private function entitySortClientSide(array $uids, ISort $sort): array
|
private function entitySortClientSide(array $uids, ISort $sort): array
|
||||||
{
|
{
|
||||||
$options = FetchOptions::summary();
|
$options = FetchOptions::default();
|
||||||
foreach ($sort->conditions() as $condition) {
|
foreach ($sort->conditions() as $condition) {
|
||||||
if (in_array($condition['attribute'] ?? '', ['from', 'to', 'subject', 'sent'], true)) {
|
if (in_array($condition['attribute'] ?? '', ['from', 'to', 'subject', 'sent'], true)) {
|
||||||
$options = $options->withEnvelope();
|
$options = $options->withEnvelope();
|
||||||
@@ -636,7 +653,7 @@ class RemoteMailService
|
|||||||
'from' => $this->entityPrimaryAddressValue($left->from()) <=> $this->entityPrimaryAddressValue($right->from()),
|
'from' => $this->entityPrimaryAddressValue($left->from()) <=> $this->entityPrimaryAddressValue($right->from()),
|
||||||
'to' => $this->entityPrimaryAddressValue($left->to()) <=> $this->entityPrimaryAddressValue($right->to()),
|
'to' => $this->entityPrimaryAddressValue($left->to()) <=> $this->entityPrimaryAddressValue($right->to()),
|
||||||
'subject' => $this->entityScalarValue($left->subject()) <=> $this->entityScalarValue($right->subject()),
|
'subject' => $this->entityScalarValue($left->subject()) <=> $this->entityScalarValue($right->subject()),
|
||||||
'received' => $this->entityTimestampValue($left->internalDate()) <=> $this->entityTimestampValue($right->internalDate()),
|
'received' => $this->entityTimestampValue($left->receivedAt() ?? $left->internalDate()) <=> $this->entityTimestampValue($right->receivedAt() ?? $right->internalDate()),
|
||||||
'sent' => $this->entityTimestampValue($left->sentAt()) <=> $this->entityTimestampValue($right->sentAt()),
|
'sent' => $this->entityTimestampValue($left->sentAt()) <=> $this->entityTimestampValue($right->sentAt()),
|
||||||
'size' => $left->size() <=> $right->size(),
|
'size' => $left->size() <=> $right->size(),
|
||||||
default => 0,
|
default => 0,
|
||||||
|
|||||||
Reference in New Issue
Block a user