Changeset View
Changeset View
Standalone View
Standalone View
src/view/phui/PHUITimelineView.php
| Show First 20 Lines • Show All 214 Lines • ▼ Show 20 Lines | private function prepareBadgeData(array $events) { | ||||
| $can_use_badges = PhabricatorApplication::isClassInstalledForViewer( | $can_use_badges = PhabricatorApplication::isClassInstalledForViewer( | ||||
| 'PhabricatorBadgesApplication', | 'PhabricatorBadgesApplication', | ||||
| $viewer); | $viewer); | ||||
| if (!$can_use_badges) { | if (!$can_use_badges) { | ||||
| return; | return; | ||||
| } | } | ||||
| $user_phid_type = PhabricatorPeopleUserPHIDType::TYPECONST; | $user_phid_type = PhabricatorPeopleUserPHIDType::TYPECONST; | ||||
| $badge_edge_type = PhabricatorRecipientHasBadgeEdgeType::EDGECONST; | |||||
| $user_phids = array(); | $user_phids = array(); | ||||
| foreach ($events as $key => $event) { | foreach ($events as $key => $event) { | ||||
| $author_phid = $event->getAuthorPHID(); | $author_phid = $event->getAuthorPHID(); | ||||
| if (!$author_phid) { | if (!$author_phid) { | ||||
| unset($events[$key]); | unset($events[$key]); | ||||
| continue; | continue; | ||||
| } | } | ||||
| Show All 11 Lines | private function prepareBadgeData(array $events) { | ||||
| if (!$user_phids) { | if (!$user_phids) { | ||||
| return; | return; | ||||
| } | } | ||||
| $awards = id(new PhabricatorBadgesAwardQuery()) | $awards = id(new PhabricatorBadgesAwardQuery()) | ||||
| ->setViewer($this->getViewer()) | ->setViewer($this->getViewer()) | ||||
| ->withRecipientPHIDs($user_phids) | ->withRecipientPHIDs($user_phids) | ||||
| ->withBadgeStatuses(array(PhabricatorBadgesBadge::STATUS_ACTIVE)) | |||||
| ->execute(); | ->execute(); | ||||
epriestley: I think we can't do `setLimit()` here -- we'll end up with two total awards, not two awards… | |||||
| $awards = mgroup($awards, 'getRecipientPHID'); | $awards = mgroup($awards, 'getRecipientPHID'); | ||||
| foreach ($events as $event) { | foreach ($events as $event) { | ||||
| $author_awards = idx($awards, $event->getAuthorPHID(), array()); | $author_awards = idx($awards, $event->getAuthorPHID(), array()); | ||||
| $badges = array(); | $badges = array(); | ||||
| foreach ($author_awards as $award) { | foreach ($author_awards as $award) { | ||||
| $badge = $award->getBadge(); | $badge = $award->getBadge(); | ||||
| if ($badge->getStatus() == PhabricatorBadgesBadge::STATUS_ACTIVE) { | |||||
| $badges[$award->getBadgePHID()] = $badge; | $badges[$award->getBadgePHID()] = $badge; | ||||
| } | } | ||||
| } | |||||
| // TODO: Pick the "best" badges in some smart way. For now, just pick | // TODO: Pick the "best" badges in some smart way. For now, just pick | ||||
| // the first two. | // the first two. | ||||
| $badges = array_slice($badges, 0, 2); | $badges = array_slice($badges, 0, 2); | ||||
| foreach ($badges as $badge) { | foreach ($badges as $badge) { | ||||
| $badge_view = id(new PHUIBadgeMiniView()) | $badge_view = id(new PHUIBadgeMiniView()) | ||||
| ->setIcon($badge->getIcon()) | ->setIcon($badge->getIcon()) | ||||
| Show All 11 Lines | |||||
I think we can't do setLimit() here -- we'll end up with two total awards, not two awards per user, but this query is trying to fetch badges for every user who has made a comment.
There's no way to query for two awards per user and this is hard to express in SQL -- at least, I'm not sure how to do it.
For now, because there's no easy fix, I think it's OK to leave this as it was. The eventual fix is probably via T9006, where we'd build a cache that let us do this query efficiently and let users exercise control over which badges were displayed.