]> git.mxchange.org Git - quix0rs-gnu-social.git/blobdiff - classes/Profile.php
Start and End EmailFormData events include current profile
[quix0rs-gnu-social.git] / classes / Profile.php
index c40e0e0b17ccdada4a7e230b8210482cddc69072..e5d00b392d88811c7c5932a59ff6b10403568ab3 100644 (file)
@@ -22,8 +22,6 @@ if (!defined('STATUSNET') && !defined('LACONICA')) { exit(1); }
 /**
  * Table Definition for profile
  */
-require_once INSTALLDIR.'/classes/Memcached_DataObject.php';
-
 class Profile extends Managed_DataObject
 {
     ###START_AUTOCODE
@@ -92,32 +90,32 @@ class Profile extends Managed_DataObject
         return $user->getProfile();
     } 
 
-    protected $_user = -1;  // Uninitialized value distinct from null
+    protected $_user = array();
 
     public function getUser()
     {
-        if ($this->_user === -1) {
-            $this->_user = User::getKV('id', $this->id);
-        }
-        if (!$this->_user instanceof User) {
-            throw new NoSuchUserException(array('id'=>$this->id));
+        if (!isset($this->_user[$this->id])) {
+            $user = User::getKV('id', $this->id);
+            if (!$user instanceof User) {
+                throw new NoSuchUserException(array('id'=>$this->id));
+            }
+            $this->_user[$this->id] = $user;
         }
-
-        return $this->_user;
+        return $this->_user[$this->id];
     }
 
-    protected $_group = -1;
+    protected $_group = array();
 
     public function getGroup()
     {
-        if ($this->_group === -1) {
-            $this->_group = User_group::getKV('profile_id', $this->id);
-        }
-        if (!$this->_group instanceof User_group) {
-            throw new NoSuchGroupException(array('profile_id'=>$this->id));
+        if (!isset($this->_group[$this->id])) {
+            $group = User_group::getKV('profile_id', $this->id);
+            if (!$group instanceof User_group) {
+                throw new NoSuchGroupException(array('profile_id'=>$this->id));
+            }
+            $this->_group[$this->id] = $group;
         }
-
-        return $this->_group;
+        return $this->_group[$this->id];
     }
 
     public function isGroup()
@@ -140,7 +138,15 @@ class Profile extends Managed_DataObject
         return true;
     }
 
-    protected $_avatars = array();
+    public function getObjectType()
+    {
+        // FIXME: More types... like peopletags and whatever
+        if ($this->isGroup()) {
+            return ActivityObject::GROUP;
+        } else {
+            return ActivityObject::PERSON;
+        }
+    }
 
     public function getAvatar($width, $height=null)
     {
@@ -168,17 +174,6 @@ class Profile extends Managed_DataObject
             return null;
         }
 
-        foreach (array(AVATAR_PROFILE_SIZE, AVATAR_STREAM_SIZE, AVATAR_MINI_SIZE) as $size) {
-            // We don't do a scaled one if original is our scaled size
-            if (!($avatar->width == $size && $avatar->height == $size)) {
-                try {
-                    Avatar::newSize($this, $size);
-                } catch (Exception $e) {
-                    // should we abort the generation and live without smaller avatars?
-                }
-            }
-        }
-
         return $avatar;
     }
 
@@ -237,9 +232,9 @@ class Profile extends Managed_DataObject
                 return $notice->_items[0];
             }
             return $notice;
-        } else {
-            return null;
         }
+        
+        return null;
     }
 
     function getTaggedNotices($tag, $offset=0, $limit=NOTICES_PER_PAGE, $since_id=0, $max_id=0)
@@ -756,7 +751,7 @@ class Profile extends Managed_DataObject
      * @param Profile $other
      * @return boolean
      */
-    function hasPendingSubscription($other)
+    function hasPendingSubscription(Profile $other)
     {
         return Subscription_queue::exists($this, $other);
     }
@@ -767,45 +762,12 @@ class Profile extends Managed_DataObject
      * @param Profile $other
      * @return boolean
      */
-    function mutuallySubscribed($other)
+    function mutuallySubscribed(Profile $other)
     {
         return $this->isSubscribed($other) &&
           $other->isSubscribed($this);
     }
 
-    function hasFave($notice)
-    {
-        $fave = Fave::pkeyGet(array('user_id' => $this->id,
-                                    'notice_id' => $notice->id));
-        return ((is_null($fave)) ? false : true);
-    }
-
-    function faveCount()
-    {
-        $c = Cache::instance();
-        if (!empty($c)) {
-            $cnt = $c->get(Cache::key('profile:fave_count:'.$this->id));
-            if (is_integer($cnt)) {
-                return (int) $cnt;
-            }
-        }
-
-        $faves = new Fave();
-        $faves->user_id = $this->id;
-        $cnt = (int) $faves->count('notice_id');
-
-        if (!empty($c)) {
-            $c->set(Cache::key('profile:fave_count:'.$this->id), $cnt);
-        }
-
-        return $cnt;
-    }
-
-    function favoriteNotices($own=false, $offset=0, $limit=NOTICES_PER_PAGE, $since_id=0, $max_id=0)
-    {
-        return Fave::stream($this->id, $offset, $limit, $own, $since_id, $max_id);
-    }
-
     function noticeCount()
     {
         $c = Cache::instance();
@@ -828,20 +790,6 @@ class Profile extends Managed_DataObject
         return $cnt;
     }
 
-    function blowFavesCache()
-    {
-        $cache = Cache::instance();
-        if ($cache) {
-            // Faves don't happen chronologically, so we need to blow
-            // ;last cache, too
-            $cache->delete(Cache::key('fave:ids_by_user:'.$this->id));
-            $cache->delete(Cache::key('fave:ids_by_user:'.$this->id.';last'));
-            $cache->delete(Cache::key('fave:ids_by_user_own:'.$this->id));
-            $cache->delete(Cache::key('fave:ids_by_user_own:'.$this->id.';last'));
-        }
-        $this->blowFaveCount();
-    }
-
     function blowSubscriberCount()
     {
         $c = Cache::instance();
@@ -858,14 +806,6 @@ class Profile extends Managed_DataObject
         }
     }
 
-    function blowFaveCount()
-    {
-        $c = Cache::instance();
-        if (!empty($c)) {
-            $c->delete(Cache::key('profile:fave_count:'.$this->id));
-        }
-    }
-
     function blowNoticeCount()
     {
         $c = Cache::instance();
@@ -1371,10 +1311,66 @@ class Profile extends Managed_DataObject
      */
     function asActivityNoun($element)
     {
-        $noun = ActivityObject::fromProfile($this);
+        $noun = $this->asActivityObject();
         return $noun->asString('activity:' . $element);
     }
 
+    public function asActivityObject()
+    {
+        $object = new ActivityObject();
+
+        if (Event::handle('StartActivityObjectFromProfile', array($this, &$object))) {
+            $object->type   = $this->getObjectType();
+            $object->id     = $this->getUri();
+            $object->title  = $this->getBestName();
+            $object->link   = $this->getUrl();
+
+            try {
+                $avatar = Avatar::getUploaded($this);
+                $object->avatarLinks[] = AvatarLink::fromAvatar($avatar);
+            } catch (NoAvatarException $e) {
+                // Could not find an original avatar to link
+            }
+
+            $sizes = array(
+                AVATAR_PROFILE_SIZE,
+                AVATAR_STREAM_SIZE,
+                AVATAR_MINI_SIZE
+            );
+
+            foreach ($sizes as $size) {
+                $alink  = null;
+                try {
+                    $avatar = Avatar::byProfile($this, $size);
+                    $alink = AvatarLink::fromAvatar($avatar);
+                } catch (NoAvatarException $e) {
+                    $alink = new AvatarLink();
+                    $alink->type   = 'image/png';
+                    $alink->height = $size;
+                    $alink->width  = $size;
+                    $alink->url    = Avatar::defaultImage($size);
+                }
+
+                $object->avatarLinks[] = $alink;
+            }
+
+            if (isset($this->lat) && isset($this->lon)) {
+                $object->geopoint = (float)$this->lat
+                    . ' ' . (float)$this->lon;
+            }
+
+            $object->poco = PoCo::fromProfile($this);
+
+            if ($this->isLocal()) {
+                $object->extra[] = array('followers', array('url' => common_local_url('subscribers', array('nickname' => $this->getNickname()))));
+            }
+
+            Event::handle('EndActivityObjectFromProfile', array($this, &$object));
+        }
+
+        return $object;
+    }
+
     /**
      * Returns the profile's canonical url, not necessarily a uri/unique id
      *
@@ -1460,19 +1456,29 @@ class Profile extends Managed_DataObject
         return $feed;
     }
 
-    static function fromURI($uri)
+    /*
+     * Get a Profile object by URI. Will call external plugins for help
+     * using the event StartGetProfileFromURI.
+     *
+     * @param string $uri A unique identifier for a resource (profile/group/whatever)
+     */
+    static function fromUri($uri)
     {
         $profile = null;
 
         if (Event::handle('StartGetProfileFromURI', array($uri, &$profile))) {
-            // Get a local user
+            // Get a local user when plugin lookup (like OStatus) fails
             $user = User::getKV('uri', $uri);
-            if (!empty($user)) {
+            if ($user instanceof User) {
                 $profile = $user->getProfile();
             }
             Event::handle('EndGetProfileFromURI', array($uri, $profile));
         }
 
+        if (!$profile instanceof Profile) {
+            throw new UnknownUriException($uri);
+        }
+
         return $profile;
     }
 
@@ -1540,7 +1546,7 @@ class Profile extends Managed_DataObject
     function __sleep()
     {
         $vars = parent::__sleep();
-        $skip = array('_user', '_avatars');
+        $skip = array('_user', '_group');
         return array_diff($vars, $skip);
     }
 
@@ -1548,4 +1554,20 @@ class Profile extends Managed_DataObject
     {
         return $this;
     }
+
+    /**
+     * This will perform shortenLinks with the connected User object.
+     *
+     * Won't work on remote profiles or groups, so expect a
+     * NoSuchUserException if you don't know it's a local User.
+     *
+     * @param string $text      String to shorten
+     * @param boolean $always   Disrespect minimum length etc.
+     *
+     * @return string link-shortened $text
+     */
+    public function shortenLinks($text, $always=false)
+    {
+        return $this->getUser()->shortenLinks($text, $always);
+    }
 }