1
0
Fork 0
mirror of https://we.phorge.it/source/phorge.git synced 2025-03-23 17:50:10 +01:00

Consolidate User Profile loading

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
This commit is contained in:
Anh Nhan Nguyen 2013-03-24 06:42:31 -07:00 committed by epriestley
parent 1a48c7ac40
commit b0e9371a9c
5 changed files with 92 additions and 21 deletions

View file

@ -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;
@ -35,6 +37,16 @@ final class PhabricatorPeopleQuery extends PhabricatorOffsetPagedQuery {
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');
@ -56,6 +68,41 @@ final class PhabricatorPeopleQuery extends PhabricatorOffsetPagedQuery {
}
$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;
}
@ -105,4 +152,5 @@ final class PhabricatorPeopleQuery extends PhabricatorOffsetPagedQuery {
return $this->formatWhereClause($where);
}
}

View file

@ -715,12 +715,8 @@ final class PhabricatorPeopleEditController
$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('');
}

View file

@ -52,12 +52,7 @@ final class PhabricatorPeopleProfileController
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();

View file

@ -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;
@ -409,6 +412,28 @@ final class PhabricatorUser extends PhabricatorUserDAO implements PhutilPerson {
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) {
@ -629,17 +654,30 @@ EOBODY;
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() {

View file

@ -18,13 +18,7 @@ final class PhabricatorSettingsPanelProfile
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();