]> git.mxchange.org Git - quix0rs-gnu-social.git/commitdiff
Use real attachments for JSON output
authorEvan Prodromou <evan@e14n.com>
Wed, 5 Jun 2013 13:39:13 +0000 (09:39 -0400)
committerEvan Prodromou <evan@e14n.com>
Wed, 5 Jun 2013 13:39:43 +0000 (09:39 -0400)
classes/Notice.php
lib/activity.php
lib/activityobject.php

index 3891f431e460a04cd947e80edfb78dba3d8b43d3..beb6c4fe6af101e4053c67b5b00132cbdb0824b8 100644 (file)
@@ -1544,9 +1544,9 @@ class Notice extends Managed_DataObject
             $attachments = $this->attachments();
 
             foreach ($attachments as $attachment) {
-                $enclosure = $attachment->getEnclosure();
-                if ($enclosure) {
-                    $act->enclosures[] = $enclosure;
+                // Save local attachments
+                if (!empty($attachment->filename)) {
+                    $act->attachments[] = ActivityObject::fromFile($attachment);
                 }
             }
 
index 2ec2176d39f735d8140e5c5959fbd1a75491136d..ecae96b50ab0951f9e6fa2af387c11a42af2797c 100644 (file)
@@ -100,6 +100,7 @@ class Activity
     public $title;   // title of the activity
     public $categories = array(); // list of AtomCategory objects
     public $enclosures = array(); // list of enclosure URL references
+    public $attachments = array(); // list of attachments
 
     public $extra = array(); // extra elements as array(tag, attrs, content)
     public $source;  // ActivitySource object representing 'home feed'
@@ -397,47 +398,11 @@ class Activity
                 $activity['object']['objectType'] = 'activity';
             }
 
-
-            // Instead of adding enclosures as an extension to JSON
-            // Activities, it seems like we should be using the
-            // attachements property of ActivityObject
-
-            $attachments = array();
-
-            // XXX: OK, this is kinda cheating. We should probably figure out
-            // what kind of objects these are based on mime-type and then
-            // create specific object types. Right now this rely on
-            // duck-typing.  Also, we should include an embed code for
-            // video attachments.
-
-            foreach ($this->enclosures as $enclosure) {
-
-                if (is_string($enclosure)) {
-
-                    $attachments[]['id']  = $enclosure;
-
-                } else {
-
-                    $attachments[]['id']  = $enclosure->url;
-
-                    $mediaLink = new ActivityStreamsMediaLink(
-                        $enclosure->url,
-                        null,
-                        null,
-                        $enclosure->mimetype
-                        // XXX: Add 'size' as an extension to MediaLink?
-                    );
-
-                    $attachments[]['mediaLink'] = $mediaLink->asArray(); // extension
-
-                    if ($enclosure->title) {
-                        $attachments[]['displayName'] = $enclosure->title;
-                    }
+            foreach ($this->attachments as $attachment) {
+                if (empty($activity['object']['attachments'])) {
+                    $activity['object']['attachments'] = array();
                 }
-            }
-
-            if (!empty($attachments)) {
-                $activity['object']['attachments'] = $attachments;
+                $activity['object']['attachments'][] = $attachment->asArray();
             }
         }
         
index 0eff87146f42a67c034fc5424fe65710482290de..1a81d3068746db08e13fc8dcfe90fafbfcbd8d99 100644 (file)
@@ -70,6 +70,7 @@ class ActivityObject
     // ^^^^^^^^^^ tea!
     const ACTIVITY = 'http://activitystrea.ms/schema/1.0/activity';
     const SERVICE   = 'http://activitystrea.ms/schema/1.0/service';
+    const IMAGE     = 'http://activitystrea.ms/schema/1.0/image';
 
     // Atom elements we snarf
 
@@ -111,6 +112,8 @@ class ActivityObject
     public $description;
     public $extra = array();
 
+    public $stream;
+
     /**
      * Constructor
      *
@@ -549,6 +552,46 @@ class ActivityObject
         return $object;
     }
 
+    static function fromFile(File $file)
+    {
+        $object = new ActivityObject();
+
+        if (Event::handle('StartActivityObjectFromFile', array($file, &$object))) {
+
+            $object->type = self::mimeTypeToObjectType($file->mimetype);
+            $object->id   = TagURI::mint(sprintf("file:%d", $file->id));
+            $object->link = common_local_url('attachment', array('id' => $file->id));
+
+            if ($file->title) {
+                $object->title = $file->title;
+            }
+
+            if ($file->date) {
+                $object->date = $file->date;
+            }
+
+            $thumbnail = $file->getThumbnail();
+
+            if (!empty($thumbnail)) {
+                $object->thumbnail = $thumbnail;
+            }
+
+            switch ($object->type) {
+            case ActivityObject::IMAGE:
+                $object->largerImage = $file->url;
+                break;
+            case ActivityObject::VIDEO:
+            case ActivityObject::AUDIO:
+                $object->stream = $file->url;
+                break;
+            }
+
+            Event::handle('EndActivityObjectFromFile', array($file, &$object));
+        }
+
+        return $object;
+    }
+
     function outputTo($xo, $tag='activity:object')
     {
         if (!empty($tag)) {
@@ -809,6 +852,34 @@ class ActivityObject
                 $object['portablecontacts_net'] = array_filter($this->poco->asArray());
             }
 
+            if (!empty($this->thumbnail)) {
+                if (is_string($this->thumbnail)) {
+                    $object['image'] = array('url' => $this->thumbnail);
+                } else {
+                    $object['image'] = array('url' => $this->thumbnail->url);
+                    if ($this->thumbnail->width) {
+                        $object['image']['width'] = $this->thumbnail->width;
+                    }
+                    if ($this->thumbnail->height) {
+                        $object['image']['height'] = $this->thumbnail->height;
+                    }
+                }
+            }
+
+            switch ($object->type) {
+            case self::IMAGE:
+                if (!empty($this->largerImage)) {
+                    $object['fullImage'] = array('url' => $this->largerImage);
+                }
+                break;
+            case self::AUDIO:
+            case self::VIDEO:
+                if (!empty($this->stream)) {
+                    $object['stream'] = array('url' => $this->stream);
+                }
+                break;
+            }
+
             Event::handle('EndActivityObjectOutputJson', array($this, &$object));
         }
         return array_filter($object);
@@ -822,4 +893,25 @@ class ActivityObject
             return $type;
         }
     }
+
+    static function mimeTypeToObjectType($mimeType) {
+        $ot = null;
+        $parts = explode('/', $mimeType);
+
+        switch ($parts[0]) {
+        case 'image':
+            $ot = self::IMAGE;
+            break;
+        case 'audio':
+            $ot = self::AUDIO;
+            break;
+        case 'video':
+            $ot = self::VIDEO;
+            break;
+        default:
+            $ot = self::FILE;
+        }
+
+        return $ot;
+    }
 }