7 Commits

Author SHA1 Message Date
Sebastian 689d196176 chore(deps): update dependency phpunit/phpunit to v12.5.35
Build Test / test (pull_request) Successful in 50s
JS Unit Tests / test (pull_request) Successful in 1m13s
PHP Integration Tests / Integration Tests (pull_request) Failing after 1m18s
PHP Unit Tests / test (pull_request) Successful in 2m4s
2026-09-10 03:05:04 +00:00
Sebastian c8a632d20f refactor(module): register integrations through runtime contexts
Signed-off-by: Sebastian Krupinski <krupinski01@gmail.com>
2026-08-31 22:55:11 -04:00
Sebastian 320db9f82b fix: move draft to sent and clear draft flag on submission
Signed-off-by: Sebastian Krupinski <krupinski01@gmail.com>
2026-08-25 22:42:16 -04:00
Sebastian 72d72b29c5 feat: implement entityCreate and entityModify
Signed-off-by: Sebastian Krupinski <krupinski01@gmail.com>
2026-08-19 21:49:56 -04:00
Sebastian a7a83f5737 chore: adjust logging folder name
Signed-off-by: Sebastian Krupinski <krupinski01@gmail.com>
2026-08-19 21:24:55 -04:00
Sebastian f901f13953 feat: listen to user life cycle events
Signed-off-by: Sebastian Krupinski <krupinski01@gmail.com>
2026-08-09 22:36:41 -04:00
Sebastian f39dd75ae5 fix: folder deletion while in trash
Signed-off-by: Sebastian Krupinski <krupinski01@gmail.com>
2026-08-07 20:45:29 -04:00
7 changed files with 442 additions and 168 deletions
Generated
+58 -46
View File
@@ -8,21 +8,21 @@
"packages": [ "packages": [
{ {
"name": "guzzlehttp/guzzle", "name": "guzzlehttp/guzzle",
"version": "7.15.3", "version": "7.15.1",
"source": { "source": {
"type": "git", "type": "git",
"url": "https://github.com/guzzle/guzzle.git", "url": "https://github.com/guzzle/guzzle.git",
"reference": "ae311b8f045ea93ce7b1c9cdb7cec06c53f944bc" "reference": "61443dfb33c62f308ee8add20f45b4d6e4bf8d2f"
}, },
"dist": { "dist": {
"type": "zip", "type": "zip",
"url": "https://api.github.com/repos/guzzle/guzzle/zipball/ae311b8f045ea93ce7b1c9cdb7cec06c53f944bc", "url": "https://api.github.com/repos/guzzle/guzzle/zipball/61443dfb33c62f308ee8add20f45b4d6e4bf8d2f",
"reference": "ae311b8f045ea93ce7b1c9cdb7cec06c53f944bc", "reference": "61443dfb33c62f308ee8add20f45b4d6e4bf8d2f",
"shasum": "" "shasum": ""
}, },
"require": { "require": {
"ext-json": "*", "ext-json": "*",
"guzzlehttp/promises": "^2.5.2", "guzzlehttp/promises": "^2.5.1",
"guzzlehttp/psr7": "^2.13", "guzzlehttp/psr7": "^2.13",
"php": "^7.2.5 || ^8.0", "php": "^7.2.5 || ^8.0",
"psr/http-client": "^1.0", "psr/http-client": "^1.0",
@@ -116,7 +116,7 @@
], ],
"support": { "support": {
"issues": "https://github.com/guzzle/guzzle/issues", "issues": "https://github.com/guzzle/guzzle/issues",
"source": "https://github.com/guzzle/guzzle/tree/7.15.3" "source": "https://github.com/guzzle/guzzle/tree/7.15.1"
}, },
"funding": [ "funding": [
{ {
@@ -132,20 +132,20 @@
"type": "tidelift" "type": "tidelift"
} }
], ],
"time": "2026-08-05T19:48:21+00:00" "time": "2026-07-18T11:23:11+00:00"
}, },
{ {
"name": "guzzlehttp/promises", "name": "guzzlehttp/promises",
"version": "2.5.2", "version": "2.5.1",
"source": { "source": {
"type": "git", "type": "git",
"url": "https://github.com/guzzle/promises.git", "url": "https://github.com/guzzle/promises.git",
"reference": "2823687acff28b2dbe67b2508a6b300e2c3fa4ce" "reference": "9ad1e4fc607446a055b95870c7f668e93b5cff29"
}, },
"dist": { "dist": {
"type": "zip", "type": "zip",
"url": "https://api.github.com/repos/guzzle/promises/zipball/2823687acff28b2dbe67b2508a6b300e2c3fa4ce", "url": "https://api.github.com/repos/guzzle/promises/zipball/9ad1e4fc607446a055b95870c7f668e93b5cff29",
"reference": "2823687acff28b2dbe67b2508a6b300e2c3fa4ce", "reference": "9ad1e4fc607446a055b95870c7f668e93b5cff29",
"shasum": "" "shasum": ""
}, },
"require": { "require": {
@@ -200,7 +200,7 @@
], ],
"support": { "support": {
"issues": "https://github.com/guzzle/promises/issues", "issues": "https://github.com/guzzle/promises/issues",
"source": "https://github.com/guzzle/promises/tree/2.5.2" "source": "https://github.com/guzzle/promises/tree/2.5.1"
}, },
"funding": [ "funding": [
{ {
@@ -216,7 +216,7 @@
"type": "tidelift" "type": "tidelift"
} }
], ],
"time": "2026-08-05T19:30:54+00:00" "time": "2026-07-08T15:48:39+00:00"
}, },
{ {
"name": "guzzlehttp/psr7", "name": "guzzlehttp/psr7",
@@ -862,20 +862,20 @@
"packages-dev": [ "packages-dev": [
{ {
"name": "myclabs/deep-copy", "name": "myclabs/deep-copy",
"version": "1.13.4", "version": "1.14.0",
"source": { "source": {
"type": "git", "type": "git",
"url": "https://github.com/myclabs/DeepCopy.git", "url": "https://github.com/myclabs/DeepCopy.git",
"reference": "07d290f0c47959fd5eed98c95ee5602db07e0b6a" "reference": "8680aa248f8e07bc8fb43f56f0f5fc77a0c96aae"
}, },
"dist": { "dist": {
"type": "zip", "type": "zip",
"url": "https://api.github.com/repos/myclabs/DeepCopy/zipball/07d290f0c47959fd5eed98c95ee5602db07e0b6a", "url": "https://api.github.com/repos/myclabs/DeepCopy/zipball/8680aa248f8e07bc8fb43f56f0f5fc77a0c96aae",
"reference": "07d290f0c47959fd5eed98c95ee5602db07e0b6a", "reference": "8680aa248f8e07bc8fb43f56f0f5fc77a0c96aae",
"shasum": "" "shasum": ""
}, },
"require": { "require": {
"php": "^7.1 || ^8.0" "php": "^8.0"
}, },
"conflict": { "conflict": {
"doctrine/collections": "<1.6.8", "doctrine/collections": "<1.6.8",
@@ -910,15 +910,15 @@
], ],
"support": { "support": {
"issues": "https://github.com/myclabs/DeepCopy/issues", "issues": "https://github.com/myclabs/DeepCopy/issues",
"source": "https://github.com/myclabs/DeepCopy/tree/1.13.4" "source": "https://github.com/myclabs/DeepCopy/tree/1.14.0"
}, },
"funding": [ "funding": [
{ {
"url": "https://tidelift.com/funding/github/packagist/myclabs/deep-copy", "url": "https://github.com/mnapoli",
"type": "tidelift" "type": "github"
} }
], ],
"time": "2025-08-01T08:46:24+00:00" "time": "2026-08-11T10:17:44+00:00"
}, },
{ {
"name": "nikic/php-parser", "name": "nikic/php-parser",
@@ -1185,23 +1185,23 @@
}, },
{ {
"name": "phpunit/php-file-iterator", "name": "phpunit/php-file-iterator",
"version": "6.0.1", "version": "6.0.2",
"source": { "source": {
"type": "git", "type": "git",
"url": "https://github.com/sebastianbergmann/php-file-iterator.git", "url": "https://github.com/sebastianbergmann/php-file-iterator.git",
"reference": "3d1cd096ef6bea4bf2762ba586e35dbd317cbfd5" "reference": "a248d1640ab059b075f53a2ef0f9856e864e06b5"
}, },
"dist": { "dist": {
"type": "zip", "type": "zip",
"url": "https://api.github.com/repos/sebastianbergmann/php-file-iterator/zipball/3d1cd096ef6bea4bf2762ba586e35dbd317cbfd5", "url": "https://api.github.com/repos/sebastianbergmann/php-file-iterator/zipball/a248d1640ab059b075f53a2ef0f9856e864e06b5",
"reference": "3d1cd096ef6bea4bf2762ba586e35dbd317cbfd5", "reference": "a248d1640ab059b075f53a2ef0f9856e864e06b5",
"shasum": "" "shasum": ""
}, },
"require": { "require": {
"php": ">=8.3" "php": ">=8.3"
}, },
"require-dev": { "require-dev": {
"phpunit/phpunit": "^12.0" "phpunit/phpunit": "^12.5.33"
}, },
"type": "library", "type": "library",
"extra": { "extra": {
@@ -1234,7 +1234,7 @@
"support": { "support": {
"issues": "https://github.com/sebastianbergmann/php-file-iterator/issues", "issues": "https://github.com/sebastianbergmann/php-file-iterator/issues",
"security": "https://github.com/sebastianbergmann/php-file-iterator/security/policy", "security": "https://github.com/sebastianbergmann/php-file-iterator/security/policy",
"source": "https://github.com/sebastianbergmann/php-file-iterator/tree/6.0.1" "source": "https://github.com/sebastianbergmann/php-file-iterator/tree/6.0.2"
}, },
"funding": [ "funding": [
{ {
@@ -1254,7 +1254,7 @@
"type": "tidelift" "type": "tidelift"
} }
], ],
"time": "2026-02-02T14:04:18+00:00" "time": "2026-08-25T14:40:53+00:00"
}, },
{ {
"name": "phpunit/php-invoker", "name": "phpunit/php-invoker",
@@ -1442,16 +1442,16 @@
}, },
{ {
"name": "phpunit/phpunit", "name": "phpunit/phpunit",
"version": "12.5.31", "version": "12.5.35",
"source": { "source": {
"type": "git", "type": "git",
"url": "https://github.com/sebastianbergmann/phpunit.git", "url": "https://github.com/sebastianbergmann/phpunit.git",
"reference": "0608d157a284f15cc73b99a3327eff06b66a176d" "reference": "345ac22e42cb14cdc7aa8428655d8bb8c82a4aab"
}, },
"dist": { "dist": {
"type": "zip", "type": "zip",
"url": "https://api.github.com/repos/sebastianbergmann/phpunit/zipball/0608d157a284f15cc73b99a3327eff06b66a176d", "url": "https://api.github.com/repos/sebastianbergmann/phpunit/zipball/345ac22e42cb14cdc7aa8428655d8bb8c82a4aab",
"reference": "0608d157a284f15cc73b99a3327eff06b66a176d", "reference": "345ac22e42cb14cdc7aa8428655d8bb8c82a4aab",
"shasum": "" "shasum": ""
}, },
"require": { "require": {
@@ -1461,18 +1461,18 @@
"ext-libxml": "*", "ext-libxml": "*",
"ext-mbstring": "*", "ext-mbstring": "*",
"ext-xmlwriter": "*", "ext-xmlwriter": "*",
"myclabs/deep-copy": "^1.13.4", "myclabs/deep-copy": "^1.14.0",
"phar-io/manifest": "^2.0.4", "phar-io/manifest": "^2.0.4",
"phar-io/version": "^3.2.1", "phar-io/version": "^3.2.1",
"php": ">=8.3", "php": ">=8.3",
"phpunit/php-code-coverage": "^12.5.7", "phpunit/php-code-coverage": "^12.5.7",
"phpunit/php-file-iterator": "^6.0.1", "phpunit/php-file-iterator": "^6.0.2",
"phpunit/php-invoker": "^6.0.0", "phpunit/php-invoker": "^6.0.0",
"phpunit/php-text-template": "^5.0.0", "phpunit/php-text-template": "^5.0.0",
"phpunit/php-timer": "^8.0.0", "phpunit/php-timer": "^8.0.0",
"sebastian/cli-parser": "^4.2.1", "sebastian/cli-parser": "^4.2.1",
"sebastian/comparator": "^7.1.8", "sebastian/comparator": "^7.1.8",
"sebastian/diff": "^7.0.0", "sebastian/diff": "^7.0.1",
"sebastian/environment": "^8.1.2", "sebastian/environment": "^8.1.2",
"sebastian/exporter": "^7.0.3", "sebastian/exporter": "^7.0.3",
"sebastian/global-state": "^8.0.3", "sebastian/global-state": "^8.0.3",
@@ -1520,7 +1520,7 @@
"support": { "support": {
"issues": "https://github.com/sebastianbergmann/phpunit/issues", "issues": "https://github.com/sebastianbergmann/phpunit/issues",
"security": "https://github.com/sebastianbergmann/phpunit/security/policy", "security": "https://github.com/sebastianbergmann/phpunit/security/policy",
"source": "https://github.com/sebastianbergmann/phpunit/tree/12.5.31" "source": "https://github.com/sebastianbergmann/phpunit/tree/12.5.35"
}, },
"funding": [ "funding": [
{ {
@@ -1528,7 +1528,7 @@
"type": "other" "type": "other"
} }
], ],
"time": "2026-07-06T14:54:16+00:00" "time": "2026-09-09T04:48:50+00:00"
}, },
{ {
"name": "sebastian/cli-parser", "name": "sebastian/cli-parser",
@@ -1751,24 +1751,24 @@
}, },
{ {
"name": "sebastian/diff", "name": "sebastian/diff",
"version": "7.0.0", "version": "7.0.1",
"source": { "source": {
"type": "git", "type": "git",
"url": "https://github.com/sebastianbergmann/diff.git", "url": "https://github.com/sebastianbergmann/diff.git",
"reference": "7ab1ea946c012266ca32390913653d844ecd085f" "reference": "cd4cabe39f8a4e8ee6818ba99f10a05561ea4ad6"
}, },
"dist": { "dist": {
"type": "zip", "type": "zip",
"url": "https://api.github.com/repos/sebastianbergmann/diff/zipball/7ab1ea946c012266ca32390913653d844ecd085f", "url": "https://api.github.com/repos/sebastianbergmann/diff/zipball/cd4cabe39f8a4e8ee6818ba99f10a05561ea4ad6",
"reference": "7ab1ea946c012266ca32390913653d844ecd085f", "reference": "cd4cabe39f8a4e8ee6818ba99f10a05561ea4ad6",
"shasum": "" "shasum": ""
}, },
"require": { "require": {
"php": ">=8.3" "php": ">=8.3"
}, },
"require-dev": { "require-dev": {
"phpunit/phpunit": "^12.0", "phpunit/phpunit": "^12.5.33",
"symfony/process": "^7.2" "symfony/process": "^7.4.17"
}, },
"type": "library", "type": "library",
"extra": { "extra": {
@@ -1806,15 +1806,27 @@
"support": { "support": {
"issues": "https://github.com/sebastianbergmann/diff/issues", "issues": "https://github.com/sebastianbergmann/diff/issues",
"security": "https://github.com/sebastianbergmann/diff/security/policy", "security": "https://github.com/sebastianbergmann/diff/security/policy",
"source": "https://github.com/sebastianbergmann/diff/tree/7.0.0" "source": "https://github.com/sebastianbergmann/diff/tree/7.0.1"
}, },
"funding": [ "funding": [
{ {
"url": "https://github.com/sebastianbergmann", "url": "https://github.com/sebastianbergmann",
"type": "github" "type": "github"
},
{
"url": "https://liberapay.com/sebastianbergmann",
"type": "liberapay"
},
{
"url": "https://thanks.dev/u/gh/sebastianbergmann",
"type": "thanks_dev"
},
{
"url": "https://tidelift.com/funding/github/packagist/sebastian/diff",
"type": "tidelift"
} }
], ],
"time": "2025-02-07T04:55:46+00:00" "time": "2026-08-25T15:35:54+00:00"
}, },
{ {
"name": "sebastian/environment", "name": "sebastian/environment",
+27
View File
@@ -0,0 +1,27 @@
<?php
declare(strict_types=1);
namespace KTXM\ProviderJmapc\Listeners;
use KTXC\User\Event\UserCreatedEvent;
use KTXC\User\Event\UserDeletingEvent;
use KTXM\ProviderJmapc\Stores\ServiceStore;
final class UserEventListener
{
public function __construct(
private readonly ServiceStore $serviceStore,
) {
}
public function onUserCreated(UserCreatedEvent $event): void
{
// TODO: implement provisioning configuration
}
public function onUserDeleting(UserDeletingEvent $event): void
{
$this->serviceStore->deleteByUser($event->tenantIdentifier(), $event->userIdentifier());
}
}
+17 -12
View File
@@ -10,9 +10,15 @@ declare(strict_types=1);
namespace KTXM\ProviderJmapc; namespace KTXM\ProviderJmapc;
use KTXC\Resource\ProviderManager; use KTXC\Resource\ProviderManager;
use KTXF\Module\ModuleBrowserInterface; use KTXC\User\Event\UserCreatedEvent;
use KTXC\User\Event\UserDeletingEvent;
use KTXF\Event\DeliveryMode;
use KTXF\Event\EventListenerRegistrarInterface;
use KTXF\Module\Configuration\BrowserModuleContextInterface;
use KTXF\Module\Configuration\ModuleContextInterface;
use KTXF\Module\ModuleInstanceAbstract; use KTXF\Module\ModuleInstanceAbstract;
use KTXF\Resource\Provider\ProviderInterface; use KTXF\Resource\Provider\ProviderInterface;
use KTXM\ProviderJmapc\Listeners\UserEventListener;
use KTXM\ProviderJmapc\Providers\Mail\Provider as MailProvider; use KTXM\ProviderJmapc\Providers\Mail\Provider as MailProvider;
use KTXM\ProviderJmapc\Providers\Chrono\Provider as ChronoProvider; use KTXM\ProviderJmapc\Providers\Chrono\Provider as ChronoProvider;
use KTXM\ProviderJmapc\Providers\People\Provider as PeopleProvider; use KTXM\ProviderJmapc\Providers\People\Provider as PeopleProvider;
@@ -23,10 +29,11 @@ use KTXM\ProviderJmapc\Providers\Document\Provider as DocumentProvider;
* *
* Provides mail, calendar, and contacts services via JMAP protocol. * Provides mail, calendar, and contacts services via JMAP protocol.
*/ */
class Module extends ModuleInstanceAbstract implements ModuleBrowserInterface class Module extends ModuleInstanceAbstract
{ {
public function __construct( public function __construct(
private readonly ProviderManager $providerManager, private readonly ProviderManager $providerManager,
private readonly EventListenerRegistrarInterface $events,
) {} ) {}
public function handle(): string public function handle(): string
@@ -67,6 +74,9 @@ class Module extends ModuleInstanceAbstract implements ModuleBrowserInterface
public function boot(): void public function boot(): void
{ {
// Register listeners
$this->events->listen($this->handle(), UserCreatedEvent::class, UserEventListener::class, 'onUserCreated', DeliveryMode::Deferred);
$this->events->listen($this->handle(), UserDeletingEvent::class, UserEventListener::class, 'onUserDeleting', DeliveryMode::Deferred);
// Register JMAP providers - all three share the same service store // Register JMAP providers - all three share the same service store
$this->providerManager->register(ProviderInterface::TYPE_MAIL, 'jmap', MailProvider::class); $this->providerManager->register(ProviderInterface::TYPE_MAIL, 'jmap', MailProvider::class);
//$this->providerManager->register(ProviderInterface::TYPE_CHRONO, 'jmap', ChronoProvider::class); //$this->providerManager->register(ProviderInterface::TYPE_CHRONO, 'jmap', ChronoProvider::class);
@@ -74,15 +84,10 @@ class Module extends ModuleInstanceAbstract implements ModuleBrowserInterface
$this->providerManager->register(ProviderInterface::TYPE_DOCUMENT, 'jmap', DocumentProvider::class); $this->providerManager->register(ProviderInterface::TYPE_DOCUMENT, 'jmap', DocumentProvider::class);
} }
public function registerBI(): array { public function configure(ModuleContextInterface $context): void
return [ {
'handle' => $this->handle(), if ($context instanceof BrowserModuleContextInterface) {
'namespace' => 'ProviderJmapc', $context->registerModule($this, 'ProviderJmapc', 'static/module.mjs');
'version' => $this->version(), }
'label' => $this->label(),
'author' => $this->author(),
'description' => $this->description(),
'boot' => 'static/module.mjs',
];
} }
} }
+139 -52
View File
@@ -102,8 +102,8 @@ class Service implements ServiceBaseInterface, ServiceMutableInterface, ServiceC
], ],
self::CAPABILITY_ENTITY_FETCH => true, self::CAPABILITY_ENTITY_FETCH => true,
self::CAPABILITY_ENTITY_EXTANT => true, self::CAPABILITY_ENTITY_EXTANT => true,
self::CAPABILITY_ENTITY_CREATE => false, self::CAPABILITY_ENTITY_CREATE => true,
self::CAPABILITY_ENTITY_MODIFY => false, self::CAPABILITY_ENTITY_MODIFY => true,
self::CAPABILITY_ENTITY_PATCH => true, self::CAPABILITY_ENTITY_PATCH => true,
self::CAPABILITY_ENTITY_DELETE => true, self::CAPABILITY_ENTITY_DELETE => true,
self::CAPABILITY_ENTITY_MOVE => true, self::CAPABILITY_ENTITY_MOVE => true,
@@ -484,17 +484,34 @@ class Service implements ServiceBaseInterface, ServiceMutableInterface, ServiceC
} }
} }
// we need to determine if the folder being deleted is already in the trash if ($deleteMode === 'soft') {
if (str_starts_with((string) $target->collection(), (string) $deleteTargetIdentifier?->collection())) { $targetMailbox = $this->mailService->collectionFetch((string) $target->collection());
// if so, we should hard delete instead of moving to avoid duplicates in the trash if ($targetMailbox === null) {
$deleteMode = 'hard'; throw new \RuntimeException('Collection not found for delete operation');
}
$targetIdentifier = (string) $target->collection();
$trashIdentifier = (string) $deleteTargetIdentifier->collection();
$parentIdentifier = isset($targetMailbox['parentId'])
? (string) $targetMailbox['parentId']
: null;
// JMAP mailbox IDs are opaque, so containment must be determined
// from parentId rather than by comparing ID prefixes.
if ($targetIdentifier === $trashIdentifier || $parentIdentifier === $trashIdentifier) {
$deleteMode = 'hard';
}
} }
$result = match ($deleteMode) { if ($deleteMode === 'soft') {
'soft' => $this->collectionMove($deleteTargetIdentifier, $target), return $this->collectionMove($deleteTargetIdentifier, $target);
'hard' => $this->mailService->collectionDestroy($target->collection(), $force), }
};
return $result; if ($this->mailService->collectionDestroy((string) $target->collection(), $force) === null) {
throw new \RuntimeException("Failed to delete collection: {$target->collection()}");
}
return true;
} }
public function collectionMove(CollectionIdentifier $target, CollectionIdentifier $source): CollectionBaseInterface public function collectionMove(CollectionIdentifier $target, CollectionIdentifier $source): CollectionBaseInterface
@@ -621,14 +638,42 @@ 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
{ {
// TODO: Implement entity create if ($target->provider() !== $this->provider() || (string)$target->service() !== (string)$this->identifier()) {
return $this->entityFresh(); throw new \InvalidArgumentException('Target collection does not belong to this service: ' . (string)$target);
}
$this->initialize();
[$nativeMessage, $nativeAttachments] = $this->messagePayload($properties);
$created = $this->mailService->entityCreate((string)$target->collection(), $nativeMessage, $nativeAttachments);
if ($created === null || !isset($created['id'])) {
throw new \RuntimeException('JMAP entity creation did not return an identifier');
}
$created['mailboxIds'] ??= [(string)$target->collection() => true];
return $this->entityFresh()->fromJmap($created);
} }
public function entityModify(EntityIdentifier $identifier, MessagePropertiesMutableInterface $properties): EntityResource public function entityModify(EntityIdentifier $target, MessagePropertiesMutableInterface $properties): EntityResource
{ {
// TODO: Implement entity modify if ($target->provider() !== $this->provider() || (string)$target->service() !== (string)$this->identifier()) {
return $this->entityFresh(); throw new \InvalidArgumentException('Target collection does not belong to this service: ' . (string)$target);
}
$this->initialize();
[$nativeMessage, $nativeAttachments] = $this->messagePayload($properties);
$nativeMessage['id'] = (string)$target->entity();
$nativeMessage['mailboxIds'] = [(string)$target->collection() => true];
// JMAP replaces immutable Email content and returns the new authoritative ID.
$modified = $this->mailService->entityReplace($nativeMessage, $nativeAttachments);
if ($modified === null) {
throw new \RuntimeException('JMAP entity modification failed');
}
return $this->entityFresh()->fromJmap($modified);
} }
public function entityPatch(MessagePropertiesMutableInterface $properties, EntityIdentifier ...$targets): array public function entityPatch(MessagePropertiesMutableInterface $properties, EntityIdentifier ...$targets): array
@@ -880,32 +925,14 @@ class Service implements ServiceBaseInterface, ServiceMutableInterface, ServiceC
); );
} }
$nativeProperties = $this->normalizeMessageProperties($message); [$nativeMessage, $nativeAttachments] = $this->messagePayload($message);
$attachments = $nativeProperties->getAttachments(); $transportId = $this->mailService->entitySubmitFresh(
foreach ($attachments as $attachment) { $identityId,
if ($attachment->getBlobId() !== null) { $nativeMessage,
continue; $preSendTarget->collection(),
} $postSentTarget->collection(),
$nativeAttachments,
$content = $attachment->getContent(); );
if ($content === null) {
throw new \InvalidArgumentException(sprintf(
'Attachment "%s" has neither content nor a JMAP blob identifier',
$attachment->getName() ?? 'unnamed',
));
}
$attachment->setBlobId($this->mailService->blobDeposit(
$attachment->getType() ?? 'application/octet-stream',
$content,
));
}
if ($attachments !== []) {
$nativeProperties->setAttachments(...$attachments);
}
$nativeMessage = $nativeProperties->toJmap();
$transportId = $this->mailService->entitySubmitFresh($identityId, $nativeMessage, $preSendTarget->collection(), $postSentTarget->collection());
return new EntitySubmitResult( return new EntitySubmitResult(
EntitySubmitResult::DISPOSITION_SENT, EntitySubmitResult::DISPOSITION_SENT,
@@ -923,12 +950,38 @@ class Service implements ServiceBaseInterface, ServiceMutableInterface, ServiceC
); );
} }
$patch = null; // determine the sent folder
if ($message !== null) { // if no explicit sent folder is configured, we will attempt to find a folder with the "sent" role.
$patch = $this->normalizeMessageProperties($message)->toJmap(); $postSentTarget = $this->auxiliary['sentTarget'] ?? null;
$postSentTarget = $postSentTarget !== null && (string) $postSentTarget !== ''
? new CollectionIdentifier($this->provider(), (string) $this->identifier(), (string) $postSentTarget)
: $this->resolveCollectionByRole(CollectionRoles::Sent->value);
if ($postSentTarget === null
|| $postSentTarget->provider() !== $this->provider()
|| $postSentTarget->service() !== (string)$this->identifier()) {
return new EntitySubmitResult(
EntitySubmitResult::DISPOSITION_ERROR,
errorCode: 'sent_collection_not_found',
errorMessage: 'No Sent target collection is configured or could be resolved for this service',
);
} }
$transportId = $this->mailService->entitySubmitDraft($identityId, (string)$source->entity(), $patch); if ($message !== null) {
[$nativeMessage, $nativeAttachments] = $this->messagePayload($message);
} else {
$nativeMessage = null;
$nativeAttachments = [];
}
$transportId = $this->mailService->entitySubmitDraft(
$identityId,
(string)$source->entity(),
(string)$source->collection(),
$postSentTarget->collection(),
$nativeMessage,
$nativeAttachments,
);
return new EntitySubmitResult( return new EntitySubmitResult(
EntitySubmitResult::DISPOSITION_SENT, EntitySubmitResult::DISPOSITION_SENT,
@@ -937,16 +990,50 @@ class Service implements ServiceBaseInterface, ServiceMutableInterface, ServiceC
); );
} }
private function normalizeMessageProperties(MessagePropertiesMutableInterface $message): MessageProperties /**
* Convert message properties and describe attachment blobs for the remote
* service. Uploading and request batching remain remote-layer concerns.
*
* @return array{0: array, 1: array<string, array{type: string, content: string}>}
*/
private function messagePayload(MessagePropertiesMutableInterface $message): array
{ {
if ($message instanceof MessageProperties) { if ($message instanceof MessageProperties === false) {
return $message; $native = new MessageProperties([]);
$native->jsonDeserialize($message->jsonSerialize());
} else {
$native = $message;
} }
$native = new MessageProperties([]); $attachments = $native->getAttachments();
$native->jsonDeserialize($message->jsonSerialize()); $uploads = [];
return $native; foreach ($attachments as $attachment) {
if ($attachment->getBlobId() !== null) {
continue;
}
$content = $attachment->getContent();
if ($content === null) {
throw new \InvalidArgumentException(sprintf(
'Attachment "%s" has neither content nor a JMAP blob identifier',
$attachment->getName() ?? 'unnamed',
));
}
$creationId = 'attachment_' . count($uploads);
$attachment->setBlobId('#' . $creationId);
$uploads[$creationId] = [
'type' => $attachment->getType() ?? 'application/octet-stream',
'content' => $content,
];
}
if ($attachments !== []) {
$native->setAttachments(...$attachments);
}
return [$native->toJmap(), $uploads];
} }
private function resolveCollectionByRole(string $role): ?CollectionIdentifier private function resolveCollectionByRole(string $role): ?CollectionIdentifier
+189 -57
View File
@@ -11,6 +11,7 @@ namespace KTXM\ProviderJmapc\Service\Remote;
use Exception; use Exception;
use JmapClient\Client; use JmapClient\Client;
use JmapClient\Requests\Blob\BlobSet;
use JmapClient\Requests\Mail\MailboxGet; use JmapClient\Requests\Mail\MailboxGet;
use JmapClient\Requests\Mail\MailboxParameters as MailboxParametersRequest; use JmapClient\Requests\Mail\MailboxParameters as MailboxParametersRequest;
use JmapClient\Requests\Mail\MailboxQuery; use JmapClient\Requests\Mail\MailboxQuery;
@@ -727,7 +728,8 @@ class RemoteMailService {
* *
* @since Release 1.0.0 * @since Release 1.0.0
*/ */
public function entityCreate(string $location, array $so): ?array { public function entityCreate(string $location, array $so, array $attachments = []): ?array {
$blobRequest = $this->prepareAttachmentRequest($so, $attachments);
// convert entity // convert entity
$to = new MailParametersRequest(); $to = new MailParametersRequest();
$to->parametersRaw($so); $to->parametersRaw($so);
@@ -737,9 +739,11 @@ class RemoteMailService {
$r0 = new MailSet($this->dataAccount, null, $this->resourceNamespace, $this->resourceEntityLabel); $r0 = new MailSet($this->dataAccount, null, $this->resourceNamespace, $this->resourceEntityLabel);
$r0->create($id, $to); $r0->create($id, $to);
// transceive // transceive
$bundle = $this->dataStore->perform([$r0]); $requests = $blobRequest !== null ? [$blobRequest, $r0] : [$r0];
$bundle = $this->dataStore->perform($requests);
$this->assertAttachmentUploads($bundle, $blobRequest, $attachments, $so);
// extract response // extract response
$response = $bundle->response(0); $response = $bundle->response($blobRequest !== null ? 1 : 0);
// check for command error // check for command error
if ($response instanceof ResponseException) { if ($response instanceof ResponseException) {
if ($response->type() === 'unknownMethod') { if ($response->type() === 'unknownMethod') {
@@ -769,27 +773,64 @@ class RemoteMailService {
* *
* @since Release 1.0.0 * @since Release 1.0.0
*/ */
public function entityModify(array $so): ?array { public function entityReplace(array $so, array $attachments = []): ?array {
// extract entity id // JMAP Email content is immutable after creation. Replacing a draft must
$id = $so['id']; // therefore create a new Email and remove the superseded Email together.
// convert entity $id = (string)($so['id'] ?? '');
$to = new MailParametersRequest(); unset($so['id']);
$to->parametersRaw($so); if ($id === '') {
// construct request throw new Exception('Draft identifier is required for replacement', 1);
$r0 = new MailSet($this->dataAccount, null, $this->resourceNamespace, $this->resourceEntityLabel);
$r0->update($id, $to);
// transceive
$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;
$location = null;
foreach (($so['mailboxIds'] ?? []) as $mailboxId => $included) {
if ($included === true) {
$location = (string)$mailboxId;
break;
}
}
if ($location === null || $location === '') {
throw new Exception('Draft mailbox is required for replacement', 1);
}
$blobRequest = $this->prepareAttachmentRequest($so, $attachments);
$replacement = new MailParametersRequest();
$replacement->parametersRaw($so);
$replacement->in($location);
$creationId = uniqid();
// Email/set supports create and destroy in one method call. When attachment
// content is present, Blob/upload is included in the same HTTP request.
$request = new MailSet($this->dataAccount, null, $this->resourceNamespace, $this->resourceEntityLabel);
$request->create($creationId, $replacement);
$request->delete($id);
$requests = $blobRequest !== null ? [$blobRequest, $request] : [$request];
$bundle = $this->dataStore->perform($requests);
$this->assertAttachmentUploads($bundle, $blobRequest, $attachments, $so);
$response = $bundle->response($blobRequest !== null ? 1 : 0);
if ($response instanceof ResponseException) {
if ($response->type() === 'unknownMethod') {
throw new JmapUnknownMethod($response->description(), 1);
}
throw new Exception($response->type() . ': ' . $response->description(), 1);
}
$created = $response->createSuccess($creationId);
if ($created === null) {
$failure = $response->createFailure($creationId);
$type = $failure['type'] ?? 'unknownError';
$description = $failure['description'] ?? 'JMAP draft replacement failed.';
throw new Exception("$type: $description", 1);
}
if ($response->deleteSuccess($id) === null) {
$failure = $response->deleteFailure($id);
$type = $failure['type'] ?? 'unknownError';
$description = $failure['description'] ?? 'The superseded JMAP draft could not be deleted.';
throw new Exception("$type: $description", 1);
}
return array_merge($so, $created);
} }
public function entityPatch(MailParametersRequest $properties, string ...$identifiers): ?array { public function entityPatch(MailParametersRequest $properties, string ...$identifiers): ?array {
@@ -912,7 +953,8 @@ class RemoteMailService {
* *
* @since Release 2.0.0 * @since Release 2.0.0
*/ */
public function entitySubmitFresh(string $identityId, array $data, string $preSendTarget, string $postSentTarget): string { public function entitySubmitFresh(string $identityId, array $data, string $preSendTarget, string $postSentTarget, array $attachments = []): string {
$blobRequest = $this->prepareAttachmentRequest($data, $attachments);
$message = new MailParametersRequest(); $message = new MailParametersRequest();
$message->parametersRaw($data); $message->parametersRaw($data);
@@ -936,8 +978,11 @@ class RemoteMailService {
$s0->identity($identityId); $s0->identity($identityId);
$s0->message('#' . $messageId); $s0->message('#' . $messageId);
$bundle = $this->dataStore->perform([$r0, $r1]); $requests = $blobRequest !== null ? [$blobRequest, $r0, $r1] : [$r0, $r1];
$messageResponse = $bundle->response(0); $bundle = $this->dataStore->perform($requests);
$this->assertAttachmentUploads($bundle, $blobRequest, $attachments, $data);
$responseOffset = $blobRequest !== null ? 1 : 0;
$messageResponse = $bundle->response($responseOffset);
if ($messageResponse instanceof ResponseException) { if ($messageResponse instanceof ResponseException) {
throw new Exception($messageResponse->type() . ': ' . $messageResponse->description(), 1); throw new Exception($messageResponse->type() . ': ' . $messageResponse->description(), 1);
} }
@@ -950,7 +995,59 @@ class RemoteMailService {
} }
} }
$response = $bundle->response(1); $response = $bundle->response($responseOffset + 1);
if ($response instanceof ResponseException) {
if ($response->type() === 'unknownMethod') {
throw new JmapUnknownMethod($response->description(), 1);
}
throw new Exception($response->type() . ': ' . $response->description(), 1);
}
return $this->extractSubmissionIdentifier($response, $submissionId);
}
/**
* Submit an existing draft message. On success, the draft Email is moved
* from the drafts mailbox to the sent mailbox and its \Draft keyword is
* cleared in the same request, so the submitted draft becomes the sent
* copy rather than lingering as an unsent draft.
*
* @since Release 2.0.0
*/
public function entitySubmitDraft(string $identityId, string $draftId, string $preSendTarget, string $postSentTarget, ?array $patch = null, array $attachments = []): string {
if ($draftId === '') {
throw new Exception('Draft identifier is required for draft submission', 1);
}
$blobRequest = $patch !== null ? $this->prepareAttachmentRequest($patch, $attachments) : null;
$requests = $blobRequest !== null ? [$blobRequest] : [];
if ($patch !== null && $patch !== []) {
$m0 = new MailParametersRequest();
$m0->parametersRaw($patch);
$r0 = new MailSet($this->dataAccount, null, $this->resourceNamespace, $this->resourceEntityLabel);
$r0->update($draftId, $m0);
$requests[] = $r0;
}
$submissionId = 's_' . uniqid();
$r1 = new MailSubmissionSet($this->dataAccount, null, $this->resourceNamespace, $this->resourceEntityLabel);
$r1->completionUpdate($draftId, [
'mailboxIds/' . $postSentTarget => true,
'mailboxIds/' . $preSendTarget => null,
'keywords/$draft' => null,
]);
$s0 = $r1->create($submissionId);
$s0->identity($identityId);
$s0->message($draftId);
$requests[] = $r1;
$bundle = $this->dataStore->perform($requests);
if ($patch !== null) {
$this->assertAttachmentUploads($bundle, $blobRequest, $attachments, $patch);
}
$responseIndex = count($requests) - 1;
$response = $bundle->response($responseIndex);
if ($response instanceof ResponseException) { if ($response instanceof ResponseException) {
if ($response->type() === 'unknownMethod') { if ($response->type() === 'unknownMethod') {
throw new JmapUnknownMethod($response->description(), 1); throw new JmapUnknownMethod($response->description(), 1);
@@ -974,43 +1071,78 @@ class RemoteMailService {
} }
/** /**
* Submit an existing draft message. * Build one Blob/upload method call for all new attachment content. Servers
* without the Blob capability fall back to the standard upload endpoint.
* *
* @since Release 2.0.0 * @param array<string, array{type: string, content: string}> $attachments
*/ */
public function entitySubmitDraft(string $identityId, string $draftId, ?array $patch = null): string { private function prepareAttachmentRequest(array &$data, array $attachments): ?BlobSet {
if ($draftId === '') { if ($attachments === []) {
throw new Exception('Draft identifier is required for draft submission', 1); return null;
} }
$requests = []; if (!$this->dataStore->sessionCapable('blob')) {
foreach ($attachments as $creationId => $attachment) {
if ($patch !== null && $patch !== []) { $blobId = $this->blobDeposit($attachment['type'], $attachment['content']);
$m0 = new MailParametersRequest(); $this->replaceBlobReference($data, '#' . $creationId, $blobId);
$m0->parametersRaw($patch);
$r0 = new MailSet($this->dataAccount, null, $this->resourceNamespace, $this->resourceEntityLabel);
$r0->update($draftId, $m0);
$requests[] = $r0;
}
$submissionId = 's_' . uniqid();
$r1 = new MailSubmissionSet($this->dataAccount, null, $this->resourceNamespace, $this->resourceEntityLabel);
$s0 = $r1->create($submissionId);
$s0->identity($identityId);
$s0->message($draftId);
$requests[] = $r1;
$bundle = $this->dataStore->perform($requests);
$responseIndex = count($requests) - 1;
$response = $bundle->response($responseIndex);
if ($response instanceof ResponseException) {
if ($response->type() === 'unknownMethod') {
throw new JmapUnknownMethod($response->description(), 1);
} }
throw new Exception($response->type() . ': ' . $response->description(), 1); return null;
} }
return $this->extractSubmissionIdentifier($response, $submissionId); $request = new BlobSet($this->dataAccount);
foreach ($attachments as $creationId => $attachment) {
$request->create($creationId)
->type($attachment['type'])
->dataEncoded(base64_encode($attachment['content']));
}
return $request;
}
/**
* Validate a batched Blob/upload response and replace creation references
* in the returned local representation with actual server blob IDs.
*
* @param array<string, array{type: string, content: string}> $attachments
*/
private function assertAttachmentUploads(object $bundle, ?BlobSet $request, array $attachments, array &$data): void {
if ($request === null) {
return;
}
$response = $bundle->response(0);
if ($response instanceof ResponseException) {
throw new Exception('Attachment upload failed: ' . $response->type() . ': ' . $response->description(), 1);
}
foreach (array_keys($attachments) as $creationId) {
$failure = $response->createFailure($creationId);
if ($failure !== null) {
$type = $failure['type'] ?? 'unknownError';
$description = $failure['description'] ?? 'Attachment upload failed.';
throw new Exception("$type: $description", 1);
}
$success = $response->createSuccess($creationId);
$blobId = is_array($success) ? ($success['id'] ?? null) : null;
if (!is_string($blobId) || $blobId === '') {
throw new Exception('Attachment upload did not return a blob identifier', 1);
}
$this->replaceBlobReference($data, '#' . $creationId, $blobId);
}
}
private function replaceBlobReference(array &$data, string $reference, string $blobId): void {
foreach ($data as &$value) {
if (is_array($value)) {
$this->replaceBlobReference($value, $reference, $blobId);
continue;
}
if ($value === $reference) {
$value = $blobId;
}
}
unset($value);
} }
private function extractSubmissionIdentifier(object $response, string $createId): string { private function extractSubmissionIdentifier(object $response, string $createId): string {
+1 -1
View File
@@ -73,7 +73,7 @@ class RemoteService {
} }
// debugging // debugging
if ($service->getDebug()) { if ($service->getDebug()) {
$logDirectory = dirname(__DIR__, 5) . '/var/logs/jmap'; $logDirectory = dirname(__DIR__, 5) . '/var/logs/provider_jmap';
if (!is_dir($logDirectory) && !mkdir($logDirectory, 0775, true) && !is_dir($logDirectory)) { if (!is_dir($logDirectory) && !mkdir($logDirectory, 0775, true) && !is_dir($logDirectory)) {
throw new \RuntimeException(sprintf('Unable to create JMAP log directory: %s', $logDirectory)); throw new \RuntimeException(sprintf('Unable to create JMAP log directory: %s', $logDirectory));
} }
+11
View File
@@ -176,4 +176,15 @@ class ServiceStore
return $result->getDeletedCount() > 0; return $result->getDeletedCount() > 0;
} }
/**
* Delete every service owned by a user.
*/
public function deleteByUser(string $tenantId, string $userId): void
{
$this->dataStore->selectCollection(self::COLLECTION_NAME)->deleteMany([
'tid' => $tenantId,
'uid' => $userId,
]);
}
} }