From 0bfbe18c3ed3b003ed56495425200c2d0cf00ce7 Mon Sep 17 00:00:00 2001 From: Bob Trahan Date: Mon, 8 Jul 2013 13:41:10 -0700 Subject: [PATCH] UIEvents - add support for "subscribers property" Summary: Fixes T3487 and reduces a bit of code duplication. Test Plan: viewed legalpad, macro, and pholio and saw proper subscriber properties Reviewers: epriestley Reviewed By: epriestley CC: aran, Korvin Maniphest Tasks: T3487 Differential Revision: https://secure.phabricator.com/D6383 --- .../LegalpadDocumentViewController.php | 19 +--------- .../PhabricatorMacroViewController.php | 27 +++---------- .../controller/PholioMockViewController.php | 29 ++------------ ...habricatorSubscriptionsUIEventListener.php | 38 ++++++++++++++++++- 4 files changed, 48 insertions(+), 65 deletions(-) diff --git a/src/applications/legalpad/controller/LegalpadDocumentViewController.php b/src/applications/legalpad/controller/LegalpadDocumentViewController.php index 51a340d35a..6ac821505a 100644 --- a/src/applications/legalpad/controller/LegalpadDocumentViewController.php +++ b/src/applications/legalpad/controller/LegalpadDocumentViewController.php @@ -68,7 +68,7 @@ final class LegalpadDocumentViewController extends LegalpadController { ->setHeader($title); $actions = $this->buildActionView($document); - $properties = $this->buildPropertyView($document, $engine, $subscribers); + $properties = $this->buildPropertyView($document, $engine); $comment_form_id = celerity_generate_unique_node_id(); @@ -146,8 +146,7 @@ final class LegalpadDocumentViewController extends LegalpadController { private function buildPropertyView( LegalpadDocument $document, - PhabricatorMarkupEngine $engine, - array $subscribers) { + PhabricatorMarkupEngine $engine) { $user = $this->getRequest()->getUser(); @@ -185,20 +184,6 @@ final class LegalpadDocumentViewController extends LegalpadController { pht('Visible To'), $descriptions[PhabricatorPolicyCapability::CAN_VIEW]); - if ($subscribers) { - $sub_view = array(); - foreach ($subscribers as $subscriber) { - $sub_view[] = $this->getHandle($subscriber)->renderLink(); - } - $sub_view = phutil_implode_html(', ', $sub_view); - } else { - $sub_view = phutil_tag('em', array(), pht('None')); - } - - $properties->addProperty( - pht('Subscribers'), - $sub_view); - $properties->invokeWillRenderEvent(); return $properties; diff --git a/src/applications/macro/controller/PhabricatorMacroViewController.php b/src/applications/macro/controller/PhabricatorMacroViewController.php index efe77eeaea..a65c87beaa 100644 --- a/src/applications/macro/controller/PhabricatorMacroViewController.php +++ b/src/applications/macro/controller/PhabricatorMacroViewController.php @@ -26,10 +26,6 @@ final class PhabricatorMacroViewController $title_short = pht('Macro "%s"', $macro->getName()); $title_long = pht('Image Macro "%s"', $macro->getName()); - $subscribers = PhabricatorSubscribersQuery::loadSubscribersForPHID( - $macro->getPHID()); - - $this->loadHandles($subscribers); $actions = $this->buildActionView($macro); $crumbs = $this->buildApplicationCrumbs(); @@ -39,7 +35,7 @@ final class PhabricatorMacroViewController ->setHref($this->getApplicationURI('/view/'.$macro->getID().'/')) ->setName($title_short)); - $properties = $this->buildPropertyView($macro, $file, $subscribers); + $properties = $this->buildPropertyView($macro, $file); $xactions = id(new PhabricatorMacroTransactionQuery()) ->setViewer($request->getUser()) @@ -140,24 +136,13 @@ final class PhabricatorMacroViewController private function buildPropertyView( PhabricatorFileImageMacro $macro, - PhabricatorFile $file = null, - array $subscribers) { + PhabricatorFile $file = null) { - $view = new PhabricatorPropertyListView(); + $view = id(new PhabricatorPropertyListView()) + ->setUser($this->getRequest()->getUser()) + ->setObject($macro); - if ($subscribers) { - $sub_view = array(); - foreach ($subscribers as $subscriber) { - $sub_view[] = $this->getHandle($subscriber)->renderLink(); - } - $sub_view = phutil_implode_html(', ', $sub_view); - } else { - $sub_view = phutil_tag('em', array(), pht('None')); - } - - $view->addProperty( - pht('Subscribers'), - $sub_view); + $view->invokeWillRenderEvent(); if ($file) { $view->addImageContent( diff --git a/src/applications/pholio/controller/PholioMockViewController.php b/src/applications/pholio/controller/PholioMockViewController.php index 87047ad0fa..396157b552 100644 --- a/src/applications/pholio/controller/PholioMockViewController.php +++ b/src/applications/pholio/controller/PholioMockViewController.php @@ -37,17 +37,9 @@ final class PholioMockViewController extends PholioController { ->withObjectPHIDs(array($mock->getPHID())) ->execute(); - $subscribers = PhabricatorSubscribersQuery::loadSubscribersForPHID( - $mock->getPHID()); - - $phids = array(); - $phids[] = $mock->getAuthorPHID(); - foreach ($subscribers as $subscriber) { - $phids[] = $subscriber; - } + $phids = array($mock->getAuthorPHID()); $this->loadHandles($phids); - $engine = id(new PhabricatorMarkupEngine()) ->setViewer($user); $engine->addObject($mock, PholioMock::MARKUP_FIELD_DESCRIPTION); @@ -66,7 +58,7 @@ final class PholioMockViewController extends PholioController { ->setHeader($title); $actions = $this->buildActionView($mock); - $properties = $this->buildPropertyView($mock, $engine, $subscribers); + $properties = $this->buildPropertyView($mock, $engine); require_celerity_resource('pholio-css'); require_celerity_resource('pholio-inline-comments-css'); @@ -137,8 +129,7 @@ final class PholioMockViewController extends PholioController { private function buildPropertyView( PholioMock $mock, - PhabricatorMarkupEngine $engine, - array $subscribers) { + PhabricatorMarkupEngine $engine) { $user = $this->getRequest()->getUser(); @@ -162,20 +153,6 @@ final class PholioMockViewController extends PholioController { pht('Visible To'), $descriptions[PhabricatorPolicyCapability::CAN_VIEW]); - if ($subscribers) { - $sub_view = array(); - foreach ($subscribers as $subscriber) { - $sub_view[] = $this->getHandle($subscriber)->renderLink(); - } - $sub_view = phutil_implode_html(', ', $sub_view); - } else { - $sub_view = phutil_tag('em', array(), pht('None')); - } - - $properties->addProperty( - pht('Subscribers'), - $sub_view); - $properties->invokeWillRenderEvent(); $properties->addImageContent( diff --git a/src/applications/subscriptions/events/PhabricatorSubscriptionsUIEventListener.php b/src/applications/subscriptions/events/PhabricatorSubscriptionsUIEventListener.php index 346a0fa35a..e67aa9cc7a 100644 --- a/src/applications/subscriptions/events/PhabricatorSubscriptionsUIEventListener.php +++ b/src/applications/subscriptions/events/PhabricatorSubscriptionsUIEventListener.php @@ -5,13 +5,17 @@ final class PhabricatorSubscriptionsUIEventListener public function register() { $this->listen(PhabricatorEventType::TYPE_UI_DIDRENDERACTIONS); + $this->listen(PhabricatorEventType::TYPE_UI_WILLRENDERPROPERTIES); } public function handleEvent(PhutilEvent $event) { switch ($event->getType()) { case PhabricatorEventType::TYPE_UI_DIDRENDERACTIONS: $this->handleActionEvent($event); - break; + break; + case PhabricatorEventType::TYPE_UI_WILLRENDERPROPERTIES: + $this->handlePropertyEvent($event); + break; } } @@ -81,4 +85,36 @@ final class PhabricatorSubscriptionsUIEventListener $event->setValue('actions', $actions); } + private function handlePropertyEvent($event) { + $user = $event->getUser(); + $object = $event->getValue('object'); + + if (!$object || !$object->getPHID()) { + // No object, or the object has no PHID yet.. + return; + } + + if (!($object instanceof PhabricatorSubscribableInterface)) { + // This object isn't subscribable. + return; + } + $subscribers = PhabricatorSubscribersQuery::loadSubscribersForPHID( + $object->getPHID()); + if ($subscribers) { + $handles = id(new PhabricatorObjectHandleData($subscribers)) + ->setViewer($user) + ->loadHandles(); + $sub_view = array(); + foreach ($subscribers as $subscriber) { + $sub_view[] = $handles[$subscriber]->renderLink(); + } + $sub_view = phutil_implode_html(', ', $sub_view); + } else { + $sub_view = phutil_tag('em', array(), pht('None')); + } + + $view = $event->getValue('view'); + $view->addProperty(pht('Subscribers'), $sub_view); + } + }