Add rsvp command
authorChristian Weiske <cweiske@cweiske.de>
Thu, 15 Sep 2016 05:45:51 +0000 (07:45 +0200)
committerChristian Weiske <cweiske@cweiske.de>
Thu, 15 Sep 2016 05:45:51 +0000 (07:45 +0200)
src/shpub/Cli.php
src/shpub/Command/Rsvp.php [new file with mode: 0644]
src/shpub/Validator.php

index 9daa59bd681edd2bf92474e1a144f23bc208b122..75f51bdd9c78f120a0de991a7cff283662f472f6 100644 (file)
@@ -165,6 +165,7 @@ class Cli
         Command_Reply::opts($optParser);
         Command_Like::opts($optParser);
         Command_Repost::opts($optParser);
+        Command_Rsvp::opts($optParser);
 
         Command_Delete::opts($optParser);
         Command_Undelete::opts($optParser);
diff --git a/src/shpub/Command/Rsvp.php b/src/shpub/Command/Rsvp.php
new file mode 100644 (file)
index 0000000..5ed8ecd
--- /dev/null
@@ -0,0 +1,64 @@
+<?php
+namespace shpub;
+
+/**
+ * Repondez s'il vous plait - an answer to an invitation
+ */
+class Command_Rsvp extends Command_AbstractProps
+{
+    public static function opts(\Console_CommandLine $optParser)
+    {
+        $cmd = $optParser->addCommand('rsvp');
+        static::optsGeneric($cmd);
+        $cmd->addArgument(
+            'url',
+            [
+                'optional'    => false,
+                'description' => 'URL that is replied to',
+            ]
+        );
+        $cmd->addArgument(
+            'rsvp',
+            [
+                'optional'    => false,
+                'multiple'    => false,
+                'description' => 'Answer: yes, no, maybe',
+            ]
+        );
+        $cmd->addArgument(
+            'text',
+            [
+                'optional'    => true,
+                'multiple'    => false,
+                'description' => 'Answer text',
+            ]
+        );
+    }
+
+    public function run(\Console_CommandLine_Result $cmdRes)
+    {
+        $url = Validator::url($cmdRes->args['url'], 'url');
+        if ($url === false) {
+            exit(10);
+        }
+        $rsvp = Validator::rsvp($cmdRes->args['rsvp']);
+        if ($rsvp === false) {
+            exit(10);
+        }
+
+        $req = new Request($this->cfg->host, $this->cfg);
+        $req->req->addPostParameter('h', 'entry');
+        $req->req->addPostParameter('in-reply-to', $url);
+        $req->req->addPostParameter('rsvp', $rsvp);
+        if ($cmdRes->args['text'] != '') {
+            $req->req->addPostParameter('content', $cmdRes->args['text']);
+        }
+        $this->handleGenericOptions($cmdRes, $req);
+
+        $res = $req->send();
+        $postUrl = $res->getHeader('Location');
+        Log::info('RSVP created at server');
+        Log::msg($postUrl);
+    }
+}
+?>
index 1da91cb390775b5f751efada1d8abb6f00dd8a81..283604d0acddfb6ed84dd637b4048c7e7869d981 100644 (file)
@@ -28,5 +28,18 @@ class Validator
 
         return $url;
     }
+
+    public static function rsvp($answer)
+    {
+        $allowed = ['yes', 'no', 'maybe'];
+        if (false === array_search($answer, $allowed)) {
+            Log::err(
+                'Invalid RSVP answer given; allowed are: '
+                . implode (', ', $allowed)
+            );
+            return false;
+        }
+        return $answer;
+    }
 }
 ?>