Page Menu
Home
Phabricator
Search
Configure Global Search
Log In
Files
F14759220
D17503.diff
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Size
8 KB
Referenced Files
None
Subscribers
None
D17503.diff
View Options
diff --git a/src/__phutil_library_map__.php b/src/__phutil_library_map__.php
--- a/src/__phutil_library_map__.php
+++ b/src/__phutil_library_map__.php
@@ -4067,6 +4067,7 @@
'PhabricatorUnknownContentSource' => 'infrastructure/contentsource/PhabricatorUnknownContentSource.php',
'PhabricatorUnsubscribedFromObjectEdgeType' => 'applications/transactions/edges/PhabricatorUnsubscribedFromObjectEdgeType.php',
'PhabricatorUser' => 'applications/people/storage/PhabricatorUser.php',
+ 'PhabricatorUserBadgesCacheType' => 'applications/people/cache/PhabricatorUserBadgesCacheType.php',
'PhabricatorUserBlurbField' => 'applications/people/customfield/PhabricatorUserBlurbField.php',
'PhabricatorUserCache' => 'applications/people/storage/PhabricatorUserCache.php',
'PhabricatorUserCacheType' => 'applications/people/cache/PhabricatorUserCacheType.php',
@@ -9415,6 +9416,7 @@
'PhabricatorFulltextInterface',
'PhabricatorConduitResultInterface',
),
+ 'PhabricatorUserBadgesCacheType' => 'PhabricatorUserCacheType',
'PhabricatorUserBlurbField' => 'PhabricatorUserCustomField',
'PhabricatorUserCache' => 'PhabricatorUserDAO',
'PhabricatorUserCacheType' => 'Phobject',
diff --git a/src/applications/badges/editor/PhabricatorBadgesEditor.php b/src/applications/badges/editor/PhabricatorBadgesEditor.php
--- a/src/applications/badges/editor/PhabricatorBadgesEditor.php
+++ b/src/applications/badges/editor/PhabricatorBadgesEditor.php
@@ -118,4 +118,45 @@
return pht('[Badge]');
}
+ protected function applyFinalEffects(
+ PhabricatorLiskDAO $object,
+ array $xactions) {
+
+ $badge_phid = $object->getPHID();
+ $user_phids = array();
+ $clear_everything = false;
+
+ foreach ($xactions as $xaction) {
+ switch ($xaction->getTransactionType()) {
+ case PhabricatorBadgesBadgeAwardTransaction::TRANSACTIONTYPE:
+ case PhabricatorBadgesBadgeRevokeTransaction::TRANSACTIONTYPE:
+ foreach ($xaction->getNewValue() as $user_phid) {
+ $user_phids[] = $user_phid;
+ }
+ break;
+ default:
+ $clear_everything = true;
+ break;
+ }
+ }
+
+ if ($clear_everything) {
+ $awards = id(new PhabricatorBadgesAwardQuery())
+ ->setViewer($this->getActor())
+ ->withBadgePHIDs(array($badge_phid))
+ ->execute();
+ foreach ($awards as $award) {
+ $user_phids[] = $award->getRecipientPHID();
+ }
+ }
+
+ if ($user_phids) {
+ PhabricatorUserCache::clearCaches(
+ PhabricatorUserBadgesCacheType::KEY_BADGES,
+ $user_phids);
+ }
+
+ return $xactions;
+ }
+
}
diff --git a/src/applications/people/cache/PhabricatorUserBadgesCacheType.php b/src/applications/people/cache/PhabricatorUserBadgesCacheType.php
new file mode 100644
--- /dev/null
+++ b/src/applications/people/cache/PhabricatorUserBadgesCacheType.php
@@ -0,0 +1,61 @@
+<?php
+
+final class PhabricatorUserBadgesCacheType
+ extends PhabricatorUserCacheType {
+
+ const CACHETYPE = 'badges.award';
+
+ const KEY_BADGES = 'user.badge.award.v1';
+
+ const BADGE_COUNT = 2;
+
+ public function getAutoloadKeys() {
+ return array(
+ self::KEY_BADGES,
+ );
+ }
+
+ public function canManageKey($key) {
+ return ($key === self::KEY_BADGES);
+ }
+
+ public function getValueFromStorage($value) {
+ return phutil_json_decode($value);
+ }
+
+ public function newValueForUsers($key, array $users) {
+ if (!$users) {
+ return array();
+ }
+
+ $user_phids = mpull($users, 'getPHID');
+
+ $results = array();
+ foreach ($user_phids as $user_phid) {
+ $awards = id(new PhabricatorBadgesAwardQuery())
+ ->setViewer($this->getViewer())
+ ->withRecipientPHIDs(array($user_phid))
+ ->withBadgeStatuses(array(PhabricatorBadgesBadge::STATUS_ACTIVE))
+ ->setLimit(self::BADGE_COUNT)
+ ->execute();
+
+ $award_data = array();
+ if ($awards) {
+ foreach ($awards as $award) {
+ $badge = $award->getBadge();
+ $award_data[] = array(
+ 'icon' => $badge->getIcon(),
+ 'name' => $badge->getName(),
+ 'quality' => $badge->getQuality(),
+ 'id' => $badge->getID(),
+ );
+ }
+ }
+ $results[$user_phid] = phutil_json_encode($award_data);
+
+ }
+
+ return $results;
+ }
+
+}
diff --git a/src/applications/people/query/PhabricatorPeopleQuery.php b/src/applications/people/query/PhabricatorPeopleQuery.php
--- a/src/applications/people/query/PhabricatorPeopleQuery.php
+++ b/src/applications/people/query/PhabricatorPeopleQuery.php
@@ -24,6 +24,7 @@
private $needProfile;
private $needProfileImage;
private $needAvailability;
+ private $needBadgeAwards;
private $cacheKeys = array();
public function withIDs(array $ids) {
@@ -145,6 +146,18 @@
return $this;
}
+ public function needBadgeAwards($need) {
+ $cache_key = PhabricatorUserBadgesCacheType::KEY_BADGES;
+
+ if ($need) {
+ $this->cacheKeys[$cache_key] = true;
+ } else {
+ unset($this->cacheKeys[$cache_key]);
+ }
+
+ return $this;
+ }
+
public function newResultObject() {
return new PhabricatorUser();
}
diff --git a/src/applications/people/storage/PhabricatorUser.php b/src/applications/people/storage/PhabricatorUser.php
--- a/src/applications/people/storage/PhabricatorUser.php
+++ b/src/applications/people/storage/PhabricatorUser.php
@@ -848,6 +848,11 @@
return $this->requireCacheData($message_key);
}
+ public function getRecentBadgeAwards() {
+ $badges_key = PhabricatorUserBadgesCacheType::KEY_BADGES;
+ return $this->requireCacheData($badges_key);
+ }
+
public function getFullName() {
if (strlen($this->getRealName())) {
return $this->getUsername().' ('.$this->getRealName().')';
diff --git a/src/applications/transactions/view/PhabricatorApplicationTransactionCommentView.php b/src/applications/transactions/view/PhabricatorApplicationTransactionCommentView.php
--- a/src/applications/transactions/view/PhabricatorApplicationTransactionCommentView.php
+++ b/src/applications/transactions/view/PhabricatorApplicationTransactionCommentView.php
@@ -525,25 +525,18 @@
return null;
}
- $awards = id(new PhabricatorBadgesAwardQuery())
- ->setViewer($this->getUser())
- ->withRecipientPHIDs(array($user->getPHID()))
- ->withBadgeStatuses(array(PhabricatorBadgesBadge::STATUS_ACTIVE))
- ->setLimit(2)
- ->execute();
-
- $badges = mpull($awards, 'getBadge');
-
+ // Pull Badges from UserCache
+ $badges = $user->getRecentBadgeAwards();
$badge_view = null;
if ($badges) {
$badge_list = array();
foreach ($badges as $badge) {
$badge_view = id(new PHUIBadgeMiniView())
- ->setIcon($badge->getIcon())
- ->setQuality($badge->getQuality())
- ->setHeader($badge->getName())
+ ->setIcon($badge['icon'])
+ ->setQuality($badge['quality'])
+ ->setHeader($badge['name'])
->setTipDirection('E')
- ->setHref('/badges/view/'.$badge->getID());
+ ->setHref('/badges/view/'.$badge['id'].'/');
$badge_list[] = $badge_view;
}
diff --git a/src/view/phui/PHUITimelineView.php b/src/view/phui/PHUITimelineView.php
--- a/src/view/phui/PHUITimelineView.php
+++ b/src/view/phui/PHUITimelineView.php
@@ -243,37 +243,26 @@
return;
}
-
- $awards = id(new PhabricatorBadgesAwardQuery())
- ->setViewer($this->getViewer())
- ->withRecipientPHIDs($user_phids)
- ->withBadgeStatuses(array(PhabricatorBadgesBadge::STATUS_ACTIVE))
+ $users = id(new PhabricatorPeopleQuery())
+ ->setViewer($viewer)
+ ->withPHIDs($user_phids)
+ ->needBadgeAwards(true)
->execute();
-
- $awards = mgroup($awards, 'getRecipientPHID');
+ $users = mpull($users, null, 'getPHID');
foreach ($events as $event) {
-
- $author_awards = idx($awards, $event->getAuthorPHID(), array());
-
- $badges = array();
- foreach ($author_awards as $award) {
- $badge = $award->getBadge();
- $badges[$award->getBadgePHID()] = $badge;
+ $user_phid = $event->getAuthorPHID();
+ if (!array_key_exists($user_phid, $users)) {
+ continue;
}
-
- // TODO: Pick the "best" badges in some smart way. For now, just pick
- // the first two.
- $badges = array_slice($badges, 0, 2);
-
+ $badges = $users[$user_phid]->getRecentBadgeAwards();
foreach ($badges as $badge) {
$badge_view = id(new PHUIBadgeMiniView())
- ->setIcon($badge->getIcon())
- ->setQuality($badge->getQuality())
- ->setHeader($badge->getName())
+ ->setIcon($badge['icon'])
+ ->setQuality($badge['quality'])
+ ->setHeader($badge['name'])
->setTipDirection('E')
- ->setHref('/badges/view/'.$badge->getID());
-
+ ->setHref('/badges/view/'.$badge['id'].'/');
$event->addBadge($badge_view);
}
}
File Metadata
Details
Attached
Mime Type
text/plain
Expires
Thu, Jan 23, 11:38 AM (8 h, 8 m)
Storage Engine
blob
Storage Format
Encrypted (AES-256-CBC)
Storage Handle
7031482
Default Alt Text
D17503.diff (8 KB)
Attached To
Mode
D17503: Add Badges to UserCache
Attached
Detach File
Event Timeline
Log In to Comment