feat: Added deleting task when ical todo delete has been sent

- feat: Added warn logger method to logger class
This commit is contained in:
Aerex 2020-06-27 00:56:44 -05:00
parent 98eb84b3b6
commit a89dd17991
6 changed files with 81 additions and 17 deletions

View File

@ -16,6 +16,7 @@ class Console extends AbstractConsole {
if (is_array($input)) { if (is_array($input)) {
return json_encode($input); return json_encode($input);
} }
return $input;
} }
public function execute($cmd, $args, $input = null, $envs = []) { public function execute($cmd, $args, $input = null, $envs = []) {

View File

@ -45,6 +45,11 @@ class Logger {
$this->logger->notice($message); $this->logger->notice($message);
} }
} }
public function warn($message) {
if ($this->configs['enabled']) {
$this->logger->warning($message);
}
}
public function error($message) { public function error($message) {
if ($this->configs['enabled']) { if ($this->configs['enabled']) {
@ -69,10 +74,4 @@ class Logger {
} }
} }
public function setLevel($level) {
if ($this->configs['enabled']) {
$this->logger->setLevel($level);
}
}
} }

View File

@ -61,7 +61,7 @@ class Plugin extends ServerPlugin {
*/ */
public function initializeStorages($configs) { public function initializeStorages($configs) {
$taskwarrior = new Taskwarrior(new Console(['rc.verbose=nothing', 'rc.hooks=off']), $configs, new Logger($configs, 'Taskwarrior')); $taskwarrior = new Taskwarrior(new Console(['rc.verbose=nothing', 'rc.hooks=off', 'rc.confirmation=no']), $configs, new Logger($configs, 'Taskwarrior'));
$this->storageManager->addStorage(Taskwarrior::NAME, $taskwarrior); $this->storageManager->addStorage(Taskwarrior::NAME, $taskwarrior);
} }
@ -108,6 +108,9 @@ class Plugin extends ServerPlugin {
break; break;
case 'POST': case 'POST':
$this->httpPost($request, $response); $this->httpPost($request, $response);
break;
case 'DELETE':
$this->httpDelete($request);
return; return;
} }
} }
@ -141,7 +144,6 @@ class Plugin extends ServerPlugin {
* *
* @param RequestInterface $request * @param RequestInterface $request
* *
* @return bool
*/ */
function httpPost(RequestInterface $request, ResponseInterface $response) { function httpPost(RequestInterface $request, ResponseInterface $response) {
$postVars = $request->getPostData(); $postVars = $request->getPostData();
@ -169,9 +171,35 @@ class Plugin extends ServerPlugin {
$response->setStatus(302); $response->setStatus(302);
$request->setBody($body); $request->setBody($body);
}
/**
* This method handles the DELETE method.
*
* @param RequestInterface $request
* @param ResponseInterface $response
*
*/
public function httpDelete(RequestInterface $request) {
try {
$body = $request->getBodyAsString();
$path = $request->getPath();
$paths = explode('/', $path);
if (sizeof($paths) > 1) {
$uid = str_replace('.ics', '', $paths[sizeof($paths)-1]);
$this->storageManager->remove($uid);
}
} catch(BadRequest $e){
throw new BadRequest($e->getMessage(), null, $e);
} catch(\Exception $e){
throw new \Exception($e->getMessage(), null, $e);
}
$request->setBody($body);
} }
/** /**
* Generates the 'general' configuration section * Generates the 'general' configuration section
* @return string * @return string

View File

@ -46,4 +46,17 @@ class StorageManager {
$storage->save($calendar); $storage->save($calendar);
} }
} }
public function remove($uid) {
if (!isset($this->configs)) {
throw new \Exception('StorageManger was not initialize or configs are not defined');
}
foreach ($this->configs['storages'] as $key => $value) {
$storage = $this->storages[$key];
if (!isset($storage)){
throw new \Exception();
}
$storage->remove($uid);
}
}
} }

View File

@ -6,6 +6,7 @@ use Sabre\VObject\Component\VCalendar as Calendar;
interface IStorage { interface IStorage {
public function save(Calendar $c); public function save(Calendar $c);
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

@ -98,10 +98,10 @@ class Taskwarrior implements IStorage {
break; break;
} }
} }
array_push(['description' => $descriptionLine, 'entry' => $annotationEntry]); array_push($annotations, ['description' => $descriptionLine, 'entry' => $annotationEntry]);
$task['annotations'] = $annotations;
} }
} }
if (!isset($task['entry'])){ if (!isset($task['entry'])){
$task['entry'] = $vtodo->DTSTAMP->getDateTime()->format(\DateTime::ISO8601); $task['entry'] = $vtodo->DTSTAMP->getDateTime()->format(\DateTime::ISO8601);
} }
@ -177,11 +177,6 @@ class Taskwarrior implements IStorage {
} }
} }
if (isset($vtodo->{'RELATED-TO'}) && isset($this->tasks[(string)$vtodo->{'RELATED-TO'}])) {
$relatedTask = $this->tasks[(string)$vtodo->{'RELATED-TO'}];
$task['depends'] = $relatedTask['uuid'];
}
if (isset($vtodo->GEO)){ if (isset($vtodo->GEO)){
$task['geo'] = $vtodo->GEO->getRawMimeDirValue(); $task['geo'] = $vtodo->GEO->getRawMimeDirValue();
} }
@ -209,4 +204,31 @@ class Taskwarrior implements IStorage {
throw $e; throw $e;
} }
} }
public function remove($uid) {
try {
$this->logger->info(sprintf('Deleting iCal %s from taskwarrior', $uid));
$this->refresh();
$task = $this->tasks[(string)$uid];
if (isset($task) && $task['status'] !== 'deleted') {
$uuid = $task['uuid'];
$this->logger->info(
sprintf('Executing TASKRC = %s TASKDATA = %s task delete %s', $this->configs['taskrc'], $this->configs['taskdata'], $uuid)
);
$output = $this->console->execute('task', ['delete', (string)$uuid], null,
['TASKRC' => $this->configs['taskrc'],'TASKDATA' => $this->configs['taskdata']]);
$this->logger->info($output);
} else if (isset($task) && $task['status'] === 'deleted') {
$this->logger->warn(sprintf('Task %s has already been deleted', $task['uuid']));
} else {
$this->logger->error(sprintf('Could not find task for iCal %s to be deleted', $uid));
}
} catch (\Exception $e) {
$this->logger->error($e->getMessage());
$this->logger->error($e->getTraceAsString());
throw $e;
}
}
} }