]> git.mxchange.org Git - quix0rs-gnu-social.git/blobdiff - classes/Profile.php
Core plugin list would not merge into $config
[quix0rs-gnu-social.git] / classes / Profile.php
index da779f9d4f0dc0a5d07263ba26718928b95621ec..e461254f9dde88daea334c282510942a83d0573a 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)
@@ -166,7 +135,7 @@ class Profile extends Managed_DataObject
         $avatar->filename = $filename;
         $avatar->original = true;
         $avatar->url = Avatar::url($filename);
-        $avatar->created = DB_DataObject_Cast::dateTime(); # current time
+        $avatar->created = common_sql_now();
 
         // XXX: start a transaction here
         if (!Avatar::deleteFromProfile($this, true) || !$avatar->insert()) {
@@ -242,9 +211,9 @@ class Profile extends Managed_DataObject
         return $stream->getNotices($offset, $limit, $since_id, $max_id);
     }
 
-    function getNotices($offset=0, $limit=NOTICES_PER_PAGE, $since_id=0, $max_id=0)
+    function getNotices($offset=0, $limit=NOTICES_PER_PAGE, $since_id=0, $max_id=0, Profile $scoped=null)
     {
-        $stream = new ProfileNoticeStream($this);
+        $stream = new ProfileNoticeStream($this, $scoped);
 
         return $stream->getNotices($offset, $limit, $since_id, $max_id);
     }
@@ -574,52 +543,75 @@ 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)
     {
         $subs = Subscription::getSubscribedIDs($this->id, $offset, $limit);
-        $profiles = Profile::listFind('id', $subs);
+        try {
+            $profiles = Profile::listFind('id', $subs);
+        } catch (NoResultException $e) {
+            return $e->obj;
+        }
         return $profiles;
     }
 
     function getSubscribers($offset=0, $limit=null)
     {
         $subs = Subscription::getSubscriberIDs($this->id, $offset, $limit);
-        $profiles = Profile::listFind('id', $subs);
+        try {
+            $profiles = Profile::listFind('id', $subs);
+        } catch (NoResultException $e) {
+            return $e->obj;
+        }
         return $profiles;
     }
 
-    function getTaggedSubscribers($tag)
+    function getTaggedSubscribers($tag, $offset=0, $limit=null)
     {
         $qry =
           'SELECT profile.* ' .
-          'FROM profile JOIN (subscription, profile_tag, profile_list) ' .
+          'FROM profile JOIN subscription ' .
           'ON profile.id = subscription.subscriber ' .
-          'AND profile.id = profile_tag.tagged ' .
-          'AND profile_tag.tagger = profile_list.tagger AND profile_tag.tag = profile_list.tag ' .
+          'JOIN profile_tag ON (profile_tag.tagged = subscription.subscriber ' .
+          'AND profile_tag.tagger = subscription.subscribed) ' .
           'WHERE subscription.subscribed = %d ' .
+          "AND profile_tag.tag = '%s' " .
           'AND subscription.subscribed != subscription.subscriber ' .
-          'AND profile_tag.tagger = %d AND profile_tag.tag = "%s" ' .
-          'AND profile_list.private = false ' .
-          'ORDER BY subscription.created DESC';
+          'ORDER BY subscription.created DESC ';
+
+        if ($offset) {
+            $qry .= ' LIMIT ' . $limit . ' OFFSET ' . $offset;
+        }
 
         $profile = new Profile();
-        $tagged = array();
 
-        $cnt = $profile->query(sprintf($qry, $this->id, $this->id, $profile->escape($tag)));
+        $cnt = $profile->query(sprintf($qry, $this->id, $profile->escape($tag)));
 
-        while ($profile->fetch()) {
-            $tagged[] = clone($profile);
-        }
-        return $tagged;
+        return $profile;
+    }
+
+    function getTaggedSubscriptions($tag, $offset=0, $limit=null)
+    {
+        $qry =
+          'SELECT profile.* ' .
+          'FROM profile JOIN subscription ' .
+          'ON profile.id = subscription.subscribed ' .
+          'JOIN profile_tag on (profile_tag.tagged = subscription.subscribed ' .
+          'AND profile_tag.tagger = subscription.subscriber) ' .
+          'WHERE subscription.subscriber = %d ' .
+          "AND profile_tag.tag = '%s' " .
+          'AND subscription.subscribed != subscription.subscriber ' .
+          'ORDER BY subscription.created DESC ';
+
+        $qry .= ' LIMIT ' . $limit . ' OFFSET ' . $offset;
+
+        $profile = new Profile();
+
+        $profile->query(sprintf($qry, $this->id, $profile->escape($tag)));
+
+        return $profile;
     }
 
     /**
@@ -761,6 +753,11 @@ class Profile extends Managed_DataObject
         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();
@@ -845,6 +842,31 @@ class Profile extends Managed_DataObject
         return ($biolimit > 0 && !empty($bio) && (mb_strlen($bio) > $biolimit));
     }
 
+    public function update($orig)
+    {
+        if ($this->nickname != $orig->nickname) {
+            $local = User::getKV('id', $this->id);
+            if ($local instanceof User) {
+                common_debug("Updating User ({$this->id}) nickname from {$orig->nickname} to {$this->nickname}");
+                $origuser = clone($local);
+                $local->nickname = $this->nickname;
+                $result = $local->updateKeys($origuser);
+                if ($result === false) {
+                    common_log_db_error($local, 'UPDATE', __FILE__);
+                    // TRANS: Server error thrown when user profile settings could not be updated.
+                    throw new ServerException(_('Could not update user nickname.'));
+                }
+
+                // Clear the site owner, in case nickname changed
+                if ($local->hasRole(Profile_role::OWNER)) {
+                    User::blow('user:site_owner');
+                }
+            }
+        }
+
+        return parent::update($orig);
+    }
+
     function delete()
     {
         $this->_deleteNotices();
@@ -955,7 +977,7 @@ class Profile extends Managed_DataObject
 
     // XXX: identical to Notice::getLocation.
 
-    function getLocation()
+    public function getLocation()
     {
         $location = null;
 
@@ -978,6 +1000,29 @@ class Profile extends Managed_DataObject
         return $location;
     }
 
+    public function shareLocation()
+    {
+        $cfg = common_config('location', 'share');
+
+        if ($cfg == 'always') {
+            return true;
+        } else if ($cfg == 'never') {
+            return false;
+        } else { // user
+            $share = common_config('location', 'sharedefault');
+
+            // Check if user has a personal setting for this
+            $prefs = User_location_prefs::getKV('user_id', $this->id);
+
+            if (!empty($prefs)) {
+                $share = $prefs->share_location;
+                $prefs->free();
+            }
+
+            return $share;
+        }
+    }
+
     function hasRole($name)
     {
         $has_role = false;
@@ -1105,7 +1150,7 @@ class Profile extends Managed_DataObject
      * @param $right string Name of the right, usually a constant in class Right
      * @return boolean whether the user has the right in question
      */
-    function hasRight($right)
+    public function hasRight($right)
     {
         $result = false;
 
@@ -1171,12 +1216,13 @@ class Profile extends Managed_DataObject
         return $result;
     }
 
-    function hasRepeated($notice_id)
+    // FIXME: Can't put Notice typing here due to ArrayWrapper
+    public function hasRepeated($notice)
     {
         // XXX: not really a pkey, but should work
 
         $notice = Notice::pkeyGet(array('profile_id' => $this->id,
-                                        'repeat_of' => $notice_id));
+                                        'repeat_of' => $notice->id));
 
         return !empty($notice);
     }