Skip to content
Open
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
11 changes: 10 additions & 1 deletion lib/Events/MessageDeletedEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@
use OCA\Mail\Account;
use OCA\Mail\Db\Mailbox;
use OCP\EventDispatcher\Event;
use OCP\EventDispatcher\IWebhookCompatibleEvent;

class MessageDeletedEvent extends Event {
class MessageDeletedEvent extends Event implements IWebhookCompatibleEvent {
/** @var Account */
private $account;

Expand Down Expand Up @@ -43,4 +44,12 @@ public function getMailbox(): Mailbox {
public function getMessageId(): int {
return $this->messageId;
}

public function getWebhookSerializable(): array {
return [
'accountId' => $this->account->getId(),
'mailboxId' => $this->mailbox->getId(),
'messageId' => $this->messageId,
];
}
}
13 changes: 12 additions & 1 deletion lib/Events/MessageFlaggedEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@
use OCA\Mail\Account;
use OCA\Mail\Db\Mailbox;
use OCP\EventDispatcher\Event;
use OCP\EventDispatcher\IWebhookCompatibleEvent;

class MessageFlaggedEvent extends Event {
class MessageFlaggedEvent extends Event implements IWebhookCompatibleEvent {
/** @var Account */
private $account;

Expand Down Expand Up @@ -61,4 +62,14 @@ public function getFlag(): string {
public function isSet(): bool {
return $this->set;
}

public function getWebhookSerializable(): array {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you have a use case in mind for this event?

I'm just wondering because you cannot use the messageUid for our api because the expects the message id (oc_mail_messages.id). The uid is an identifier on the imap server. The accountId and mailboxId are database ids from nextcloud mail.

public function get(int $id): DataResponse {

With the uid you could directly lookup a message on the imap server, but for that you need the name of the mailbox (which you currently won't pass, only the id but that one the imap server wont know).

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use case in mind is some hook that reacts whenever a message is flagged. Yeah I guess that would actually need the internal ID to do sth with it, tbh I just forwarded the information already available in the event as I thought that would be fine. Is there an easy way to get the internal id from here?

return [
'accountId' => $this->account->getId(),
'mailboxId' => $this->mailbox->getId(),
'messageUid' => $this->uid,
'flag' => $this->flag,
'set' => $this->set,
];
}
}
9 changes: 8 additions & 1 deletion lib/Events/MessageSentEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@
use OCA\Mail\Account;
use OCA\Mail\Db\LocalMessage;
use OCP\EventDispatcher\Event;
use OCP\EventDispatcher\IWebhookCompatibleEvent;

/**
* @psalm-immutable
*/
class MessageSentEvent extends Event {
class MessageSentEvent extends Event implements IWebhookCompatibleEvent {
/** @var Account */
private $account;

Expand All @@ -35,4 +36,10 @@ public function getAccount(): Account {
public function getLocalMessage(): LocalMessage {
return $this->localMessage;
}

public function getWebhookSerializable(): array {
return [
'message' => $this->localMessage,
];
}
}
12 changes: 11 additions & 1 deletion lib/Events/NewMessageReceivedEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,26 @@

namespace OCA\Mail\Events;

use OCA\Mail\Db\Message;
use OCP\EventDispatcher\Event;
use OCP\EventDispatcher\IWebhookCompatibleEvent;

class NewMessageReceivedEvent extends Event {
class NewMessageReceivedEvent extends Event implements IWebhookCompatibleEvent {
public function __construct(
private string $uri,
private Message $message,
) {
parent::__construct();
}

public function getUri(): string {
return $this->uri;
}

public function getWebhookSerializable(): array {
return [
'messageUri' => $this->uri,
'message' => $this->message,
];
}
}
2 changes: 1 addition & 1 deletion lib/Listener/NewMessagesNotifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public function handle(Event $event): void {

foreach ($event->getMessages() as $message) {
$uri = $this->urlGenerator->linkToOCSRouteAbsolute('mail.messageApi.get', ['id' => $message->getId()]);
$this->eventDispatcher->dispatchTyped(new NewMessageReceivedEvent($uri));
$this->eventDispatcher->dispatchTyped(new NewMessageReceivedEvent($uri, $message));
}
}
}
4 changes: 3 additions & 1 deletion tests/Unit/Events/NewMessageReceivedEventTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@
namespace OCA\Mail\Tests\Unit\Events;

use ChristophWurst\Nextcloud\Testing\TestCase;
use OCA\Mail\Db\Message;
use OCA\Mail\Events\NewMessageReceivedEvent;

class NewMessageReceivedEventTest extends TestCase {
public function testConstructorAndGetter(): void {
$uri = 'imap://user@example.com/INBOX;UID=123';
$message = new Message();

$event = new NewMessageReceivedEvent($uri);
$event = new NewMessageReceivedEvent($uri, $message);

$this->assertSame($uri, $event->getUri());
}
Expand Down
Loading