]> git.mxchange.org Git - quix0rs-gnu-social.git/commitdiff
Image file attachment support for Yammer import
authorBrion Vibber <brion@pobox.com>
Wed, 22 Sep 2010 00:08:40 +0000 (17:08 -0700)
committerBrion Vibber <brion@pobox.com>
Tue, 28 Sep 2010 14:44:21 +0000 (07:44 -0700)
plugins/YammerImport/sn_yammerclient.php
plugins/YammerImport/yamdump.php
plugins/YammerImport/yammer-import.php
plugins/YammerImport/yammerimporter.php

index 21caa7b7caae23c7e66e20e09715cd29c93d03a0..f7382abae19c1e66eb7c3a773898c629954e77bd 100644 (file)
@@ -38,25 +38,34 @@ class SN_YammerClient
     }
 
     /**
-     * Make an HTTP hit with OAuth headers and return the response body on success.
+     * Make an HTTP GET request with OAuth headers and return an HTTPResponse
+     * with the returned body and codes.
      *
-     * @param string $path URL chunk for the API method
-     * @param array $params
-     * @return array
+     * @param string $url
+     * @return HTTPResponse
      *
-     * @throws Exception for HTTP error
+     * @throws Exception on low-level network error
      */
-    protected function fetch($path, $params=array())
+    protected function httpGet($url)
     {
-        $url = $this->apiBase . '/' . $path;
-        if ($params) {
-            $url .= '?' . http_build_query($params, null, '&');
-        }
         $headers = array('Authorization: ' . $this->authHeader());
 
         $client = HTTPClient::start();
-        $response = $client->get($url, $headers);
+        return $client->get($url, $headers);
+    }
 
+    /**
+     * Make an HTTP GET request with OAuth headers and return the response body
+     * on success.
+     *
+     * @param string $url
+     * @return string
+     *
+     * @throws Exception on low-level network or HTTP error
+     */
+    public function fetchUrl($url)
+    {
+        $response = $this->httpGet($url);
         if ($response->isOk()) {
             return $response->getBody();
         } else {
@@ -64,6 +73,24 @@ class SN_YammerClient
         }
     }
 
+    /**
+     * Make an HTTP hit with OAuth headers and return the response body on success.
+     *
+     * @param string $path URL chunk for the API method
+     * @param array $params
+     * @return string
+     *
+     * @throws Exception on low-level network or HTTP error
+     */
+    protected function fetchApi($path, $params=array())
+    {
+        $url = $this->apiBase . '/' . $path;
+        if ($params) {
+            $url .= '?' . http_build_query($params, null, '&');
+        }
+        return $this->fetchUrl($url);
+    }
+
     /**
      * Hit the main Yammer API point and decode returned JSON data.
      *
@@ -75,7 +102,7 @@ class SN_YammerClient
      */
     protected function api($method, $params=array())
     {
-        $body = $this->fetch("api/v1/$method.json", $params);
+        $body = $this->fetchApi("api/v1/$method.json", $params);
         $data = json_decode($body, true);
         if (!$data) {
             throw new Exception("Invalid JSON response from Yammer API");
index 96127dd1762a8c55a6972db1f86ce7e7cac57bbb..ef392759810c3bc729b60e7aa36f19d3d27dd0fe 100644 (file)
@@ -11,7 +11,7 @@ require INSTALLDIR . "/scripts/commandline.inc";
 // temp stuff
 require 'yam-config.php';
 $yam = new SN_YammerClient($consumerKey, $consumerSecret, $token, $tokenSecret);
-$imp = new YammerImporter();
+$imp = new YammerImporter($yam);
 
 $data = $yam->messages();
 var_dump($data);
index d6ec9751324c52fcbba6a62813a69e28451e4cb5..da99c48e903c5f2e085a6950ecf728692631edec 100644 (file)
@@ -11,7 +11,7 @@ require INSTALLDIR . "/scripts/commandline.inc";
 // temp stuff
 require 'yam-config.php';
 $yam = new SN_YammerClient($consumerKey, $consumerSecret, $token, $tokenSecret);
-$imp = new YammerImporter();
+$imp = new YammerImporter($yam);
 
 $data = $yam->messages();
 /*
index 08cbbf790bad57b6dd2a636484dbee96accd708d..48bb5dda299ae39600cebdfa1b4492c9f36034c1 100644 (file)
  */
 class YammerImporter
 {
-
+    protected $client;
     protected $users=array();
     protected $groups=array();
     protected $notices=array();
 
+    function __construct(SN_YammerClient $client)
+    {
+        $this->client = $client;
+    }
+
     /**
      * Load or create an imported profile from Yammer data.
      * 
@@ -95,17 +100,39 @@ class YammerImporter
         if ($noticeId) {
             return Notice::staticGet('id', $noticeId);
         } else {
-            $notice = Notice::saveNew($data['profile'],
-                                      $data['content'],
+            $content = $data['content'];
+            $user = User::staticGet($data['profile']);
+
+            // Fetch file attachments and add the URLs...
+            $uploads = array();
+            foreach ($data['attachments'] as $url) {
+                try {
+                    $upload = $this->saveAttachment($url, $user);
+                    $content .= ' ' . $upload->shortUrl();
+                    $uploads[] = $upload;
+                } catch (Exception $e) {
+                    common_log(LOG_ERROR, "Error importing Yammer attachment: " . $e->getMessage());
+                }
+            }
+
+            // Here's the meat! Actually save the dang ol' notice.
+            $notice = Notice::saveNew($user->id,
+                                      $content,
                                       $data['source'],
                                       $data['options']);
+
+            // Save "likes" as favorites...
             foreach ($data['faves'] as $nickname) {
                 $user = User::staticGet('nickname', $nickname);
                 if ($user) {
                     Fave::addNew($user->getProfile(), $notice);
                 }
             }
-            // @fixme attachments?
+
+            // And finally attach the upload records...
+            foreach ($uploads as $upload) {
+                $upload->attachToNotice($notice);
+            }
             $this->recordImportedNotice($data['orig_id'], $notice->id);
             return $notice;
         }
@@ -219,8 +246,14 @@ class YammerImporter
             $faves[] = $liker['permalink'];
         }
 
-        // Parse/save rendered text?
-        // @todo attachments?
+        $attachments = array();
+        foreach ($item['attachments'] as $attach) {
+            if ($attach['type'] == 'image') {
+                $attachments[] = $attach['image']['url'];
+            } else {
+                common_log(LOG_WARNING, "Unrecognized Yammer attachment type: " . $attach['type']);
+            }
+        }
 
         return array('orig_id' => $origId,
                      'orig_url' => $origUrl,
@@ -228,7 +261,8 @@ class YammerImporter
                      'content' => $content,
                      'source' => $source,
                      'options' => $options,
-                     'faves' => $faves);
+                     'faves' => $faves,
+                     'attachments' => $attachments);
     }
 
     private function findImportedUser($origId)
@@ -326,4 +360,34 @@ class YammerImporter
 
         $dest->setOriginal($filename);
     }
+
+    /**
+     * Fetch an attachment from Yammer and save it into our system.
+     * Unlike avatars, the attachment URLs are guarded by authentication,
+     * so we need to run the HTTP hit through our OAuth API client.
+     *
+     * @param string $url
+     * @param User $user
+     * @return MediaFile
+     *
+     * @throws Exception on low-level network or HTTP error
+     */
+    private function saveAttachment($url, User $user)
+    {
+        // Fetch the attachment...
+        // WARNING: file must fit in memory here :(
+        $body = $this->client->fetchUrl($url);
+
+        // Save to a temporary file and shove it into our file-attachment space...
+        $temp = tmpfile();
+        fwrite($temp, $body);
+        try {
+            $upload = MediaFile::fromFileHandle($temp, $user);
+            fclose($temp);
+            return $upload;
+        } catch (Exception $e) {
+            fclose($temp);
+            throw $e;
+        }
+    }
 }