Permalink
Browse files

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
  • Loading branch information...
1 parent 1a48c7a commit b0e9371a9c92947cc908767f0fdbf48c749828ff @AnhNhan AnhNhan committed with epriestley Mar 24, 2013
@@ -8,6 +8,8 @@
private $ids;
private $needPrimaryEmail;
+ private $needProfile;
+ private $needProfileImage;
public function withIds(array $ids) {
$this->ids = $ids;
@@ -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');
@@ -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;
}
@@ -105,4 +152,5 @@ private function buildWhereClause($conn_r) {
return $this->formatWhereClause($where);
}
+
}
@@ -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('');
}
@@ -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();
@@ -25,6 +25,9 @@
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 @@ 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) {
@@ -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() {
@@ -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();

0 comments on commit b0e9371

Please sign in to comment.