Differential D14630 Diff 35407 src/applications/subscriptions/controller/PhabricatorSubscriptionsEditController.php
Changeset View
Changeset View
Standalone View
Standalone View
src/applications/subscriptions/controller/PhabricatorSubscriptionsEditController.php
| <?php | <?php | ||||
| final class PhabricatorSubscriptionsEditController | final class PhabricatorSubscriptionsEditController | ||||
| extends PhabricatorController { | extends PhabricatorController { | ||||
| private $phid; | public function handleRequest(AphrontRequest $request) { | ||||
| private $action; | $viewer = $request->getViewer(); | ||||
| $phid = $request->getURIData('phid'); | |||||
| public function willProcessRequest(array $data) { | $action = $request->getURIData('action'); | ||||
| $this->phid = idx($data, 'phid'); | |||||
| $this->action = idx($data, 'action'); | |||||
| } | |||||
| public function processRequest() { | |||||
| $request = $this->getRequest(); | |||||
| if (!$request->isFormPost()) { | if (!$request->isFormPost()) { | ||||
| return new Aphront400Response(); | return new Aphront400Response(); | ||||
| } | } | ||||
| switch ($this->action) { | switch ($action) { | ||||
| case 'add': | case 'add': | ||||
| $is_add = true; | $is_add = true; | ||||
| break; | break; | ||||
| case 'delete': | case 'delete': | ||||
| $is_add = false; | $is_add = false; | ||||
| break; | break; | ||||
| default: | default: | ||||
| return new Aphront400Response(); | return new Aphront400Response(); | ||||
| } | } | ||||
| $user = $request->getUser(); | |||||
| $phid = $this->phid; | |||||
| $handle = id(new PhabricatorHandleQuery()) | $handle = id(new PhabricatorHandleQuery()) | ||||
| ->setViewer($user) | ->setViewer($viewer) | ||||
| ->withPHIDs(array($phid)) | ->withPHIDs(array($phid)) | ||||
| ->executeOne(); | ->executeOne(); | ||||
| if (phid_get_type($phid) == PhabricatorProjectProjectPHIDType::TYPECONST) { | if (phid_get_type($phid) == PhabricatorProjectProjectPHIDType::TYPECONST) { | ||||
| // TODO: This is a big hack, but a weak argument for adding some kind | // TODO: This is a big hack, but a weak argument for adding some kind | ||||
| // of "load for role" feature to ObjectQuery, and also not a really great | // of "load for role" feature to ObjectQuery, and also not a really great | ||||
| // argument for adding some kind of "load extra stuff" feature to | // argument for adding some kind of "load extra stuff" feature to | ||||
| // SubscriberInterface. Do this for now and wait for the best way forward | // SubscriberInterface. Do this for now and wait for the best way forward | ||||
| // to become more clear? | // to become more clear? | ||||
| $object = id(new PhabricatorProjectQuery()) | $object = id(new PhabricatorProjectQuery()) | ||||
| ->setViewer($user) | ->setViewer($viewer) | ||||
| ->withPHIDs(array($phid)) | ->withPHIDs(array($phid)) | ||||
| ->needWatchers(true) | ->needWatchers(true) | ||||
| ->executeOne(); | ->executeOne(); | ||||
| } else { | } else { | ||||
| $object = id(new PhabricatorObjectQuery()) | $object = id(new PhabricatorObjectQuery()) | ||||
| ->setViewer($user) | ->setViewer($viewer) | ||||
| ->withPHIDs(array($phid)) | ->withPHIDs(array($phid)) | ||||
| ->executeOne(); | ->executeOne(); | ||||
| } | } | ||||
| if (!($object instanceof PhabricatorSubscribableInterface)) { | if (!($object instanceof PhabricatorSubscribableInterface)) { | ||||
| return $this->buildErrorResponse( | return $this->buildErrorResponse( | ||||
| pht('Bad Object'), | pht('Bad Object'), | ||||
| pht('This object is not subscribable.'), | pht('This object is not subscribable.'), | ||||
| $handle->getURI()); | $handle->getURI()); | ||||
| } | } | ||||
| if ($object->isAutomaticallySubscribed($user->getPHID())) { | if ($object->isAutomaticallySubscribed($viewer->getPHID())) { | ||||
| return $this->buildErrorResponse( | return $this->buildErrorResponse( | ||||
| pht('Automatically Subscribed'), | pht('Automatically Subscribed'), | ||||
| pht('You are automatically subscribed to this object.'), | pht('You are automatically subscribed to this object.'), | ||||
| $handle->getURI()); | $handle->getURI()); | ||||
| } | } | ||||
| if (!$object->shouldAllowSubscription($user->getPHID())) { | if (!$object->shouldAllowSubscription($viewer->getPHID())) { | ||||
| return $this->buildErrorResponse( | return $this->buildErrorResponse( | ||||
| pht('You Can Not Subscribe'), | pht('You Can Not Subscribe'), | ||||
| pht('You can not subscribe to this object.'), | pht('You can not subscribe to this object.'), | ||||
| $handle->getURI()); | $handle->getURI()); | ||||
| } | } | ||||
| if ($object instanceof PhabricatorApplicationTransactionInterface) { | if ($object instanceof PhabricatorApplicationTransactionInterface) { | ||||
| if ($is_add) { | if ($is_add) { | ||||
| $xaction_value = array( | $xaction_value = array( | ||||
| '+' => array($user->getPHID()), | '+' => array($viewer->getPHID()), | ||||
| ); | ); | ||||
| } else { | } else { | ||||
| $xaction_value = array( | $xaction_value = array( | ||||
| '-' => array($user->getPHID()), | '-' => array($viewer->getPHID()), | ||||
| ); | ); | ||||
| } | } | ||||
| $xaction = id($object->getApplicationTransactionTemplate()) | $xaction = id($object->getApplicationTransactionTemplate()) | ||||
| ->setTransactionType(PhabricatorTransactions::TYPE_SUBSCRIBERS) | ->setTransactionType(PhabricatorTransactions::TYPE_SUBSCRIBERS) | ||||
| ->setNewValue($xaction_value); | ->setNewValue($xaction_value); | ||||
| $editor = id($object->getApplicationTransactionEditor()) | $editor = id($object->getApplicationTransactionEditor()) | ||||
| ->setActor($user) | ->setActor($viewer) | ||||
| ->setContinueOnNoEffect(true) | ->setContinueOnNoEffect(true) | ||||
| ->setContinueOnMissingFields(true) | ->setContinueOnMissingFields(true) | ||||
| ->setContentSourceFromRequest($request); | ->setContentSourceFromRequest($request); | ||||
| $editor->applyTransactions( | $editor->applyTransactions( | ||||
| $object->getApplicationTransactionObject(), | $object->getApplicationTransactionObject(), | ||||
| array($xaction)); | array($xaction)); | ||||
| } else { | } else { | ||||
| // TODO: Eventually, get rid of this once everything implements | // TODO: Eventually, get rid of this once everything implements | ||||
| // PhabriatorApplicationTransactionInterface. | // PhabriatorApplicationTransactionInterface. | ||||
| $editor = id(new PhabricatorSubscriptionsEditor()) | $editor = id(new PhabricatorSubscriptionsEditor()) | ||||
| ->setActor($user) | ->setActor($viewer) | ||||
| ->setObject($object); | ->setObject($object); | ||||
| if ($is_add) { | if ($is_add) { | ||||
| $editor->subscribeExplicit(array($user->getPHID()), $explicit = true); | $editor->subscribeExplicit(array($viewer->getPHID()), $explicit = true); | ||||
| } else { | } else { | ||||
| $editor->unsubscribe(array($user->getPHID())); | $editor->unsubscribe(array($viewer->getPHID())); | ||||
| } | } | ||||
| $editor->save(); | $editor->save(); | ||||
| } | } | ||||
| // TODO: We should just render the "Unsubscribe" action and swap it out | // TODO: We should just render the "Unsubscribe" action and swap it out | ||||
| // in the document for Ajax requests. | // in the document for Ajax requests. | ||||
| return id(new AphrontReloadResponse())->setURI($handle->getURI()); | return id(new AphrontReloadResponse())->setURI($handle->getURI()); | ||||
| } | } | ||||
| private function buildErrorResponse($title, $message, $uri) { | private function buildErrorResponse($title, $message, $uri) { | ||||
| $request = $this->getRequest(); | $request = $this->getRequest(); | ||||
| $user = $request->getUser(); | $viewer = $request->getUser(); | ||||
| $dialog = id(new AphrontDialogView()) | $dialog = id(new AphrontDialogView()) | ||||
| ->setUser($user) | ->setUser($viewer) | ||||
| ->setTitle($title) | ->setTitle($title) | ||||
| ->appendChild($message) | ->appendChild($message) | ||||
| ->addCancelButton($uri); | ->addCancelButton($uri); | ||||
| return id(new AphrontDialogResponse())->setDialog($dialog); | return id(new AphrontDialogResponse())->setDialog($dialog); | ||||
| } | } | ||||
| } | } | ||||