]> git.mxchange.org Git - quix0rs-gnu-social.git/commitdiff
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!

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

index 5c4abc41d0200918cf9d6ea01480502ce014e7f4..01e6da902cefefa5a38f0f57505dafa421dcd138 100644 (file)
@@ -125,7 +125,11 @@ class EventPlugin extends MicroAppPlugin
     }
 
     function types() {
-        return array(Happening::OBJECT_TYPE,
+        return array(Happening::OBJECT_TYPE);
+    }
+
+    function verbs() {
+        return array(ActivityVerb::POST,
                      RSVP::POSITIVE,
                      RSVP::NEGATIVE,
                      RSVP::POSSIBLE);
@@ -189,8 +193,6 @@ class EventPlugin extends MicroAppPlugin
             $url = $url_object->item(0)->nodeValue;
         }
 
-        $notice = null;
-
         switch ($activity->verb) {
         case ActivityVerb::POST:
                // FIXME: get startTime, endTime, location and URL
@@ -205,6 +207,22 @@ class EventPlugin extends MicroAppPlugin
             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)) {
@@ -212,14 +230,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 520347eeb629a731105e4f5cb9df745ec2931ccd..5c74bfee4f2e2a552f48f8faacd8690323d9ce48 100644 (file)
@@ -126,14 +126,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();
+
         try {
             $other = RSVP::getByKeys( [ 'profile_id' => $profile->getID(),
                                         'event_uri' => $event->getUri(),
@@ -146,54 +169,19 @@ class RSVP extends Managed_DataObject
 
         $rsvp = new RSVP();
 
-        $rsvp->id          = UUID::gen();
-        $rsvp->profile_id  = $profile->getID();
-        $rsvp->event_uri    = $event->getUri();
-        $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));
-        }
+        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);
+        $rsvp->created     = $notice->created;
+        $rsvp->uri         = $notice->uri;
 
         $rsvp->insert();
 
         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)