X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=classes%2FProfile.php;h=ea87d217ab4112706d6673b2bda6a6cac82bdea0;hb=bc554ea9c233943e7b1077d724aa8070fdb7b0f9;hp=2f734aafd7223e4e0584442e831093427469b184;hpb=390556d932335e5f5349c4b439f1862aebe87df2;p=quix0rs-gnu-social.git diff --git a/classes/Profile.php b/classes/Profile.php index 2f734aafd7..ea87d217ab 100644 --- a/classes/Profile.php +++ b/classes/Profile.php @@ -99,13 +99,37 @@ class Profile extends Managed_DataObject if ($this->_user === -1) { $this->_user = User::getKV('id', $this->id); } - if (!($this->_user instanceof User)) { + if (!$this->_user instanceof User) { throw new NoSuchUserException(array('id'=>$this->id)); } return $this->_user; } + protected $_group = -1; + + public function getGroup() + { + if ($this->_group === -1) { + $this->_group = User_group::getKV('profile_id', $this->id); + } + if (!$this->_group instanceof User_group) { + throw new NoSuchGroupException(array('profile_id'=>$this->id)); + } + + return $this->_group; + } + + public function isGroup() + { + try { + $this->getGroup(); + return true; + } catch (NoSuchGroupException $e) { + return false; + } + } + public function isLocal() { try { @@ -168,6 +192,20 @@ class Profile extends Managed_DataObject return ($this->fullname) ? $this->fullname : $this->nickname; } + /** + * Takes the currently scoped profile into account to give a name + * to list in notice streams. Preferences may differ between profiles. + */ + function getStreamName() + { + $user = common_current_user(); + if ($user instanceof User && $user->streamNicknames()) { + return $this->nickname; + } + + return $this->getBestName(); + } + /** * Gets the full name (if filled) with nickname as a parenthetical, or the nickname alone * if no fullname is provided. @@ -218,19 +256,18 @@ class Profile extends Managed_DataObject return $stream->getNotices($offset, $limit, $since_id, $max_id); } - function isMember($group) + function isMember(User_group $group) { $groups = $this->getGroups(0, null); - $gs = $groups->fetchAll(); - foreach ($gs as $g) { - if ($group->id == $g->id) { + while ($groups instanceof User_group && $groups->fetch()) { + if ($groups->id == $group->id) { return true; } } return false; } - function isAdmin($group) + function isAdmin(User_group $group) { $gm = Group_member::pkeyGet(array('profile_id' => $this->id, 'group_id' => $group->id)); @@ -272,7 +309,18 @@ class Profile extends Managed_DataObject $ids = array_slice($ids, $offset, $limit); } - return User_group::multiGet('id', $ids); + try { + return User_group::listFind('id', $ids); + } catch (NoResultException $e) { + return null; // throw exception when we handle it everywhere + } + } + + function getGroupCount() { + $groups = $this->getGroups(0, null); + return $groups instanceof User_group + ? $groups->N + : 0; } function isTagged($peopletag) @@ -568,29 +616,50 @@ class Profile extends Managed_DataObject 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; } /** @@ -732,6 +801,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(); @@ -816,7 +890,34 @@ class Profile extends Managed_DataObject return ($biolimit > 0 && !empty($bio) && (mb_strlen($bio) > $biolimit)); } - function delete() + function update($dataObject=false) + { + if (is_object($dataObject) && $this->nickname != $dataObject->nickname) { + try { + $local = $this->getUser(); + common_debug("Updating User ({$this->id}) nickname from {$dataObject->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'); + } + } catch (NoSuchUserException $e) { + // Nevermind... + } + } + + return parent::update($dataObject); + } + + function delete($useWhere=false) { $this->_deleteNotices(); $this->_deleteSubscriptions(); @@ -838,7 +939,7 @@ class Profile extends Managed_DataObject $inst->delete(); } - parent::delete(); + return parent::delete($useWhere); } function _deleteNotices() @@ -1298,6 +1399,26 @@ class Profile extends Managed_DataObject return $uri; } + /** + * Returns an assumed acct: URI for a profile. Plugins are required. + * + * @return string $uri + */ + public function getAcctUri() + { + $acct = null; + + if (Event::handle('StartGetProfileAcctUri', array($this, &$acct))) { + Event::handle('EndGetProfileAcctUri', array($this, &$acct)); + } + + if ($acct === null) { + throw new ProfileNoAcctUriException($this); + } + + return $acct; + } + function hasBlocked($other) { $block = Profile_block::exists($this, $other); @@ -1325,7 +1446,7 @@ class Profile extends Managed_DataObject $profile = null; if (Event::handle('StartGetProfileFromURI', array($uri, &$profile))) { - // Get a local user or remote (OMB 0.1) profile + // Get a local user $user = User::getKV('uri', $uri); if (!empty($user)) { $profile = $user->getProfile();