]> git.mxchange.org Git - quix0rs-gnu-social.git/commitdiff
Moved Avatar retrieval into Avatar class
authorMikael Nordfeldth <mmn@hethane.se>
Sun, 6 Oct 2013 13:54:06 +0000 (15:54 +0200)
committerMikael Nordfeldth <mmn@hethane.se>
Sun, 6 Oct 2013 13:55:06 +0000 (15:55 +0200)
Backwards compatible functions are still in Profile class.

actions/avatarbynickname.php
actions/avatarsettings.php
actions/foaf.php
classes/Avatar.php
classes/Profile.php
lib/activityobject.php
lib/avatarlink.php
lib/noavatarexception.php [new file with mode: 0644]
lib/noresultexception.php
plugins/TwitterBridge/lib/twitterimport.php

index 0922bd47ec2577c4964fd604f4e3ec98decd7553..c357cdf17e99567bf9ac8f25eaec1b16247a377c 100644 (file)
@@ -75,7 +75,7 @@ class AvatarbynicknameAction extends Action
             try {
                 $avatar = Avatar::getUploaded($profile);
                 $url = $avatar->displayUrl();
-            } catch (Exception $e) {
+            } catch (NoAvatarException $e) {
                 $url = Avatar::defaultImage(AVATAR_PROFILE_SIZE);
             }
         } else {
index bc671c621be1480b37a247f8f5320009e628b0b1..76974265e6dadce606c1ba556601cf26ed4eea13 100644 (file)
@@ -136,7 +136,7 @@ class AvatarsettingsAction extends SettingsAction
                                             'alt' => $user->nickname));
                 $this->elementEnd('div');
                 $this->elementEnd('li');
-            } catch (NoResultException $e) {
+            } catch (NoAvatarException $e) {
                 // No original avatar found!
             }
 
@@ -157,7 +157,7 @@ class AvatarsettingsAction extends SettingsAction
                     $this->submit('delete', _m('BUTTON','Delete'));
                 }
                 $this->elementEnd('li');
-            } catch (Exception $e) {
+            } catch (NoAvatarException $e) {
                 // No previously uploaded avatar to preview.
             }
 
index 9bb1df61c68d3e81d4c5086719c27d1ef25e8097..e0662aa2fa43cc53ecd526573941e657897db18d 100644 (file)
@@ -157,7 +157,7 @@ class FoafAction extends Action
             }
             $this->elementEnd('Image');
             $this->elementEnd('img');
-        } catch (Exception $e) {
+        } catch (NoAvatarException $e) {
             // No avatar for this user!
         }
 
index 3eada14c97f15014bbf11771a9ac4c84a187af87..c7f6a56b0420bcca6cbd5825404e85afb5e9b12d 100644 (file)
@@ -74,26 +74,69 @@ class Avatar extends Managed_DataObject
                 }
                 $avatar->delete();
             }
-        } catch (NoResultException $e) {
+        } catch (NoAvatarException $e) {
             // There are no avatars to delete, a sort of success.
         }
 
         return true;
     }
 
+    static protected $_avatars = array();
+
+    /*
+     * Get an avatar by profile. Currently can't call newSize with $height
+     */
+    public static function byProfile(Profile $target, $width=null, $height=null)
+    {
+        $width  = (int) floor($width);
+        $height = !is_null($height) ? (int) floor($height) : null;
+        if (is_null($height)) {
+            $height = $width;
+        }
+
+        $size = "{$width}x{$height}";
+        if (!isset(self::$_avatars[$target->id])) {
+            self::$_avatars[$target->id] = array();
+        } elseif (isset(self::$_avatars[$target->id][$size])){
+            return self::$_avatars[$target->id][$size];
+        }
+
+        $avatar = null;
+        if (Event::handle('StartProfileGetAvatar', array($target, $width, &$avatar))) {
+            $avatar = self::pkeyGet(
+                array(
+                    'profile_id' => $target->id,
+                    'width'      => $width,
+                    'height'     => $height,
+                )
+            );
+            Event::handle('EndProfileGetAvatar', array($target, $width, &$avatar));
+        }
+
+        if (is_null($avatar)) {
+            // Obviously we can't find an avatar, so let's resize the original!
+            $avatar = Avatar::newSize($target, $width);
+        } elseif (!($avatar instanceof Avatar)) {
+            throw new NoAvatarException($target, $avatar);
+        }
+
+        self::$_avatars[$target->id]["{$avatar->width}x{$avatar->height}"] = $avatar;
+        return $avatar;
+    }
+
     public static function getUploaded(Profile $target)
     {
         $avatar = new Avatar();
         $avatar->profile_id = $target->id;
         $avatar->original = true;
         if (!$avatar->find(true)) {
-            throw new NoResultException($avatar);
+            throw new NoAvatarException($target, $avatar);
         }
         if (!file_exists(Avatar::path($avatar->filename))) {
             // The delete call may be odd for, say, unmounted filesystems
             // that cause a file to currently not exist, but actually it does...
             $avatar->delete();
-            throw new FileNotFoundException(Avatar::path($avatar->filename));
+            throw new NoAvatarException($target, $avatar);
         }
         return $avatar;
     }
@@ -102,7 +145,7 @@ class Avatar extends Managed_DataObject
         $avatar = new Avatar();
         $avatar->profile_id = $target->id;
         if (!$avatar->find()) {
-            throw new NoResultException($avatar);
+            throw new NoAvatarException($target, $avatar);
         }
         return $avatar->fetchAll();
     }
@@ -174,6 +217,14 @@ class Avatar extends Managed_DataObject
         }
     }
 
+    static function urlByProfile(Profile $target, $width=null, $height=null) {
+        try {
+            return self::byProfile($target,  $width, $height)->displayUrl();
+        } catch (Exception $e) {
+            return self::defaultImage($width);
+        }
+    }
+
     static function defaultImage($size)
     {
         static $sizenames = array(AVATAR_PROFILE_SIZE => 'profile',
@@ -182,9 +233,9 @@ class Avatar extends Managed_DataObject
         return Theme::path('default-avatar-'.$sizenames[$size].'.png');
     }
 
-    static function newSize(Profile $target, $size) {
-        $size = floor($size);
-        if ($size < 1 || $size > common_config('avatar', 'maxsize')) {
+    static function newSize(Profile $target, $width) {
+        $width = (int) floor($width);
+        if ($width < 1 || $width > common_config('avatar', 'maxsize')) {
             // TRANS: An error message when avatar size is unreasonable
             throw new Exception(_m('Avatar size too large'));
         }
@@ -192,12 +243,12 @@ class Avatar extends Managed_DataObject
         $original = Avatar::getUploaded($target);
 
         $imagefile = new ImageFile($target->id, Avatar::path($original->filename));
-        $filename = $imagefile->resize($size);
+        $filename = $imagefile->resize($width);
 
         $scaled = clone($original);
         $scaled->original = false;
-        $scaled->width = $size;
-        $scaled->height = $size;
+        $scaled->width = $width;
+        $scaled->height = $width;
         $scaled->url = Avatar::url($filename);
         $scaled->filename = $filename;
         $scaled->created = common_sql_now();
index 1f8e7fc8822abea86d5094a4099f3e5e4f2fe848..c0af2635a7d0705563eba491f15874411e3d17cd 100644 (file)
@@ -120,38 +120,7 @@ class Profile extends Managed_DataObject
 
     public function getAvatar($width, $height=null)
     {
-        $width = (int) floor($width);
-
-        if (is_null($height)) {
-            $height = $width;
-        }
-
-        if (isset($this->_avatars[$width])) {
-            return $this->_avatars[$width];
-        }
-        
-        if (Event::handle('StartProfileGetAvatar', array($this, $width, &$avatar))) {
-            $avatar = Avatar::pkeyGet(
-                array(
-                    'profile_id' => $this->id,
-                    'width'      => $width,
-                    'height'     => $height
-                )
-            );
-            Event::handle('EndProfileGetAvatar', array($this, $width, &$avatar));
-        }
-
-        if (is_null($avatar)) {
-            // Obviously we can't find an avatar, so let's resize the original!
-            $avatar = Avatar::newSize($this, $width);
-        } elseif (!($avatar instanceof Avatar)) {
-            throw new Exception('Bad Avatar retrieved');
-        }
-
-        // cache the avatar for future use
-        $this->_avatars[$width] = $avatar;
-
-        return $avatar;
+        return Avatar::byProfile($this, $width, $height);
     }
 
     public function setOriginal($filename)
@@ -574,13 +543,7 @@ class Profile extends Managed_DataObject
 
     function avatarUrl($size=AVATAR_PROFILE_SIZE)
     {
-        $size = floor($size);
-        try {
-            $avatar = $this->getAvatar($size);
-            return $avatar->displayUrl();
-        } catch (Exception $e) {
-            return Avatar::defaultImage($size);
-        }
+        return Avatar::urlByProfile($this, $size);
     }
 
     function getSubscribed($offset=0, $limit=null)
index aa9eb1837b2bcef725007af77eaf0024176a5a29..d2a4f0f838a72376c93f72594193d309ea47fdf5 100644 (file)
@@ -460,9 +460,9 @@ class ActivityObject
             $object->link   = $profile->profileurl;
 
             try {
-                $orig = Avatar::getUploaded($profile);
-                $object->avatarLinks[] = AvatarLink::fromAvatar($orig);
-            } catch (Exception $e) {
+                $avatar = Avatar::getUploaded($profile);
+                $object->avatarLinks[] = AvatarLink::fromAvatar($avatar);
+            } catch (NoAvatarException $e) {
                 // Could not find an original avatar to link
             }
 
@@ -475,9 +475,9 @@ class ActivityObject
             foreach ($sizes as $size) {
                 $alink  = null;
                 try {
-                    $avatar = $profile->getAvatar($size);
+                    $avatar = Avatar::byProfile($profile, $size);
                     $alink = AvatarLink::fromAvatar($avatar);
-                } catch (Exception $e) {
+                } catch (NoAvatarException $e) {
                     $alink = new AvatarLink();
                     $alink->type   = 'image/png';
                     $alink->height = $size;
index 7d4256d6e10ab4979e741e274c9ac3ef4827d66b..6e3f050960a7f054520c442bd04fc7a6d6aaea8f 100644 (file)
@@ -58,11 +58,8 @@ class AvatarLink
         }
     }
 
-    static function fromAvatar($avatar)
+    static function fromAvatar(Avatar $avatar)
     {
-        if (empty($avatar)) {
-            return null;
-        }
         $alink = new AvatarLink();
         $alink->type   = $avatar->mediatype;
         $alink->height = $avatar->height;
diff --git a/lib/noavatarexception.php b/lib/noavatarexception.php
new file mode 100644 (file)
index 0000000..1f90aaf
--- /dev/null
@@ -0,0 +1,38 @@
+<?php
+/**
+ * StatusNet, the distributed open-source microblogging tool
+ *
+ * Class for an exception when unable to retrieve or resize a profile's avatar.
+ *
+ * PHP version 5
+ *
+ * LICENCE: This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ *
+ * @category  Exception
+ * @package   GNUSocial
+ * @author    Mikael Nordfeldth <mmn@hethane.se>
+ * @copyright 2013 Free Software Foundation, Inc.
+ * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPLv3
+ * @link      http://www.gnu.org/software/social/
+ */
+
+if (!defined('GNUSOCIAL')) { exit(1); }
+
+class NoAvatarException extends NoResultException
+{
+    public function __construct(Profile $target, Avatar $avatar)
+    {
+        parent::__construct($avatar);
+    }
+}
index 61e1a1f75a2ca42c1aeba4706d91938b39ce1fc7..6eb83b89d264e58b5ff51b6596c6d6b46b8baf8e 100644 (file)
@@ -34,6 +34,6 @@ class NoResultException extends ServerException
     public function __construct(DB_DataObject $obj)
     {
         // We could log an entry here with the search parameters
-        parent::__construct(_('No result found on lookup.'));
+        parent::__construct(sprintf(_('No result found on %s lookup.'), get_class($obj)));
     }
 }
index 7f27818d89c0540cde856d61651ba5cd0c84a360..e6ddc3b3ac612fbcfa3ed95c6840b5729deae482 100644 (file)
@@ -320,8 +320,8 @@ class TwitterImport
             common_debug(__METHOD__ . " - Updating profile avatar (profile_id={$profile->id}) " .
                        "from {$avatar->filename} to {$filename}");
             // else we continue with creating a new avatar
-        } catch (Exception $e) {
-            // Avatar was not found. We can catch NoResultException or FileNotFoundException
+        } catch (NoAvatarException $e) {
+            // Avatar was not found. We can catch NoAvatarException or FileNotFoundException
             // but generally we just want to continue creating a new avatar.
             common_debug(__METHOD__ . " - No avatar found for (profile_id={$profile->id})");
         }