]> git.mxchange.org Git - quix0rs-gnu-social.git/blobdiff - plugins/YammerImport/lib/yammerimporter.php
Merge remote-tracking branch 'upstream/master'
[quix0rs-gnu-social.git] / plugins / YammerImport / lib / yammerimporter.php
index 0425b8b04e1aee0de35f255311716b61ef4baddb..abd41dd99e271028e44acdf54d1524e5fd8e6695 100644 (file)
@@ -20,6 +20,8 @@
 /**
  * Basic client class for Yammer's OAuth/JSON API.
  *
+ * Depends on Favorite plugin
+ *
  * @package YammerImportPlugin
  * @author Brion Vibber <brion@status.net>
  */
@@ -27,27 +29,38 @@ class YammerImporter
 {
     protected $client;
 
-    function __construct(SN_YammerClient $client)
+    function __construct(SNYammerClient $client)
     {
         $this->client = $client;
     }
 
     /**
      * Load or create an imported profile from Yammer data.
-     * 
+     *
      * @param object $item loaded JSON data for Yammer importer
      * @return Profile
      */
     function importUser($item)
     {
         $data = $this->prepUser($item);
+        $nickname = $data['options']['nickname'];
 
         $profileId = $this->findImportedUser($data['orig_id']);
         if ($profileId) {
-            return Profile::staticGet('id', $profileId);
+            return Profile::getKV('id', $profileId);
         } else {
-            $user = User::register($data['options']);
-            $profile = $user->getProfile();
+            $user = User::getKV('nickname', $nickname);
+
+            if ($user instanceof User) {
+                common_log(LOG_WARNING, "Copying Yammer profile info onto existing user $nickname");
+                $profile = $user->getProfile();
+                $this->savePropertiesOn($profile, $data['options'],
+                        array('fullname', 'homepage', 'bio', 'location'));
+            } else {
+                $user = User::register($data['options']);
+                $profile = $user->getProfile();
+            }
+
             if ($data['avatar']) {
                 try {
                     $this->saveAvatar($data['avatar'], $profile);
@@ -69,12 +82,21 @@ class YammerImporter
     function importGroup($item)
     {
         $data = $this->prepGroup($item);
+        $nickname = $data['options']['nickname'];
 
         $groupId = $this->findImportedGroup($data['orig_id']);
         if ($groupId) {
-            return User_group::staticGet('id', $groupId);
+            return User_group::getKV('id', $groupId);
         } else {
-            $group = User_group::register($data['options']);
+            $local = Local_group::getKV('nickname', $nickname);
+            if ($local) {
+                common_log(LOG_WARNING, "Copying Yammer group info onto existing group $nickname");
+                $group = User_group::getKV('id', $local->group_id);
+                $this->savePropertiesOn($group, $data['options'],
+                        array('fullname', 'description'));
+            } else {
+                $group = User_group::register($data['options']);
+            }
             if ($data['avatar']) {
                 try {
                     $this->saveAvatar($data['avatar'], $group);
@@ -87,6 +109,19 @@ class YammerImporter
         }
     }
 
+    private function savePropertiesOn($target, $options, $propList)
+    {
+        $changed = 0;
+        $orig = clone($target);
+        foreach ($propList as $prop) {
+            if (!empty($options[$prop]) && $target->$prop != $options[$prop]) {
+                $target->$prop = $options[$prop];
+                $changed++;
+            }
+        }
+        $target->update($orig);
+    }
+
     /**
      * Load or create an imported notice from Yammer data.
      *
@@ -99,10 +134,11 @@ class YammerImporter
 
         $noticeId = $this->findImportedNotice($data['orig_id']);
         if ($noticeId) {
-            return Notice::staticGet('id', $noticeId);
+            return Notice::getKV('id', $noticeId);
         } else {
+            $notice = Notice::getKV('uri', $data['options']['uri']);
             $content = $data['content'];
-            $user = User::staticGet($data['profile']);
+            $user = User::getKV($data['profile']);
 
             // Fetch file attachments and add the URLs...
             $uploads = array();
@@ -124,9 +160,14 @@ class YammerImporter
 
             // Save "likes" as favorites...
             foreach ($data['faves'] as $nickname) {
-                $user = User::staticGet('nickname', $nickname);
-                if ($user) {
-                    Fave::addNew($user->getProfile(), $notice);
+                $user = User::getKV('nickname', $nickname);
+                if ($user instanceof User) {
+                    try {
+                        Fave::addNew($user->getProfile(), $notice);
+                    } catch (Exception $e) {
+                        // failed, let's move to the next
+                        common_debug('YammerImport failed favoriting a notice: '.$e->getMessage());
+                    }
                 }
             }
 
@@ -148,7 +189,8 @@ class YammerImporter
     function prepUser($item)
     {
         if ($item['type'] != 'user') {
-            throw new Exception('Wrong item type sent to Yammer user import processing.');
+            // TRANS: Exception thrown when a non-user item type is used, but expected.
+            throw new Exception(_m('Wrong item type sent to Yammer user import processing.'));
         }
 
         $origId = $item['id'];
@@ -195,6 +237,7 @@ class YammerImporter
             $bio[] = $item['summary'];
         }
         if (!empty($item['expertise'])) {
+            // TRANS: Used as a prefix for the Yammer expertise field contents.
             $bio[] = _m('Expertise:') . ' ' . $item['expertise'];
         }
         $options['bio'] = implode("\n\n", $bio);
@@ -230,7 +273,8 @@ class YammerImporter
     function prepGroup($item)
     {
         if ($item['type'] != 'group') {
-            throw new Exception('Wrong item type sent to Yammer group import processing.');
+            // TRANS: Exception thrown when a non-group item type is used, but expected.
+            throw new Exception(_m('Wrong item type sent to Yammer group import processing.'));
         }
 
         $origId = $item['id'];
@@ -245,12 +289,14 @@ class YammerImporter
 
         $avatar = $item['mugshot_url']; // as with user profiles...
 
-
         $options['mainpage'] = common_local_url('showgroup',
                                    array('nickname' => $options['nickname']));
 
-        // @fixme what about admin user for the group?
-        // bio? homepage etc? aliases?
+        // Set some default vals or User_group::register will whine
+        $options['homepage'] = '';
+        $options['location'] = '';
+        $options['aliases'] = array();
+        // @todo FIXME: What about admin user for the group?
 
         $options['local'] = true;
         return array('orig_id' => $origId,
@@ -268,7 +314,8 @@ class YammerImporter
     function prepNotice($item)
     {
         if (isset($item['type']) && $item['type'] != 'message') {
-            throw new Exception('Wrong item type sent to Yammer message import processing.');
+            // TRANS: Exception thrown when a non-message item type is used, but expected.
+            throw new Exception(_m('Wrong item type sent to Yammer message import processing.'));
         }
 
         $origId = $item['id'];
@@ -287,14 +334,14 @@ class YammerImporter
         }
         $options['created'] = $this->timestamp($item['created_at']);
 
-        if ($item['group_id']) {
+        if (!empty($item['group_id'])) {
             $groupId = $this->findImportedGroup($item['group_id']);
             if ($groupId) {
                 $options['groups'] = array($groupId);
 
                 // @fixme if we see a group link inline, don't add this?
-                $group = User_group::staticGet('id', $groupId);
-                if ($group) {
+                $group = User_group::getKV('id', $groupId);
+                if ($group instanceof User_group) {
                     $content .= ' !' . $group->nickname;
                 }
             }
@@ -327,19 +374,19 @@ class YammerImporter
 
     private function findImportedUser($origId)
     {
-        $map = Yammer_user::staticGet('id', $origId);
+        $map = Yammer_user::getKV('id', $origId);
         return $map ? $map->user_id : null;
     }
 
     private function findImportedGroup($origId)
     {
-        $map = Yammer_group::staticGet('id', $origId);
+        $map = Yammer_group::getKV('id', $origId);
         return $map ? $map->group_id : null;
     }
 
     private function findImportedNotice($origId)
     {
-        $map = Yammer_notice::staticGet('id', $origId);
+        $map = Yammer_notice::getKV('id', $origId);
         return $map ? $map->notice_id : null;
     }
 
@@ -395,24 +442,33 @@ class YammerImporter
         $url = preg_replace('/_small(\..*?)$/', '$1', $url);
 
         if (!common_valid_http_url($url)) {
-            throw new ServerException(sprintf(_m("Invalid avatar URL %s."), $url));
+            // TRANS: Server exception thrown when an avatar URL is invalid.
+            // TRANS: %s is the invalid avatar URL.
+            throw new ServerException(sprintf(_m('Invalid avatar URL %s.'), $url));
         }
 
         // @fixme this should be better encapsulated
         // ripped from oauthstore.php (for old OMB client)
-        $temp_filename = tempnam(sys_get_temp_dir(), 'listener_avatar');
-        if (!copy($url, $temp_filename)) {
-            throw new ServerException(sprintf(_m("Unable to fetch avatar from %s."), $url));
-        }
+        $temp_filename = tempnam(common_get_temp_dir(), 'listener_avatar');
+        try {
+            if (!copy($url, $temp_filename)) {
+                // TRANS: Server exception thrown when an avatar could not be fetched.
+                // TRANS: %s is the failed avatar URL.
+                throw new ServerException(sprintf(_m('Unable to fetch avatar from %s.'), $url));
+            }
 
-        $id = $dest->id;
-        // @fixme should we be using different ids?
-        $imagefile = new ImageFile($id, $temp_filename);
-        $filename = Avatar::filename($id,
-                                     image_type_to_extension($imagefile->type),
-                                     null,
-                                     common_timestamp());
-        rename($temp_filename, Avatar::path($filename));
+            $id = $dest->id;
+            // @fixme should we be using different ids?
+            $imagefile = new ImageFile($id, $temp_filename);
+            $filename = Avatar::filename($id,
+                                         image_type_to_extension($imagefile->type),
+                                         null,
+                                         common_timestamp());
+            rename($temp_filename, Avatar::path($filename));
+        } catch (Exception $e) {
+            unlink($temp_filename);
+            throw $e;
+        }
         // @fixme hardcoded chmod is lame, but seems to be necessary to
         // keep from accidentally saving images from command-line (queues)
         // that can't be read from web server, which causes hard-to-notice
@@ -445,7 +501,7 @@ class YammerImporter
         $temp = tmpfile();
         fwrite($temp, $body);
         try {
-            $upload = MediaFile::fromFileHandle($temp, $user);
+            $upload = MediaFile::fromFilehandle($temp, $user->getProfile());
             fclose($temp);
             return $upload;
         } catch (Exception $e) {