Merge branch 'nightly' into singpolyma/gnu-social-events-saveObjectFromActivity
authorMikael Nordfeldth <mmn@hethane.se>
Sun, 3 Jan 2016 12:08:34 +0000 (13:08 +0100)
committerMikael Nordfeldth <mmn@hethane.se>
Sun, 3 Jan 2016 12:08:34 +0000 (13:08 +0100)
Conflicts:
plugins/Event/EventPlugin.php
plugins/Event/classes/RSVP.php

I just fixed 'em with magic!

1  2 
plugins/Event/EventPlugin.php
plugins/Event/classes/RSVP.php

index 33f08d69c99e2abd467903d8b0129057d47a66c7,5c4abc41d0200918cf9d6ea01480502ce014e7f4..01e6da902cefefa5a38f0f57505dafa421dcd138
@@@ -112,14 -125,7 +125,11 @@@ class EventPlugin extends MicroAppPlugi
      }
  
      function types() {
--        return array(Happening::OBJECT_TYPE,
-                      RSVP::POSITIVE,
-                      RSVP::NEGATIVE,
-                      RSVP::POSSIBLE);
++        return array(Happening::OBJECT_TYPE);
 +    }
 +
 +    function verbs() {
 +        return array(ActivityVerb::POST,
                       RSVP::POSITIVE,
                       RSVP::NEGATIVE,
                       RSVP::POSSIBLE);
              throw new Exception(_m('Wrong type for object.'));
          }
  
 -        $notice = null;
 -
+         $dtstart = $happeningObj->element->getElementsByTagName('dtstart');
+         if($dtstart->length == 0) {
+             // TRANS: Exception thrown when has no start date
+             throw new Exception(_m('No start date for event.'));
+         }
+         $dtend = $happeningObj->element->getElementsByTagName('dtend');
+         if($dtend->length == 0) {
+             // TRANS: Exception thrown when has no end date
+             throw new Exception(_m('No end date for event.'));
+         }
+         // convert RFC3339 dates delivered in Activity Stream to MySQL DATETIME date format
+         $start_time = new DateTime($dtstart->item(0)->nodeValue);
+         $start_time->setTimezone(new DateTimeZone('UTC'));
+         $start_time = $start_time->format('Y-m-d H:i:s');
+         $end_time = new DateTime($dtend->item(0)->nodeValue);
+         $end_time->setTimezone(new DateTimeZone('UTC'));
+         $end_time = $end_time->format('Y-m-d H:i:s');
+         // location is optional
+         $location = null;
+         $location_object = $happeningObj->element->getElementsByTagName('location');
+         if($location_object->length > 0) {
+             $location = $location_object->item(0)->nodeValue;
+         }
+         // url is optional
+         $url = null;
+         $url_object = $happeningObj->element->getElementsByTagName('url');
+         if($url_object->length > 0) {
+             $url = $url_object->item(0)->nodeValue;
+         }
          switch ($activity->verb) {
          case ActivityVerb::POST:
-             // FIXME: get startTime, endTime, location and URL
-             return Happening::saveNew($actor,
-                                       $start_time,
-                                       $end_time,
-                                       $happeningObj->title,
-                                       null,
-                                       $happeningObj->summary,
-                                       null,
-                                       $options);
+               // FIXME: get startTime, endTime, location and URL
+             $notice = Happening::saveNew($actor,
+                                          $start_time,
+                                          $end_time,
+                                          $happeningObj->title,
+                                          $location,
+                                          $happeningObj->summary,
+                                          $url,
+                                          $options);
              break;
          case RSVP::POSITIVE:
          case RSVP::NEGATIVE:
index cc9dbfd6c9f4197a6e2dd6e90214c8060586dc62,520347eeb629a731105e4f5cb9df745ec2931ccd..5c74bfee4f2e2a552f48f8faacd8690323d9ce48
@@@ -107,45 -91,57 +91,80 @@@ class RSVP extends Managed_DataObjec
          );
      }
  
+     static public function beforeSchemaUpdate()
+     {
+         $table = strtolower(get_called_class());
+         $schema = Schema::get();
+         $schemadef = $schema->getTableDef($table);
+         // 2015-12-31 RSVPs refer to Happening by event_uri now, not event_id. Let's migrate!
+         if (isset($schemadef['fields']['event_uri'])) {
+             // We seem to have already migrated, good!
+             return;
+         }
+         // this is a "normal" upgrade from StatusNet for example
+         echo "\nFound old $table table, upgrading it to add 'event_uri' field...";
+         $schemadef['fields']['event_uri'] = array('type' => 'varchar', 'length' => 191, 'not null' => true, 'description' => 'Event URI');
+         $schema->ensureTable($table, $schemadef);
+         $rsvp = new RSVP();
+         $rsvp->find();
+         while ($rsvp->fetch()) {
+             $event = Happening::getKV('id', $rsvp->event_id);
+             if (!$event instanceof Happening) {
+                 continue;
+             }
+             $orig = clone($rsvp);
+             $rsvp->event_uri = $event->uri;
+             $rsvp->updateWithKeys($orig);
+         }
+         print "DONE.\n";
+         print "Resuming core schema upgrade...";
+     }
      function saveNew($profile, $event, $verb, $options=array())
      {
 -        if (array_key_exists('uri', $options)) {
 -            $other = RSVP::getKV('uri', $options['uri']);
 -            if (!empty($other)) {
 -                // TRANS: Client exception thrown when trying to save an already existing RSVP ("please respond").
 -                throw new ClientException(_m('RSVP already exists.'));
 -            }
 +        $eventNotice = $event->getNotice();
 +        $options = array_merge(array('source' => 'web'), $options);
 +
 +        $act = new Activity();
 +        $act->type    = ActivityObject::ACTIVITY;
 +        $act->verb    = $verb;
 +        $act->time    = $options['created'] ? strtotime($options['created']) : time();
 +        $act->title   = _m("RSVP");
 +        $act->actor   = $profile->asActivityObject();
 +        $act->target  = $eventNotice->asActivityObject();
 +        $act->objects = array(clone($act->target));
 +        $act->content = RSVP::toHTML($profile, $event, self::codeFor($verb));
 +
 +        $act->id = common_local_url('showrsvp', array('id' => UUID::gen()));
 +        $act->link = $act->id;
 +
 +        $saved = Notice::saveActivity($act, $profile, $options);
 +
 +        return $saved;
 +    }
 +
 +    function saveNewFromNotice($notice, $event, $verb)
 +    {
 +        $other = RSVP::getKV('uri', $notice->uri);
 +        if (!empty($other)) {
 +            // TRANS: Client exception thrown when trying to save an already existing RSVP ("please respond").
 +            throw new ClientException(_m('RSVP already exists.'));
          }
  
-         $other = RSVP::pkeyGet(array('profile_id' => $profile->id,
-                                      'event_id' => $event->id));
-         if (!empty($other)) {
 +        $profile = $notice->getProfile();
 +
+         try {
+             $other = RSVP::getByKeys( [ 'profile_id' => $profile->getID(),
+                                         'event_uri' => $event->getUri(),
+                                       ] );
              // TRANS: Client exception thrown when trying to save an already existing RSVP ("please respond").
-             throw new ClientException(_m('RSVP already exists.'));
+             throw new AlreadyFulfilledException(_m('RSVP already exists.'));
+         } catch (NoResultException $e) {
+             // No previous RSVP, so go ahead and add.
          }
  
          $rsvp = new RSVP();
  
          $rsvp->insert();
  
-         self::blow('rsvp:for-event:%s', $event->id);
+         self::blow('rsvp:for-event:%s', $event->getUri());
  
 -        // XXX: come up with something sexier
 -
 -        $content = $rsvp->asString();
 -
 -        $rendered = $rsvp->asHTML();
 -
 -        $options = array_merge(array('object_type' => $verb),
 -                               $options);
 -
 -        if (!array_key_exists('uri', $options)) {
 -            $options['uri'] = $rsvp->uri;
 -        }
 -
 -        $eventNotice = $event->getNotice();
 -
 -        if (!empty($eventNotice)) {
 -            $options['reply_to'] = $eventNotice->getID();
 -        }
 -
 -        $saved = Notice::saveNew($profile->getID(),
 -                                 $content,
 -                                 array_key_exists('source', $options) ?
 -                                 $options['source'] : 'web',
 -                                 $options);
 -
 -        return $saved;
 +        return $rsvp;
      }
  
      function codeFor($verb)