]> git.mxchange.org Git - quix0rs-gnu-social.git/blobdiff - lib/activity.php
Add image to JSON ActivityObject and title + links to the JSON document
[quix0rs-gnu-social.git] / lib / activity.php
index 802d09304a7c3750e8296d3cad062373fae08b24..e6fefca28f9a9523a9dfeb037d7e2117674ca899 100644 (file)
@@ -205,18 +205,19 @@ class Activity
             // the surrounding feed.
             $this->actor = new ActivityObject($authorEl);
 
-        } else if (!empty($feed) && $authorEl = $this->_child($feed, self::AUTHOR,
-                                                              self::ATOM)) {
-
-            // If there's no <atom:author> on the entry, it's safe to assume
-            // the containing feed's authorship info applies.
-            $this->actor = new ActivityObject($authorEl);
         } else if (!empty($feed) &&
                    $subjectEl = $this->_child($feed, self::SUBJECT)) {
 
             // Feed subject is used for things like groups.
             // Should actually possibly not be interpreted as an actor...?
             $this->actor = new ActivityObject($subjectEl);
+
+        } else if (!empty($feed) && $authorEl = $this->_child($feed, self::AUTHOR,
+                                                              self::ATOM)) {
+
+            // If there's no <atom:author> on the entry, it's safe to assume
+            // the containing feed's authorship info applies.
+            $this->actor = new ActivityObject($authorEl);
         }
 
         $contextEl = $this->_child($entry, self::CONTEXT);
@@ -336,6 +337,59 @@ class Activity
         return null;
     }
 
+    /**
+     * Returns an array based on this activity suitable
+     * for encoding as a JSON object
+     *
+     * @return array $activity
+     */
+
+    function asArray()
+    {
+        $activity = array();
+
+        // actor
+        $activity['actor'] = $this->actor->asArray();
+
+        // body
+        $activity['body'] = $this->content;
+
+        // generator <--- might be useful; might be too much junk
+
+        // icon <-- should we use this?
+
+        // object
+        if ($this->verb == ActivityVerb::POST && count($this->objects) == 1) {
+            $activity['object'] = $this->objects[0]->asArray();
+        } else {
+            $activity['object'] = array();
+            foreach($this->objects as $object) {
+                $activity['object'][] = $object->asArray();
+            }
+        }
+
+        $activity['postedTime'] = self::iso8601Date($this->time); // Change to exactly be RFC3339?
+
+        // provider <--- again not sure we should use this
+
+        // target
+        if (!empty($this->target)) {
+            $activity['target'] = $this->target->asArray();
+        }
+
+        // title
+        $activity['title'] = $this->title;
+
+        // updatedTime <-- should we use? spec says activity MAY have this
+
+        // verb
+        $activity['verb'] = $this->verb;
+
+        // TODO: extensions (ActivityContext, OStatus stuff, etc.)
+
+        return array_filter($activity);
+    }
+
     function asString($namespace=false, $author=true, $source=false)
     {
         $xs = new XMLStringer(true);
@@ -370,11 +424,11 @@ class Activity
             $xs->element('title', null, $this->title);
 
             $xs->element('content', array('type' => 'html'), $this->content);
-            
+
             if (!empty($this->summary)) {
                 $xs->element('summary', null, $this->summary);
             }
-            
+
             if (!empty($this->link)) {
                 $xs->element('link', array('rel' => 'alternate',
                                            'type' => 'text/html'),
@@ -386,12 +440,24 @@ class Activity
         $xs->element('activity:verb', null, $this->verb);
 
         $published = self::iso8601Date($this->time);
-            
+
         $xs->element('published', null, $published);
         $xs->element('updated', null, $published);
-            
+
         if ($author) {
             $this->actor->outputTo($xs, 'author');
+
+            // XXX: Remove <activity:actor> ASAP! Author information
+            // has been moved to the author element in the Activity
+            // Streams spec. We're outputting actor only for backward
+            // compatibility with clients that can only parse
+            // activities based on older versions of the spec.
+
+            $depMsg = 'Deprecation warning: activity:actor is present '
+                . 'only for backward compatibility. It will be '
+                . 'removed in the next version of StatusNet.';
+            $xs->comment($depMsg);
+            $this->actor->outputTo($xs, 'activity:actor');
         }
 
         if ($this->verb != ActivityVerb::POST || count($this->objects) != 1) {
@@ -458,7 +524,7 @@ class Activity
         }
 
         // can be either URLs or enclosure objects
-        
+
         foreach ($this->enclosures as $enclosure) {
             if (is_string($enclosure)) {
                 $xs->element('link', array('rel' => 'enclosure',
@@ -479,7 +545,7 @@ class Activity
 
         if ($source && !empty($this->source)) {
             $xs->elementStart('source');
-           
+
             $xs->element('id', null, $this->source->id);
             $xs->element('title', null, $this->source->title);
 
@@ -488,7 +554,7 @@ class Activity
                                            'type' => 'text/html',
                                            'href' => $this->source->links['alternate']));
             }
-           
+
             if (array_key_exists('self', $this->source->links)) {
                 $xs->element('link', array('rel' => 'self',
                                            'type' => 'application/atom+xml',
@@ -507,7 +573,7 @@ class Activity
             if (!empty($this->source->updated)) {
                 $xs->element('updated', null, $this->source->updated);
             }
-           
+
             $xs->elementEnd('source');
         }
 
@@ -524,7 +590,7 @@ class Activity
         }
 
         // For throwing in extra elements; used for statusnet:notice_info
-       
+
         foreach ($this->extra as $el) {
             list($tag, $attrs, $content) = $el;
             $xs->element($tag, $attrs, $content);