Skip to content

Commit

Permalink
Create a separate cronjob
Browse files Browse the repository at this point in the history
  • Loading branch information
alfredbuehler committed Feb 24, 2023
1 parent eb2623e commit 7e26eef
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 3 deletions.
2 changes: 2 additions & 0 deletions Action/ConvertEmailToComment.php
Expand Up @@ -11,6 +11,7 @@
use Kanboard\Model\ProjectUserRoleModel;
use Kanboard\Model\TaskModel;
use Kanboard\Model\UserModel;
use Kanboard\Plugin\Mailmagik\Helper\MailHelper;
use League\HTMLToMarkdown\HtmlConverter;
use PhpImap;

Expand Down Expand Up @@ -41,6 +42,7 @@ public function getCompatibleEvents()
{
return array(
TaskModel::EVENT_DAILY_CRONJOB,
MailHelper::EVENT_FETCHMAIL,
);
}
/**
Expand Down
2 changes: 2 additions & 0 deletions Action/ConvertEmailToTask.php
Expand Up @@ -9,6 +9,7 @@
use Kanboard\Model\ProjectModel;
use Kanboard\Model\TaskModel;
use Kanboard\Model\UserModel;
use Kanboard\Plugin\Mailmagik\Helper\MailHelper;
use League\HTMLToMarkdown\HtmlConverter;
use PhpImap;

Expand Down Expand Up @@ -39,6 +40,7 @@ public function getCompatibleEvents()
{
return array(
TaskModel::EVENT_DAILY_CRONJOB,
MailHelper::EVENT_FETCHMAIL,
);
}
/**
Expand Down
28 changes: 28 additions & 0 deletions Console/Command.php
@@ -0,0 +1,28 @@
<?php

namespace Kanboard\Plugin\Mailmagik\Console;

use Kanboard\Plugin\Mailmagik\Helper\MailHelper;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\NullOutput;
use Symfony\Component\Console\Output\OutputInterface;

class Command extends \Kanboard\Console\BaseCommand
{
protected function configure()
{
$this
->setName('mailmagik:fetchmail') // Name for the commandline
->setDescription('Trigger Mailmagik mail fetching by automatic actions')
;
}

protected function execute(InputInterface $input, OutputInterface $output)
{
$command = 'projects:fetchmail';

$job = $this->getApplication()->find($command);
$job->run(new ArrayInput(array('command' => $command)), new NullOutput());
}
}
43 changes: 43 additions & 0 deletions Console/FetchMail.php
@@ -0,0 +1,43 @@
<?php

namespace Kanboard\Plugin\Mailmagik\Console;

use Kanboard\Console\BaseCommand;
use Kanboard\Event\GenericEvent;
use Kanboard\Plugin\Mailmagik\Helper\MailHelper;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class FetchMail extends BaseCommand
{
public const EVENT = MailHelper::EVENT_FETCHMAIL;

protected function configure()
{
$this
->setName('projects:fetchmail')
->setDescription('Trigger scheduler event for all tasks');
}

protected function execute(InputInterface $input, OutputInterface $output)
{
foreach ($this->getProjectIds() as $project_id) {
$this->dispatcher->dispatch(
self::EVENT,
new GenericEvent(array('project_id' => $project_id))
);
}
}

private function getProjectIds()
{
$listeners = $this->dispatcher->getListeners(self::EVENT);
$project_ids = array();

foreach ($listeners as $listener) {
$project_ids[] = $listener[0]->getProjectId();
}

return array_unique($project_ids);
}
}
2 changes: 2 additions & 0 deletions Helper/MailHelper.php
Expand Up @@ -9,6 +9,8 @@

class MailHelper extends Base
{
public const EVENT_FETCHMAIL = 'mailmagik.fetchmail';

/**
* Login to the IMAP server
*
Expand Down
18 changes: 15 additions & 3 deletions Plugin.php
Expand Up @@ -3,8 +3,11 @@
namespace Kanboard\Plugin\Mailmagik;

use Kanboard\Core\Plugin\Base;
use Kanboard\Plugin\Mailmagik\Action\ConvertEmailToTask;
use Kanboard\Plugin\Mailmagik\Action\ConvertEmailToComment;
use Kanboard\Plugin\Mailmagik\Action\ConvertEmailToTask;
use Kanboard\Plugin\Mailmagik\Console\Command;
use Kanboard\Plugin\Mailmagik\Console\FetchMail;
use Kanboard\Plugin\Mailmagik\Helper\MailHelper;

class Plugin extends Base
{
Expand All @@ -28,16 +31,25 @@ public function initialize()
$this->template->hook->attach('template:task:sidebar:information', 'mailmagik:task/emails');
}

//CONFIG HOOK
// Config hook
$this->template->hook->attach('template:config:email', 'mailmagik:config/config');

//css
$this->hook->on('template:layout:css', array('template' => 'plugins/Mailmagik/Assets/css/mailmagik.css'));
$this->hook->on('template:layout:js', array('template' => 'plugins/Mailmagik/Assets/js/mailmagik.js'));

//ACTIONS
// Actions
$this->actionManager->register(new ConvertEmailToTask($this->container));
$this->actionManager->register(new ConvertEmailToComment($this->container));

// Commandline: ./cli mailmagik:fetchmail
$this->cli->add(new Command($this->container));
$this->cli->add(new FetchMail($this->container));
}

public function onStartup()
{
$this->eventManager->register(MailHelper::EVENT_FETCHMAIL, t('Trigger Mailmagik mail fetching'));
}

public function getPluginName()
Expand Down

0 comments on commit 7e26eef

Please sign in to comment.