]> git.mxchange.org Git - quix0rs-gnu-social.git/blobdiff - classes/Profile.php
instanceof instead of is_a is faster (thanks quix0r)
[quix0rs-gnu-social.git] / classes / Profile.php
index 59a2e0eced0e4c4942ef8c553e40f385c0ec0751..51b1fb72a87500911a895eebf3f25c102e977f43 100644 (file)
@@ -44,11 +44,6 @@ class Profile extends Managed_DataObject
     public $created;                         // datetime()   not_null
     public $modified;                        // timestamp()   not_null default_CURRENT_TIMESTAMP
 
-    /* Static get */
-    function staticGet($k,$v=NULL) {
-        return Memcached_DataObject::staticGet('Profile',$k,$v);
-    }
-
     public static function schemaDef()
     {
         $def = array(
@@ -83,26 +78,34 @@ class Profile extends Managed_DataObject
 
         return $def;
     }
-
-       function multiGet($keyCol, $keyVals, $skipNulls=true)
-       {
-           return parent::multiGet('Profile', $keyCol, $keyVals, $skipNulls);
-       }
        
     /* the code above is auto generated do not remove the tag below */
     ###END_AUTOCODE
 
     protected $_user = -1;  // Uninitialized value distinct from null
 
-    function getUser()
+    public function getUser()
     {
-        if (is_int($this->_user) && $this->_user == -1) {
-            $this->_user = User::staticGet('id', $this->id);
+        if ($this->_user === -1) {
+            $this->_user = User::getKV('id', $this->id);
+        }
+        if (!($this->_user instanceof User)) {
+            throw new NoSuchUserException(array('id'=>$this->id));
         }
 
         return $this->_user;
     }
 
+    public function isLocal()
+    {
+        try {
+            $this->getUser();
+        } catch (NoSuchUserException $e) {
+            return false;
+        }
+        return true;
+    }
+
     protected $_avatars;
 
     function getAvatar($width, $height=null)
@@ -160,10 +163,10 @@ class Profile extends Managed_DataObject
 
     function getOriginalAvatar()
     {
-        $avatar = DB_DataObject::factory('avatar');
-        $avatar->profile_id = $this->id;
-        $avatar->original = true;
-        if ($avatar->find(true)) {
+        $pkey = array('profile_id' => $this->id,
+                      'original'   => true);
+        $avatar = Avatar::pkeyGet($pkey);
+        if (!empty($avatar)) {
             return $avatar;
         } else {
             return null;
@@ -445,7 +448,7 @@ class Profile extends Managed_DataObject
         $lists = array();
 
         foreach ($ids as $id) {
-            $list = Profile_list::staticGet('id', $id);
+            $list = Profile_list::getKV('id', $id);
             if (!empty($list) &&
                 ($showPrivate || !$list->private)) {
 
@@ -554,7 +557,7 @@ class Profile extends Managed_DataObject
         $lists = new Profile_list();
         $subs = new Profile_tag_subscription();
 
-        $lists->joinAdd('id', 'profile_tag_subscription:profile_tag_id');
+        $lists->joinAdd(array('id', 'profile_tag_subscription:profile_tag_id'));
 
         #@fixme: postgres (round(date_part('epoch', my_date)))
         $lists->selectAdd('unix_timestamp(profile_tag_subscription.created) as "cursor"');
@@ -595,6 +598,8 @@ class Profile extends Managed_DataObject
             if (Event::handle('StartJoinGroup', array($group, $this))) {
                 $join = Group_member::join($group->id, $this->id);
                 self::blow('profile:groups:%d', $this->id);
+                self::blow('group:member_ids:%d', $group->id);
+                self::blow('group:member_count:%d', $group->id);
                 Event::handle('EndJoinGroup', array($group, $this));
             }
         }
@@ -615,6 +620,8 @@ class Profile extends Managed_DataObject
         if (Event::handle('StartLeaveGroup', array($group, $this))) {
             Group_member::leave($group->id, $this->id);
             self::blow('profile:groups:%d', $this->id);
+            self::blow('group:member_ids:%d', $group->id);
+            self::blow('group:member_count:%d', $group->id);
             Event::handle('EndLeaveGroup', array($group, $this));
         }
     }
@@ -638,7 +645,7 @@ class Profile extends Managed_DataObject
         $profiles = array();
 
         while ($subs->fetch()) {
-            $profile = Profile::staticGet($subs->subscribed);
+            $profile = Profile::getKV($subs->subscribed);
             if ($profile) {
                 $profiles[] = $profile;
             }
@@ -656,7 +663,7 @@ class Profile extends Managed_DataObject
         $profiles = array();
 
         while ($subs->fetch()) {
-            $profile = Profile::staticGet($subs->subscriber);
+            $profile = Profile::getKV($subs->subscriber);
             if ($profile) {
                 $profiles[] = $profile;
             }
@@ -682,7 +689,7 @@ class Profile extends Managed_DataObject
         $profile = new Profile();
         $tagged = array();
 
-        $cnt = $profile->query(sprintf($qry, $this->id, $this->id, $tag));
+        $cnt = $profile->query(sprintf($qry, $this->id, $this->id, $profile->escape($tag)));
 
         while ($profile->fetch()) {
             $tagged[] = clone($profile);
@@ -959,7 +966,7 @@ class Profile extends Managed_DataObject
         $sub->find();
 
         while ($sub->fetch()) {
-            $other = Profile::staticGet('id', $sub->subscribed);
+            $other = Profile::getKV('id', $sub->subscribed);
             if (empty($other)) {
                 continue;
             }
@@ -974,7 +981,7 @@ class Profile extends Managed_DataObject
         $subd->find();
 
         while ($subd->fetch()) {
-            $other = Profile::staticGet('id', $subd->subscriber);
+            $other = Profile::getKV('id', $subd->subscriber);
             if (empty($other)) {
                 continue;
             }
@@ -1139,20 +1146,27 @@ class Profile extends Managed_DataObject
     function silence()
     {
         $this->grantRole(Profile_role::SILENCED);
-        $this->blowRecentNoticeScope();
+        if (common_config('notice', 'hidespam')) {
+            $this->flushVisibility();
+        }
     }
 
     function unsilence()
     {
         $this->revokeRole(Profile_role::SILENCED);
-        $this->blowRecentNoticeScope();
+        if (common_config('notice', 'hidespam')) {
+            $this->flushVisibility();
+        }
     }
 
-    function blowRecentNoticeScope() {
-        $notice = $this->getNotices(0, CachingNoticeStream::CACHE_WINDOW);
-        while ($notice->fetch()) {
-            self::blow(sprintf('notice:in-scope-for:%d:null', $notice->id));
-        }        
+    function flushVisibility()
+    {
+        // Get all notices
+        $stream = new ProfileNoticeStream($this, $this);
+        $ids = $stream->getNoticeIds(0, CachingNoticeStream::CACHE_WINDOW);
+        foreach ($ids as $id) {
+            self::blow('notice:in-scope-for:%d:null', $id);
+        }
     }
 
     /**
@@ -1183,6 +1197,8 @@ class Profile extends Managed_DataObject
             case Right::SILENCEUSER:
             case Right::DELETEUSER:
             case Right::DELETEGROUP:
+            case Right::TRAINSPAM:
+            case Right::REVIEWSPAM:
                 $result = $this->hasRole(Profile_role::MODERATOR);
                 break;
             case Right::CONFIGURESITE:
@@ -1234,9 +1250,8 @@ class Profile extends Managed_DataObject
     {
         // XXX: not really a pkey, but should work
 
-        $notice = Memcached_DataObject::pkeyGet('Notice',
-                                                array('profile_id' => $this->id,
-                                                      'repeat_of' => $notice_id));
+        $notice = Notice::pkeyGet(array('profile_id' => $this->id,
+                                        'repeat_of' => $notice_id));
 
         return !empty($notice);
     }
@@ -1337,7 +1352,7 @@ class Profile extends Managed_DataObject
         if (Event::handle('StartGetProfileUri', array($this, &$uri))) {
 
             // check for a local user first
-            $user = User::staticGet('id', $this->id);
+            $user = User::getKV('id', $this->id);
 
             if (!empty($user)) {
                 $uri = $user->uri;
@@ -1351,15 +1366,8 @@ class Profile extends Managed_DataObject
 
     function hasBlocked($other)
     {
-        $block = Profile_block::get($this->id, $other->id);
-
-        if (empty($block)) {
-            $result = false;
-        } else {
-            $result = true;
-        }
-
-        return $result;
+        $block = Profile_block::exists($this, $other);
+        return !empty($block);
     }
 
     function getAtomFeed()
@@ -1367,7 +1375,7 @@ class Profile extends Managed_DataObject
         $feed = null;
 
         if (Event::handle('StartProfileGetAtomFeed', array($this, &$feed))) {
-            $user = User::staticGet('id', $this->id);
+            $user = User::getKV('id', $this->id);
             if (!empty($user)) {
                 $feed = common_local_url('ApiTimelineUser', array('id' => $user->id,
                                                                   'format' => 'atom'));
@@ -1384,7 +1392,7 @@ class Profile extends Managed_DataObject
 
         if (Event::handle('StartGetProfileFromURI', array($uri, &$profile))) {
             // Get a local user or remote (OMB 0.1) profile
-            $user = User::staticGet('uri', $uri);
+            $user = User::getKV('uri', $uri);
             if (!empty($user)) {
                 $profile = $user->getProfile();
             }
@@ -1487,8 +1495,4 @@ class Profile extends Managed_DataObject
     {
         return $this;
     }
-
-    static function pivotGet($key, $values, $otherCols=array()) {
-        return Memcached_DataObject::pivotGet('Profile', $key, $values, $otherCols);
-    }
 }