feat: Added tasks with no projects to default calendar

This commit is contained in:
Aerex 2020-09-18 22:15:12 -05:00
parent ce60c373f8
commit 0a01e6e905
5 changed files with 52 additions and 27 deletions

View File

@ -17,8 +17,12 @@ class TaskwarriorConfig {
->defaultValue('~/.taskrc') ->defaultValue('~/.taskrc')
->info('The enivronment variable overrides the default and the command line specification of the .taskrc file') ->info('The enivronment variable overrides the default and the command line specification of the .taskrc file')
->end() ->end()
->scalarNode('default_calendar')
->info('The default calendar to send tasks if no task project is set. The value is the calendar\'s displayname')
->end()
->end(); ->end();
return $node; return $node;
} }
} }

View File

@ -12,6 +12,7 @@ use Sabre\HTTP\RequestInterface;
use Sabre\HTTP\ResponseInterface; use Sabre\HTTP\ResponseInterface;
use Sabre\DAV\ServerPlugin; use Sabre\DAV\ServerPlugin;
use Sabre\DAV\Server; use Sabre\DAV\Server;
use Sabre\DAV\PropFind;
/** /**
* The plugin to interact with Baikal and external storages * The plugin to interact with Baikal and external storages
@ -49,6 +50,14 @@ class Plugin extends ServerPlugin {
$this->initializeStorages($this->rawConfigs); $this->initializeStorages($this->rawConfigs);
} }
private function getDisplayName($path) {
$node = $this->server->tree->getNodeForPath($path);
$propFind = new PropFind($path, []);
$properties = $this->server->getPropertiesByNode($propFind, $node);
return $properties['d:displayname'] ?? '';
}
public function buildConfigurations($configFile) { public function buildConfigurations($configFile) {
$this->config = new ConfigBuilder($configFile); $this->config = new ConfigBuilder($configFile);
$this->config->add(new TaskwarriorConfig()); $this->config->add(new TaskwarriorConfig());
@ -117,7 +126,6 @@ class Plugin extends ServerPlugin {
/** /**
* This method handles the PUT method. * This method handles the PUT method.
* *
@ -128,15 +136,16 @@ class Plugin extends ServerPlugin {
function httpPut(RequestInterface $request){ function httpPut(RequestInterface $request){
$body = $request->getBodyAsString(); $body = $request->getBodyAsString();
$vCal = \Sabre\VObject\Reader::read($body); $vCal = \Sabre\VObject\Reader::read($body);
if (!stristr($vCal->PRODID, 'taskwarrior')) { $displayname = $this->getDisplayName($request->getPath());
try { try {
$this->storageManager->import($vCal); if (!stristr($vCal->PRODID, 'taskwarrior')) {
$this->storageManager->import($vCal, $displayname);
}
} catch(BadRequest $e){ } catch(BadRequest $e){
throw new BadRequest($e->getMessage(), null, $e); throw new BadRequest($e->getMessage(), null, $e);
} catch(\Exception $e){ } catch(\Exception $e){
throw new \Exception($e->getMessage(), null, $e); throw new \Exception($e->getMessage(), null, $e);
} }
}
$request->setBody($body); $request->setBody($body);
@ -188,9 +197,9 @@ class Plugin extends ServerPlugin {
$body = $request->getBodyAsString(); $body = $request->getBodyAsString();
$path = $request->getPath(); $path = $request->getPath();
$paths = explode('/', $path); $paths = explode('/', $path);
if (isset($paths) && sizeof($paths) > 1) { if (sizeof($paths) > 1) {
$uid = str_replace('.ics', '', $paths[sizeof($paths)-1]); $uid = str_replace('.ics', '', $paths[sizeof($paths)-1]);
// Check if deleting an ics file // Attempt to delete if we are removing an ics file
if ($uid != '') { if ($uid != '') {
$this->storageManager->remove($uid); $this->storageManager->remove($uid);
} }
@ -297,3 +306,4 @@ class Plugin extends ServerPlugin {
} }
} }

View File

@ -34,7 +34,7 @@ class StorageManager {
$this->storages[$name] = $storage; $this->storages[$name] = $storage;
} }
public function import(Calendar $calendar) { public function import(Calendar $calendar, string $displayname) {
if (!isset($this->configs)) { if (!isset($this->configs)) {
throw new \Exception('StorageManger was not initialize or configs are not defined'); throw new \Exception('StorageManger was not initialize or configs are not defined');
} }
@ -43,7 +43,7 @@ class StorageManager {
if (!isset($storage)){ if (!isset($storage)){
throw new \Exception(); throw new \Exception();
} }
$storage->save($calendar); $storage->save($calendar, $displayname);
} }
} }
@ -60,3 +60,4 @@ class StorageManager {
} }
} }
} }

View File

@ -5,9 +5,10 @@ namespace Aerex\BaikalStorage\Storages;
use Sabre\VObject\Component\VCalendar as Calendar; use Sabre\VObject\Component\VCalendar as Calendar;
interface IStorage { interface IStorage {
public function save(Calendar $c); public function save(Calendar $c, string $displayname);
public function remove($uid); public function remove($uid);
public function refresh(); public function refresh();
public function getConfigBrowser(); public function getConfigBrowser();
public function updateConfigs($postData); public function updateConfigs($postData);
} }

View File

@ -18,11 +18,6 @@ class Taskwarrior implements IStorage {
} }
public function getConfigBrowser() { public function getConfigBrowser() {
$html = '<tr>';
$html .= '<th>taskrc</th>';
$html .= '<td>The enivronment variable overrides the default and the command line specification of the .taskrc file</td>';
$html .= '<td><input name="tw_taskrc" type="text" value="' . $this->configs['taskrc'] . '"></td>';
$html .= '</tr>';
$html = '<tr>'; $html = '<tr>';
$html .= '<th>taskrc</th>'; $html .= '<th>taskrc</th>';
$html .= '<td>The enivronment variable overrides the default and the command line specification of the .taskrc file</td>'; $html .= '<td>The enivronment variable overrides the default and the command line specification of the .taskrc file</td>';
@ -33,6 +28,11 @@ class Taskwarrior implements IStorage {
$html .= '<td>The environment variable overrides the default and the command line, and the "data.location" configuration setting of the task data directory</td>'; $html .= '<td>The environment variable overrides the default and the command line, and the "data.location" configuration setting of the task data directory</td>';
$html .= '<td><input name="tw_taskdata" type="text" value="' . $this->configs['taskdata'] . '"></td>'; $html .= '<td><input name="tw_taskdata" type="text" value="' . $this->configs['taskdata'] . '"></td>';
$html .= '</tr>'; $html .= '</tr>';
$html .= '<tr>';
$html .= '<th>default_calendar</th>';
$html .= '<td>The default calendar to send tasks if no task project is set. The value is the calendar\'s displayname</td>';
$html .= '<td><input name="tw_default_calendar" type="text" value="' . $this->configs['default_calendar'] . '"></td>';
$html .= '</tr>';
return $html; return $html;
} }
@ -44,13 +44,17 @@ class Taskwarrior implements IStorage {
if (isset($postData['tw_taskdata'])){ if (isset($postData['tw_taskdata'])){
$this->configs['taskdata'] = $postData['tw_taskdata']; $this->configs['taskdata'] = $postData['tw_taskdata'];
} }
if (isset($postData['tw_default_calendar'])){
$this->configs['default_calendar'] = $postData['tw_default_calendar'];
}
return $this->configs; return $this->configs;
} }
public function refresh() { public function refresh() {
$output = $this->console->execute('task', ['sync'], null, $this->logger->info('Syncing taskwarrior tasks...');
$this->console->execute('task', ['sync'], null,
['TASKRC' => $this->configs['taskrc'],'TASKDATA' => $this->configs['taskdata']]); ['TASKRC' => $this->configs['taskrc'],'TASKDATA' => $this->configs['taskdata']]);
$this->tasks = json_decode($this->console->execute('task', ['export'], null, $this->tasks = json_decode($this->console->execute('task', ['export'], null,
['TASKRC' => $this->configs['taskrc'], 'TASKDATA' => $this->configs['taskdata']]), true); ['TASKRC' => $this->configs['taskrc'], 'TASKDATA' => $this->configs['taskdata']]), true);
@ -59,10 +63,9 @@ class Taskwarrior implements IStorage {
$this->tasks[$task['uid']] = $task; $this->tasks[$task['uid']] = $task;
} }
} }
$this->logger->info($output);
} }
public function vObjectToTask($vtodo) { public function vObjectToTask($vtodo, string $displayname) {
if (isset($this->tasks[(string)$vtodo->UID])) { if (isset($this->tasks[(string)$vtodo->UID])) {
$task = $this->tasks[(string)$vtodo->UID]; $task = $this->tasks[(string)$vtodo->UID];
} else { } else {
@ -101,8 +104,8 @@ class Taskwarrior implements IStorage {
$task['start'] = $vtodo->DTSTART->getDateTime()->format(\DateTime::ISO8601); $task['start'] = $vtodo->DTSTART->getDateTime()->format(\DateTime::ISO8601);
} }
if (isset($vtodo->COMPLETED)){ if (isset($vtodo->DTEND)){
$task['end'] = $vtodo->COMPLETED->getDateTime()->format(\DateTime::ISO8601); $task['end'] = $vtodo->DTEND->getDateTime()->format(\DateTime::ISO8601);
} }
if (isset($vtodo->{'LAST-MODIFIED'})) { if (isset($vtodo->{'LAST-MODIFIED'})) {
@ -162,23 +165,28 @@ class Taskwarrior implements IStorage {
$task['geo'] = $vtodo->GEO->getRawMimeDirValue(); $task['geo'] = $vtodo->GEO->getRawMimeDirValue();
} }
if ($this->configs['default_calendar'] != $displayname) {
$task['project'] = $displayname;
}
return $task; return $task;
} }
public function save(Calendar $c) { public function save(Calendar $c, string $displayname) {
try { try {
if (!isset($c->VTODO)){ if (!isset($c->VTODO)){
throw new \Exception('Calendar event does not contain VTODO'); throw new \Exception('Calendar event does not contain VTODO');
} }
$this->logger->info(json_encode($c->jsonSerialize())); $this->logger->info(json_encode($c->jsonSerialize()));
$this->refresh(); $this->refresh();
$task = $this->vObjectToTask($c->VTODO); $task = $this->vObjectToTask($c->VTODO, $displayname);
$this->logger->info(json_encode($task)); $this->logger->info(json_encode($task));
$this->logger->info( $this->logger->info(
sprintf('Executing TASKRC = %s TASKDATA = %s task import %s', $this->configs['taskrc'], $this->configs['taskdata'], json_encode($task)) sprintf('Executing TASKRC = %s TASKDATA = %s task import %s', $this->configs['taskrc'], $this->configs['taskdata'], json_encode($task))
); );
$output = $this->console->execute('task', ['import'], $task, $output = $this->console->execute('task', ['import'], $task,
['TASKRC' => $this->configs['taskrc'],'TASKDATA' => $this->configs['taskdata']]); ['TASKRC' => $this->configs['taskrc'],'TASKDATA' => $this->configs['taskdata']]);
$this->refresh();
$this->logger->info($output); $this->logger->info($output);
} catch (\Exception $e) { } catch (\Exception $e) {
$this->logger->error($e->getTraceAsString()); $this->logger->error($e->getTraceAsString());
@ -191,7 +199,7 @@ class Taskwarrior implements IStorage {
$this->logger->info(sprintf('Deleting iCal %s from taskwarrior', $uid)); $this->logger->info(sprintf('Deleting iCal %s from taskwarrior', $uid));
$this->refresh(); $this->refresh();
if (!array_key_exists((string)$uid, $this->tasks)) { if (!array_key_exists((string)$uid, $this->tasks)) {
$this->logger->warn(sprintf('Could not find task %s to delete. Skipping', (string)$uid)); $this->logger->warn(sprintf('Could not find task %s to be remove. Skipping', (string)$uid));
return; return;
} }
$task = $this->tasks[(string)$uid]; $task = $this->tasks[(string)$uid];
@ -203,6 +211,7 @@ class Taskwarrior implements IStorage {
$output = $this->console->execute('task', ['delete', (string)$uuid], null, $output = $this->console->execute('task', ['delete', (string)$uuid], null,
['TASKRC' => $this->configs['taskrc'],'TASKDATA' => $this->configs['taskdata']]); ['TASKRC' => $this->configs['taskrc'],'TASKDATA' => $this->configs['taskdata']]);
$this->logger->info($output); $this->logger->info($output);
$this->refresh();
} else if (isset($task) && $task['status'] === 'deleted') { } else if (isset($task) && $task['status'] === 'deleted') {
$this->logger->warn(sprintf('Task %s has already been deleted', $task['uuid'])); $this->logger->warn(sprintf('Task %s has already been deleted', $task['uuid']));
} else { } else {