]> git.mxchange.org Git - quix0rs-gnu-social.git/blobdiff - classes/Profile.php
Move getConnectedApps() from Profile to User, where it belongs
[quix0rs-gnu-social.git] / classes / Profile.php
index 78223b34a1956d0e16901e949810cbc3c367f6ba..e29e6db7b7c3c92e4a9f708fa29cacda05a54e82 100644 (file)
@@ -103,7 +103,6 @@ class Profile extends Memcached_DataObject
         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)) {
-
                 $scaled_filename = $imagefile->resize($size);
 
                 //$scaled = DB_DataObject::factory('avatar');
@@ -126,6 +125,14 @@ class Profile extends Memcached_DataObject
         return $avatar;
     }
 
+    /**
+     * Delete attached avatars for this user from the database and filesystem.
+     * This should be used instead of a batch delete() to ensure that files
+     * get removed correctly.
+     *
+     * @param boolean $original true to delete only the original-size file
+     * @return <type>
+     */
     function delete_avatars($original=true)
     {
         $avatar = new Avatar();
@@ -147,43 +154,44 @@ class Profile extends Memcached_DataObject
         return ($this->fullname) ? $this->fullname : $this->nickname;
     }
 
-    # Get latest notice on or before date; default now
-    function getCurrentNotice($dt=null)
+    /**
+     * Get the most recent notice posted by this user, if any.
+     *
+     * @return mixed Notice or null
+     */
+
+    function getCurrentNotice()
     {
-        $notice = new Notice();
-        $notice->profile_id = $this->id;
-        if ($dt) {
-            $notice->whereAdd('created < "' . $dt . '"');
-        }
-        $notice->orderBy('created DESC, notice.id DESC');
-        $notice->limit(1);
-        if ($notice->find(true)) {
+        $notice = $this->getNotices(0, 1);
+
+        if ($notice->fetch()) {
             return $notice;
+        } else {
+            return null;
         }
-        return null;
     }
 
-    function getTaggedNotices($tag, $offset=0, $limit=NOTICES_PER_PAGE, $since_id=0, $max_id=0, $since=null)
+    function getTaggedNotices($tag, $offset=0, $limit=NOTICES_PER_PAGE, $since_id=0, $max_id=0)
     {
         $ids = Notice::stream(array($this, '_streamTaggedDirect'),
                               array($tag),
                               'profile:notice_ids_tagged:' . $this->id . ':' . $tag,
-                              $offset, $limit, $since_id, $max_id, $since);
+                              $offset, $limit, $since_id, $max_id);
         return Notice::getStreamByIds($ids);
     }
 
-    function getNotices($offset=0, $limit=NOTICES_PER_PAGE, $since_id=0, $max_id=0, $since=null)
+    function getNotices($offset=0, $limit=NOTICES_PER_PAGE, $since_id=0, $max_id=0)
     {
         // XXX: I'm not sure this is going to be any faster. It probably isn't.
         $ids = Notice::stream(array($this, '_streamDirect'),
                               array(),
                               'profile:notice_ids:' . $this->id,
-                              $offset, $limit, $since_id, $max_id, $since);
+                              $offset, $limit, $since_id, $max_id);
 
         return Notice::getStreamByIds($ids);
     }
 
-    function _streamTaggedDirect($tag, $offset, $limit, $since_id, $max_id, $since)
+    function _streamTaggedDirect($tag, $offset, $limit, $since_id, $max_id)
     {
         // XXX It would be nice to do this without a join
 
@@ -199,11 +207,7 @@ class Profile extends Memcached_DataObject
         }
 
         if ($max_id != 0) {
-            $query .= " and id < $max_id";
-        }
-
-        if (!is_null($since)) {
-            $query .= " and created > '" . date('Y-m-d H:i:s', $since) . "'";
+            $query .= " and id <= $max_id";
         }
 
         $query .= ' order by id DESC';
@@ -223,39 +227,66 @@ class Profile extends Memcached_DataObject
         return $ids;
     }
 
-    function _streamDirect($offset, $limit, $since_id, $max_id, $since = null)
+    function _streamDirect($offset, $limit, $since_id, $max_id)
     {
         $notice = new Notice();
 
-        $notice->profile_id = $this->id;
+        // Temporary hack until notice_profile_id_idx is updated
+        // to (profile_id, id) instead of (profile_id, created, id).
+        // It's been falling back to PRIMARY instead, which is really
+        // very inefficient for a profile that hasn't posted in a few
+        // months. Even though forcing the index will cause a filesort,
+        // it's usually going to be better.
+        if (common_config('db', 'type') == 'mysql') {
+            $index = '';
+            $query =
+              "select id from notice force index (notice_profile_id_idx) ".
+              "where profile_id=" . $notice->escape($this->id);
+
+            if ($since_id != 0) {
+                $query .= " and id > $since_id";
+            }
 
-        $notice->selectAdd();
-        $notice->selectAdd('id');
+            if ($max_id != 0) {
+                $query .= " and id <= $max_id";
+            }
 
-        if ($since_id != 0) {
-            $notice->whereAdd('id > ' . $since_id);
-        }
+            $query .= ' order by id DESC';
 
-        if ($max_id != 0) {
-            $notice->whereAdd('id <= ' . $max_id);
-        }
+            if (!is_null($offset)) {
+                $query .= " LIMIT $limit OFFSET $offset";
+            }
 
-        if (!is_null($since)) {
-            $notice->whereAdd('created > \'' . date('Y-m-d H:i:s', $since) . '\'');
-        }
+            $notice->query($query);
+        } else {
+            $index = '';
 
-        $notice->orderBy('id DESC');
+            $notice->profile_id = $this->id;
 
-        if (!is_null($offset)) {
-            $notice->limit($offset, $limit);
+            $notice->selectAdd();
+            $notice->selectAdd('id');
+
+            if ($since_id != 0) {
+                $notice->whereAdd('id > ' . $since_id);
+            }
+
+            if ($max_id != 0) {
+                $notice->whereAdd('id <= ' . $max_id);
+            }
+
+            $notice->orderBy('id DESC');
+
+            if (!is_null($offset)) {
+                $notice->limit($offset, $limit);
+            }
+
+            $notice->find();
         }
 
         $ids = array();
 
-        if ($notice->find()) {
-            while ($notice->fetch()) {
-                $ids[] = $notice->id;
-            }
+        while ($notice->fetch()) {
+            $ids[] = $notice->id;
         }
 
         return $ids;
@@ -290,6 +321,32 @@ class Profile extends Memcached_DataObject
         }
     }
 
+    function getGroups($offset=0, $limit=null)
+    {
+        $qry =
+          'SELECT user_group.* ' .
+          'FROM user_group JOIN group_member '.
+          'ON user_group.id = group_member.group_id ' .
+          'WHERE group_member.profile_id = %d ' .
+          'ORDER BY group_member.created DESC ';
+
+        if ($offset>0 && !is_null($limit)) {
+            if ($offset) {
+                if (common_config('db','type') == 'pgsql') {
+                    $qry .= ' LIMIT ' . $limit . ' OFFSET ' . $offset;
+                } else {
+                    $qry .= ' LIMIT ' . $offset . ', ' . $limit;
+                }
+            }
+        }
+
+        $groups = new User_group();
+
+        $cnt = $groups->query(sprintf($qry, $this->id));
+
+        return $groups;
+    }
+
     function avatarUrl($size=AVATAR_PROFILE_SIZE)
     {
         $avatar = $this->getAvatar($size);
@@ -352,31 +409,6 @@ class Profile extends Memcached_DataObject
         return $profile;
     }
 
-    function getApplications($offset = 0, $limit = null)
-    {
-        $qry =
-          'SELECT a.* ' .
-          'FROM oauth_application_user u, oauth_application a ' .
-          'WHERE u.profile_id = %d ' .
-          'AND a.id = u.application_id ' .
-          'AND u.access_type > 0 ' .
-          'ORDER BY u.created DESC ';
-
-        if ($offset > 0) {
-            if (common_config('db','type') == 'pgsql') {
-                $qry .= ' LIMIT ' . $limit . ' OFFSET ' . $offset;
-            } else {
-                $qry .= ' LIMIT ' . $offset . ', ' . $limit;
-            }
-        }
-
-        $application = new Oauth_application();
-
-        $cnt = $application->query(sprintf($qry, $this->id));
-
-        return $application;
-    }
-
     function subscriptionCount()
     {
         $c = common_memcache();
@@ -414,11 +446,9 @@ class Profile extends Memcached_DataObject
 
         $sub = new Subscription();
         $sub->subscribed = $this->id;
-
+        $sub->whereAdd('subscriber != subscribed');
         $cnt = (int) $sub->count('distinct subscriber');
 
-        $cnt = ($cnt > 0) ? $cnt - 1 : $cnt;
-
         if (!empty($c)) {
             $c->set(common_cache_key('profile:subscriber_count:'.$this->id), $cnt);
         }
@@ -426,6 +456,64 @@ class Profile extends Memcached_DataObject
         return $cnt;
     }
 
+    /**
+     * Is this profile subscribed to another profile?
+     *
+     * @param Profile $other
+     * @return boolean
+     */
+    function isSubscribed($other)
+    {
+        return Subscription::exists($this, $other);
+    }
+
+    /**
+     * Are these two profiles subscribed to each other?
+     *
+     * @param Profile $other
+     * @return boolean
+     */
+    function mutuallySubscribed($other)
+    {
+        return $this->isSubscribed($other) &&
+          $other->isSubscribed($this);
+    }
+
+    function hasFave($notice)
+    {
+        $cache = common_memcache();
+
+        // XXX: Kind of a hack.
+
+        if (!empty($cache)) {
+            // This is the stream of favorite notices, in rev chron
+            // order. This forces it into cache.
+
+            $ids = Fave::stream($this->id, 0, NOTICE_CACHE_WINDOW);
+
+            // If it's in the list, then it's a fave
+
+            if (in_array($notice->id, $ids)) {
+                return true;
+            }
+
+            // If we're not past the end of the cache window,
+            // then the cache has all available faves, so this one
+            // is not a fave.
+
+            if (count($ids) < NOTICE_CACHE_WINDOW) {
+                return false;
+            }
+
+            // Otherwise, cache doesn't have all faves;
+            // fall through to the default
+        }
+
+        $fave = Fave::pkeyGet(array('user_id' => $this->id,
+                                    'notice_id' => $notice->id));
+        return ((is_null($fave)) ? false : true);
+    }
+
     function faveCount()
     {
         $c = common_memcache();
@@ -469,6 +557,20 @@ class Profile extends Memcached_DataObject
         return $cnt;
     }
 
+    function blowFavesCache()
+    {
+        $cache = common_memcache();
+        if ($cache) {
+            // Faves don't happen chronologically, so we need to blow
+            // ;last cache, too
+            $cache->delete(common_cache_key('fave:ids_by_user:'.$this->id));
+            $cache->delete(common_cache_key('fave:ids_by_user:'.$this->id.';last'));
+            $cache->delete(common_cache_key('fave:ids_by_user_own:'.$this->id));
+            $cache->delete(common_cache_key('fave:ids_by_user_own:'.$this->id.';last'));
+        }
+        $this->blowFaveCount();
+    }
+
     function blowSubscriberCount()
     {
         $c = common_memcache();
@@ -524,9 +626,11 @@ class Profile extends Memcached_DataObject
         $this->_deleteMessages();
         $this->_deleteTags();
         $this->_deleteBlocks();
+        $this->delete_avatars();
 
-        $related = array('Avatar',
-                         'Reply',
+        // Warning: delete() will run on the batch objects,
+        // not on individual objects.
+        $related = array('Reply',
                          'Group_member',
                          );
         Event::handle('ProfileDeleteRelated', array($this, &$related));
@@ -557,11 +661,41 @@ class Profile extends Memcached_DataObject
     {
         $sub = new Subscription();
         $sub->subscriber = $this->id;
-        $sub->delete();
+
+        $sub->find();
+
+        while ($sub->fetch()) {
+            $other = Profile::staticGet('id', $sub->subscribed);
+            if (empty($other)) {
+                continue;
+            }
+            if ($other->id == $this->id) {
+                continue;
+            }
+            Subscription::cancel($this, $other);
+        }
 
         $subd = new Subscription();
         $subd->subscribed = $this->id;
-        $subd->delete();
+        $subd->find();
+
+        while ($subd->fetch()) {
+            $other = Profile::staticGet('id', $subd->subscriber);
+            if (empty($other)) {
+                continue;
+            }
+            if ($other->id == $this->id) {
+                continue;
+            }
+            Subscription::cancel($other, $this);
+        }
+
+        $self = new Subscription();
+
+        $self->subscriber = $this->id;
+        $self->subscribed = $this->id;
+
+        $self->delete();
     }
 
     function _deleteMessages()
@@ -632,39 +766,52 @@ class Profile extends Memcached_DataObject
 
     function grantRole($name)
     {
-        $role = new Profile_role();
+        if (Event::handle('StartGrantRole', array($this, $name))) {
 
-        $role->profile_id = $this->id;
-        $role->role       = $name;
-        $role->created    = common_sql_now();
+            $role = new Profile_role();
 
-        $result = $role->insert();
+            $role->profile_id = $this->id;
+            $role->role       = $name;
+            $role->created    = common_sql_now();
 
-        if (!$result) {
-            common_log_db_error($role, 'INSERT', __FILE__);
-            return false;
+            $result = $role->insert();
+
+            if (!$result) {
+                throw new Exception("Can't save role '$name' for profile '{$this->id}'");
+            }
+
+            Event::handle('EndGrantRole', array($this, $name));
         }
 
-        return true;
+        return $result;
     }
 
     function revokeRole($name)
     {
-        $role = Profile_role::pkeyGet(array('profile_id' => $this->id,
-                                            'role' => $name));
+        if (Event::handle('StartRevokeRole', array($this, $name))) {
 
-        if (empty($role)) {
-            throw new Exception('Cannot revoke role "'.$name.'" for user #'.$this->id.'; does not exist.');
-        }
+            $role = Profile_role::pkeyGet(array('profile_id' => $this->id,
+                                                'role' => $name));
+
+            if (empty($role)) {
+                // TRANS: Exception thrown when trying to revoke an existing role for a user that does not exist.
+                // TRANS: %1$s is the role name, %2$s is the user ID (number).
+                throw new Exception(sprintf(_('Cannot revoke role "%1$s" for user #%2$d; does not exist.'),$name, $this->id));
+            }
 
-        $result = $role->delete();
+            $result = $role->delete();
 
-        if (!$result) {
-            common_log_db_error($role, 'DELETE', __FILE__);
-            throw new Exception('Cannot revoke role "'.$name.'" for user #'.$this->id.'; database error.');
-        }
+            if (!$result) {
+                common_log_db_error($role, 'DELETE', __FILE__);
+                // TRANS: Exception thrown when trying to revoke a role for a user with a failing database query.
+                // TRANS: %1$s is the role name, %2$s is the user ID (number).
+                throw new Exception(sprintf(_('Cannot revoke role "%1$s" for user #%2$d; database error.'),$name, $this->id));
+            }
 
-        return true;
+            Event::handle('EndRevokeRole', array($this, $name));
+
+            return true;
+        }
     }
 
     function isSandboxed()
@@ -708,10 +855,14 @@ class Profile extends Memcached_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)
     {
         $result = false;
+
+        if ($this->hasRole(Profile_role::DELETED)) {
+            return false;
+        }
+
         if (Event::handle('UserRightsCheck', array($this, $right, &$result))) {
             switch ($right)
             {
@@ -720,14 +871,20 @@ class Profile extends Memcached_DataObject
             case Right::SANDBOXUSER:
             case Right::SILENCEUSER:
             case Right::DELETEUSER:
+            case Right::DELETEGROUP:
                 $result = $this->hasRole(Profile_role::MODERATOR);
                 break;
             case Right::CONFIGURESITE:
                 $result = $this->hasRole(Profile_role::ADMINISTRATOR);
                 break;
+            case Right::GRANTROLE:
+            case Right::REVOKEROLE:
+                $result = $this->hasRole(Profile_role::OWNER);
+                break;
             case Right::NEWNOTICE:
             case Right::NEWMESSAGE:
             case Right::SUBSCRIBE:
+            case Right::CREATEGROUP:
                 $result = !$this->isSilenced();
                 break;
             case Right::PUBLICNOTICE:
@@ -761,15 +918,23 @@ class Profile extends Memcached_DataObject
      *
      * Assumes that Atom has been previously set up as the base namespace.
      *
+     * @param Profile $cur the current authenticated user
+     *
      * @return string
      */
-    function asAtomAuthor()
+    function asAtomAuthor($cur = null)
     {
         $xs = new XMLStringer(true);
 
         $xs->elementStart('author');
         $xs->element('name', null, $this->nickname);
         $xs->element('uri', null, $this->getUri());
+        if ($cur != null) {
+            $attrs = Array();
+            $attrs['following'] = $cur->isSubscribed($this) ? 'true' : 'false';
+            $attrs['blocking']  = $cur->hasBlocked($this) ? 'true' : 'false';
+            $xs->element('statusnet:profile_info', $attrs, null);
+        }
         $xs->elementEnd('author');
 
         return $xs->getString();
@@ -847,4 +1012,41 @@ class Profile extends Memcached_DataObject
 
         return $result;
     }
+
+    function getAtomFeed()
+    {
+        $feed = null;
+
+        if (Event::handle('StartProfileGetAtomFeed', array($this, &$feed))) {
+            $user = User::staticGet('id', $this->id);
+            if (!empty($user)) {
+                $feed = common_local_url('ApiTimelineUser', array('id' => $user->id,
+                                                                  'format' => 'atom'));
+            }
+            Event::handle('EndProfileGetAtomFeed', array($this, $feed));
+        }
+
+        return $feed;
+    }
+
+    static function fromURI($uri)
+    {
+        $profile = null;
+
+        if (Event::handle('StartGetProfileFromURI', array($uri, &$profile))) {
+            // Get a local user or remote (OMB 0.1) profile
+            $user = User::staticGet('uri', $uri);
+            if (!empty($user)) {
+                $profile = $user->getProfile();
+            } else {
+                $remote_profile = Remote_profile::staticGet('uri', $uri);
+                if (!empty($remote_profile)) {
+                    $profile = Profile::staticGet('id', $remote_profile->profile_id);
+                }
+            }
+            Event::handle('EndGetProfileFromURI', array($uri, $profile));
+        }
+
+        return $profile;
+    }
 }