feat: implement entityCreate and entityModify
Signed-off-by: Sebastian Krupinski <krupinski01@gmail.com>
This commit is contained in:
@@ -102,8 +102,8 @@ class Service implements ServiceBaseInterface, ServiceMutableInterface, ServiceC
|
||||
],
|
||||
self::CAPABILITY_ENTITY_FETCH => true,
|
||||
self::CAPABILITY_ENTITY_EXTANT => true,
|
||||
self::CAPABILITY_ENTITY_CREATE => false,
|
||||
self::CAPABILITY_ENTITY_MODIFY => false,
|
||||
self::CAPABILITY_ENTITY_CREATE => true,
|
||||
self::CAPABILITY_ENTITY_MODIFY => true,
|
||||
self::CAPABILITY_ENTITY_PATCH => true,
|
||||
self::CAPABILITY_ENTITY_DELETE => true,
|
||||
self::CAPABILITY_ENTITY_MOVE => true,
|
||||
@@ -638,14 +638,42 @@ class Service implements ServiceBaseInterface, ServiceMutableInterface, ServiceC
|
||||
|
||||
public function entityCreate(CollectionIdentifier $target, MessagePropertiesMutableInterface $properties, array $options = []): EntityResource
|
||||
{
|
||||
// TODO: Implement entity create
|
||||
return $this->entityFresh();
|
||||
if ($target->provider() !== $this->provider() || (string)$target->service() !== (string)$this->identifier()) {
|
||||
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
|
||||
return $this->entityFresh();
|
||||
if ($target->provider() !== $this->provider() || (string)$target->service() !== (string)$this->identifier()) {
|
||||
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
|
||||
@@ -897,32 +925,14 @@ class Service implements ServiceBaseInterface, ServiceMutableInterface, ServiceC
|
||||
);
|
||||
}
|
||||
|
||||
$nativeProperties = $this->normalizeMessageProperties($message);
|
||||
$attachments = $nativeProperties->getAttachments();
|
||||
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',
|
||||
));
|
||||
}
|
||||
|
||||
$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());
|
||||
[$nativeMessage, $nativeAttachments] = $this->messagePayload($message);
|
||||
$transportId = $this->mailService->entitySubmitFresh(
|
||||
$identityId,
|
||||
$nativeMessage,
|
||||
$preSendTarget->collection(),
|
||||
$postSentTarget->collection(),
|
||||
$nativeAttachments,
|
||||
);
|
||||
|
||||
return new EntitySubmitResult(
|
||||
EntitySubmitResult::DISPOSITION_SENT,
|
||||
@@ -940,12 +950,19 @@ class Service implements ServiceBaseInterface, ServiceMutableInterface, ServiceC
|
||||
);
|
||||
}
|
||||
|
||||
$patch = null;
|
||||
if ($message !== null) {
|
||||
$patch = $this->normalizeMessageProperties($message)->toJmap();
|
||||
[$nativeMessage, $nativeAttachments] = $this->messagePayload($message);
|
||||
} else {
|
||||
$nativeMessage = null;
|
||||
$nativeAttachments = [];
|
||||
}
|
||||
|
||||
$transportId = $this->mailService->entitySubmitDraft($identityId, (string)$source->entity(), $patch);
|
||||
$transportId = $this->mailService->entitySubmitDraft(
|
||||
$identityId,
|
||||
(string)$source->entity(),
|
||||
$nativeMessage,
|
||||
$nativeAttachments,
|
||||
);
|
||||
|
||||
return new EntitySubmitResult(
|
||||
EntitySubmitResult::DISPOSITION_SENT,
|
||||
@@ -954,16 +971,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) {
|
||||
return $message;
|
||||
if ($message instanceof MessageProperties === false) {
|
||||
$native = new MessageProperties([]);
|
||||
$native->jsonDeserialize($message->jsonSerialize());
|
||||
} else {
|
||||
$native = $message;
|
||||
}
|
||||
|
||||
$native = new MessageProperties([]);
|
||||
$native->jsonDeserialize($message->jsonSerialize());
|
||||
$attachments = $native->getAttachments();
|
||||
$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
|
||||
|
||||
Reference in New Issue
Block a user