]> git.mxchange.org Git - quix0rs-gnu-social.git/blobdiff - classes/Profile.php
Properly unlink all old avatars when deleting/uploading a new
[quix0rs-gnu-social.git] / classes / Profile.php
index 5eebd64a0d39401fd75a2e1db38af1f8abc80b71..afb3df6a5b8cebf98f386aa24a93d2d2ac2ea801 100644 (file)
@@ -24,7 +24,7 @@ if (!defined('STATUSNET') && !defined('LACONICA')) { exit(1); }
  */
 require_once INSTALLDIR.'/classes/Memcached_DataObject.php';
 
-class Profile extends Memcached_DataObject
+class Profile extends Managed_DataObject
 {
     ###START_AUTOCODE
     /* the code below is auto generated do not remove the above tag */
@@ -44,56 +44,145 @@ class Profile extends Memcached_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(
+            'description' => 'local and remote users have profiles',
+            'fields' => array(
+                'id' => array('type' => 'serial', 'not null' => true, 'description' => 'unique identifier'),
+                'nickname' => array('type' => 'varchar', 'length' => 64, 'not null' => true, 'description' => 'nickname or username', 'collate' => 'utf8_general_ci'),
+                'fullname' => array('type' => 'varchar', 'length' => 255, 'description' => 'display name', 'collate' => 'utf8_general_ci'),
+                'profileurl' => array('type' => 'varchar', 'length' => 255, 'description' => 'URL, cached so we dont regenerate'),
+                'homepage' => array('type' => 'varchar', 'length' => 255, 'description' => 'identifying URL', 'collate' => 'utf8_general_ci'),
+                'bio' => array('type' => 'text', 'description' => 'descriptive biography', 'collate' => 'utf8_general_ci'),
+                'location' => array('type' => 'varchar', 'length' => 255, 'description' => 'physical location', 'collate' => 'utf8_general_ci'),
+                'lat' => array('type' => 'numeric', 'precision' => 10, 'scale' => 7, 'description' => 'latitude'),
+                'lon' => array('type' => 'numeric', 'precision' => 10, 'scale' => 7, 'description' => 'longitude'),
+                'location_id' => array('type' => 'int', 'description' => 'location id if possible'),
+                'location_ns' => array('type' => 'int', 'description' => 'namespace for location'),
+
+                'created' => array('type' => 'datetime', 'not null' => true, 'description' => 'date this record was created'),
+                'modified' => array('type' => 'timestamp', 'not null' => true, 'description' => 'date this record was modified'),
+            ),
+            'primary key' => array('id'),
+            'indexes' => array(
+                'profile_nickname_idx' => array('nickname'),
+            )
+        );
+
+        // Add a fulltext index
 
-       function multiGet($keyCol, $keyVals, $skipNulls=true)
-       {
-           return parent::multiGet('Profile', $keyCol, $keyVals, $skipNulls);
-       }
+        if (common_config('search', 'type') == 'fulltext') {
+            $def['fulltext indexes'] = array('nickname' => array('nickname', 'fullname', 'location', 'bio', 'homepage'));
+        }
+
+        return $def;
+    }
        
     /* the code above is auto generated do not remove the tag below */
     ###END_AUTOCODE
 
+    public static function getByEmail($email)
+    {
+        // in the future, profiles should have emails stored...
+        $user = User::getKV('email', $email);
+        if (!($user instanceof User)) {
+            throw new NoSuchUserException(array('email'=>$email));
+        }
+        return $user->getProfile();
+    } 
+
     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;
     }
 
-    function getAvatar($width, $height=null)
+    public function isLocal()
+    {
+        try {
+            $this->getUser();
+        } catch (NoSuchUserException $e) {
+            return false;
+        }
+        return true;
+    }
+
+    public function getAvatar($width, $height=null)
     {
+        $width = (int) floor($width);
+
         if (is_null($height)) {
             $height = $width;
         }
 
-        $avatar = null;
-
+        try {
+            return $this->_getAvatar($width);
+        } catch (Exception $e) {
+            $avatar = null;
+        }
+        
         if (Event::handle('StartProfileGetAvatar', array($this, $width, &$avatar))) {
-            $avatar = Avatar::pkeyGet(array('profile_id' => $this->id,
-                                            'width' => $width,
-                                            'height' => $height));
+            $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);
+        }
+
+        // cache the avatar for future use
+        $this->_fillAvatar($width, $avatar);
+
         return $avatar;
     }
 
-    function getOriginalAvatar()
+    protected $_avatars = array();
+
+    // XXX: @Fix me gargargar
+    function _getAvatar($width)
     {
-        $avatar = DB_DataObject::factory('avatar');
-        $avatar->profile_id = $this->id;
-        $avatar->original = true;
-        if ($avatar->find(true)) {
-            return $avatar;
-        } else {
+        // GAR! I cannot figure out where _avatars gets pre-filled with the avatar from
+        // the previously used profile! Please shoot me now! --Zach
+        if (array_key_exists($width, $this->_avatars)) {
+            // Don't return cached avatar unless it's really for this profile
+            if ($this->_avatars[$width]->profile_id == $this->id) {
+                return $this->_avatars[$width];
+            }
+        }
+
+        throw new Exception('No cached avatar available for size ');
+    }
+
+    protected function _fillAvatar($width, $avatar)
+    {
+        // This avoids storing null values, a problem report in issue #3478
+        if (!empty($avatar)) {
+           $this->_avatars[$width] = $avatar;
+        }
+    }
+
+    // For backwards compatibility only!
+    public function getOriginalAvatar()
+    {
+        try {
+            return Avatar::getOriginal($this);
+        } catch (Exception $e) {
             return null;
         }
     }
@@ -113,8 +202,8 @@ class Profile extends Memcached_DataObject
         $avatar->created = DB_DataObject_Cast::dateTime(); # current time
 
         // XXX: start a transaction here
-
-        if (!$this->delete_avatars() || !$avatar->insert()) {
+        if (!Avatar::deleteFromProfile($this, true) || !$avatar->insert()) {
+            // If we can't delete the old avatars, let's abort right here.
             @unlink(Avatar::path($filename));
             return null;
         }
@@ -122,21 +211,10 @@ 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');
-                $scaled = new Avatar();
-                $scaled->profile_id = $this->id;
-                $scaled->width = $size;
-                $scaled->height = $size;
-                $scaled->original = false;
-                $scaled->mediatype = image_type_to_mime_type($imagefile->type);
-                $scaled->filename = $scaled_filename;
-                $scaled->url = Avatar::url($scaled_filename);
-                $scaled->created = DB_DataObject_Cast::dateTime(); # current time
-
-                if (!$scaled->insert()) {
-                    return null;
+                try {
+                    Avatar::newSize($this, $size);
+                } catch (Exception $e) {
+                    // should we abort the generation and live without smaller avatars?
                 }
             }
         }
@@ -144,30 +222,6 @@ 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();
-        $avatar->profile_id = $this->id;
-        $avatar->find();
-        while ($avatar->fetch()) {
-            if ($avatar->original) {
-                if ($original == false) {
-                    continue;
-                }
-            }
-            $avatar->delete();
-        }
-        return true;
-    }
-
     /**
      * Gets either the full name (if filled) or the nickname.
      *
@@ -230,9 +284,14 @@ class Profile extends Memcached_DataObject
 
     function isMember($group)
     {
-        $gm = Group_member::pkeyGet(array('profile_id' => $this->id,
-                                          'group_id' => $group->id));
-        return (!empty($gm));
+       $groups = $this->getGroups(0, null);
+       $gs = $groups->fetchAll();
+       foreach ($gs as $g) {
+           if ($group->id == $g->id) {
+               return true;
+           }
+       }
+       return false;
     }
 
     function isAdmin($group)
@@ -273,6 +332,10 @@ class Profile extends Memcached_DataObject
             self::cacheSet($keypart, implode(',', $ids));
         }
 
+        if (!is_null($offset) && !is_null($limit)) {
+            $ids = array_slice($ids, $offset, $limit);
+        }
+
         return User_group::multiGet('id', $ids);
     }
 
@@ -364,7 +427,7 @@ class Profile extends Memcached_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)) {
 
@@ -379,41 +442,55 @@ class Profile extends Memcached_DataObject
         return new ArrayWrapper($lists);
     }
 
+    /**
+     * Get tags that other people put on this profile, in reverse-chron order
+     *
+     * @param (Profile|User) $auth_user  Authorized user (used for privacy)
+     * @param int            $offset     Offset from latest
+     * @param int            $limit      Max number to get
+     * @param datetime       $since_id   max date
+     * @param datetime       $max_id     min date
+     *
+     * @return Profile_list resulting lists
+     */
+
     function getOtherTags($auth_user=null, $offset=0, $limit=null, $since_id=0, $max_id=0)
     {
-        $lists = new Profile_list();
+        $list = new Profile_list();
 
-        $tags = new Profile_tag();
-        $tags->tagged = $this->id;
+        $qry = sprintf('select profile_list.*, unix_timestamp(profile_tag.modified) as "cursor" ' .
+                       'from profile_tag join profile_list '.
+                       'on (profile_tag.tagger = profile_list.tagger ' .
+                       '    and profile_tag.tag = profile_list.tag) ' .
+                       'where profile_tag.tagged = %d ',
+                       $this->id);
 
-        $lists->joinAdd($tags);
-        #@fixme: postgres (round(date_part('epoch', my_date)))
-        $lists->selectAdd('unix_timestamp(profile_tag.modified) as "cursor"');
 
         if ($auth_user instanceof User || $auth_user instanceof Profile) {
-            $lists->whereAdd('( ( profile_list.private = false ) ' .
-                             'OR ( profile_list.tagger = ' . $auth_user->id . ' AND ' .
-                             'profile_list.private = true ) )');
+            $qry .= sprintf('AND ( ( profile_list.private = false ) ' .
+                            'OR ( profile_list.tagger = %d AND ' .
+                            'profile_list.private = true ) )',
+                            $auth_user->id);
         } else {
-            $lists->private = false;
+            $qry .= 'AND profile_list.private = 0 ';
         }
 
-        if ($since_id>0) {
-           $lists->whereAdd('cursor > '.$since_id);
+        if ($since_id > 0) {
+            $qry .= sprintf('AND (cursor > %d) ', $since_id);
         }
 
-        if ($max_id>0) {
-            $lists->whereAdd('cursor <= '.$max_id);
+        if ($max_id > 0) {
+            $qry .= sprintf('AND (cursor < %d) ', $max_id);
         }
 
-        if($offset>=0 && !is_null($limit)) {
-            $lists->limit($offset, $limit);
-        }
+        $qry .= 'ORDER BY profile_tag.modified DESC ';
 
-        $lists->orderBy('profile_tag.modified DESC');
-        $lists->find();
+        if ($offset >= 0 && !is_null($limit)) {
+            $qry .= sprintf('LIMIT %d OFFSET %d ', $limit, $offset);
+        }
 
-        return $lists;
+        $list->query($qry);
+        return $list;
     }
 
     function getPrivateTags($offset=0, $limit=null, $since_id=0, $max_id=0)
@@ -459,7 +536,8 @@ class Profile extends Memcached_DataObject
         $lists = new Profile_list();
         $subs = new Profile_tag_subscription();
 
-        $lists->joinAdd($subs);
+        $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"');
 
@@ -499,6 +577,8 @@ class Profile extends Memcached_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));
             }
         }
@@ -519,54 +599,35 @@ class Profile extends Memcached_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));
         }
     }
 
     function avatarUrl($size=AVATAR_PROFILE_SIZE)
     {
-        $avatar = $this->getAvatar($size);
-        if ($avatar) {
+        $size = floor($size);
+        try {
+            $avatar = $this->getAvatar($size);
             return $avatar->displayUrl();
-        } else {
+        } catch (Exception $e) {
             return Avatar::defaultImage($size);
         }
     }
 
-    function getSubscriptions($offset=0, $limit=null)
+    function getSubscribed($offset=0, $limit=null)
     {
-        $subs = Subscription::bySubscriber($this->id,
-                                           $offset,
-                                           $limit);
-
-        $profiles = array();
-
-        while ($subs->fetch()) {
-            $profile = Profile::staticGet($subs->subscribed);
-            if ($profile) {
-                $profiles[] = $profile;
-            }
-        }
-
-        return new ArrayWrapper($profiles);
+        $subs = Subscription::getSubscribedIDs($this->id, $offset, $limit);
+        $profiles = Profile::listFind('id', $subs);
+        return $profiles;
     }
 
     function getSubscribers($offset=0, $limit=null)
     {
-        $subs = Subscription::bySubscribed($this->id,
-                                           $offset,
-                                           $limit);
-
-        $profiles = array();
-
-        while ($subs->fetch()) {
-            $profile = Profile::staticGet($subs->subscriber);
-            if ($profile) {
-                $profiles[] = $profile;
-            }
-        }
-
-        return new ArrayWrapper($profiles);
+        $subs = Subscription::getSubscriberIDs($this->id, $offset, $limit);
+        $profiles = Profile::listFind('id', $subs);
+        return $profiles;
     }
 
     function getTaggedSubscribers($tag)
@@ -586,7 +647,7 @@ class Profile extends Memcached_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);
@@ -724,7 +785,7 @@ class Profile extends Memcached_DataObject
 
         $faves = new Fave();
         $faves->user_id = $this->id;
-        $cnt = (int) $faves->count('distinct notice_id');
+        $cnt = (int) $faves->count('notice_id');
 
         if (!empty($c)) {
             $c->set(Cache::key('profile:fave_count:'.$this->id), $cnt);
@@ -824,7 +885,7 @@ class Profile extends Memcached_DataObject
         $this->_deleteMessages();
         $this->_deleteTags();
         $this->_deleteBlocks();
-        $this->delete_avatars();
+        Avatar::deleteFromProfile($this, true);
 
         // Warning: delete() will run on the batch objects,
         // not on individual objects.
@@ -863,7 +924,7 @@ class Profile extends Memcached_DataObject
         $sub->find();
 
         while ($sub->fetch()) {
-            $other = Profile::staticGet('id', $sub->subscribed);
+            $other = Profile::getKV('id', $sub->subscribed);
             if (empty($other)) {
                 continue;
             }
@@ -878,7 +939,7 @@ class Profile extends Memcached_DataObject
         $subd->find();
 
         while ($subd->fetch()) {
-            $other = Profile::staticGet('id', $subd->subscriber);
+            $other = Profile::getKV('id', $subd->subscriber);
             if (empty($other)) {
                 continue;
             }
@@ -1043,11 +1104,27 @@ class Profile extends Memcached_DataObject
     function silence()
     {
         $this->grantRole(Profile_role::SILENCED);
+        if (common_config('notice', 'hidespam')) {
+            $this->flushVisibility();
+        }
     }
 
     function unsilence()
     {
         $this->revokeRole(Profile_role::SILENCED);
+        if (common_config('notice', 'hidespam')) {
+            $this->flushVisibility();
+        }
+    }
+
+    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);
+        }
     }
 
     /**
@@ -1078,6 +1155,8 @@ class Profile extends Memcached_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:
@@ -1129,9 +1208,8 @@ class Profile extends Memcached_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);
     }
@@ -1219,12 +1297,26 @@ class Profile extends Memcached_DataObject
         return $noun->asString('activity:' . $element);
     }
 
+    /**
+     * Returns the profile's canonical url, not necessarily a uri/unique id
+     *
+     * @return string $profileurl
+     */
+    public function getUrl()
+    {
+        if (empty($this->profileurl) ||
+                !filter_var($this->profileurl, FILTER_VALIDATE_URL)) {
+            throw new InvalidUrlException($this->profileurl);
+        }
+        return $this->profileurl;
+    }
+
     /**
      * Returns the best URI for a profile. Plugins may override.
      *
      * @return string $uri
      */
-    function getUri()
+    public function getUri()
     {
         $uri = null;
 
@@ -1232,7 +1324,7 @@ class Profile extends Memcached_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;
@@ -1246,15 +1338,8 @@ class Profile extends Memcached_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()
@@ -1262,7 +1347,7 @@ class Profile extends Memcached_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'));
@@ -1279,7 +1364,7 @@ class Profile extends Memcached_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();
             }
@@ -1353,7 +1438,33 @@ class Profile extends Memcached_DataObject
     function __sleep()
     {
         $vars = parent::__sleep();
-        $skip = array('_user');
+        $skip = array('_user', '_avatars');
         return array_diff($vars, $skip);
     }
+    
+    static function fillAvatars(&$profiles, $width)
+    {
+       $ids = array();
+       foreach ($profiles as $profile) {
+            if (!empty($profile)) {
+                $ids[] = $profile->id;
+            }
+       }
+       
+       $avatars = Avatar::pivotGet('profile_id', $ids, array('width' => $width,
+                                                                                                                         'height' => $width));
+       
+       foreach ($profiles as $profile) {
+            if (!empty($profile)) { // ???
+                $profile->_fillAvatar($width, $avatars[$profile->id]);
+            }
+       }
+    }
+    
+    // Can't seem to find how to fix this.
+
+    function getProfile()
+    {
+        return $this;
+    }
 }