Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Local Outbox #5922

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 26 additions & 1 deletion appinfo/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,32 @@
'name' => 'thread#move',
'url' => '/api/thread/{id}',
'verb' => 'POST'
]
],
[
'name' => 'outbox#index',
'url' => '/api/outbox',
'verb' => 'GET'
],
[
'name' => 'outbox#get',
'url' => '/api/outbox/{id}',
'verb' => 'GET'
],
[
'name' => 'outbox#send',
'url' => '/api/outbox/{id}',
'verb' => 'PATCH'
],
[
'name' => 'outbox#save',
'url' => '/api/outbox',
'verb' => 'POST'
],
[
'name' => 'outbox#delete',
'url' => '/api/outbox/{id}',
'verb' => 'DELETE'
],
],
'resources' => [
'accounts' => ['url' => '/api/accounts'],
Expand Down
77 changes: 77 additions & 0 deletions lib/Contracts/ILocalMailbox.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php

declare(strict_types=1);

/**
* Mail App
*
* @copyright 2022 Anna Larch <anna.larch@gmx.net>
*
* @author Anna Larch <anna.larch@gmx.net>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
* License as published by the Free Software Foundation; either
* version 3 of the License, or any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU AFFERO GENERAL PUBLIC LICENSE for more details.
*
* You should have received a copy of the GNU Affero General Public
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
*
*/

namespace OCA\Mail\Contracts;

use OCA\Mail\Account;
use OCA\Mail\Db\LocalMailboxMessage;
use OCA\Mail\Exception\ClientException;
use OCA\Mail\Exception\ServiceException;

interface ILocalMailbox {

/**
* @param string $userID
* @return mixed
*/
public function getMessages(string $userId): array;

/**
* @param array $accountIds
* @param int $id
*
* @return LocalMailboxMessage
*
* @throws ServiceException
*/
public function getMessage(int $id): LocalMailboxMessage;

/**
* @param LocalMailboxMessage $message
* @param array $recipients
* @param array $attachmentIds
* @return LocalMailboxMessage
*/
public function saveMessage(LocalMailboxMessage $message, array $recipients, array $attachmentIds = []): LocalMailboxMessage;

/**
* @param string $userId
* @param int $messageId
*
* @throws ClientException
* @throws ServiceException
*/
public function deleteMessage(LocalMailboxMessage $message, string $userId): void;

/**
* @param LocalMailboxMessage $message
* @param Account $account
* @return bool
* @throws ClientException
* @throws ServiceException
*/
public function sendMessage(LocalMailboxMessage $message, Account $account): void;
}
10 changes: 10 additions & 0 deletions lib/Contracts/IMailTransmission.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

use OCA\Mail\Account;
use OCA\Mail\Db\Alias;
use OCA\Mail\Db\LocalMailboxMessage;
use OCA\Mail\Db\Mailbox;
use OCA\Mail\Db\Message;
use OCA\Mail\Exception\ClientException;
Expand All @@ -51,6 +52,15 @@ public function sendMessage(NewMessageData $messageData,
Alias $alias = null,
Message $draft = null): void;

/**
* @param Account $account
* @param LocalMailboxMessage $message
* @param array $recipients
* @param array $attachments
* @return void
*/
public function sendLocalMessage(Account $account, LocalMailboxMessage $message, array $recipients, array $attachments = []): void;

/**
* Save a message draft
*
Expand Down
16 changes: 8 additions & 8 deletions lib/Controller/AccountsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ public function create(string $accountName, string $emailAddress, string $passwo
* @NoAdminRequired
* @TrapError
*
* @param int $id
* @param int $accountId
* @param string $subject
* @param string $body
* @param string $to
Expand All @@ -385,20 +385,20 @@ public function create(string $accountName, string $emailAddress, string $passwo
* @throws ClientException
* @throws ServiceException
*/
public function send(int $id,
public function send(int $accountId,
string $subject,
string $body,
string $to,
string $cc,
string $bcc,
bool $isHtml = true,
bool $requestMdn = false,
int $draftId = null,
int $messageId = null,
array $attachments = [],
bool $isHtml = true,
bool $requestMdn = false,
int $draftId = null,
int $messageId = null,
array $attachments = [],
int $aliasId = null,
bool $force = false): JSONResponse {
$account = $this->accountService->find($this->currentUserId, $id);
$account = $this->accountService->find($this->currentUserId, $accountId);
$alias = $aliasId ? $this->aliasesService->find($aliasId, $this->currentUserId) : null;

$expandedTo = $this->groupsIntegration->expand($to);
Expand Down
1 change: 0 additions & 1 deletion lib/Controller/MessagesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,6 @@ public function __construct(string $appName,
parent::__construct($appName, $request);

$this->accountService = $accountService;
$this->mailManager = $mailManager;
$this->mailSearch = $mailSearch;
$this->itineraryService = $itineraryService;
$this->currentUserId = $UserId;
Expand Down
184 changes: 184 additions & 0 deletions lib/Controller/OutboxController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,184 @@
<?php

declare(strict_types=1);

/**
* Mail App
*
* @copyright 2022 Anna Larch <anna.larch@gmx.net>
*
* @author Anna Larch <anna.larch@gmx.net>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
* License as published by the Free Software Foundation; either
* version 3 of the License, or any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU AFFERO GENERAL PUBLIC LICENSE for more details.
*
* You should have received a copy of the GNU Affero General Public
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
*
*/

namespace OCA\Mail\Controller;

use OCA\Mail\Db\LocalMailboxMessage;
use OCA\Mail\Exception\ClientException;
use OCA\Mail\Exception\ServiceException;
use OCA\Mail\Service\AccountService;
use OCA\Mail\Service\OutboxService;
use OCP\AppFramework\Controller;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\JSONResponse;
use OCP\IRequest;

class OutboxController extends Controller {

/** @var OutboxService */
private $service;

/** @var string */
private $userId;

/** @var AccountService */
private $accountService;

public function __construct(string $appName,
$UserId,
IRequest $request,
OutboxService $service,
AccountService $accountService) {
parent::__construct($appName, $request);
$this->userId = $UserId;
$this->service = $service;
$this->accountService = $accountService;
}

/**
* @NoAdminRequired
miaulalala marked this conversation as resolved.
Show resolved Hide resolved
* @TrapError
*
* @return JSONResponse
*/
public function index(): JSONResponse {
return new JSONResponse(
[
// TODO: wrap me in try/catch?!
'messages' => $this->service->getMessages($this->userId)
]
);
}

/**
* @NoAdminRequired
miaulalala marked this conversation as resolved.
Show resolved Hide resolved
* @TrapError
*
* @param int $id
* @return JSONResponse
*/
public function get(int $id): JSONResponse {
try {
$message = $this->service->getMessage($id);
$this->accountService->find($this->userId, $message->getAccountId());
} catch (ServiceException | ClientException $e) {
return new JSONResponse($e->getMessage(), $e->getCode());
}

return new JSONResponse($message);
}

/**
* @NoAdminRequired
miaulalala marked this conversation as resolved.
Show resolved Hide resolved
* @TrapError
*
* @param int $accountId
* @param int $sendAt
* @param string $subject
* @param string $body
* @param bool $isHtml
* @param bool $isMdn
* @param string $inReplyToMessageId
* @param array $recipients
* @param array $attachmentIds
* @return JSONResponse
*
* @throws ServiceException
*/
public function save(
miaulalala marked this conversation as resolved.
Show resolved Hide resolved
int $accountId,
int $sendAt,
string $subject,
string $body,
bool $isHtml,
bool $isMdn,
string $inReplyToMessageId,
array $recipients,
miaulalala marked this conversation as resolved.
Show resolved Hide resolved
array $attachmentIds
): JSONResponse {
try {
$this->accountService->find($this->userId, $accountId);
} catch (ClientException $e) {
return new JSONResponse([], Http::STATUS_FORBIDDEN);
}

$message = new LocalMailboxMessage();
$message->setType(LocalMailboxMessage::OUTGOING);
$message->setAccountId($accountId);
$message->setSendAt($sendAt);
$message->setSubject($subject);
$message->setBody($body);
$message->setHtml($isHtml);
$message->setMdn($isMdn);
miaulalala marked this conversation as resolved.
Show resolved Hide resolved
$message->setInReplyToMessageId($inReplyToMessageId);

// TODO: wrap me in try/catch?!
$this->service->saveMessage($message, $recipients, $attachmentIds);

// Return with related here?
return new JSONResponse(
$message, Http::STATUS_CREATED
);
}

/**
* @NoAdminRequired
miaulalala marked this conversation as resolved.
Show resolved Hide resolved
* @TrapError
*
* @param int $id
* @return JSONResponse
*/
public function send(int $id):JSONResponse {
try {
$message = $this->service->getMessage($id);
$account = $this->accountService->find($this->userId, $message->getAccountId());
$this->service->sendMessage($message, $account);
} catch (ServiceException | ClientException $e) {
return new JSONResponse($e->getMessage(), $e->getCode());
}
return new JSONResponse(
'Message sent', Http::STATUS_ACCEPTED
);
}

/**
* @NoAdminRequired
miaulalala marked this conversation as resolved.
Show resolved Hide resolved
* @TrapError
*
* @param int $id
* @return JSONResponse
*/
public function delete(int $id): JSONResponse {
try {
$message = $this->service->getMessage($id);
$this->accountService->find($this->userId, $message->getAccountId());
$this->service->deleteMessage($message, $this->userId);
} catch (ServiceException | ClientException $e) {
return new JSONResponse($e->getMessage(), $e->getCode());
}
return new JSONResponse('Message deleted', Http::STATUS_ACCEPTED);
}
}
6 changes: 4 additions & 2 deletions lib/Db/LocalAttachment.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,15 @@ class LocalAttachment extends Entity implements JsonSerializable {
/** @var string */
protected $mimeType;

/** @var mixed */
/** @var int */
protected $createdAt;

public function jsonSerialize() {
return [
'id' => $this->id,
'fileName' => $this->fileName,
'fileName' => $this->getFileName(),
'mimeType' => $this->getMimeType(),
'createdAt' => $this->getCreatedAt()
];
}
}
Loading