diff --git a/CHANGES.md b/CHANGES.md index 8a7764c31..702d9ebf3 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -1,6 +1,7 @@ # Version 1.0.2 ## Bugs - Fixed default value for creating Boolean Attribute [#4040](https://github.com/appwrite/appwrite/pull/404) +- Fixed migration for audit by migrating the `time` attribute [4038](https://github.com/appwrite/appwrite/pull/4038) # Version 1.0.1 ## Bugs diff --git a/app/controllers/api/functions.php b/app/controllers/api/functions.php index ca2f237f1..b26117127 100644 --- a/app/controllers/api/functions.php +++ b/app/controllers/api/functions.php @@ -71,8 +71,13 @@ App::post('/v1/functions') ->param('enabled', true, new Boolean(), 'Is function enabled?', true) ->inject('response') ->inject('dbForProject') + ->inject('project') + ->inject('user') ->inject('events') - ->action(function (string $functionId, string $name, array $execute, string $runtime, array $events, string $schedule, int $timeout, bool $enabled, Response $response, Database $dbForProject, Event $eventsInstance) { + ->action(function (string $functionId, string $name, array $execute, string $runtime, array $events, string $schedule, int $timeout, bool $enabled, Response $response, Database $dbForProject, Document $project, Document $user, Event $eventsInstance) { + + $cron = !empty($schedule) ? new CronExpression($schedule) : null; + $next = !empty($schedule) ? DateTime::format($cron->getNextRunDate()) : null; $functionId = ($functionId == 'unique()') ? ID::unique() : $functionId; $function = $dbForProject->createDocument('functions', new Document([ @@ -86,11 +91,22 @@ App::post('/v1/functions') 'schedule' => $schedule, 'scheduleUpdatedAt' => DateTime::now(), 'schedulePrevious' => null, - 'scheduleNext' => null, + 'scheduleNext' => $next, 'timeout' => $timeout, 'search' => implode(' ', [$functionId, $name, $runtime]) ])); + if ($next) { + // Async task reschedule + $functionEvent = new Func(); + $functionEvent + ->setFunction($function) + ->setType('schedule') + ->setUser($user) + ->setProject($project) + ->schedule(new \DateTime($next)); + } + $eventsInstance->setParam('functionId', $function->getId()); $response @@ -442,11 +458,9 @@ App::put('/v1/functions/:functionId') throw new Exception(Exception::FUNCTION_NOT_FOUND); } - $original = $function->getAttribute('schedule', ''); - $cron = (!empty($function->getAttribute('deployment')) && !empty($schedule)) ? new CronExpression($schedule) : null; - $next = (!empty($function->getAttribute('deployment')) && !empty($schedule)) ? DateTime::format($cron->getNextRunDate()) : null; + $cron = !empty($schedule) ? new CronExpression($schedule) : null; + $next = !empty($schedule) ? DateTime::format($cron->getNextRunDate()) : null; - $scheduleUpdatedAt = $schedule !== $original ? DateTime::now() : $function->getAttribute('scheduleUpdatedAt'); $enabled ??= $function->getAttribute('enabled', true); $function = $dbForProject->updateDocument('functions', $function->getId(), new Document(array_merge($function->getArrayCopy(), [ @@ -454,14 +468,14 @@ App::put('/v1/functions/:functionId') 'name' => $name, 'events' => $events, 'schedule' => $schedule, - 'scheduleUpdatedAt' => $scheduleUpdatedAt, + 'scheduleUpdatedAt' => DateTime::now(), 'scheduleNext' => $next, 'timeout' => $timeout, 'enabled' => $enabled, 'search' => implode(' ', [$functionId, $name, $function->getAttribute('runtime')]), ]))); - if ($next && $schedule !== $original) { + if ($next) { // Async task reschedule $functionEvent = new Func(); $functionEvent @@ -519,24 +533,10 @@ App::patch('/v1/functions/:functionId/deployments/:deploymentId') throw new Exception(Exception::BUILD_NOT_READY); } - $schedule = $function->getAttribute('schedule', ''); - $cron = (empty($function->getAttribute('deployment')) && !empty($schedule)) ? new CronExpression($schedule) : null; - $next = (empty($function->getAttribute('deployment')) && !empty($schedule)) ? DateTime::format($cron->getNextRunDate()) : null; - $function = $dbForProject->updateDocument('functions', $function->getId(), new Document(array_merge($function->getArrayCopy(), [ - 'deployment' => $deployment->getId(), - 'scheduleNext' => $next, + 'deployment' => $deployment->getId() ]))); - if ($next) { // Init first schedule - $functionEvent = new Func(); - $functionEvent - ->setType('schedule') - ->setFunction($function) - ->setProject($project) - ->schedule(new \DateTime($next)); - } - $events ->setParam('functionId', $function->getId()) ->setParam('deploymentId', $deployment->getId()); diff --git a/app/workers/builds.php b/app/workers/builds.php index 3d78aeb65..bf780c646 100644 --- a/app/workers/builds.php +++ b/app/workers/builds.php @@ -109,6 +109,7 @@ class BuildsV1 extends Worker /** Trigger Webhook */ $deploymentModel = new Deployment(); + $deploymentUpdate = new Event(Event::WEBHOOK_QUEUE_NAME, Event::WEBHOOK_CLASS_NAME); $deploymentUpdate ->setProject($project) diff --git a/app/workers/certificates.php b/app/workers/certificates.php index f932ba4bc..e7885bc66 100644 --- a/app/workers/certificates.php +++ b/app/workers/certificates.php @@ -1,6 +1,7 @@ 'console', - 'project' => 'console', - 'name' => 'Appwrite Administrator', - 'recipient' => App::getEnv('_APP_SYSTEM_SECURITY_EMAIL_ADDRESS'), - 'url' => 'https://' . $domain, - 'locale' => App::getEnv('_APP_LOCALE', 'en'), - 'type' => MAIL_TYPE_CERTIFICATE, - - 'domain' => $domain, - 'error' => $errorMessage, - 'attempt' => $attempt - ]); + $mail = new Mail(); + $mail + ->setType(MAIL_TYPE_CERTIFICATE) + ->setRecipient(App::getEnv('_APP_SYSTEM_SECURITY_EMAIL_ADDRESS')) + ->setUrl('https://' . $domain) + ->setLocale(App::getEnv('_APP_LOCALE', 'en')) + ->setName('Appwrite Administrator') + ->setPayload([ + 'domain' => $domain, + 'error' => $errorMessage, + 'attempt' => $attempt + ]) + ->trigger(); } /** diff --git a/app/workers/mails.php b/app/workers/mails.php index 9b81353f0..90e9e9a66 100644 --- a/app/workers/mails.php +++ b/app/workers/mails.php @@ -32,9 +32,10 @@ class MailsV1 extends Worker return; } - $project = new Document($this->args['project']); + $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']; @@ -42,20 +43,20 @@ class MailsV1 extends Worker $type = $this->args['type']; $prefix = $this->getPrefix($type); $locale = new Locale($this->args['locale']); - $projectName = $project->getAttribute('name', '[APP-NAME]'); + $projectName = $project->isEmpty() ? 'Console' : $project->getAttribute('name', '[APP-NAME]'); if (!$this->doesLocaleExist($locale, $prefix)) { $locale->setDefault('en'); } - $from = $project->getId() === 'console' ? '' : \sprintf($locale->getText('emails.sender'), $projectName); + $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 = $this->args['domain']; - $error = $this->args['error']; - $attempt = $this->args['attempt']; + $domain = $payload['domain']; + $error = $payload['error']; + $attempt = $payload['attempt']; $subject = \sprintf($locale->getText("$prefix.subject"), $domain); $body->setParam('{{domain}}', $domain); diff --git a/src/Appwrite/Migration/Version/V15.php b/src/Appwrite/Migration/Version/V15.php index 6c7e59228..f549c7fbd 100644 --- a/src/Appwrite/Migration/Version/V15.php +++ b/src/Appwrite/Migration/Version/V15.php @@ -504,6 +504,7 @@ class V15 extends Migration $this->createPermissionsColumn($id); $this->migrateDateTimeAttribute($id, '_createdAt'); $this->migrateDateTimeAttribute($id, '_updatedAt'); + $this->migrateDateTimeAttribute($id, 'time'); break; case 'buckets':