Skip to content

Commit fa0c16e

Browse files
feat: use task processing to send emails
Signed-off-by: SebastianKrupinski <[email protected]>
1 parent 503fda6 commit fa0c16e

File tree

5 files changed

+177
-3
lines changed

5 files changed

+177
-3
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors
7+
* SPDX-License-Identifier: AGPL-3.0-or-later
8+
*/
9+
10+
namespace OCP\Json;
11+
12+
/**
13+
* Interface for objects that can be deserialized from JSON data.
14+
*
15+
* @since 33.0.0
16+
*/
17+
interface JsonDeserializable {
18+
19+
public function jsonDeserialize(array|string $data): void;
20+
21+
}

lib/public/Mail/Provider/Address.php

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
*/
99
namespace OCP\Mail\Provider;
1010

11+
use JsonSerializable;
12+
use OCP\Json\JsonDeserializable;
13+
1114
/**
1215
* Mail Address Object
1316
*
@@ -16,7 +19,7 @@
1619
* @since 30.0.0
1720
*
1821
*/
19-
class Address implements \OCP\Mail\Provider\IAddress {
22+
class Address implements IAddress, JsonSerializable, JsonDeserializable {
2023

2124
/**
2225
* initialize the mail address object
@@ -32,6 +35,35 @@ public function __construct(
3235
) {
3336
}
3437

38+
/**
39+
* export this objects data as an array
40+
*
41+
* @since 33.0.0
42+
*
43+
* @return array representation of this object as an array
44+
*/
45+
public function jsonSerialize(): array {
46+
return [
47+
'address' => $this->address,
48+
'label' => $this->label,
49+
];
50+
}
51+
52+
/**
53+
* import this objects data from an array
54+
*
55+
* @since 33.0.0
56+
*
57+
* @param array array representation of this object
58+
*/
59+
public function jsonDeserialize(array|string $data): void {
60+
if (is_string($data)) {
61+
$data = json_decode($data, true);
62+
}
63+
$this->address = $data['address'] ?? null;
64+
$this->label = $data['label'] ?? null;
65+
}
66+
3567
/**
3668
* sets the mail address
3769
*

lib/public/Mail/Provider/Attachment.php

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
*/
99
namespace OCP\Mail\Provider;
1010

11+
use JsonSerializable;
12+
use OCP\Json\JsonDeserializable;
13+
1114
/**
1215
* Mail Attachment Object
1316
*
@@ -16,7 +19,7 @@
1619
* @since 30.0.0
1720
*
1821
*/
19-
class Attachment implements \OCP\Mail\Provider\IAttachment {
22+
class Attachment implements IAttachment, JsonSerializable, JsonDeserializable {
2023

2124
/**
2225
* initialize the mail attachment object
@@ -36,6 +39,39 @@ public function __construct(
3639
) {
3740
}
3841

42+
/**
43+
* export this objects data as an array
44+
*
45+
* @since 33.0.0
46+
*
47+
* @return array representation of this object as an array
48+
*/
49+
public function jsonSerialize(): array {
50+
return [
51+
'contents' => base64_encode($this->contents ?? ''),
52+
'name' => $this->name,
53+
'type' => $this->type,
54+
'embedded' => $this->embedded,
55+
];
56+
}
57+
58+
/**
59+
* import this objects data from an array
60+
*
61+
* @since 33.0.0
62+
*
63+
* @param array array representation of this object
64+
*/
65+
public function jsonDeserialize(array|string $data): void {
66+
if (is_string($data)) {
67+
$data = json_decode($data, true);
68+
}
69+
$this->contents = base64_decode($data['contents'] ?? '');
70+
$this->name = $data['name'] ?? null;
71+
$this->type = $data['type'] ?? null;
72+
$this->embedded = $data['embedded'] ?? false;
73+
}
74+
3975
/**
4076
* sets the attachment file name
4177
*

lib/public/Mail/Provider/Message.php

Lines changed: 66 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
*/
99
namespace OCP\Mail\Provider;
1010

11+
use JsonSerializable;
12+
use OCP\Json\JsonDeserializable;
13+
1114
/**
1215
* Mail Message Object
1316
*
@@ -16,7 +19,7 @@
1619
* @since 30.0.0
1720
*
1821
*/
19-
class Message implements \OCP\Mail\Provider\IMessage {
22+
class Message implements IMessage, JsonSerializable, JsonDeserializable {
2023

2124
/**
2225
* initialize the mail message object
@@ -30,6 +33,68 @@ public function __construct(
3033
) {
3134
}
3235

36+
/**
37+
* export this objects data as an array
38+
*
39+
* @since 33.0.0
40+
*
41+
* @return array representation of this object as an array
42+
*/
43+
public function jsonSerialize(): array {
44+
$data = $this->data;
45+
$data['bodyHtml'] = base64_encode($this->data['bodyHtml'] ?? '');
46+
$data['bodyPlain'] = base64_encode($this->data['bodyPlain'] ?? '');
47+
$data['subject'] = base64_encode($this->data['subject'] ?? '');
48+
return $data;
49+
}
50+
51+
/**
52+
* import this objects data from an array
53+
*
54+
* @since 33.0.0
55+
*
56+
* @param array array representation of this object
57+
*/
58+
public function jsonDeserialize(array|string $data): void {
59+
if (is_string($data)) {
60+
$this->data = json_decode($data, true);
61+
} else {
62+
$this->data = $data;
63+
}
64+
// decode encoded fields
65+
$this->data['bodyHtml'] = base64_decode($data['bodyHtml'] ?? '');
66+
$this->data['bodyPlain'] = base64_decode($data['bodyPlain'] ?? '');
67+
$this->data['subject'] = base64_decode($data['subject'] ?? '');
68+
// convert object fields
69+
foreach (['from', 'replyTo'] as $field) {
70+
if (isset($data[$field]) && is_array($data[$field])) {
71+
$address = new Address();
72+
$address->jsonDeserialize($data[$field]);
73+
$this->data[$field] = $address;
74+
} else {
75+
$this->data[$field] = null;
76+
}
77+
}
78+
foreach (['to', 'cc', 'bcc', 'attachments'] as $field) {
79+
$this->data[$field] = [];
80+
if (isset($data[$field]) && is_array($data[$field])) {
81+
foreach ($data[$field] as $item) {
82+
if (is_array($item)) {
83+
if ($field === 'attachments') {
84+
$attachment = new Attachment(null, null, null);
85+
$attachment->jsonDeserialize($item);
86+
$this->data[$field][] = $attachment;
87+
} else {
88+
$address = new Address();
89+
$address->jsonDeserialize($item);
90+
$this->data[$field][] = $address;
91+
}
92+
}
93+
}
94+
}
95+
}
96+
}
97+
3398
/**
3499
* arbitrary unique text string identifying this message
35100
*

lib/public/TaskProcessing/EShapeType.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ enum EShapeType: int {
2424
case Video = 4;
2525
case File = 5;
2626
case Enum = 6;
27+
case Array = 7;
28+
case Object = 8;
2729
case ListOfNumbers = 10;
2830
case ListOfTexts = 11;
2931
case ListOfImages = 12;
@@ -72,6 +74,12 @@ private function validateNonFileType(mixed $value): void {
7274
if ($this === EShapeType::ListOfNumbers && (!is_array($value) || count(array_filter($value, fn ($item) => !is_numeric($item))) > 0)) {
7375
throw new ValidationException('Non-numeric list item provided for ListOfNumbers slot');
7476
}
77+
if ($this === EShapeType::Array && !is_array($value)) {
78+
throw new ValidationException('Non-array item provided for Array slot');
79+
}
80+
if ($this === EShapeType::Object && !is_object($value)) {
81+
throw new ValidationException('Non-object item provided for Object slot');
82+
}
7583
}
7684

7785
/**
@@ -106,6 +114,12 @@ public function validateInput(mixed $value): void {
106114
if ($this === EShapeType::ListOfFiles && (!is_array($value) || count(array_filter($value, fn ($item) => !is_numeric($item))) > 0)) {
107115
throw new ValidationException('Non-audio list item provided for ListOfFiles slot');
108116
}
117+
if ($this === EShapeType::Array && !is_array($value)) {
118+
throw new ValidationException('Non-array item provided for Array slot');
119+
}
120+
if ($this === EShapeType::Object && !is_object($value)) {
121+
throw new ValidationException('Non-object item provided for Object slot');
122+
}
109123
}
110124

111125
/**
@@ -172,6 +186,12 @@ public function validateOutputWithFileIds(mixed $value): void {
172186
if ($this === EShapeType::ListOfFiles && (!is_array($value) || count(array_filter($value, fn ($item) => !is_numeric($item))) > 0)) {
173187
throw new ValidationException('Non-audio list item provided for ListOfFiles slot');
174188
}
189+
if ($this === EShapeType::Array && !is_array($value)) {
190+
throw new ValidationException('Non-array item provided for Array slot');
191+
}
192+
if ($this === EShapeType::Object && !is_object($value)) {
193+
throw new ValidationException('Non-object item provided for Object slot');
194+
}
175195
}
176196

177197
/**

0 commit comments

Comments
 (0)