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
|
|
|
|
2014-07-24 00:05:46 +02:00
|
|
|
if (phid_get_type($phid) == PhabricatorProjectProjectPHIDType::TYPECONST) {
|
Fix unsubscribing from projects in a gross, hacky way
Summary:
Fixes T5261.
This fix isn't very good. Two better fixes would be:
# Add some sort of `setRole(SUBSCRIPTIONS)` method to `ObjectQuery`, which gets passed down until it reaches `ProjectQuery`, and `ProjectQuery` knows that it needs to load more data. This feels OK, but is a very general approach and I don't think we have many/any other use cases right now. I //think// this is the right way in the long run, but I'd like to have more use cases in mind before implementing it.
# Add some sort of `loadAllTheSubscriptionStuffYouNeed()` method to `PhabricatorSubscribableInterface`. This feels OK-ish too, but kind of yuck, and doesn't lend itself to proper batching, and is silly if we do the above instead, which I think we probably will.
For now, just fix the issue without committing to an infrastructure direction. I think (1) is the right way to go eventually, but I want a better second use case before writing it, since I might be crazy.
Test Plan: Unsubscribed from a project.
Reviewers: joshuaspence
Reviewed By: joshuaspence
Subscribers: epriestley
Maniphest Tasks: T5261
Differential Revision: https://secure.phabricator.com/D9377
2014-06-05 00:17:19 +02:00
|
|
|
// 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
|
|
|
|
// argument for adding some kind of "load extra stuff" feature to
|
|
|
|
// SubscriberInterface. Do this for now and wait for the best way forward
|
|
|
|
// to become more clear?
|
|
|
|
|
|
|
|
$object = id(new PhabricatorProjectQuery())
|
|
|
|
->setViewer($user)
|
|
|
|
->withPHIDs(array($phid))
|
|
|
|
->needWatchers(true)
|
|
|
|
->executeOne();
|
|
|
|
} else {
|
|
|
|
$object = id(new PhabricatorObjectQuery())
|
|
|
|
->setViewer($user)
|
|
|
|
->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());
|
|
|
|
}
|
|
|
|
|
Make Projects a PhabricatorSubscribableInterface, but with restricted defaults
Summary:
Ref T4379. I want project subscriptions to work like this (yell if this seems whacky, since it makes subscriptions mean somethign a little different for projects than they do for other objects):
- You can only subscribe to a project if you're a project member.
- When you're added as a member, you're added as a subscriber.
- When you're removed as a member, you're removed as a subscriber.
- While you're a member, you can optionally unsubscribe.
From a UI perspective:
- We don't show the subscriber list, since it's going to be some uninteresting subset of the member list.
- We don't show CC transactions in history, since they're an uninteresting near-approximation of the membership transactions.
- You only see the subscription controls if you're a member.
To do this, I've augmented `PhabricatorSubscribableInterface` with two new methods. It would be nice if we were on PHP 5.4+ and could just use traits for this, but we should get data about version usage before we think about this. For now, copy/paste the default implementations into every implementing class.
Then, I implemented the interface in `PhabricatorProject` but with alternate defaults.
Test Plan:
- Used the normal interaction on existing objects.
- This has no actual effect on projects, verified no subscription stuff mysteriously appeared.
- Hit the new error case by fiddling with the UI.
Reviewers: btrahan
Reviewed By: btrahan
CC: chad, aran
Maniphest Tasks: T4379
Differential Revision: https://secure.phabricator.com/D8165
2014-02-10 23:29:17 +01:00
|
|
|
if (!$object->shouldAllowSubscription($user->getPHID())) {
|
|
|
|
return $this->buildErrorResponse(
|
|
|
|
pht('You Can Not Subscribe'),
|
|
|
|
pht('You can not subscribe 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()),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2014-04-18 01:03:24 +02:00
|
|
|
$xaction = id($object->getApplicationTransactionTemplate())
|
2013-02-17 15:37:09 +01:00
|
|
|
->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
|
|
|
|
2014-04-18 01:03:24 +02:00
|
|
|
$editor->applyTransactions(
|
|
|
|
$object->getApplicationTransactionObject(),
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|