Skip to content

Commit

Permalink
Consolidate User Profile loading
Browse files Browse the repository at this point in the history
Summary:
Introduce `PhabricatorUserQuery::attachProfilesForUsers()` for batch attachment of user profiles (for a future diff)
Introduce `PhabricatorUser::loadUserProfile()` to load attached user profiles (or load them on their own, if no one is attached).

USed them in code

Test Plan:
verified that use sites did not break

- Uploaded user profile image for bots
- changed my own profile image and blurb
- looked a lot at my own profile

Reviewers: epriestley, btrahan, chad

Reviewed By: epriestley

CC: aran, Korvin

Differential Revision: https://secure.phabricator.com/D5395
  • Loading branch information
AnhNhan authored and epriestley committed Mar 24, 2013
1 parent 1a48c7a commit b0e9371
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 21 deletions.
48 changes: 48 additions & 0 deletions src/applications/people/PhabricatorPeopleQuery.php
Expand Up @@ -8,6 +8,8 @@ final class PhabricatorPeopleQuery extends PhabricatorOffsetPagedQuery {
private $ids;

private $needPrimaryEmail;
private $needProfile;
private $needProfileImage;

public function withIds(array $ids) {
$this->ids = $ids;
Expand Down Expand Up @@ -35,6 +37,16 @@ public function needPrimaryEmail($need) {
return $this;
}

public function needProfile($need) {
$this->needProfile = $need;
return $this;
}

public function needProfileImage($need) {
$this->needProfileImage = $need;
return $this;
}

public function execute() {
$table = new PhabricatorUser();
$conn_r = $table->establishConnection('r');
Expand All @@ -56,6 +68,41 @@ public function execute() {
}

$users = $table->loadAllFromArray($data);

if ($this->needProfile) {
$user_list = mpull($users, null, 'getPHID');
$profiles = new PhabricatorUserProfile();
$profiles = $profiles->loadAllWhere('userPHID IN (%Ls)',
array_keys($user_list));

$profiles = mpull($profiles, null, 'getUserPHID');
foreach ($user_list as $user_phid => $user) {
$profile = idx($profiles, $user_phid);
if (!$profile) {
$profile = new PhabricatorUserProfile();
$profile->setUserPHID($user_phid);
}

$user->attachUserProfile($profile);
}
}

if ($this->needProfileImage) {
// Change this once we migrate this to CursorPagedPolicyAwareQuery
$files = id(new PhabricatorFile())
->loadAllWhere('phid IN (%Ls)', mpull($users, 'getProfileImagePHID'));
$files = mpull($files, null, 'getPHID');
foreach ($users as $user) {
$image_phid = $user->getProfileImagePHID();
if (isset($files[$image_phid])) {
$profile_image_uri = $files[$image_phid]->getBestURI();
} else {
$profile_image_uri = PhabricatorUser::getDefaultProfileImageURI();
}
$user->attachProfileImageURI($profile_image_uri);
}
}

return $users;
}

Expand Down Expand Up @@ -105,4 +152,5 @@ private function buildWhereClause($conn_r) {

return $this->formatWhereClause($where);
}

}
Expand Up @@ -715,12 +715,8 @@ private function processSetAccountPicture(PhabricatorUser $user) {
$request = $this->getRequest();
$admin = $request->getUser();

$profile = id(new PhabricatorUserProfile())->loadOneWhere(
'userPHID = %s',
$user->getPHID());
if (!$profile) {
$profile = new PhabricatorUserProfile();
$profile->setUserPHID($user->getPHID());
$profile = $user->loadUserProfile();
if (!$profile->getID()) {
$profile->setTitle('');
$profile->setBlurb('');
}
Expand Down
Expand Up @@ -52,12 +52,7 @@ public function processRequest() {

require_celerity_resource('phabricator-profile-css');

$profile = id(new PhabricatorUserProfile())->loadOneWhere(
'userPHID = %s',
$user->getPHID());
if (!$profile) {
$profile = new PhabricatorUserProfile();
}
$profile = $user->loadUserProfile();
$username = phutil_escape_uri($user->getUserName());

$menu = new PhabricatorMenuView();
Expand Down
42 changes: 40 additions & 2 deletions src/applications/people/storage/PhabricatorUser.php
Expand Up @@ -25,6 +25,9 @@ final class PhabricatorUser extends PhabricatorUserDAO implements PhutilPerson {
protected $isAdmin = 0;
protected $isDisabled = 0;

private $profileImage = null;
private $profile = null;
private $status = null;
private $preferences = null;
private $omnipotent = false;

Expand Down Expand Up @@ -409,6 +412,28 @@ public function getEmailLoginURI(PhabricatorUserEmail $email = null) {
return $uri->alter('email', $email->getAddress());
}

public function attachUserProfile(PhabricatorUserProfile $profile) {
$this->profile = $profile;
return $this;
}

public function loadUserProfile() {
if ($this->profile) {
return $this->profile;
}

$profile_dao = new PhabricatorUserProfile();
$this->profile = $profile_dao->loadOneWhere('userPHID = %s',
$this->getPHID());

if (!$this->profile) {
$profile_dao->setUserPHID($this->getPHID());
$this->profile = $profile_dao;
}

return $this->profile;
}

public function loadPrimaryEmailAddress() {
$email = $this->loadPrimaryEmail();
if (!$email) {
Expand Down Expand Up @@ -629,17 +654,30 @@ public static function getDefaultProfileImageURI() {
return celerity_get_resource_uri('/rsrc/image/avatar.png');
}

public function attachProfileImageURI($uri) {
$this->profileImage = $uri;
return $this;
}

public function loadProfileImageURI() {
if ($this->profileImage) {
return $this->profileImage;
}

$src_phid = $this->getProfileImagePHID();

if ($src_phid) {
$file = id(new PhabricatorFile())->loadOneWhere('phid = %s', $src_phid);
if ($file) {
return $file->getBestURI();
$this->profileImage = $file->getBestURI();
}
}

return self::getDefaultProfileImageURI();
if (!$this->profileImage) {
$this->profileImage = self::getDefaultProfileImageURI();
}

return $this->profileImage;
}

public function getFullName() {
Expand Down
Expand Up @@ -18,13 +18,7 @@ public function getPanelGroup() {
public function processRequest(AphrontRequest $request) {
$user = $request->getUser();

$profile = id(new PhabricatorUserProfile())->loadOneWhere(
'userPHID = %s',
$user->getPHID());
if (!$profile) {
$profile = new PhabricatorUserProfile();
$profile->setUserPHID($user->getPHID());
}
$profile = $user->loadUserProfile();

$supported_formats = PhabricatorFile::getTransformableImageFormats();

Expand Down

0 comments on commit b0e9371

Please sign in to comment.