@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
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

Issue commands to Nuance items, at least roughly

Summary:
Ref T12738. This makes clicking "Throw In Trash" technically do something, sort of.

In Nuance, the default mode of operation for actions is asynchronous -- so you don't have to wait for a response from Twitter or GitHub after you mash the "send default reply tweet" / "close this pull request with a nice response" button and can move directly to the next item instead.

In the future, some operations will attempt to apply synchronously (e.g., local actions like "ignore this item forever"). This fakes our way through that for now.

There's also no connection to the action actually doing anything yet, but I'll probably rig that up next.

Test Plan: {F4975227}

Reviewers: chad

Reviewed By: chad

Maniphest Tasks: T12738

Differential Revision: https://secure.phabricator.com/D18010

+247 -51
+8
resources/sql/autopatches/20170524.nuance.01.command.sql
··· 1 + ALTER TABLE {$NAMESPACE}_nuance.nuance_itemcommand 2 + ADD dateCreated INT UNSIGNED NOT NULL; 3 + 4 + ALTER TABLE {$NAMESPACE}_nuance.nuance_itemcommand 5 + ADD dateModified INT UNSIGNED NOT NULL; 6 + 7 + ALTER TABLE {$NAMESPACE}_nuance.nuance_itemcommand 8 + ADD queuePHID VARBINARY(64);
+5
resources/sql/autopatches/20170524.nuance.02.commandstatus.sql
··· 1 + ALTER TABLE {$NAMESPACE}_nuance.nuance_itemcommand 2 + ADD status VARCHAR(64) NOT NULL; 3 + 4 + UPDATE {$NAMESPACE}_nuance.nuance_itemcommand 5 + SET status = 'done' WHERE status = '';
+2
src/applications/nuance/application/PhabricatorNuanceApplication.php
··· 52 52 $this->getEditRoutePattern('edit/') => 'NuanceQueueEditController', 53 53 'view/(?P<id>[1-9]\d*)/' => 'NuanceQueueViewController', 54 54 'work/(?P<id>[1-9]\d*)/' => 'NuanceQueueWorkController', 55 + 'action/(?P<queueID>[1-9]\d*)/(?P<action>[^/]+)/(?P<id>[1-9]\d*)/' 56 + => 'NuanceItemActionController', 55 57 ), 56 58 ), 57 59 '/action/' => array(
+65 -1
src/applications/nuance/controller/NuanceItemActionController.php
··· 6 6 $viewer = $this->getViewer(); 7 7 $id = $request->getURIData('id'); 8 8 9 + if (!$request->validateCSRF()) { 10 + return new Aphront400Response(); 11 + } 12 + 13 + // NOTE: This controller can be reached from an individual item (usually 14 + // by a user) or while working through a queue (usually by staff). When 15 + // a command originates from a queue, the URI will have a queue ID. 16 + 9 17 $item = id(new NuanceItemQuery()) 10 18 ->setViewer($viewer) 11 19 ->withIDs(array($id)) 12 20 ->executeOne(); 13 21 if (!$item) { 14 22 return new Aphront404Response(); 23 + } 24 + 25 + $cancel_uri = $item->getURI(); 26 + 27 + $queue_id = $request->getURIData('queueID'); 28 + $queue = null; 29 + if ($queue_id) { 30 + $queue = id(new NuanceQueueQuery()) 31 + ->setViewer($viewer) 32 + ->withIDs(array($queue_id)) 33 + ->executeOne(); 34 + if (!$queue) { 35 + return new Aphront404Response(); 36 + } 37 + 38 + $item_queue = $item->getQueue(); 39 + if (!$item_queue || ($item_queue->getPHID() != $queue->getPHID())) { 40 + return $this->newDialog() 41 + ->setTitle(pht('Wrong Queue')) 42 + ->appendParagraph( 43 + pht( 44 + 'You are trying to act on this item from the wrong queue: it '. 45 + 'is currently in a different queue.')) 46 + ->addCancelButton($cancel_uri); 47 + } 15 48 } 16 49 17 50 $action = $request->getURIData('action'); ··· 20 53 $impl->setViewer($viewer); 21 54 $impl->setController($this); 22 55 23 - return $impl->buildActionResponse($item, $action); 56 + $command = NuanceItemCommand::initializeNewCommand() 57 + ->setItemPHID($item->getPHID()) 58 + ->setAuthorPHID($viewer->getPHID()) 59 + ->setCommand($action); 60 + 61 + if ($queue) { 62 + $command->setQueuePHID($queue->getPHID()); 63 + } 64 + 65 + $command->save(); 66 + 67 + // TODO: Here, we should check if the command should be tried immediately, 68 + // and just defer it to the daemons if not. If we're going to try to apply 69 + // the command directly, we should first acquire the worker lock. If we 70 + // can not, we should defer the command even if it's an immediate command. 71 + // For the moment, skip all this stuff by deferring unconditionally. 72 + 73 + $should_defer = true; 74 + if ($should_defer) { 75 + $item->scheduleUpdate(); 76 + } else { 77 + // ... 78 + } 79 + 80 + if ($queue) { 81 + $done_uri = $queue->getWorkURI(); 82 + } else { 83 + $done_uri = $item->getURI(); 84 + } 85 + 86 + return id(new AphrontRedirectResponse()) 87 + ->setURI($done_uri); 24 88 } 25 89 26 90 }
-34
src/applications/nuance/controller/NuanceItemViewController.php
··· 26 26 27 27 $curtain = $this->buildCurtain($item); 28 28 $content = $this->buildContent($item); 29 - $commands = $this->buildCommands($item); 30 29 31 30 $timeline = $this->buildTransactionTimeline( 32 31 $item, 33 32 new NuanceItemTransactionQuery()); 34 33 35 34 $main = array( 36 - $commands, 37 35 $content, 38 36 $timeline, 39 37 ); ··· 89 87 90 88 $impl->setViewer($viewer); 91 89 return $impl->buildItemView($item); 92 - } 93 - 94 - private function buildCommands(NuanceItem $item) { 95 - $viewer = $this->getViewer(); 96 - 97 - $commands = id(new NuanceItemCommandQuery()) 98 - ->setViewer($viewer) 99 - ->withItemPHIDs(array($item->getPHID())) 100 - ->execute(); 101 - $commands = msort($commands, 'getID'); 102 - 103 - if (!$commands) { 104 - return null; 105 - } 106 - 107 - $rows = array(); 108 - foreach ($commands as $command) { 109 - $rows[] = array( 110 - $command->getCommand(), 111 - ); 112 - } 113 - 114 - $table = id(new AphrontTableView($rows)) 115 - ->setHeaders( 116 - array( 117 - pht('Command'), 118 - )); 119 - 120 - return id(new PHUIObjectBoxView()) 121 - ->setHeaderText(pht('Pending Commands')) 122 - ->setBackground(PHUIObjectBoxView::BLUE_PROPERTY) 123 - ->setTable($table); 124 90 } 125 91 126 92 }
+65 -2
src/applications/nuance/controller/NuanceQueueWorkController.php
··· 64 64 $impl = $item->getImplementation() 65 65 ->setViewer($viewer); 66 66 67 + $commands = $this->buildCommands($item); 67 68 $work_content = $impl->buildItemWorkView($item); 68 69 69 70 $view = id(new PHUITwoColumnView()) 70 71 ->setCurtain($curtain) 71 72 ->setMainColumn( 72 73 array( 74 + $commands, 73 75 $work_content, 74 76 $timeline, 75 77 )); ··· 94 96 95 97 $item_id = $item->getID(); 96 98 99 + $action_uri = "queue/action/{$id}/{$command_key}/{$item_id}/"; 100 + $action_uri = $this->getApplicationURI($action_uri); 101 + 97 102 $curtain->addAction( 98 103 id(new PhabricatorActionView()) 99 104 ->setName($command->getName()) 100 105 ->setIcon($command->getIcon()) 101 - ->setHref("queue/command/{$id}/{$command_key}/{$item_id}/")) 102 - ->setWorkflow(true); 106 + ->setHref($action_uri) 107 + ->setWorkflow(true)); 103 108 } 104 109 105 110 $curtain->addAction( ··· 118 123 ->setHref($this->getApplicationURI("queue/view/{$id}/"))); 119 124 120 125 return $curtain; 126 + } 127 + 128 + private function buildCommands(NuanceItem $item) { 129 + $viewer = $this->getViewer(); 130 + 131 + $commands = id(new NuanceItemCommandQuery()) 132 + ->setViewer($viewer) 133 + ->withItemPHIDs(array($item->getPHID())) 134 + ->withStatuses( 135 + array( 136 + NuanceItemCommand::STATUS_ISSUED, 137 + NuanceItemCommand::STATUS_EXECUTING, 138 + NuanceItemCommand::STATUS_FAILED, 139 + )) 140 + ->execute(); 141 + $commands = msort($commands, 'getID'); 142 + 143 + if (!$commands) { 144 + return null; 145 + } 146 + 147 + $rows = array(); 148 + foreach ($commands as $command) { 149 + $icon = $command->getStatusIcon(); 150 + $color = $command->getStatusColor(); 151 + 152 + $rows[] = array( 153 + $command->getID(), 154 + id(new PHUIIconView()) 155 + ->setIcon($icon, $color), 156 + $viewer->renderHandle($command->getAuthorPHID()), 157 + $command->getCommand(), 158 + phabricator_datetime($command->getDateCreated(), $viewer), 159 + ); 160 + } 161 + 162 + $table = id(new AphrontTableView($rows)) 163 + ->setHeaders( 164 + array( 165 + pht('ID'), 166 + null, 167 + pht('Actor'), 168 + pht('Command'), 169 + pht('Date'), 170 + )) 171 + ->setColumnClasses( 172 + array( 173 + null, 174 + 'icon', 175 + null, 176 + 'pri', 177 + 'wide right', 178 + )); 179 + 180 + return id(new PHUIObjectBoxView()) 181 + ->setHeaderText(pht('Pending Commands')) 182 + ->setBackground(PHUIObjectBoxView::BLUE_PROPERTY) 183 + ->setTable($table); 121 184 } 122 185 123 186 }
+4
src/applications/nuance/item/NuanceFormItemType.php
··· 47 47 ); 48 48 } 49 49 50 + protected function handleAction(NuanceItem $item, $action) { 51 + return null; 52 + } 53 + 50 54 }
+1 -7
src/applications/nuance/item/NuanceItemType.php
··· 95 95 } 96 96 97 97 final public function buildActionResponse(NuanceItem $item, $action) { 98 - $response = $this->handleAction($item, $action); 99 - 100 - if ($response === null) { 101 - return new Aphront404Response(); 102 - } 103 - 104 - return $response; 98 + return $this->handleAction($item, $action); 105 99 } 106 100 107 101 protected function handleAction(NuanceItem $item, $action) {
+13
src/applications/nuance/query/NuanceItemCommandQuery.php
··· 5 5 6 6 private $ids; 7 7 private $itemPHIDs; 8 + private $statuses; 8 9 9 10 public function withIDs(array $ids) { 10 11 $this->ids = $ids; ··· 13 14 14 15 public function withItemPHIDs(array $item_phids) { 15 16 $this->itemPHIDs = $item_phids; 17 + return $this; 18 + } 19 + 20 + public function withStatuses(array $statuses) { 21 + $this->statuses = $statuses; 16 22 return $this; 17 23 } 18 24 ··· 39 45 $conn, 40 46 'itemPHID IN (%Ls)', 41 47 $this->itemPHIDs); 48 + } 49 + 50 + if ($this->statuses !== null) { 51 + $where[] = qsprintf( 52 + $conn, 53 + 'status IN (%Ls)', 54 + $this->statuses); 42 55 } 43 56 44 57 return $where;
+59 -5
src/applications/nuance/storage/NuanceItemCommand.php
··· 4 4 extends NuanceDAO 5 5 implements PhabricatorPolicyInterface { 6 6 7 + const STATUS_ISSUED = 'issued'; 8 + const STATUS_EXECUTING = 'executing'; 9 + const STATUS_DONE = 'done'; 10 + const STATUS_FAILED = 'failed'; 11 + 7 12 protected $itemPHID; 8 13 protected $authorPHID; 14 + protected $queuePHID; 9 15 protected $command; 10 - protected $parameters; 16 + protected $status; 17 + protected $parameters = array(); 11 18 12 19 public static function initializeNewCommand() { 13 - return new self(); 20 + return id(new self()) 21 + ->setStatus(self::STATUS_ISSUED); 14 22 } 15 23 16 24 protected function getConfiguration() { 17 25 return array( 18 - self::CONFIG_TIMESTAMPS => false, 19 26 self::CONFIG_SERIALIZATION => array( 20 27 'parameters' => self::SERIALIZATION_JSON, 21 28 ), 22 29 self::CONFIG_COLUMN_SCHEMA => array( 23 30 'command' => 'text64', 31 + 'status' => 'text64', 32 + 'queuePHID' => 'phid?', 24 33 ), 25 34 self::CONFIG_KEY_SCHEMA => array( 26 - 'key_item' => array( 27 - 'columns' => array('itemPHID'), 35 + 'key_pending' => array( 36 + 'columns' => array('itemPHID', 'status'), 28 37 ), 29 38 ), 30 39 ) + parent::getConfiguration(); 40 + } 41 + 42 + public static function getStatusMap() { 43 + return array( 44 + self::STATUS_ISSUED => array( 45 + 'name' => pht('Issued'), 46 + 'icon' => 'fa-clock-o', 47 + 'color' => 'bluegrey', 48 + ), 49 + self::STATUS_EXECUTING => array( 50 + 'name' => pht('Executing'), 51 + 'icon' => 'fa-play', 52 + 'color' => 'green', 53 + ), 54 + self::STATUS_DONE => array( 55 + 'name' => pht('Done'), 56 + 'icon' => 'fa-check', 57 + 'color' => 'blue', 58 + ), 59 + self::STATUS_FAILED => array( 60 + 'name' => pht('Failed'), 61 + 'icon' => 'fa-times', 62 + 'color' => 'red', 63 + ), 64 + ); 65 + } 66 + 67 + private function getStatusSpec() { 68 + $map = self::getStatusMap(); 69 + return idx($map, $this->getStatus(), array()); 70 + } 71 + 72 + public function getStatusIcon() { 73 + $spec = $this->getStatusSpec(); 74 + return idx($spec, 'icon', 'fa-question'); 75 + } 76 + 77 + public function getStatusColor() { 78 + $spec = $this->getStatusSpec(); 79 + return idx($spec, 'color', 'indigo'); 80 + } 81 + 82 + public function getStatusName() { 83 + $spec = $this->getStatusSpec(); 84 + return idx($spec, 'name', $this->getStatus()); 31 85 } 32 86 33 87
+4
src/applications/nuance/storage/NuanceQueue.php
··· 43 43 return '/nuance/queue/view/'.$this->getID().'/'; 44 44 } 45 45 46 + public function getWorkURI() { 47 + return '/nuance/queue/work/'.$this->getID().'/'; 48 + } 49 + 46 50 47 51 /* -( PhabricatorPolicyInterface )----------------------------------------- */ 48 52
+21 -2
src/applications/nuance/worker/NuanceItemUpdateWorker.php
··· 61 61 $commands = id(new NuanceItemCommandQuery()) 62 62 ->setViewer($viewer) 63 63 ->withItemPHIDs(array($item->getPHID())) 64 + ->withStatuses( 65 + array( 66 + NuanceItemCommand::STATUS_ISSUED, 67 + )) 64 68 ->execute(); 65 69 $commands = msort($commands, 'getID'); 66 70 67 71 foreach ($commands as $command) { 68 - $impl->applyCommand($item, $command); 69 - $command->delete(); 72 + $command 73 + ->setStatus(NuanceItemCommand::STATUS_EXECUTING) 74 + ->save(); 75 + 76 + try { 77 + $impl->applyCommand($item, $command); 78 + 79 + $command 80 + ->setStatus(NuanceItemCommand::STATUS_DONE) 81 + ->save(); 82 + } catch (Exception $ex) { 83 + $command 84 + ->setStatus(NuanceItemCommand::STATUS_FAILED) 85 + ->save(); 86 + 87 + throw $ex; 88 + } 70 89 } 71 90 } 72 91