X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=classes%2FUser.php;h=e784fd9e9a7278ec8c49955bcdeb5ad94588facc;hb=8fc904e95b66ac4ce97fa3b493797da9aa0a54af;hp=10b1f486513a4bb5b38ecc5c48d4f8e371c5b684;hpb=4a71753f2046a17a98c139cc308ff7f77bd4f271;p=quix0rs-gnu-social.git diff --git a/classes/User.php b/classes/User.php index 10b1f48651..e784fd9e9a 100644 --- a/classes/User.php +++ b/classes/User.php @@ -75,7 +75,11 @@ class User extends Memcached_DataObject function getProfile() { - return Profile::staticGet('id', $this->id); + $profile = Profile::staticGet('id', $this->id); + if (empty($profile)) { + throw new UserNoProfileException($this); + } + return $profile; } function isSubscribed($other) @@ -87,6 +91,7 @@ class User extends Memcached_DataObject function updateKeys(&$orig) { + $this->_connect(); $parts = array(); foreach (array('nickname', 'email', 'jabber', 'incomingemail', 'sms', 'carrier', 'smsemail', 'language', 'timezone') as $k) { if (strcmp($this->$k, $orig->$k) != 0) { @@ -132,13 +137,15 @@ class User extends Memcached_DataObject return !in_array($nickname, $blacklist); } - function getCurrentNotice($dt=null) + /** + * Get the most recent notice posted by this user, if any. + * + * @return mixed Notice or null + */ + function getCurrentNotice() { $profile = $this->getProfile(); - if (!$profile) { - return null; - } - return $profile->getCurrentNotice($dt); + return $profile->getCurrentNotice(); } function getCarrier() @@ -146,19 +153,12 @@ class User extends Memcached_DataObject return Sms_carrier::staticGet('id', $this->carrier); } + /** + * @deprecated use Subscription::start($sub, $other); + */ function subscribeTo($other) { - $sub = new Subscription(); - $sub->subscriber = $this->id; - $sub->subscribed = $other->id; - - $sub->created = common_sql_now(); // current time - - if (!$sub->insert()) { - return false; - } - - return true; + return Subscription::start($this->getProfile(), $other); } function hasBlocked($other) @@ -206,6 +206,7 @@ class User extends Memcached_DataObject if(! User::allowed_nickname($nickname)){ common_log(LOG_WARNING, sprintf("Attempted to register a nickname that is not allowed: %s", $profile->nickname), __FILE__); + return false; } $profile->profileurl = common_profile_url($nickname); @@ -254,6 +255,19 @@ class User extends Memcached_DataObject $user->inboxed = 1; + // Set default-on options here, otherwise they'll be disabled + // initially for sites using caching, since the initial encache + // doesn't know about the defaults in the database. + $user->emailnotifysub = 1; + $user->emailnotifyfav = 1; + $user->emailnotifynudge = 1; + $user->emailnotifymsg = 1; + $user->emailnotifyattn = 1; + $user->emailmicroid = 1; + $user->emailpost = 1; + $user->jabbermicroid = 1; + $user->viewdesigns = 1; + $user->created = common_sql_now(); if (Event::handle('StartUserRegister', array(&$user, &$profile))) { @@ -268,7 +282,13 @@ class User extends Memcached_DataObject } $user->id = $id; - $user->uri = common_user_uri($user); + + if (!empty($uri)) { + $user->uri = $uri; + } else { + $user->uri = common_user_uri($user); + } + if (!empty($password)) { // may not have a password for OpenID users $user->password = common_munge_password($password, $id); } @@ -338,17 +358,7 @@ class User extends Memcached_DataObject common_log(LOG_WARNING, sprintf("Default user %s does not exist.", $defnick), __FILE__); } else { - $defsub = new Subscription(); - $defsub->subscriber = $user->id; - $defsub->subscribed = $defuser->id; - $defsub->created = $user->created; - - $result = $defsub->insert(); - - if (!$result) { - common_log_db_error($defsub, 'INSERT', __FILE__); - return false; - } + Subscription::start($user, $defuser); } } @@ -369,11 +379,12 @@ class User extends Memcached_DataObject __FILE__); } else { $notice = Notice::saveNew($welcomeuser->id, + // TRANS: Notice given on user registration. + // TRANS: %1$s is the sitename, $2$s is the registering user's nickname. sprintf(_('Welcome to %1$s, @%2$s!'), common_config('site', 'name'), $user->nickname), 'system'); - } } @@ -384,7 +395,6 @@ class User extends Memcached_DataObject } // Things we do when the email changes - function emailChanged() { @@ -402,37 +412,8 @@ class User extends Memcached_DataObject function hasFave($notice) { - $cache = common_memcache(); - - // XXX: Kind of a hack. - - if ($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); + $profile = $this->getProfile(); + return $profile->hasFave($notice); } function mutuallySubscribed($other) @@ -456,70 +437,53 @@ class User extends Memcached_DataObject return $user; } - function getReplies($offset=0, $limit=NOTICES_PER_PAGE, $since_id=0, $before_id=0, $since=null) + function getReplies($offset=0, $limit=NOTICES_PER_PAGE, $since_id=0, $before_id=0) { - $ids = Reply::stream($this->id, $offset, $limit, $since_id, $before_id, $since); + $ids = Reply::stream($this->id, $offset, $limit, $since_id, $before_id); return Notice::getStreamByIds($ids); } - function getTaggedNotices($tag, $offset=0, $limit=NOTICES_PER_PAGE, $since_id=0, $before_id=0, $since=null) { + function getTaggedNotices($tag, $offset=0, $limit=NOTICES_PER_PAGE, $since_id=0, $before_id=0) { $profile = $this->getProfile(); - if (!$profile) { - return null; - } else { - return $profile->getTaggedNotices($tag, $offset, $limit, $since_id, $before_id, $since); - } + return $profile->getTaggedNotices($tag, $offset, $limit, $since_id, $before_id); } - function getNotices($offset=0, $limit=NOTICES_PER_PAGE, $since_id=0, $before_id=0, $since=null) + function getNotices($offset=0, $limit=NOTICES_PER_PAGE, $since_id=0, $before_id=0) { $profile = $this->getProfile(); - if (!$profile) { - return null; - } else { - return $profile->getNotices($offset, $limit, $since_id, $before_id, $since); - } + return $profile->getNotices($offset, $limit, $since_id, $before_id); } - function favoriteNotices($offset=0, $limit=NOTICES_PER_PAGE, $own=false) + function favoriteNotices($own=false, $offset=0, $limit=NOTICES_PER_PAGE, $since_id=0, $max_id=0) { - $ids = Fave::stream($this->id, $offset, $limit, $own); + $ids = Fave::stream($this->id, $offset, $limit, $own, $since_id, $max_id); return Notice::getStreamByIds($ids); } - function noticesWithFriends($offset=0, $limit=NOTICES_PER_PAGE, $since_id=0, $before_id=0, $since=null) + function noticesWithFriends($offset=0, $limit=NOTICES_PER_PAGE, $since_id=0, $before_id=0) { - return Inbox::streamNotices($this->id, $offset, $limit, $since_id, $before_id, $since, false); + return Inbox::streamNotices($this->id, $offset, $limit, $since_id, $before_id, false); } - function noticeInbox($offset=0, $limit=NOTICES_PER_PAGE, $since_id=0, $before_id=0, $since=null) + function noticeInbox($offset=0, $limit=NOTICES_PER_PAGE, $since_id=0, $before_id=0) { - return Inbox::streamNotices($this->id, $offset, $limit, $since_id, $before_id, $since, true); + return Inbox::streamNotices($this->id, $offset, $limit, $since_id, $before_id, true); } - function friendsTimeline($offset=0, $limit=NOTICES_PER_PAGE, $since_id=0, $before_id=0, $since=null) + function friendsTimeline($offset=0, $limit=NOTICES_PER_PAGE, $since_id=0, $before_id=0) { - return Inbox::streamNotices($this->id, $offset, $limit, $since_id, $before_id, $since, false); + return Inbox::streamNotices($this->id, $offset, $limit, $since_id, $before_id, false); } - function ownFriendsTimeline($offset=0, $limit=NOTICES_PER_PAGE, $since_id=0, $before_id=0, $since=null) + function ownFriendsTimeline($offset=0, $limit=NOTICES_PER_PAGE, $since_id=0, $before_id=0) { - return Inbox::streamNotices($this->id, $offset, $limit, $since_id, $before_id, $since, true); + return Inbox::streamNotices($this->id, $offset, $limit, $since_id, $before_id, true); } 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')); - } $profile = $this->getProfile(); - $profile->blowFaveCount(); + $profile->blowFavesCache(); } function getSelfTags() @@ -541,9 +505,9 @@ class User extends Memcached_DataObject if ($this->id == $other->id) { common_log(LOG_WARNING, sprintf( - "Profile ID %d (%s) tried to block his or herself.", - $profile->id, - $profile->nickname + "Profile ID %d (%s) tried to block themself.", + $this->id, + $this->nickname ) ); return false; @@ -565,12 +529,12 @@ class User extends Memcached_DataObject return false; } - // Cancel their subscription, if it exists - - $otherUser = User::staticGet('id', $other->id); - - if (!empty($otherUser)) { - subs_unsubscribe_to($otherUser, $this->getProfile()); + $self = $this->getProfile(); + if (Subscription::exists($other, $self)) { + Subscription::cancel($other, $self); + } + if (Subscription::exists($self, $other)) { + Subscription::cancel($self, $other); } $block->query('COMMIT'); @@ -612,41 +576,19 @@ class User 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; + $profile = $this->getProfile(); + return $profile->getGroups($offset, $limit); } function getSubscriptions($offset=0, $limit=null) { $profile = $this->getProfile(); - assert(!empty($profile)); return $profile->getSubscriptions($offset, $limit); } function getSubscribers($offset=0, $limit=null) { $profile = $this->getProfile(); - assert(!empty($profile)); return $profile->getSubscribers($offset, $limit); } @@ -709,9 +651,11 @@ class User extends Memcached_DataObject function delete() { - $profile = $this->getProfile(); - if ($profile) { + try { + $profile = $this->getProfile(); $profile->delete(); + } catch (UserNoProfileException $unp) { + common_log(LOG_INFO, "User {$this->nickname} has no profile; continuing deletion."); } $related = array('Fave', @@ -720,6 +664,7 @@ class User extends Memcached_DataObject 'Foreign_link', 'Invitation', ); + Event::handle('UserDeleteRelated', array($this, &$related)); foreach ($related as $cls) { @@ -789,7 +734,7 @@ class User extends Memcached_DataObject return Notice::getStreamByIds($ids); } - function _repeatedByMeDirect($offset, $limit, $since_id, $max_id, $since) + function _repeatedByMeDirect($offset, $limit, $since_id, $max_id) { $notice = new Notice(); @@ -813,10 +758,6 @@ class User extends Memcached_DataObject $notice->whereAdd('id <= ' . $max_id); } - if (!is_null($since)) { - $notice->whereAdd('created > \'' . date('Y-m-d H:i:s', $since) . '\''); - } - $ids = array(); if ($notice->find()) { @@ -836,12 +777,12 @@ class User extends Memcached_DataObject $ids = Notice::stream(array($this, '_repeatsOfMeDirect'), array(), 'user:repeats_of_me:'.$this->id, - $offset, $limit, $since_id, $max_id, null); + $offset, $limit, $since_id, $max_id); return Notice::getStreamByIds($ids); } - function _repeatsOfMeDirect($offset, $limit, $since_id, $max_id, $since) + function _repeatsOfMeDirect($offset, $limit, $since_id, $max_id) { $qry = 'SELECT DISTINCT original.id AS id ' . @@ -856,10 +797,6 @@ class User extends Memcached_DataObject $qry .= 'AND original.id <= ' . $max_id . ' '; } - if (!is_null($since)) { - $qry .= 'AND original.modified > \'' . date('Y-m-d H:i:s', $since) . '\' '; - } - // NOTE: we sort by fave time, not by notice time! $qry .= 'ORDER BY original.id DESC ';