]> git.mxchange.org Git - quix0rs-gnu-social.git/commitdiff
Event plugin uses saveObjectFromActivity for RSVP
authorStephen Paul Weber <singpolyma@singpolyma.net>
Thu, 22 Oct 2015 17:10:50 +0000 (17:10 +0000)
committerStephen Paul Weber <singpolyma@singpolyma.net>
Thu, 22 Oct 2015 17:10:50 +0000 (17:10 +0000)
The Event plugin is still using a deprecated hook for saving extra data,
which makes it harder to hook into from other plugins.  This commit
fixes it to use the new hook for at least RSVPs.

NOTE: this plugin incorrectly sets the object_type of RSVP notices to
their verb.  This was existing behaviour which I have preserved for
backwards-compatability.

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

index 9fa6e17fc3f2c0014cf98afb7aeb4a66b2be8b53..33f08d69c99e2abd467903d8b0129057d47a66c7 100644 (file)
@@ -118,6 +118,13 @@ class EventPlugin extends MicroAppPlugin
                      RSVP::POSSIBLE);
     }
 
+    function verbs() {
+        return array(ActivityVerb::POST,
+                     RSVP::POSITIVE,
+                     RSVP::NEGATIVE,
+                     RSVP::POSSIBLE);
+    }
+
     /**
      * Given a parsed ActivityStreams activity, save it into a notice
      * and other data structures.
@@ -142,22 +149,36 @@ class EventPlugin extends MicroAppPlugin
             throw new Exception(_m('Wrong type for object.'));
         }
 
-        $notice = null;
-
         switch ($activity->verb) {
         case ActivityVerb::POST:
-               // FIXME: get startTime, endTime, location and URL
-            $notice = Happening::saveNew($actor,
-                                         $start_time,
-                                         $end_time,
-                                         $happeningObj->title,
-                                         null,
-                                         $happeningObj->summary,
-                                         null,
-                                         $options);
+            // FIXME: get startTime, endTime, location and URL
+            return Happening::saveNew($actor,
+                                      $start_time,
+                                      $end_time,
+                                      $happeningObj->title,
+                                      null,
+                                      $happeningObj->summary,
+                                      null,
+                                      $options);
             break;
         case RSVP::POSITIVE:
         case RSVP::NEGATIVE:
+        case RSVP::POSSIBLE:
+            return Notice::saveActivity($activity, $actor, $options);
+            break;
+        default:
+            // TRANS: Exception thrown when event plugin comes across a undefined verb.
+            throw new Exception(_m('Unknown verb for events.'));
+        }
+    }
+
+    protected function saveObjectFromActivity(Activity $activity, Notice $stored, array $options=array())
+    {
+        $happeningObj = $activity->objects[0];
+
+        switch ($activity->verb) {
+        case RSVP::POSITIVE:
+        case RSVP::NEGATIVE:
         case RSVP::POSSIBLE:
             $happening = Happening::getKV('uri', $happeningObj->id);
             if (empty($happening)) {
@@ -165,14 +186,15 @@ class EventPlugin extends MicroAppPlugin
                 // TRANS: Exception thrown when trying to RSVP for an unknown event.
                 throw new Exception(_m('RSVP for unknown event.'));
             }
-            $notice = RSVP::saveNew($actor, $happening, $activity->verb, $options);
+            $object = RSVP::saveNewFromNotice($stored, $happening, $activity->verb);
+            // Our data model expects this
+            $stored->object_type = $activity->verb;
+            return $object;
             break;
         default:
-            // TRANS: Exception thrown when event plugin comes across a undefined verb.
-            throw new Exception(_m('Unknown verb for events.'));
+            common_log(LOG_ERR, 'Unknown verb for events.');
+            return NULL;
         }
-
-        return $notice;
     }
 
     /**
index 3e0efc689a6a5dcbab2c9ccf14bf70b31d12fedb..cc9dbfd6c9f4197a6e2dd6e90214c8060586dc62 100644 (file)
@@ -109,14 +109,37 @@ class RSVP extends Managed_DataObject
 
     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.'));
         }
 
+        $profile = $notice->getProfile();
+
         $other = RSVP::pkeyGet(array('profile_id' => $profile->id,
                                      'event_id' => $event->id));
 
@@ -127,54 +150,19 @@ class RSVP extends Managed_DataObject
 
         $rsvp = new RSVP();
 
-        $rsvp->id          = UUID::gen();
+        preg_match('/\/([^\/]+)\/*/', $notice->uri, $match);
+        $rsvp->id          = $match[1] ? $match[1] : UUID::gen();
         $rsvp->profile_id  = $profile->id;
         $rsvp->event_id    = $event->id;
-        $rsvp->response      = self::codeFor($verb);
-
-        if (array_key_exists('created', $options)) {
-            $rsvp->created = $options['created'];
-        } else {
-            $rsvp->created = common_sql_now();
-        }
-
-        if (array_key_exists('uri', $options)) {
-            $rsvp->uri = $options['uri'];
-        } else {
-            $rsvp->uri = common_local_url('showrsvp',
-                                        array('id' => $rsvp->id));
-        }
+        $rsvp->response    = self::codeFor($verb);
+        $rsvp->created     = $notice->created;
+        $rsvp->uri         = $notice->uri;
 
         $rsvp->insert();
 
         self::blow('rsvp:for-event:%s', $event->id);
 
-        // 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->id;
-        }
-
-        $saved = Notice::saveNew($profile->id,
-                                 $content,
-                                 array_key_exists('source', $options) ?
-                                 $options['source'] : 'web',
-                                 $options);
-
-        return $saved;
+        return $rsvp;
     }
 
     function codeFor($verb)