Compare commits
3 Commits
2e9a6a5857
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
| f901f13953 | |||
| f39dd75ae5 | |||
| b60341f62d |
@@ -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());
|
||||
}
|
||||
}
|
||||
@@ -10,9 +10,14 @@ declare(strict_types=1);
|
||||
namespace KTXM\ProviderJmapc;
|
||||
|
||||
use KTXC\Resource\ProviderManager;
|
||||
use KTXC\User\Event\UserCreatedEvent;
|
||||
use KTXC\User\Event\UserDeletingEvent;
|
||||
use KTXF\Event\DeliveryMode;
|
||||
use KTXF\Event\EventListenerRegistrarInterface;
|
||||
use KTXF\Module\ModuleBrowserInterface;
|
||||
use KTXF\Module\ModuleInstanceAbstract;
|
||||
use KTXF\Resource\Provider\ProviderInterface;
|
||||
use KTXM\ProviderJmapc\Listeners\UserEventListener;
|
||||
use KTXM\ProviderJmapc\Providers\Mail\Provider as MailProvider;
|
||||
use KTXM\ProviderJmapc\Providers\Chrono\Provider as ChronoProvider;
|
||||
use KTXM\ProviderJmapc\Providers\People\Provider as PeopleProvider;
|
||||
@@ -27,6 +32,7 @@ class Module extends ModuleInstanceAbstract implements ModuleBrowserInterface
|
||||
{
|
||||
public function __construct(
|
||||
private readonly ProviderManager $providerManager,
|
||||
private readonly EventListenerRegistrarInterface $events,
|
||||
) {}
|
||||
|
||||
public function handle(): string
|
||||
@@ -67,6 +73,9 @@ class Module extends ModuleInstanceAbstract implements ModuleBrowserInterface
|
||||
|
||||
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
|
||||
$this->providerManager->register(ProviderInterface::TYPE_MAIL, 'jmap', MailProvider::class);
|
||||
//$this->providerManager->register(ProviderInterface::TYPE_CHRONO, 'jmap', ChronoProvider::class);
|
||||
|
||||
@@ -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 (str_starts_with((string) $target->collection(), (string) $deleteTargetIdentifier?->collection())) {
|
||||
// if so, we should hard delete instead of moving to avoid duplicates in the trash
|
||||
$deleteMode = 'hard';
|
||||
if ($deleteMode === 'soft') {
|
||||
$targetMailbox = $this->mailService->collectionFetch((string) $target->collection());
|
||||
if ($targetMailbox === null) {
|
||||
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) {
|
||||
'soft' => $this->collectionMove($deleteTargetIdentifier, $target),
|
||||
'hard' => $this->mailService->collectionDestroy($target->collection(), $force),
|
||||
};
|
||||
return $result;
|
||||
if ($deleteMode === 'soft') {
|
||||
return $this->collectionMove($deleteTargetIdentifier, $target);
|
||||
}
|
||||
|
||||
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
|
||||
|
||||
@@ -912,21 +912,29 @@ class RemoteMailService {
|
||||
*
|
||||
* @since Release 2.0.0
|
||||
*/
|
||||
public function entitySubmitFresh(string $identityId, array $message, string $preSendTarget, string $postSentTarget): string {
|
||||
public function entitySubmitFresh(string $identityId, array $data, string $preSendTarget, string $postSentTarget): string {
|
||||
|
||||
$mail = new MailParametersRequest();
|
||||
$mail->parametersRaw($message);
|
||||
$message = new MailParametersRequest();
|
||||
$message->parametersRaw($data);
|
||||
|
||||
$messageCreateId = 'm_' . uniqid();
|
||||
$submissionCreateId = 's_' . uniqid();
|
||||
$messageId = 'm_' . uniqid();
|
||||
$submissionId = 's_' . uniqid();
|
||||
|
||||
$r0 = new MailSet($this->dataAccount, null, $this->resourceNamespace, $this->resourceEntityLabel);
|
||||
$createdMessage = $r0->create($messageCreateId, $mail);
|
||||
$createdMessage->in($preSendTarget);
|
||||
$m0 = $r0->create($messageId, $message);
|
||||
$m0->in($preSendTarget);
|
||||
$m0->draft(true);
|
||||
$m0->seen(true);
|
||||
|
||||
$r1 = new MailSubmissionSet($this->dataAccount, null, $this->resourceNamespace, $this->resourceEntityLabel);
|
||||
$submission = $r1->create($submissionCreateId);
|
||||
$submission->identity($identityId);
|
||||
$submission->message('#' . $messageCreateId);
|
||||
$r1->completionUpdate('#' . $messageId, [
|
||||
'mailboxIds/' . $postSentTarget => true,
|
||||
'mailboxIds/' . $preSendTarget => null,
|
||||
'keywords/$draft' => null,
|
||||
]);
|
||||
$s0 = $r1->create($submissionId);
|
||||
$s0->identity($identityId);
|
||||
$s0->message('#' . $messageId);
|
||||
|
||||
$bundle = $this->dataStore->perform([$r0, $r1]);
|
||||
$messageResponse = $bundle->response(0);
|
||||
@@ -934,7 +942,7 @@ class RemoteMailService {
|
||||
throw new Exception($messageResponse->type() . ': ' . $messageResponse->description(), 1);
|
||||
}
|
||||
if (method_exists($messageResponse, 'createFailure')) {
|
||||
$failure = $messageResponse->createFailure($messageCreateId);
|
||||
$failure = $messageResponse->createFailure($messageId);
|
||||
if (is_array($failure)) {
|
||||
$type = $failure['type'] ?? 'unknownError';
|
||||
$description = $failure['description'] ?? 'Email creation failed';
|
||||
@@ -950,7 +958,7 @@ class RemoteMailService {
|
||||
throw new Exception($response->type() . ': ' . $response->description(), 1);
|
||||
}
|
||||
|
||||
return $this->extractSubmissionIdentifier($response, $submissionCreateId);
|
||||
return $this->extractSubmissionIdentifier($response, $submissionId);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -978,18 +986,18 @@ class RemoteMailService {
|
||||
$requests = [];
|
||||
|
||||
if ($patch !== null && $patch !== []) {
|
||||
$draftMutation = new MailParametersRequest();
|
||||
$draftMutation->parametersRaw($patch);
|
||||
$m0 = new MailParametersRequest();
|
||||
$m0->parametersRaw($patch);
|
||||
$r0 = new MailSet($this->dataAccount, null, $this->resourceNamespace, $this->resourceEntityLabel);
|
||||
$r0->update($draftId, $draftMutation);
|
||||
$r0->update($draftId, $m0);
|
||||
$requests[] = $r0;
|
||||
}
|
||||
|
||||
$submissionCreateId = 's_' . uniqid();
|
||||
$submissionId = 's_' . uniqid();
|
||||
$r1 = new MailSubmissionSet($this->dataAccount, null, $this->resourceNamespace, $this->resourceEntityLabel);
|
||||
$submission = $r1->create($submissionCreateId);
|
||||
$submission->identity($identityId);
|
||||
$submission->message($draftId);
|
||||
$s0 = $r1->create($submissionId);
|
||||
$s0->identity($identityId);
|
||||
$s0->message($draftId);
|
||||
$requests[] = $r1;
|
||||
|
||||
$bundle = $this->dataStore->perform($requests);
|
||||
@@ -1002,19 +1010,7 @@ class RemoteMailService {
|
||||
throw new Exception($response->type() . ': ' . $response->description(), 1);
|
||||
}
|
||||
|
||||
return $this->extractSubmissionIdentifier($response, $submissionCreateId);
|
||||
}
|
||||
|
||||
private function collectionByRole(string $role): ?string {
|
||||
$filter = $this->collectionListFilter();
|
||||
$filter->condition('role', $role);
|
||||
|
||||
$collections = $this->collectionList(null, $filter, null);
|
||||
if ($collections === []) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return (string)array_key_first($collections);
|
||||
return $this->extractSubmissionIdentifier($response, $submissionId);
|
||||
}
|
||||
|
||||
private function extractSubmissionIdentifier(object $response, string $createId): string {
|
||||
|
||||
@@ -176,4 +176,15 @@ class ServiceStore
|
||||
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,
|
||||
]);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user