1
0
Fork 0
mirror of https://we.phorge.it/source/phorge.git synced 2025-02-02 09:58:24 +01:00

Conpherence - add createthread method

Summary: Ref T3166. I moved the create logic into a static method in the editor class to keep things tidy.

Test Plan: created a conpherence from UI. purdy. tried errors and got UI to show "required". for conduit, created a thread with all the bells and whistles and it worked. verified i got proper exceptions with bum conduit calls

Reviewers: epriestley

Reviewed By: epriestley

CC: chad, aran, Korvin

Maniphest Tasks: T3166

Differential Revision: https://secure.phabricator.com/D6083
This commit is contained in:
Bob Trahan 2013-05-30 16:37:51 -07:00
parent 01a6d580ac
commit 4295de508f
8 changed files with 186 additions and 64 deletions

View file

@ -118,6 +118,8 @@ phutil_register_library_map(array(
'ConduitAPI_conduit_getcertificate_Method' => 'applications/conduit/method/ConduitAPI_conduit_getcertificate_Method.php', 'ConduitAPI_conduit_getcertificate_Method' => 'applications/conduit/method/ConduitAPI_conduit_getcertificate_Method.php',
'ConduitAPI_conduit_ping_Method' => 'applications/conduit/method/ConduitAPI_conduit_ping_Method.php', 'ConduitAPI_conduit_ping_Method' => 'applications/conduit/method/ConduitAPI_conduit_ping_Method.php',
'ConduitAPI_conduit_query_Method' => 'applications/conduit/method/ConduitAPI_conduit_query_Method.php', 'ConduitAPI_conduit_query_Method' => 'applications/conduit/method/ConduitAPI_conduit_query_Method.php',
'ConduitAPI_conpherence_Method' => 'applications/conpherence/conduit/ConduitAPI_conpherence_Method.php',
'ConduitAPI_conpherence_createthread_Method' => 'applications/conpherence/conduit/ConduitAPI_conpherence_createthread_Method.php',
'ConduitAPI_daemon_launched_Method' => 'applications/daemon/conduit/ConduitAPI_daemon_launched_Method.php', 'ConduitAPI_daemon_launched_Method' => 'applications/daemon/conduit/ConduitAPI_daemon_launched_Method.php',
'ConduitAPI_daemon_log_Method' => 'applications/daemon/conduit/ConduitAPI_daemon_log_Method.php', 'ConduitAPI_daemon_log_Method' => 'applications/daemon/conduit/ConduitAPI_daemon_log_Method.php',
'ConduitAPI_daemon_setstatus_Method' => 'applications/daemon/conduit/ConduitAPI_daemon_setstatus_Method.php', 'ConduitAPI_daemon_setstatus_Method' => 'applications/daemon/conduit/ConduitAPI_daemon_setstatus_Method.php',
@ -1947,6 +1949,8 @@ phutil_register_library_map(array(
'ConduitAPI_conduit_getcertificate_Method' => 'ConduitAPIMethod', 'ConduitAPI_conduit_getcertificate_Method' => 'ConduitAPIMethod',
'ConduitAPI_conduit_ping_Method' => 'ConduitAPIMethod', 'ConduitAPI_conduit_ping_Method' => 'ConduitAPIMethod',
'ConduitAPI_conduit_query_Method' => 'ConduitAPIMethod', 'ConduitAPI_conduit_query_Method' => 'ConduitAPIMethod',
'ConduitAPI_conpherence_Method' => 'ConduitAPIMethod',
'ConduitAPI_conpherence_createthread_Method' => 'ConduitAPI_conpherence_Method',
'ConduitAPI_daemon_launched_Method' => 'ConduitAPIMethod', 'ConduitAPI_daemon_launched_Method' => 'ConduitAPIMethod',
'ConduitAPI_daemon_log_Method' => 'ConduitAPIMethod', 'ConduitAPI_daemon_log_Method' => 'ConduitAPIMethod',
'ConduitAPI_daemon_setstatus_Method' => 'ConduitAPIMethod', 'ConduitAPI_daemon_setstatus_Method' => 'ConduitAPIMethod',

View file

@ -98,6 +98,10 @@ abstract class PhabricatorApplication {
return null; return null;
} }
public function getApplicationURI($path = '') {
return $this->getBaseURI().ltrim($path, '/');
}
public function getIconURI() { public function getIconURI() {
return null; return null;
} }

View file

@ -141,7 +141,7 @@ abstract class PhabricatorController extends AphrontController {
if (!$this->getCurrentApplication()) { if (!$this->getCurrentApplication()) {
throw new Exception("No application!"); throw new Exception("No application!");
} }
return $this->getCurrentApplication()->getBaseURI().ltrim($path, '/'); return $this->getCurrentApplication()->getApplicationURI($path);
} }
public function buildApplicationPage($view, array $options) { public function buildApplicationPage($view, array $options) {

View file

@ -0,0 +1,14 @@
<?php
/**
* @group conduit
*/
abstract class ConduitAPI_conpherence_Method
extends ConduitAPIMethod {
public function getApplication() {
return PhabricatorApplication::getByClass(
'PhabricatorApplicationConpherence');
}
}

View file

@ -0,0 +1,65 @@
<?php
/**
* @group conduit
*/
final class ConduitAPI_conpherence_createthread_Method
extends ConduitAPI_conpherence_Method {
public function getMethodDescription() {
}
public function defineParamTypes() {
return array(
'title' => 'optional string',
'message' => 'required string',
'participantPHIDs' => 'required list<phids>'
);
}
public function defineReturnType() {
return 'nonempty dict';
}
public function defineErrorTypes() {
return array(
'ERR_EMPTY_PARTICIPANT_PHIDS' => 'You must specify participant phids.',
'ERR_EMPTY_MESSAGE' => 'You must specify a message.'
);
}
protected function execute(ConduitAPIRequest $request) {
$participant_phids = $request->getValue('participantPHIDs', array());
$message = $request->getValue('message');
$title = $request->getValue('title');
list($errors, $conpherence) = ConpherenceEditor::createConpherence(
$request->getUser(),
$participant_phids,
$title,
$message,
PhabricatorContentSource::newFromConduitRequest($request));
if ($errors) {
foreach ($errors as $error_code) {
switch ($error_code) {
case ConpherenceEditor::ERROR_EMPTY_MESSAGE:
throw new ConduitException('ERR_EMPTY_MESSAGE');
break;
case ConpherenceEditor::ERROR_EMPTY_PARTICIPANTS:
throw new ConduitException('ERR_EMPTY_PARTICIPANT_PHIDS');
break;
}
}
}
$id = $conpherence->getID();
$uri = $this->getApplication()->getApplicationURI($id);
return array(
'conpherenceID' => $id,
'conpherencePHID' => $conpherence->getPHID(),
'conpherenceURI' => $uri);
}
}

View file

@ -9,15 +9,9 @@ final class ConpherenceNewController extends ConpherenceController {
$request = $this->getRequest(); $request = $this->getRequest();
$user = $request->getUser(); $user = $request->getUser();
$conpherence = id(new ConpherenceThread())
->attachParticipants(array())
->attachFilePHIDs(array())
->setMessageCount(0);
$title = pht('New Message'); $title = pht('New Message');
$participants = array(); $participants = array();
$message = ''; $message = '';
$files = array();
$errors = array();
$e_participants = null; $e_participants = null;
$e_message = null; $e_message = null;
@ -29,72 +23,32 @@ final class ConpherenceNewController extends ConpherenceController {
if ($request->isFormPost()) { if ($request->isFormPost()) {
$participants = $request->getArr('participants'); $participants = $request->getArr('participants');
if (empty($participants)) {
$e_participants = true;
$errors[] = pht('You must specify participants.');
} else {
$participants[] = $user->getPHID();
$participants = array_unique($participants);
$conpherence->setRecentParticipantPHIDs(
array_slice($participants, 0, 10));
}
$message = $request->getStr('message'); $message = $request->getStr('message');
if (empty($message)) { list($error_codes, $conpherence) = ConpherenceEditor::createConpherence(
$e_message = true; $user,
$errors[] = pht('You must write a message.'); $participants,
} $conpherence_title = null,
$message,
PhabricatorContentSource::newFromRequest($request));
$file_phids = if ($error_codes) {
PhabricatorMarkupEngine::extractFilePHIDsFromEmbeddedFiles( foreach ($error_codes as $error_code) {
array($message)); switch ($error_code) {
if ($file_phids) { case ConpherenceEditor::ERROR_EMPTY_MESSAGE:
$files = id(new PhabricatorFileQuery()) $e_message = true;
->setViewer($user) break;
->withPHIDs($file_phids) case ConpherenceEditor::ERROR_EMPTY_PARTICIPANTS:
->execute(); $e_participants = true;
} break;
}
if (!$errors) {
$conpherence->openTransaction();
$conpherence->save();
$xactions = array();
$xactions[] = id(new ConpherenceTransaction())
->setTransactionType(ConpherenceTransactionType::TYPE_PARTICIPANTS)
->setNewValue(array('+' => $participants));
if ($files) {
$xactions[] = id(new ConpherenceTransaction())
->setTransactionType(ConpherenceTransactionType::TYPE_FILES)
->setNewValue(array('+' => mpull($files, 'getPHID')));
} }
$xactions[] = id(new ConpherenceTransaction()) } else {
->setTransactionType(PhabricatorTransactions::TYPE_COMMENT)
->attachComment(
id(new ConpherenceTransactionComment())
->setContent($message)
->setConpherencePHID($conpherence->getPHID()));
id(new ConpherenceEditor())
->setContentSourceFromRequest($request)
->setContinueOnNoEffect(true)
->setActor($user)
->applyTransactions($conpherence, $xactions);
$conpherence->saveTransaction();
$uri = $this->getApplicationURI($conpherence->getID()); $uri = $this->getApplicationURI($conpherence->getID());
return id(new AphrontRedirectResponse()) return id(new AphrontRedirectResponse())
->setURI($uri); ->setURI($uri);
} }
} }
$error_view = null;
if ($errors) {
$error_view = id(new AphrontErrorView())
->setTitle(pht('Conpherence Errors'))
->setErrors($errors);
}
$participant_handles = array(); $participant_handles = array();
if ($participants) { if ($participants) {
$handles = id(new PhabricatorObjectHandleData($participants)) $handles = id(new PhabricatorObjectHandleData($participants))

View file

@ -5,6 +5,81 @@
*/ */
final class ConpherenceEditor extends PhabricatorApplicationTransactionEditor { final class ConpherenceEditor extends PhabricatorApplicationTransactionEditor {
const ERROR_EMPTY_PARTICIPANTS = 'error-empty-participants';
const ERROR_EMPTY_MESSAGE = 'error-empty-message';
public static function createConpherence(
PhabricatorUser $creator,
array $participant_phids,
$title,
$message,
PhabricatorContentSource $source) {
$conpherence = id(new ConpherenceThread())
->attachParticipants(array())
->attachFilePHIDs(array())
->setMessageCount(0);
$files = array();
$errors = array();
if (empty($participant_phids)) {
$errors[] = self::ERROR_EMPTY_PARTICIPANTS;
} else {
$participant_phids[] = $creator->getPHID();
$participant_phids = array_unique($participant_phids);
$conpherence->setRecentParticipantPHIDs(
array_slice($participant_phids, 0, 10));
}
if (empty($message)) {
$errors[] = self::ERROR_EMPTY_MESSAGE;
}
$file_phids =
PhabricatorMarkupEngine::extractFilePHIDsFromEmbeddedFiles(
array($message));
if ($file_phids) {
$files = id(new PhabricatorFileQuery())
->setViewer($creator)
->withPHIDs($file_phids)
->execute();
}
if (!$errors) {
$conpherence->openTransaction();
$conpherence->save();
$xactions = array();
$xactions[] = id(new ConpherenceTransaction())
->setTransactionType(ConpherenceTransactionType::TYPE_PARTICIPANTS)
->setNewValue(array('+' => $participant_phids));
if ($files) {
$xactions[] = id(new ConpherenceTransaction())
->setTransactionType(ConpherenceTransactionType::TYPE_FILES)
->setNewValue(array('+' => mpull($files, 'getPHID')));
}
if ($title) {
$xactions[] = id(new ConpherenceTransaction())
->setTransactionType(ConpherenceTransactionType::TYPE_TITLE)
->setNewValue($title);
}
$xactions[] = id(new ConpherenceTransaction())
->setTransactionType(PhabricatorTransactions::TYPE_COMMENT)
->attachComment(
id(new ConpherenceTransactionComment())
->setContent($message)
->setConpherencePHID($conpherence->getPHID()));
id(new ConpherenceEditor())
->setContentSource($source)
->setContinueOnNoEffect(true)
->setActor($creator)
->applyTransactions($conpherence, $xactions);
$conpherence->saveTransaction();
}
return array($errors, $conpherence);
}
public function generateTransactionsFromText( public function generateTransactionsFromText(
ConpherenceThread $conpherence, ConpherenceThread $conpherence,
$text) { $text) {

View file

@ -46,6 +46,12 @@ final class PhabricatorContentSource {
)); ));
} }
public static function newFromConduitRequest(ConduitAPIRequest $request) {
return self::newForSource(
PhabricatorContentSource::SOURCE_CONDUIT,
array());
}
public function serialize() { public function serialize() {
return json_encode(array( return json_encode(array(
'source' => $this->getSource(), 'source' => $this->getSource(),