1
0
Fork 0
mirror of https://we.phorge.it/source/phorge.git synced 2024-09-20 09:18:48 +02:00

Remove needRecipients and needAwards from Badges

Summary: Fixes T10798. Separates these two since they don't need to be combined and it allows for more flexibility / scalability.

Test Plan:
- Add Badge
- Edit Badge
- Add myself as Recipient
- Remove myself
- Go to my profile
- Award Badge from there
- Assign myself a badge, try to re-assign it, see validation error.

Also, validation errors on dialog forms are ugly.

{F3495630}

Reviewers: epriestley

Reviewed By: epriestley

Subscribers: Korvin

Maniphest Tasks: T10798, T12270

Differential Revision: https://secure.phabricator.com/D17447
This commit is contained in:
Chad Little 2017-03-03 08:41:42 -08:00
parent c102620a29
commit d2a420d13a
10 changed files with 49 additions and 97 deletions

View file

@ -22,7 +22,6 @@ final class PhabricatorBadgesAwardController
$badges = id(new PhabricatorBadgesQuery()) $badges = id(new PhabricatorBadgesQuery())
->setViewer($viewer) ->setViewer($viewer)
->withPHIDs($badge_phids) ->withPHIDs($badge_phids)
->needRecipients(true)
->requireCapabilities( ->requireCapabilities(
array( array(
PhabricatorPolicyCapability::CAN_EDIT, PhabricatorPolicyCapability::CAN_EDIT,

View file

@ -11,7 +11,6 @@ final class PhabricatorBadgesEditRecipientsController
$badge = id(new PhabricatorBadgesQuery()) $badge = id(new PhabricatorBadgesQuery())
->setViewer($viewer) ->setViewer($viewer)
->withIDs(array($id)) ->withIDs(array($id))
->needRecipients(true)
->requireCapabilities( ->requireCapabilities(
array( array(
PhabricatorPolicyCapability::CAN_EDIT, PhabricatorPolicyCapability::CAN_EDIT,
@ -23,8 +22,6 @@ final class PhabricatorBadgesEditRecipientsController
} }
$view_uri = $this->getApplicationURI('recipients/'.$badge->getID().'/'); $view_uri = $this->getApplicationURI('recipients/'.$badge->getID().'/');
$awards = $badge->getAwards();
$recipient_phids = mpull($awards, 'getRecipientPHID');
if ($request->isFormPost()) { if ($request->isFormPost()) {
$award_phids = array(); $award_phids = array();
@ -52,17 +49,6 @@ final class PhabricatorBadgesEditRecipientsController
->setURI($view_uri); ->setURI($view_uri);
} }
$recipient_phids = array_reverse($recipient_phids);
$handles = $this->loadViewerHandles($recipient_phids);
$state = array();
foreach ($handles as $handle) {
$state[] = array(
'phid' => $handle->getPHID(),
'name' => $handle->getFullName(),
);
}
$can_edit = PhabricatorPolicyFilter::hasCapability( $can_edit = PhabricatorPolicyFilter::hasCapability(
$viewer, $viewer,
$badge, $badge,

View file

@ -14,14 +14,21 @@ final class PhabricatorBadgesRecipientsController
$badge = id(new PhabricatorBadgesQuery()) $badge = id(new PhabricatorBadgesQuery())
->setViewer($viewer) ->setViewer($viewer)
->withIDs(array($id)) ->withIDs(array($id))
->needRecipients(true)
->executeOne(); ->executeOne();
if (!$badge) { if (!$badge) {
return new Aphront404Response(); return new Aphront404Response();
} }
$this->setBadge($badge); $this->setBadge($badge);
$awards = id(new PhabricatorBadgesAwardQuery())
->setViewer($viewer)
->withBadgePHIDs(array($badge->getPHID()))
->execute();
$recipient_phids = mpull($awards, 'getRecipientPHID');
$recipient_phids = array_reverse($recipient_phids);
$handles = $this->loadViewerHandles($recipient_phids);
$crumbs = $this->buildApplicationCrumbs(); $crumbs = $this->buildApplicationCrumbs();
$crumbs->addTextCrumb(pht('Recipients')); $crumbs->addTextCrumb(pht('Recipients'));
$crumbs->setBorder(true); $crumbs->setBorder(true);
@ -29,13 +36,9 @@ final class PhabricatorBadgesRecipientsController
$header = $this->buildHeaderView(); $header = $this->buildHeaderView();
$awards = $badge->getAwards();
$recipient_phids = mpull($awards, 'getRecipientPHID');
$recipient_phids = array_reverse($recipient_phids);
$handles = $this->loadViewerHandles($recipient_phids);
$recipient_list = id(new PhabricatorBadgesRecipientsListView()) $recipient_list = id(new PhabricatorBadgesRecipientsListView())
->setBadge($badge) ->setBadge($badge)
->setAwards($awards)
->setHandles($handles) ->setHandles($handles)
->setUser($viewer); ->setUser($viewer);

View file

@ -10,7 +10,6 @@ final class PhabricatorBadgesRemoveRecipientsController
$badge = id(new PhabricatorBadgesQuery()) $badge = id(new PhabricatorBadgesQuery())
->setViewer($viewer) ->setViewer($viewer)
->withIDs(array($id)) ->withIDs(array($id))
->needRecipients(true)
->requireCapabilities( ->requireCapabilities(
array( array(
PhabricatorPolicyCapability::CAN_VIEW, PhabricatorPolicyCapability::CAN_VIEW,
@ -21,14 +20,7 @@ final class PhabricatorBadgesRemoveRecipientsController
return new Aphront404Response(); return new Aphront404Response();
} }
$awards = $badge->getAwards();
$recipient_phids = mpull($awards, 'getRecipientPHID');
$remove_phid = $request->getStr('phid'); $remove_phid = $request->getStr('phid');
if (!in_array($remove_phid, $recipient_phids)) {
return new Aphront404Response();
}
$view_uri = $this->getApplicationURI('recipients/'.$badge->getID().'/'); $view_uri = $this->getApplicationURI('recipients/'.$badge->getID().'/');
if ($request->isFormPost()) { if ($request->isFormPost()) {

View file

@ -9,21 +9,19 @@ final class PhabricatorBadgesAwardQuery
protected function willFilterPage(array $awards) { protected function willFilterPage(array $awards) {
$badge_phids = array();
foreach ($awards as $key => $award) {
$badge_phids[] = $award->getBadgePHID();
}
$badges = id(new PhabricatorBadgesQuery()) $badges = id(new PhabricatorBadgesQuery())
->setViewer($this->getViewer()) ->setViewer($this->getViewer())
->withRecipientPHIDs(mpull($awards, null, 'getRecipientPHID')) ->withPHIDs($badge_phids)
->execute(); ->execute();
$badges = mpull($badges, null, 'getPHID'); $badges = mpull($badges, null, 'getPHID');
foreach ($awards as $key => $award) { foreach ($awards as $key => $award) {
$award_badge = idx($badges, $award->getBadgePHID()); $award_badge = idx($badges, $award->getBadgePHID());
if ($award_badge === null) {
$this->didRejectResult($award);
unset($awards[$key]);
continue;
}
$award->attachBadge($award_badge); $award->attachBadge($award_badge);
} }

View file

@ -7,9 +7,6 @@ final class PhabricatorBadgesQuery
private $phids; private $phids;
private $qualities; private $qualities;
private $statuses; private $statuses;
private $recipientPHIDs;
private $needRecipients;
public function withIDs(array $ids) { public function withIDs(array $ids) {
$this->ids = $ids; $this->ids = $ids;
@ -31,22 +28,12 @@ final class PhabricatorBadgesQuery
return $this; return $this;
} }
public function withRecipientPHIDs(array $recipient_phids) {
$this->recipientPHIDs = $recipient_phids;
return $this;
}
public function withNameNgrams($ngrams) { public function withNameNgrams($ngrams) {
return $this->withNgramsConstraint( return $this->withNgramsConstraint(
id(new PhabricatorBadgesBadgeNameNgrams()), id(new PhabricatorBadgesBadgeNameNgrams()),
$ngrams); $ngrams);
} }
public function needRecipients($need_recipients) {
$this->needRecipients = $need_recipients;
return $this;
}
protected function loadPage() { protected function loadPage() {
return $this->loadStandardPage($this->newResultObject()); return $this->loadStandardPage($this->newResultObject());
} }
@ -59,24 +46,6 @@ final class PhabricatorBadgesQuery
return new PhabricatorBadgesBadge(); return new PhabricatorBadgesBadge();
} }
protected function didFilterPage(array $badges) {
if ($this->needRecipients) {
$query = id(new PhabricatorBadgesAwardQuery())
->setViewer($this->getViewer())
->withBadgePHIDs(mpull($badges, 'getPHID'))
->execute();
$awards = mgroup($query, 'getBadgePHID');
foreach ($badges as $badge) {
$badge_awards = idx($awards, $badge->getPHID(), array());
$badge->attachAwards($badge_awards);
}
}
return $badges;
}
protected function buildWhereClauseParts(AphrontDatabaseConnection $conn) { protected function buildWhereClauseParts(AphrontDatabaseConnection $conn) {
$where = parent::buildWhereClauseParts($conn); $where = parent::buildWhereClauseParts($conn);

View file

@ -20,8 +20,6 @@ final class PhabricatorBadgesBadge extends PhabricatorBadgesDAO
protected $status; protected $status;
protected $creatorPHID; protected $creatorPHID;
private $awards = self::ATTACHABLE;
const STATUS_ACTIVE = 'open'; const STATUS_ACTIVE = 'open';
const STATUS_ARCHIVED = 'closed'; const STATUS_ARCHIVED = 'closed';
@ -84,15 +82,6 @@ final class PhabricatorBadgesBadge extends PhabricatorBadgesDAO
return ($this->getStatus() == self::STATUS_ARCHIVED); return ($this->getStatus() == self::STATUS_ARCHIVED);
} }
public function attachAwards(array $awards) {
$this->awards = $awards;
return $this;
}
public function getAwards() {
return $this->assertAttached($this->awards);
}
public function getViewURI() { public function getViewURI() {
return '/badges/view/'.$this->getID().'/'; return '/badges/view/'.$this->getID().'/';
} }

View file

@ -3,6 +3,7 @@
final class PhabricatorBadgesRecipientsListView extends AphrontView { final class PhabricatorBadgesRecipientsListView extends AphrontView {
private $badge; private $badge;
private $awards;
private $handles; private $handles;
public function setBadge(PhabricatorBadgesBadge $badge) { public function setBadge(PhabricatorBadgesBadge $badge) {
@ -10,6 +11,11 @@ final class PhabricatorBadgesRecipientsListView extends AphrontView {
return $this; return $this;
} }
public function setAwards(array $awards) {
$this->awards = $awards;
return $this;
}
public function setHandles(array $handles) { public function setHandles(array $handles) {
$this->handles = $handles; $this->handles = $handles;
return $this; return $this;
@ -20,7 +26,7 @@ final class PhabricatorBadgesRecipientsListView extends AphrontView {
$badge = $this->badge; $badge = $this->badge;
$handles = $this->handles; $handles = $this->handles;
$awards = mpull($badge->getAwards(), null, 'getRecipientPHID'); $awards = mpull($this->awards, null, 'getRecipientPHID');
$can_edit = PhabricatorPolicyFilter::hasCapability( $can_edit = PhabricatorPolicyFilter::hasCapability(
$viewer, $viewer,

View file

@ -6,25 +6,17 @@ final class PhabricatorBadgesBadgeAwardTransaction
const TRANSACTIONTYPE = 'badge.award'; const TRANSACTIONTYPE = 'badge.award';
public function generateOldValue($object) { public function generateOldValue($object) {
return mpull($object->getAwards(), 'getRecipientPHID'); return null;
} }
public function applyExternalEffects($object, $value) { public function applyExternalEffects($object, $value) {
$awards = $object->getAwards();
$awards = mpull($awards, null, 'getRecipientPHID');
foreach ($value as $phid) { foreach ($value as $phid) {
$award = idx($awards, $phid); $award = PhabricatorBadgesAward::initializeNewBadgesAward(
if (!$award) { $this->getActor(),
$award = PhabricatorBadgesAward::initializeNewBadgesAward( $object,
$this->getActor(), $phid);
$object, $award->save();
$phid);
$award->save();
$awards[] = $award;
}
} }
$object->attachAwards($awards);
return; return;
} }
@ -71,6 +63,7 @@ final class PhabricatorBadgesBadgeAwardTransaction
} }
foreach ($user_phids as $user_phid) { foreach ($user_phids as $user_phid) {
// Check if a valid user
$user = id(new PhabricatorPeopleQuery()) $user = id(new PhabricatorPeopleQuery())
->setViewer($this->getActor()) ->setViewer($this->getActor())
->withPHIDs(array($user_phid)) ->withPHIDs(array($user_phid))
@ -80,6 +73,20 @@ final class PhabricatorBadgesBadgeAwardTransaction
pht( pht(
'Recipient PHID "%s" is not a valid user PHID.', 'Recipient PHID "%s" is not a valid user PHID.',
$user_phid)); $user_phid));
continue;
}
// Check if already awarded
$award = id(new PhabricatorBadgesAwardQuery())
->setViewer($this->getActor())
->withRecipientPHIDs(array($user_phid))
->withBadgePHIDs(array($object->getPHID()))
->executeOne();
if ($award) {
$errors[] = $this->newInvalidError(
pht(
'%s has already been awarded this badge.',
$user->getUsername()));
} }
} }
} }

View file

@ -10,13 +10,16 @@ final class PhabricatorBadgesBadgeRevokeTransaction
} }
public function applyExternalEffects($object, $value) { public function applyExternalEffects($object, $value) {
$awards = $object->getAwards(); $awards = id(new PhabricatorBadgesAwardQuery())
->setViewer($this->getActor())
->withRecipientPHIDs($value)
->withBadgePHIDs(array($object->getPHID()))
->execute();
$awards = mpull($awards, null, 'getRecipientPHID'); $awards = mpull($awards, null, 'getRecipientPHID');
foreach ($value as $phid) { foreach ($value as $phid) {
$awards[$phid]->delete(); $awards[$phid]->delete();
} }
$object->attachAwards($awards);
return; return;
} }