1
0
Fork 0
mirror of synced 2024-07-01 04:30:59 +12:00

feat: make mails event and worker general

This commit is contained in:
Damodar Lohani 2022-12-14 06:23:14 +00:00
parent 8dfdef6006
commit 42c855cb41
2 changed files with 30 additions and 159 deletions

View file

@ -1,11 +1,8 @@
<?php
use Appwrite\Resque\Worker;
use Appwrite\Template\Template;
use Utopia\App;
use Utopia\CLI\Console;
use Utopia\Database\Document;
use Utopia\Locale\Locale;
require_once __DIR__ . '/../init.php';
@ -32,67 +29,12 @@ class MailsV1 extends Worker
return;
}
$project = new Document($this->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;
}
}

View file

@ -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())
]);
}