@recaptime-dev's working patches + fork for Phorge, a community fork of Phabricator. (Upstream dev and stable branches are at upstream/main and upstream/stable respectively.) hq.recaptime.dev/wiki/Phorge
phorge phabricator
at upstream/main 109 lines 3.1 kB view raw
1<?php 2 3final class PhabricatorSubscriptionsEditController 4 extends PhabricatorController { 5 6 public function handleRequest(AphrontRequest $request) { 7 $viewer = $request->getViewer(); 8 $phid = $request->getURIData('phid'); 9 $action = $request->getURIData('action'); 10 11 if (!$request->isFormOrHisecPost()) { 12 return new Aphront400Response(); 13 } 14 15 switch ($action) { 16 case 'add': 17 $is_add = true; 18 break; 19 case 'delete': 20 $is_add = false; 21 break; 22 default: 23 return new Aphront400Response(); 24 } 25 26 $handle = id(new PhabricatorHandleQuery()) 27 ->setViewer($viewer) 28 ->withPHIDs(array($phid)) 29 ->executeOne(); 30 31 $object = id(new PhabricatorObjectQuery()) 32 ->setViewer($viewer) 33 ->withPHIDs(array($phid)) 34 ->executeOne(); 35 36 if (!($object instanceof PhabricatorSubscribableInterface)) { 37 return $this->buildErrorResponse( 38 pht('Bad Object'), 39 pht('This object is not subscribable.'), 40 $handle->getURI()); 41 } 42 43 if ($object->isAutomaticallySubscribed($viewer->getPHID())) { 44 return $this->buildErrorResponse( 45 pht('Automatically Subscribed'), 46 pht('You are automatically subscribed to this object.'), 47 $handle->getURI()); 48 } 49 50 if ($object instanceof PhabricatorApplicationTransactionInterface) { 51 if ($is_add) { 52 $xaction_value = array( 53 '+' => array($viewer->getPHID()), 54 ); 55 } else { 56 $xaction_value = array( 57 '-' => array($viewer->getPHID()), 58 ); 59 } 60 61 $xaction = id($object->getApplicationTransactionTemplate()) 62 ->setTransactionType(PhabricatorTransactions::TYPE_SUBSCRIBERS) 63 ->setNewValue($xaction_value); 64 65 $editor = id($object->getApplicationTransactionEditor()) 66 ->setActor($viewer) 67 ->setCancelURI($handle->getURI()) 68 ->setContinueOnNoEffect(true) 69 ->setContinueOnMissingFields(true) 70 ->setContentSourceFromRequest($request); 71 72 $editor->applyTransactions($object, array($xaction)); 73 } else { 74 75 // TODO: Eventually, get rid of this once everything implements 76 // PhabricatorApplicationTransactionInterface. 77 78 $editor = id(new PhabricatorSubscriptionsEditor()) 79 ->setActor($viewer) 80 ->setObject($object); 81 82 if ($is_add) { 83 $editor->subscribeExplicit(array($viewer->getPHID()), $explicit = true); 84 } else { 85 $editor->unsubscribe(array($viewer->getPHID())); 86 } 87 88 $editor->save(); 89 } 90 91 // TODO: We should just render the "Unsubscribe" action and swap it out 92 // in the document for Ajax requests. 93 return id(new AphrontReloadResponse())->setURI($handle->getURI()); 94 } 95 96 private function buildErrorResponse($title, $message, $uri) { 97 $request = $this->getRequest(); 98 $viewer = $request->getUser(); 99 100 $dialog = id(new AphrontDialogView()) 101 ->setUser($viewer) 102 ->setTitle($title) 103 ->appendChild($message) 104 ->addCancelButton($uri); 105 106 return id(new AphrontDialogResponse())->setDialog($dialog); 107 } 108 109}