[Pkg-owncloud-commits] [php-sabre-vobject] 06/106: Started work on parsing iTip messages.

David Prévot taffit at moszumanska.debian.org
Fri Aug 22 15:10:53 UTC 2014


This is an automated email from the git hooks/post-receive script.

taffit pushed a commit to branch master
in repository php-sabre-vobject.

commit 64c3ca754564bf1b0842effea5897d768f47a820
Author: Evert Pot <me at evertpot.com>
Date:   Thu Jul 17 23:03:21 2014 -0400

    Started work on parsing iTip messages.
---
 lib/Sabre/VObject/ITip/Broker.php                  |  85 +++++++++-
 tests/Sabre/VObject/ITip/BrokerNewEventTest.php    |   2 +-
 .../VObject/ITip/BrokerProcessMessageTest.php      | 173 +++++++++++++++++++++
 tests/Sabre/VObject/ITip/BrokerUpdateEventTest.php |   2 +-
 4 files changed, 258 insertions(+), 4 deletions(-)

diff --git a/lib/Sabre/VObject/ITip/Broker.php b/lib/Sabre/VObject/ITip/Broker.php
index 3feba0d..9462aff 100644
--- a/lib/Sabre/VObject/ITip/Broker.php
+++ b/lib/Sabre/VObject/ITip/Broker.php
@@ -53,6 +53,87 @@ class Broker {
     public $scheduleAgentServerRules = true;
 
     /**
+     * This method is used to process an incoming itip message.
+     *
+     * Examples:
+     *
+     * 1. A user is an attendee to an event. The organizer sends an updated
+     * meeting using a new ITip message with METHOD:REQUEST. This function
+     * will process the message * and update the attendee's event accordingly.
+     *
+     * 2. The organizer cancelled the event using METHOD:CANCEL. We will update
+     * the users event to state STATUS:CANCELLED.
+     *
+     * 3. An attendee sent a reply to an invite using METHOD:REPLY. We can
+     * update the organizers event to update the ATTENDEE with its correct
+     * PARTSTAT.
+     *
+     * The $existingObject is updated in-place. If no existing object exists
+     * (because it's a new invite for example) a new object will be created.
+     *
+     * If an existing object does not exist, and the method was CANCEL or
+     * REPLY, the message effectively gets ignored, and no 'existingObject'
+     * will be created.
+     *
+     * The updated $existingObject is also returned from this function.
+     *
+     * @param Message $itipMessage
+     * @param VCalendar $existingObject
+     * @return VCalendar
+     */
+    public function processMessage(Message $itipMessage, VCalendar $existingObject = null) {
+
+        switch($itipMessage->method) {
+
+            /**
+             * This is message from an organizer, and is either a new event
+             * invite, or an update to an existing one.
+             */
+            case 'REQUEST' :
+                if (!$existingObject) {
+                    // This is a new invite, and we're just going to copy over
+                    // all the components from the invite.
+                    $existingObject = new VCalendar();
+                    foreach($itipMessage->message->getComponents() as $component) {
+                        $existingObject->add(clone $component);
+                    }
+                } else {
+                    // We need to update an existing object with all the new
+                    // information. We can just remove all existing components
+                    // and create new ones.
+                    foreach($existingObject->getComponents() as $component) {
+                        $existingObject->remove($component);
+                    }
+                    foreach($itipMessage->message->getComponents() as $component) {
+                        $existingObject->add(clone $component);
+                    }
+                }
+                break;
+
+            /**
+             * This is a message from an organizer, and means that either an
+             * attendee got removed from an event, or an event got cancelled
+             * altogether.
+             */
+            case 'CANCEL' :
+                if (!$existingObject) {
+                    // The event didn't exist in the first place, so we're just
+                    // ignoring this message.
+                } else {
+                    foreach($existingObject->VEVENT as $vevent) {
+                        $vevent->STATUS = 'CANCELLED';
+                        $vevent->SEQUENCE = $itipMessage->sequence;
+                    }
+                }
+                break;
+
+        }
+
+       return $existingObject;
+
+    }
+
+    /**
      * This function parses a VCALENDAR object, and if the object had an
      * organizer and attendees, it will generate iTip messages for every
      * attendee.
@@ -63,7 +144,7 @@ class Broker {
      * @param VCalendar|string $calendar
      * @return array
      */
-    public function createEvent($calendar) {
+    public function parseNewEvent($calendar) {
 
         if (is_string($calendar)) {
             $calendar = Reader::read($calendar);
@@ -161,7 +242,7 @@ class Broker {
      * @param VCalendar|string $oldCalendar
      * @return array
      */
-    public function updateEvent($calendar, $oldCalendar) {
+    public function parseUpdatedEvent($calendar, $oldCalendar) {
 
         if (is_string($calendar)) {
             $calendar = Reader::read($calendar);
diff --git a/tests/Sabre/VObject/ITip/BrokerNewEventTest.php b/tests/Sabre/VObject/ITip/BrokerNewEventTest.php
index 5fa2bf2..958bc6f 100644
--- a/tests/Sabre/VObject/ITip/BrokerNewEventTest.php
+++ b/tests/Sabre/VObject/ITip/BrokerNewEventTest.php
@@ -202,7 +202,7 @@ ICS
     function parse($message, $expected = array()) {
 
         $broker = new Broker();
-        $result = $broker->createEvent($message);
+        $result = $broker->parseNewEvent($message);
 
         $this->assertEquals(count($expected), count($result));
 
diff --git a/tests/Sabre/VObject/ITip/BrokerProcessMessageTest.php b/tests/Sabre/VObject/ITip/BrokerProcessMessageTest.php
new file mode 100644
index 0000000..cbf59ac
--- /dev/null
+++ b/tests/Sabre/VObject/ITip/BrokerProcessMessageTest.php
@@ -0,0 +1,173 @@
+<?php
+
+namespace Sabre\VObject\ITip;
+
+use Sabre\VObject\Reader;
+
+class BrokerProcessMessageTest extends \PHPUnit_Framework_TestCase {
+
+    function testRequestNew() {
+
+        $itip = <<<ICS
+BEGIN:VCALENDAR
+VERSION:2.0
+METHOD:REQUEST
+BEGIN:VEVENT
+SEQUENCE:1
+UID:foobar
+END:VEVENT
+END:VCALENDAR
+ICS;
+
+        $expected = <<<ICS
+BEGIN:VCALENDAR
+%foo%
+BEGIN:VEVENT
+SEQUENCE:1
+UID:foobar
+END:VEVENT
+END:VCALENDAR
+ICS;
+
+        $result = $this->process($itip, null, $expected);
+
+    }
+    function testRequestUpdate() {
+
+        $itip = <<<ICS
+BEGIN:VCALENDAR
+VERSION:2.0
+METHOD:REQUEST
+BEGIN:VEVENT
+SEQUENCE:2
+UID:foobar
+END:VEVENT
+END:VCALENDAR
+ICS;
+
+        $old = <<<ICS
+BEGIN:VCALENDAR
+%foo%
+BEGIN:VEVENT
+SEQUENCE:1
+UID:foobar
+END:VEVENT
+END:VCALENDAR
+ICS;
+
+        $expected = <<<ICS
+BEGIN:VCALENDAR
+%foo%
+BEGIN:VEVENT
+SEQUENCE:2
+UID:foobar
+END:VEVENT
+END:VCALENDAR
+ICS;
+
+        $result = $this->process($itip, $old, $expected);
+
+    }
+
+    function testCancel() {
+
+        $itip = <<<ICS
+BEGIN:VCALENDAR
+VERSION:2.0
+METHOD:CANCEL
+BEGIN:VEVENT
+SEQUENCE:2
+UID:foobar
+END:VEVENT
+END:VCALENDAR
+ICS;
+
+        $old = <<<ICS
+BEGIN:VCALENDAR
+%foo%
+BEGIN:VEVENT
+SEQUENCE:1
+UID:foobar
+END:VEVENT
+END:VCALENDAR
+ICS;
+
+        $expected = <<<ICS
+BEGIN:VCALENDAR
+%foo%
+BEGIN:VEVENT
+SEQUENCE:2
+UID:foobar
+STATUS:CANCELLED
+END:VEVENT
+END:VCALENDAR
+ICS;
+
+        $result = $this->process($itip, $old, $expected);
+
+    }
+
+    function testCancelNoExistingEvent() {
+
+        $itip = <<<ICS
+BEGIN:VCALENDAR
+VERSION:2.0
+METHOD:CANCEL
+BEGIN:VEVENT
+SEQUENCE:2
+UID:foobar
+END:VEVENT
+END:VCALENDAR
+ICS;
+
+        $old = null;
+        $expected = null;
+
+        $result = $this->process($itip, $old, $expected);
+
+    }
+
+    function process($input, $existingObject = null, $expected = false) {
+
+        $version = \Sabre\VObject\Version::VERSION;
+
+        $vcal = Reader::read($input);
+        $message = new Message();
+        $message->message = $vcal;
+        $message->method = $vcal->METHOD;
+        $message->sequence = $vcal->VEVENT[0]->SEQUENCE;
+        $broker = new Broker();
+
+        if (is_string($existingObject)) {
+            $existingObject = str_replace(
+                '%foo%',
+                "VERSION:2.0\nPRODID:-//Sabre//Sabre VObject $version//EN\nCALSCALE:GREGORIAN",
+                $existingObject
+            );
+            $existingObject = Reader::read($existingObject);
+        }
+
+        $result = $broker->processMessage($message, $existingObject);
+
+        if (is_string($expected)) {
+            $expected = str_replace(
+                '%foo%',
+                "VERSION:2.0\nPRODID:-//Sabre//Sabre VObject $version//EN\nCALSCALE:GREGORIAN",
+                $expected
+            );
+            $expected = str_replace("\n", "\r\n", $expected);
+
+        }
+        if ($result instanceof \Sabre\VObject\Component\VCalendar) {
+            $result = $result->serialize();
+            $result = rtrim($result,"\r\n");
+        }
+
+        $this->assertEquals(
+            $expected,
+            $result
+        );
+
+    }
+
+}
diff --git a/tests/Sabre/VObject/ITip/BrokerUpdateEventTest.php b/tests/Sabre/VObject/ITip/BrokerUpdateEventTest.php
index 1ae6b4f..fb3673d 100644
--- a/tests/Sabre/VObject/ITip/BrokerUpdateEventTest.php
+++ b/tests/Sabre/VObject/ITip/BrokerUpdateEventTest.php
@@ -123,7 +123,7 @@ ICS
     function parse($oldMessage, $newMessage, $expected = array()) {
 
         $broker = new Broker();
-        $result = $broker->updateEvent($newMessage, $oldMessage);
+        $result = $broker->parseUpdatedEvent($newMessage, $oldMessage);
 
         $this->assertEquals(count($expected), count($result));
 

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-owncloud/php-sabre-vobject.git



More information about the Pkg-owncloud-commits mailing list