From 42c855cb41a92e0ecc43bed4667ad3d29a86c5b1 Mon Sep 17 00:00:00 2001 From: Damodar Lohani Date: Wed, 14 Dec 2022 06:23:14 +0000 Subject: [PATCH] feat: make mails event and worker general --- app/workers/mails.php | 107 +----------------------------------- src/Appwrite/Event/Mail.php | 82 +++++++++------------------ 2 files changed, 30 insertions(+), 159 deletions(-) diff --git a/app/workers/mails.php b/app/workers/mails.php index 90e9e9a660..e850885ddc 100644 --- a/app/workers/mails.php +++ b/app/workers/mails.php @@ -1,11 +1,8 @@ args['project'] ?? []); - $user = new Document($this->args['user'] ?? []); - $team = new Document($this->args['team'] ?? []); - $payload = $this->args['payload'] ?? []; $recipient = $this->args['recipient']; - $url = $this->args['url']; + $subject = $this->args['subject']; $name = $this->args['name']; - $type = $this->args['type']; - $prefix = $this->getPrefix($type); - $locale = new Locale($this->args['locale']); - $projectName = $project->isEmpty() ? 'Console' : $project->getAttribute('name', '[APP-NAME]'); - - if (!$this->doesLocaleExist($locale, $prefix)) { - $locale->setDefault('en'); - } - - $from = $project->isEmpty() || $project->getId() === 'console' ? '' : \sprintf($locale->getText('emails.sender'), $projectName); - $body = Template::fromFile(__DIR__ . '/../config/locale/templates/email-base.tpl'); - $subject = ''; - switch ($type) { - case MAIL_TYPE_CERTIFICATE: - $domain = $payload['domain']; - $error = $payload['error']; - $attempt = $payload['attempt']; - - $subject = \sprintf($locale->getText("$prefix.subject"), $domain); - $body->setParam('{{domain}}', $domain); - $body->setParam('{{error}}', $error); - $body->setParam('{{attempt}}', $attempt); - break; - case MAIL_TYPE_INVITATION: - $subject = \sprintf($locale->getText("$prefix.subject"), $team->getAttribute('name'), $projectName); - $body->setParam('{{owner}}', $user->getAttribute('name')); - $body->setParam('{{team}}', $team->getAttribute('name')); - break; - case MAIL_TYPE_RECOVERY: - case MAIL_TYPE_VERIFICATION: - case MAIL_TYPE_MAGIC_SESSION: - $subject = $locale->getText("$prefix.subject"); - break; - default: - throw new Exception('Undefined Mail Type : ' . $type, 500); - } - - $body - ->setParam('{{subject}}', $subject) - ->setParam('{{hello}}', $locale->getText("$prefix.hello")) - ->setParam('{{name}}', $name) - ->setParam('{{body}}', $locale->getText("$prefix.body")) - ->setParam('{{redirect}}', $url) - ->setParam('{{footer}}', $locale->getText("$prefix.footer")) - ->setParam('{{thanks}}', $locale->getText("$prefix.thanks")) - ->setParam('{{signature}}', $locale->getText("$prefix.signature")) - ->setParam('{{project}}', $projectName) - ->setParam('{{direction}}', $locale->getText('settings.direction')) - ->setParam('{{bg-body}}', '#f7f7f7') - ->setParam('{{bg-content}}', '#ffffff') - ->setParam('{{text-content}}', '#000000'); - - $body = $body->render(); + $body = $this->args['body']; + $from = $this->args['from']; /** @var \PHPMailer\PHPMailer\PHPMailer $mail */ $mail = $register->get('smtp'); @@ -130,47 +72,4 @@ class MailsV1 extends Worker public function shutdown(): void { } - - /** - * Returns a prefix from a mail type - * - * @param $type - * - * @return string - */ - protected function getPrefix(string $type): string - { - switch ($type) { - case MAIL_TYPE_RECOVERY: - return 'emails.recovery'; - case MAIL_TYPE_CERTIFICATE: - return 'emails.certificate'; - case MAIL_TYPE_INVITATION: - return 'emails.invitation'; - case MAIL_TYPE_VERIFICATION: - return 'emails.verification'; - case MAIL_TYPE_MAGIC_SESSION: - return 'emails.magicSession'; - default: - throw new Exception('Undefined Mail Type : ' . $type, 500); - } - } - - /** - * Returns true if all the required terms in a locale exist. False otherwise - * - * @param $locale - * @param $prefix - * - * @return bool - */ - protected function doesLocaleExist(Locale $locale, string $prefix): bool - { - - if (!$locale->getText('emails.sender') || !$locale->getText("$prefix.hello") || !$locale->getText("$prefix.subject") || !$locale->getText("$prefix.body") || !$locale->getText("$prefix.footer") || !$locale->getText("$prefix.thanks") || !$locale->getText("$prefix.signature")) { - return false; - } - - return true; - } } diff --git a/src/Appwrite/Event/Mail.php b/src/Appwrite/Event/Mail.php index b3325c00be..1b7e218d69 100644 --- a/src/Appwrite/Event/Mail.php +++ b/src/Appwrite/Event/Mail.php @@ -8,11 +8,10 @@ use Utopia\Database\Document; class Mail extends Event { protected string $recipient = ''; - protected string $url = ''; - protected string $type = ''; + protected string $from = ''; protected string $name = ''; - protected string $locale = ''; - protected ?Document $team = null; + protected string $subject = ''; + protected string $body = ''; public function __construct() { @@ -20,14 +19,14 @@ class Mail extends Event } /** - * Sets team for the mail event. + * Sets subject for the mail event. * - * @param Document $team + * @param string $subject * @return self */ - public function setTeam(Document $team): self + public function setSubject(string $subject): self { - $this->team = $team; + $this->subject = $subject; return $this; } @@ -35,11 +34,11 @@ class Mail extends Event /** * Returns set team for the mail event. * - * @return null|Document + * @return string */ - public function getTeam(): ?Document + public function getSubject(): string { - return $this->team; + return $this->subject; } /** @@ -66,49 +65,49 @@ class Mail extends Event } /** - * Sets url for the mail event. + * Sets from for the mail event. * - * @param string $url + * @param string $from * @return self */ - public function setUrl(string $url): self + public function setFrom(string $from): self { - $this->url = $url; + $this->from = $from; return $this; } /** - * Returns set url for the mail event. + * Returns from for mail event. * * @return string */ - public function getURL(): string + public function getFrom(): string { - return $this->url; + return $this->from; } /** - * Sets type for the mail event (use the constants starting with MAIL_TYPE_*). + * Sets body for the mail event. * - * @param string $type + * @param string $body * @return self */ - public function setType(string $type): self + public function setBody(string $body): self { - $this->type = $type; + $this->body = $body; return $this; } /** - * Returns set type for the mail event. + * Returns body for the mail event. * * @return string */ - public function getType(): string + public function getBody(): string { - return $this->type; + return $this->body; } /** @@ -134,29 +133,6 @@ class Mail extends Event return $this->name; } - /** - * Sets locale for the mail event. - * - * @param string $locale - * @return self - */ - public function setLocale(string $locale): self - { - $this->locale = $locale; - - return $this; - } - - /** - * Returns set locale for the mail event. - * - * @return string - */ - public function getLocale(): string - { - return $this->locale; - } - /** * Executes the event and sends it to the mails worker. * @@ -166,15 +142,11 @@ class Mail extends Event public function trigger(): string|bool { return Resque::enqueue($this->queue, $this->class, [ - 'project' => $this->project, - 'user' => $this->user, - 'payload' => $this->payload, + 'from' => $this->from, 'recipient' => $this->recipient, - 'url' => $this->url, - 'locale' => $this->locale, - 'type' => $this->type, 'name' => $this->name, - 'team' => $this->team, + 'subject' => $this->subject, + 'body' => $this->body, 'events' => Event::generateEvents($this->getEvent(), $this->getParams()) ]); }