]> git.mxchange.org Git - quix0rs-gnu-social.git/commitdiff
OStatus: pull best-sized avatar image (96x96 if found, otherwise largest, otherwise...
authorBrion Vibber <brion@pobox.com>
Fri, 26 Feb 2010 02:51:44 +0000 (18:51 -0800)
committerBrion Vibber <brion@pobox.com>
Fri, 26 Feb 2010 02:51:44 +0000 (18:51 -0800)
lib/activity.php
plugins/OStatus/classes/Ostatus_profile.php

index 0f30e8bf517c84f1e26c0d93dbbbee9c46d0dd64..b64a82f0a18cb00e397c8dc3c99e5ca836a48149 100644 (file)
@@ -360,6 +360,25 @@ class ActivityUtils
         return null;
     }
 
+    static function getLinks(DOMNode $element, $rel, $type=null)
+    {
+        $links = $element->getElementsByTagnameNS(self::ATOM, self::LINK);
+        $out = array();
+
+        foreach ($links as $link) {
+
+            $linkRel = $link->getAttribute(self::REL);
+            $linkType = $link->getAttribute(self::TYPE);
+
+            if ($linkRel == $rel &&
+                (is_null($type) || $linkType == $type)) {
+                $out[] = $link;
+            }
+        }
+
+        return $out;
+    }
+
     /**
      * Gets the first child element with the given tag
      *
@@ -472,6 +491,24 @@ class AvatarLink
     public $type;
     public $size;
     public $width;
+    public $height;
+
+    function __construct($element=null)
+    {
+        if ($element) {
+            // @fixme use correct namespaces
+            $this->url = $element->getAttribute('href');
+            $this->type = $element->getAttribute('type');
+            $width = $element->getAttribute('media:width');
+            if ($width != null) {
+                $this->width = intval($width);
+            }
+            $height = $element->getAttribute('media:height');
+            if ($height != null) {
+                $this->height = intval($height);
+            }
+        }
+    }
 
     static function fromAvatar($avatar)
     {
@@ -640,8 +677,10 @@ class ActivityObject
         if ($this->type == self::PERSON || $this->type == self::GROUP) {
             $this->displayName = $this->title;
 
-            // @fixme we may have multiple avatars with different resolutions specified
-            $this->avatar = ActivityUtils::getLink($element, 'avatar');
+            $avatars = ActivityUtils::getLinks($element, 'avatar');
+            foreach ($avatars as $link) {
+                $this->avatarLinks[] = new AvatarLink($link);
+            }
 
             $this->poco = new PoCo($element);
         }
index ad9170f5b1386b5efceff566d72658d800827e05..9b7be4e9aa4cb8f0d0f11ba43e0285057f82a03a 100644 (file)
@@ -912,8 +912,20 @@ class Ostatus_profile extends Memcached_DataObject
 
     protected static function getActivityObjectAvatar($object, $hints=array())
     {
-        if ($object->avatar) {
-            return $object->avatar;
+        if ($object->avatarLinks) {
+            $best = false;
+            // Take the exact-size avatar, or the largest avatar, or the first avatar if all sizeless
+            foreach ($object->avatarLinks as $avatar) {
+                if ($avatar->width == AVATAR_PROFILE_SIZE && $avatar->height = AVATAR_PROFILE_SIZE) {
+                    // Exact match!
+                    $best = $avatar;
+                    break;
+                }
+                if (!$best || $avatar->width > $best->width) {
+                    $best = $avatar;
+                }
+            }
+            return $best->url;
         } else if (array_key_exists('avatar', $hints)) {
             return $hints['avatar'];
         }