2012-10-05 22:18:05 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
final class PhabricatorSubscriptionsEditController
|
|
|
|
extends PhabricatorController {
|
|
|
|
|
|
|
|
private $phid;
|
|
|
|
private $action;
|
|
|
|
|
|
|
|
public function willProcessRequest(array $data) {
|
|
|
|
$this->phid = idx($data, 'phid');
|
|
|
|
$this->action = idx($data, 'action');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function processRequest() {
|
|
|
|
$request = $this->getRequest();
|
|
|
|
|
|
|
|
if (!$request->isFormPost()) {
|
|
|
|
return new Aphront400Response();
|
|
|
|
}
|
|
|
|
|
|
|
|
switch ($this->action) {
|
|
|
|
case 'add':
|
|
|
|
$is_add = true;
|
|
|
|
break;
|
|
|
|
case 'delete':
|
|
|
|
$is_add = false;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return new Aphront400Response();
|
|
|
|
}
|
|
|
|
|
|
|
|
$user = $request->getUser();
|
|
|
|
$phid = $this->phid;
|
|
|
|
|
2013-09-11 21:27:28 +02:00
|
|
|
$handle = id(new PhabricatorHandleQuery())
|
|
|
|
->setViewer($user)
|
|
|
|
->withPHIDs(array($phid))
|
|
|
|
->executeOne();
|
2012-10-05 22:18:05 +02:00
|
|
|
|
2013-09-11 21:27:28 +02:00
|
|
|
$object = id(new PhabricatorObjectQuery())
|
2012-11-22 02:38:57 +01:00
|
|
|
->setViewer($user)
|
2013-09-11 21:27:28 +02:00
|
|
|
->withPHIDs(array($phid))
|
|
|
|
->executeOne();
|
2012-10-05 22:18:05 +02:00
|
|
|
|
|
|
|
if (!($object instanceof PhabricatorSubscribableInterface)) {
|
|
|
|
return $this->buildErrorResponse(
|
|
|
|
pht('Bad Object'),
|
|
|
|
pht('This object is not subscribable.'),
|
|
|
|
$handle->getURI());
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($object->isAutomaticallySubscribed($user->getPHID())) {
|
|
|
|
return $this->buildErrorResponse(
|
|
|
|
pht('Automatically Subscribed'),
|
|
|
|
pht('You are automatically subscribed to this object.'),
|
|
|
|
$handle->getURI());
|
|
|
|
}
|
|
|
|
|
2013-02-17 15:37:09 +01:00
|
|
|
if ($object instanceof PhabricatorApplicationTransactionInterface) {
|
|
|
|
if ($is_add) {
|
|
|
|
$xaction_value = array(
|
|
|
|
'+' => array($user->getPHID()),
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
$xaction_value = array(
|
|
|
|
'-' => array($user->getPHID()),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
$xaction = id($object->getApplicationTransactionObject())
|
|
|
|
->setTransactionType(PhabricatorTransactions::TYPE_SUBSCRIBERS)
|
|
|
|
->setNewValue($xaction_value);
|
|
|
|
|
|
|
|
$editor = id($object->getApplicationTransactionEditor())
|
|
|
|
->setActor($user)
|
|
|
|
->setContinueOnNoEffect(true)
|
2013-05-24 19:48:34 +02:00
|
|
|
->setContentSourceFromRequest($request);
|
2013-02-17 15:37:09 +01:00
|
|
|
|
|
|
|
$editor->applyTransactions($object, array($xaction));
|
2012-10-05 22:18:05 +02:00
|
|
|
} else {
|
|
|
|
|
2013-02-17 15:37:09 +01:00
|
|
|
// TODO: Eventually, get rid of this once everything implements
|
|
|
|
// PhabriatorApplicationTransactionInterface.
|
|
|
|
|
|
|
|
$editor = id(new PhabricatorSubscriptionsEditor())
|
|
|
|
->setActor($user)
|
|
|
|
->setObject($object);
|
|
|
|
|
|
|
|
if ($is_add) {
|
|
|
|
$editor->subscribeExplicit(array($user->getPHID()), $explicit = true);
|
|
|
|
} else {
|
|
|
|
$editor->unsubscribe(array($user->getPHID()));
|
|
|
|
}
|
|
|
|
|
|
|
|
$editor->save();
|
|
|
|
}
|
2012-10-05 22:18:05 +02:00
|
|
|
|
|
|
|
// TODO: We should just render the "Unsubscribe" action and swap it out
|
|
|
|
// in the document for Ajax requests.
|
|
|
|
return id(new AphrontReloadResponse())->setURI($handle->getURI());
|
|
|
|
}
|
|
|
|
|
|
|
|
private function buildErrorResponse($title, $message, $uri) {
|
|
|
|
$request = $this->getRequest();
|
|
|
|
$user = $request->getUser();
|
|
|
|
|
|
|
|
$dialog = id(new AphrontDialogView())
|
|
|
|
->setUser($user)
|
|
|
|
->setTitle($title)
|
|
|
|
->appendChild($message)
|
|
|
|
->addCancelButton($uri);
|
|
|
|
|
|
|
|
return id(new AphrontDialogResponse())->setDialog($dialog);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|