Correct Aphlict websocket URI construction after PHP8 compatibility changes
[phabricator.git] / src / applications / nuance / controller / NuanceItemActionController.php
blob39ae8fd995d7634cbdaffde038a282a0ed017e15
1 <?php
3 final class NuanceItemActionController extends NuanceController {
5 public function handleRequest(AphrontRequest $request) {
6 $viewer = $this->getViewer();
7 $id = $request->getURIData('id');
9 if (!$request->validateCSRF()) {
10 return new Aphront400Response();
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.
17 $item = id(new NuanceItemQuery())
18 ->setViewer($viewer)
19 ->withIDs(array($id))
20 ->executeOne();
21 if (!$item) {
22 return new Aphront404Response();
25 $cancel_uri = $item->getURI();
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();
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);
50 $action = $request->getURIData('action');
52 $impl = $item->getImplementation();
53 $impl->setViewer($viewer);
54 $impl->setController($this);
56 $executors = NuanceCommandImplementation::getAllCommands();
57 $executor = idx($executors, $action);
58 if (!$executor) {
59 return new Aphront404Response();
62 $executor = id(clone $executor)
63 ->setActor($viewer);
65 if (!$executor->canApplyToItem($item)) {
66 return $this->newDialog()
67 ->setTitle(pht('Command Not Supported'))
68 ->appendParagraph(
69 pht(
70 'This item does not support the specified command ("%s").',
71 $action))
72 ->addCancelButton($cancel_uri);
75 $command = NuanceItemCommand::initializeNewCommand()
76 ->setItemPHID($item->getPHID())
77 ->setAuthorPHID($viewer->getPHID())
78 ->setCommand($action);
80 if ($queue) {
81 $command->setQueuePHID($queue->getPHID());
84 $command->save();
86 // If this command can be applied immediately, try to apply it now.
88 // In most cases, local commands (like closing an item) can be applied
89 // immediately.
91 // Commands that require making a call to a remote system (for example,
92 // to reply to a tweet or close a remote object) are usually done in the
93 // background so the user doesn't have to wait for the operation to
94 // complete before they can continue work.
96 $did_apply = false;
97 $immediate = $executor->canApplyImmediately($item, $command);
98 if ($immediate) {
99 // TODO: Move this stuff to a new Engine, and have the controller and
100 // worker both call into the Engine.
101 $worker = new NuanceItemUpdateWorker(array());
102 $did_apply = $worker->executeCommands($item, array($command));
105 // If this can't be applied immediately or we were unable to get a lock
106 // fast enough, do the update in the background instead.
107 if (!$did_apply) {
108 $item->scheduleUpdate();
111 if ($queue) {
112 $done_uri = $queue->getWorkURI();
113 } else {
114 $done_uri = $item->getURI();
117 return id(new AphrontRedirectResponse())
118 ->setURI($done_uri);