Skip to content

Commit bd428bb

Browse files
committed
DB for outbox
Signed-off-by: Anna Larch <anna@nextcloud.com>
1 parent 643aedb commit bd428bb

13 files changed

+1105
-7
lines changed

appinfo/info.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
- **🙈 We’re not reinventing the wheel!** Based on the great [Horde](https://horde.org) libraries.
1313
- **📬 Want to host your own mail server?** We don’t have to reimplement this as you could set up [Mail-in-a-Box](https://mailinabox.email)!
1414
]]></description>
15-
<version>1.12.0-alpha.2</version>
15+
<version>1.12.0-alpha.3</version>
1616
<licence>agpl</licence>
1717
<author>Greta Doçi</author>
1818
<author homepage="https://github.com/nextcloud/groupware">Nextcloud Groupware Team</author>

lib/Db/LocalAttachment.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
/**
46
* @author Christoph Wurst <christoph@winzerhof-wurst.at>
57
* @author Luc Calaresu <dev@calaresu.com>
@@ -32,8 +34,10 @@
3234
* @method void setFileName(string $fileName)
3335
* @method string getMimeType()
3436
* @method void setMimeType(string $mimeType)
35-
* @method int getCreatedAt()
37+
* @method int|null getCreatedAt()
3638
* @method void setCreatedAt(int $createdAt)
39+
* @method int|null getLocalMessageId()
40+
* @method void setLocalMessageId(int $localMessageId)
3741
*/
3842
class LocalAttachment extends Entity implements JsonSerializable {
3943

@@ -46,13 +50,19 @@ class LocalAttachment extends Entity implements JsonSerializable {
4650
/** @var string */
4751
protected $mimeType;
4852

49-
/** @var mixed */
53+
/** @var int|null */
5054
protected $createdAt;
5155

56+
/** @var int|null */
57+
protected $localMessageId;
58+
5259
public function jsonSerialize() {
5360
return [
5461
'id' => $this->id,
5562
'fileName' => $this->fileName,
63+
'mimeType' => $this->mimeType,
64+
'createdAt' => $this->createdAt,
65+
'localMessageId' => $this->localMessageId
5666
];
5767
}
5868
}

lib/Db/LocalAttachmentMapper.php

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
/**
46
* @author Christoph Wurst <christoph@winzerhof-wurst.at>
57
* @author Luc Calaresu <dev@calaresu.com>
@@ -26,6 +28,7 @@
2628
use OCP\AppFramework\Db\QBMapper;
2729
use OCP\DB\QueryBuilder\IQueryBuilder;
2830
use OCP\IDBConnection;
31+
use Throwable;
2932

3033
/**
3134
* @template-extends QBMapper<LocalAttachment>
@@ -39,11 +42,34 @@ public function __construct(IDBConnection $db) {
3942
parent::__construct($db, 'mail_attachments');
4043
}
4144

45+
/**
46+
* @return LocalAttachment[]
47+
*/
48+
public function findByLocalMessageId(int $localMessageId): array {
49+
$qb = $this->db->getQueryBuilder();
50+
$qb->select('*')
51+
->from($this->getTableName())
52+
->where(
53+
$qb->expr()->eq('local_message_id', $qb->createNamedParameter($localMessageId, IQueryBuilder::PARAM_INT), IQueryBuilder::PARAM_INT)
54+
);
55+
return $this->findEntities($qb);
56+
}
57+
58+
/**
59+
* @return LocalAttachment[]
60+
*/
61+
public function findByLocalMessageIds(array $localMessageIds): array {
62+
$qb = $this->db->getQueryBuilder();
63+
$qb->select('*')
64+
->from($this->getTableName())
65+
->where(
66+
$qb->expr()->in('local_message_id', $qb->createNamedParameter($localMessageIds, IQueryBuilder::PARAM_INT_ARRAY), IQueryBuilder::PARAM_INT_ARRAY)
67+
);
68+
return $this->findEntities($qb);
69+
}
70+
4271
/**
4372
* @throws DoesNotExistException
44-
*
45-
* @param string $userId
46-
* @param int $id
4773
*/
4874
public function find(string $userId, int $id): LocalAttachment {
4975
$qb = $this->db->getQueryBuilder();
@@ -55,4 +81,18 @@ public function find(string $userId, int $id): LocalAttachment {
5581

5682
return $this->findEntity($query);
5783
}
84+
85+
public function deleteForLocalMailbox(int $localMessageId): void {
86+
$this->db->beginTransaction();
87+
try {
88+
$qb = $this->db->getQueryBuilder();
89+
$qb->delete($this->getTableName())
90+
->where($qb->expr()->eq('local_message_id', $qb->createNamedParameter($localMessageId), IQueryBuilder::PARAM_INT));
91+
$qb->execute();
92+
$this->db->commit();
93+
} catch (Throwable $e) {
94+
$this->db->rollBack();
95+
throw $e;
96+
}
97+
}
5898
}

lib/Db/LocalMessage.php

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* @copyright 2022 Anna Larch <anna@nextcloud.com>
7+
*
8+
* @author 2022 Anna Larch <anna@nextcloud.com>
9+
*
10+
* @license GNU AGPL version 3 or any later version
11+
*
12+
* This program is free software: you can redistribute it and/or modify
13+
* it under the terms of the GNU Affero General Public License as
14+
* published by the Free Software Foundation, either version 3 of the
15+
* License, or (at your option) any later version.
16+
*
17+
* This program is distributed in the hope that it will be useful,
18+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
19+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20+
* GNU Affero General Public License for more details.
21+
*
22+
* You should have received a copy of the GNU Affero General Public License
23+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
24+
*/
25+
26+
namespace OCA\Mail\Db;
27+
28+
use JsonSerializable;
29+
use OCP\AppFramework\Db\Entity;
30+
31+
/**
32+
* @method int getType()
33+
* @method void setType(int $type)
34+
* @method int getAccountId()
35+
* @method void setAccountId(int $accountId)
36+
* @method int|null getAliasId()
37+
* @method void setAliasId(?int $aliasId)
38+
* @method int getSendAt()
39+
* @method void setSendAt(int $sendAt)
40+
* @method string getSubject()
41+
* @method void setSubject(string $subject)
42+
* @method string getBody()
43+
* @method void setBody(string $body)
44+
* @method bool isHtml()
45+
* @method void setHtml(bool $html)
46+
* @method string|null getInReplyToMessageId()
47+
* @method void setInReplyToMessageId(?string $inReplyToId)
48+
*/
49+
class LocalMessage extends Entity implements JsonSerializable {
50+
public const TYPE_OUTGOING = 0;
51+
public const TYPE_DRAFT = 1;
52+
53+
/**
54+
* @var int
55+
* @psalm-var self::TYPE_*
56+
*/
57+
protected $type;
58+
59+
/** @var int */
60+
protected $accountId;
61+
62+
/** @var int|null */
63+
protected $aliasId;
64+
65+
/** @var int */
66+
protected $sendAt;
67+
68+
/** @var string */
69+
protected $subject;
70+
71+
/** @var string */
72+
protected $body;
73+
74+
/** @var bool */
75+
protected $html;
76+
77+
/** @var string|null */
78+
protected $inReplyToMessageId;
79+
80+
/** @var array|null */
81+
protected $attachments;
82+
83+
/** @var array|null */
84+
protected $recipients;
85+
86+
public function __construct() {
87+
$this->addType('type', 'integer');
88+
$this->addType('accountId', 'integer');
89+
$this->addType('aliasId', 'integer');
90+
$this->addType('sendAt', 'integer');
91+
$this->addType('html', 'boolean');
92+
}
93+
94+
/**
95+
* @return array
96+
*/
97+
public function jsonSerialize() {
98+
return [
99+
'id' => $this->getId(),
100+
'type' => $this->getType(),
101+
'accountId' => $this->getAccountId(),
102+
'aliasId' => $this->getAccountId(),
103+
'sendAt' => $this->getSendAt(),
104+
'subject' => $this->getSubject(),
105+
'text' => $this->getBody(),
106+
'html' => $this->isHtml(),
107+
'inReplyToMessageId' => $this->getInReplyToMessageId(),
108+
'attachments' => $this->getAttachments(),
109+
'recipients' => $this->getRecipients()
110+
];
111+
}
112+
113+
/**
114+
* @param LocalAttachment[] $attachments
115+
* @return void
116+
*/
117+
public function setAttachments(array $attachments): void {
118+
$this->attachments = $attachments;
119+
}
120+
121+
/**
122+
* @return LocalAttachment[]|null
123+
*/
124+
public function getAttachments(): ?array {
125+
return $this->attachments;
126+
}
127+
128+
/**
129+
* @param Recipient[] $recipients
130+
* @return void
131+
*/
132+
public function setRecipients(array $recipients): void {
133+
$this->recipients = $recipients;
134+
}
135+
136+
/**
137+
* @return Recipient[]|null
138+
*/
139+
public function getRecipients(): ?array {
140+
return $this->recipients;
141+
}
142+
}

0 commit comments

Comments
 (0)