mirror of
https://we.phorge.it/source/phorge.git
synced 2024-12-23 22:10:55 +01:00
Make the paste "Create" transaction take a file PHID instead of content
Summary: Ref T4814. Although this approach made sense at one point, we have more file infrastructure now and T4814 will be easier if we just pass a PHID in. Also swap Conduit over to use the Editor. Test Plan: - Created a paste. - Created a paste via Conduit. - Verified that files had correct permissions and appropriate object links in Files. Reviewers: btrahan Reviewed By: btrahan Subscribers: epriestley Maniphest Tasks: T4814 Differential Revision: https://secure.phabricator.com/D8969
This commit is contained in:
parent
a272ddcc78
commit
c2f58496ad
5 changed files with 92 additions and 90 deletions
|
@ -39,28 +39,36 @@ final class ConduitAPI_paste_create_Method extends ConduitAPI_paste_Method {
|
||||||
$title = nonempty($title, 'Masterwork From Distant Lands');
|
$title = nonempty($title, 'Masterwork From Distant Lands');
|
||||||
$language = nonempty($language, '');
|
$language = nonempty($language, '');
|
||||||
|
|
||||||
$user = $request->getUser();
|
$viewer = $request->getUser();
|
||||||
|
|
||||||
$paste_file = PhabricatorFile::newFromFileData(
|
$paste = PhabricatorPaste::initializeNewPaste($viewer);
|
||||||
$content,
|
|
||||||
array(
|
|
||||||
'name' => $title,
|
|
||||||
'mime-type' => 'text/plain; charset=utf-8',
|
|
||||||
'authorPHID' => $user->getPHID(),
|
|
||||||
));
|
|
||||||
|
|
||||||
// TODO: This should use PhabricatorPasteEditor.
|
$file = PhabricatorPasteEditor::initializeFileForPaste(
|
||||||
|
$viewer,
|
||||||
|
$title,
|
||||||
|
$content);
|
||||||
|
|
||||||
$paste = PhabricatorPaste::initializeNewPaste($user);
|
$xactions = array();
|
||||||
$paste->setTitle($title);
|
|
||||||
$paste->setLanguage($language);
|
|
||||||
$paste->setFilePHID($paste_file->getPHID());
|
|
||||||
$paste->save();
|
|
||||||
|
|
||||||
$paste_file->attachToObject($user, $paste->getPHID());
|
$xactions[] = id(new PhabricatorPasteTransaction())
|
||||||
|
->setTransactionType(PhabricatorPasteTransaction::TYPE_CREATE)
|
||||||
|
->setNewValue($file->getPHID());
|
||||||
|
|
||||||
|
$xactions[] = id(new PhabricatorPasteTransaction())
|
||||||
|
->setTransactionType(PhabricatorPasteTransaction::TYPE_TITLE)
|
||||||
|
->setNewValue($title);
|
||||||
|
|
||||||
|
$xactions[] = id(new PhabricatorPasteTransaction())
|
||||||
|
->setTransactionType(PhabricatorPasteTransaction::TYPE_LANGUAGE)
|
||||||
|
->setNewValue($language);
|
||||||
|
|
||||||
|
$editor = id(new PhabricatorPasteEditor())
|
||||||
|
->setActor($viewer)
|
||||||
|
->setContentSourceFromConduitRequest($request);
|
||||||
|
|
||||||
|
$xactions = $editor->applyTransactions($paste, $xactions);
|
||||||
|
|
||||||
$paste->attachRawContent($content);
|
$paste->attachRawContent($content);
|
||||||
|
|
||||||
return $this->buildPasteInfoDictionary($paste);
|
return $this->buildPasteInfoDictionary($paste);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -95,12 +95,16 @@ final class PhabricatorPasteEditController extends PhabricatorPasteController {
|
||||||
|
|
||||||
if (!$errors) {
|
if (!$errors) {
|
||||||
if ($is_create) {
|
if ($is_create) {
|
||||||
|
$file = PhabricatorPasteEditor::initializeFileForPaste(
|
||||||
|
$user,
|
||||||
|
$v_title,
|
||||||
|
$v_text);
|
||||||
|
|
||||||
$xactions[] = id(new PhabricatorPasteTransaction())
|
$xactions[] = id(new PhabricatorPasteTransaction())
|
||||||
->setTransactionType(PhabricatorPasteTransaction::TYPE_CREATE)
|
->setTransactionType(PhabricatorPasteTransaction::TYPE_CREATE)
|
||||||
->setNewValue(array(
|
->setNewValue($file->getPHID());
|
||||||
'title' => $v_title,
|
|
||||||
'text' => $v_text));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$xactions[] = id(new PhabricatorPasteTransaction())
|
$xactions[] = id(new PhabricatorPasteTransaction())
|
||||||
->setTransactionType(PhabricatorPasteTransaction::TYPE_TITLE)
|
->setTransactionType(PhabricatorPasteTransaction::TYPE_TITLE)
|
||||||
->setNewValue($v_title);
|
->setNewValue($v_title);
|
||||||
|
|
|
@ -171,6 +171,13 @@ final class PhabricatorPasteViewController extends PhabricatorPasteController {
|
||||||
->setUser($user)
|
->setUser($user)
|
||||||
->setObject($paste)
|
->setObject($paste)
|
||||||
->setObjectURI($this->getRequest()->getRequestURI())
|
->setObjectURI($this->getRequest()->getRequestURI())
|
||||||
|
->addAction(
|
||||||
|
id(new PhabricatorActionView())
|
||||||
|
->setName(pht('Edit Paste'))
|
||||||
|
->setIcon('edit')
|
||||||
|
->setDisabled(!$can_edit)
|
||||||
|
->setWorkflow(!$can_edit)
|
||||||
|
->setHref($this->getApplicationURI('/edit/'.$paste->getID().'/')))
|
||||||
->addAction(
|
->addAction(
|
||||||
id(new PhabricatorActionView())
|
id(new PhabricatorActionView())
|
||||||
->setName(pht('Fork This Paste'))
|
->setName(pht('Fork This Paste'))
|
||||||
|
@ -182,14 +189,7 @@ final class PhabricatorPasteViewController extends PhabricatorPasteController {
|
||||||
id(new PhabricatorActionView())
|
id(new PhabricatorActionView())
|
||||||
->setName(pht('View Raw File'))
|
->setName(pht('View Raw File'))
|
||||||
->setIcon('file')
|
->setIcon('file')
|
||||||
->setHref($file->getBestURI()))
|
->setHref($file->getBestURI()));
|
||||||
->addAction(
|
|
||||||
id(new PhabricatorActionView())
|
|
||||||
->setName(pht('Edit Paste'))
|
|
||||||
->setIcon('edit')
|
|
||||||
->setDisabled(!$can_edit)
|
|
||||||
->setWorkflow(!$can_edit)
|
|
||||||
->setHref($this->getApplicationURI('/edit/'.$paste->getID().'/')));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private function buildPropertyView(
|
private function buildPropertyView(
|
||||||
|
|
|
@ -5,6 +5,21 @@ final class PhabricatorPasteEditor
|
||||||
|
|
||||||
private $pasteFile;
|
private $pasteFile;
|
||||||
|
|
||||||
|
public static function initializeFileForPaste(
|
||||||
|
PhabricatorUser $actor,
|
||||||
|
$name,
|
||||||
|
$data) {
|
||||||
|
|
||||||
|
return PhabricatorFile::newFromFileData(
|
||||||
|
$data,
|
||||||
|
array(
|
||||||
|
'name' => $name,
|
||||||
|
'mime-type' => 'text/plain; charset=utf-8',
|
||||||
|
'authorPHID' => $actor->getPHID(),
|
||||||
|
'viewPolicy' => PhabricatorPolicies::POLICY_NOONE,
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
public function getTransactionTypes() {
|
public function getTransactionTypes() {
|
||||||
$types = parent::getTransactionTypes();
|
$types = parent::getTransactionTypes();
|
||||||
|
|
||||||
|
@ -37,8 +52,6 @@ final class PhabricatorPasteEditor
|
||||||
|
|
||||||
switch ($xaction->getTransactionType()) {
|
switch ($xaction->getTransactionType()) {
|
||||||
case PhabricatorPasteTransaction::TYPE_CREATE:
|
case PhabricatorPasteTransaction::TYPE_CREATE:
|
||||||
// this was set via applyInitialEffects
|
|
||||||
return $object->getFilePHID();
|
|
||||||
case PhabricatorPasteTransaction::TYPE_TITLE:
|
case PhabricatorPasteTransaction::TYPE_TITLE:
|
||||||
case PhabricatorPasteTransaction::TYPE_LANGUAGE:
|
case PhabricatorPasteTransaction::TYPE_LANGUAGE:
|
||||||
return $xaction->getNewValue();
|
return $xaction->getNewValue();
|
||||||
|
@ -50,74 +63,46 @@ final class PhabricatorPasteEditor
|
||||||
PhabricatorApplicationTransaction $xaction) {
|
PhabricatorApplicationTransaction $xaction) {
|
||||||
|
|
||||||
switch ($xaction->getTransactionType()) {
|
switch ($xaction->getTransactionType()) {
|
||||||
|
case PhabricatorPasteTransaction::TYPE_CREATE:
|
||||||
|
$object->setFilePHID($xaction->getNewValue());
|
||||||
|
return;
|
||||||
case PhabricatorPasteTransaction::TYPE_TITLE:
|
case PhabricatorPasteTransaction::TYPE_TITLE:
|
||||||
$object->setTitle($xaction->getNewValue());
|
$object->setTitle($xaction->getNewValue());
|
||||||
break;
|
return;
|
||||||
case PhabricatorPasteTransaction::TYPE_LANGUAGE:
|
case PhabricatorPasteTransaction::TYPE_LANGUAGE:
|
||||||
$object->setLanguage($xaction->getNewValue());
|
$object->setLanguage($xaction->getNewValue());
|
||||||
break;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return parent::applyCustomInternalTransaction($object, $xaction);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function applyCustomExternalTransaction(
|
protected function applyCustomExternalTransaction(
|
||||||
PhabricatorLiskDAO $object,
|
PhabricatorLiskDAO $object,
|
||||||
PhabricatorApplicationTransaction $xaction) {
|
PhabricatorApplicationTransaction $xaction) {
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
protected function shouldApplyInitialEffects(
|
|
||||||
PhabricatorLiskDAO $object,
|
|
||||||
array $xactions) {
|
|
||||||
|
|
||||||
foreach ($xactions as $xaction) {
|
|
||||||
if ($xaction->getTransactionType() ==
|
|
||||||
PhabricatorPasteTransaction::TYPE_CREATE) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
protected function applyInitialEffects(
|
|
||||||
PhabricatorLiskDAO $object,
|
|
||||||
array $xactions) {
|
|
||||||
|
|
||||||
foreach ($xactions as $xaction) {
|
|
||||||
switch ($xaction->getTransactionType()) {
|
switch ($xaction->getTransactionType()) {
|
||||||
case PhabricatorPasteTransaction::TYPE_CREATE:
|
case PhabricatorPasteTransaction::TYPE_CREATE:
|
||||||
$data = $xaction->getNewValue();
|
case PhabricatorPasteTransaction::TYPE_TITLE:
|
||||||
$paste_file = PhabricatorFile::newFromFileData(
|
case PhabricatorPasteTransaction::TYPE_LANGUAGE:
|
||||||
$data['text'],
|
return;
|
||||||
array(
|
|
||||||
'name' => $data['title'],
|
|
||||||
'mime-type' => 'text/plain; charset=utf-8',
|
|
||||||
'authorPHID' => $this->getActor()->getPHID(),
|
|
||||||
));
|
|
||||||
$object->setFilePHID($paste_file->getPHID());
|
|
||||||
|
|
||||||
$this->pasteFile = $paste_file;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function applyFinalEffects(
|
return parent::applyCustomExternalTransaction($object, $xaction);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function extractFilePHIDsFromCustomTransaction(
|
||||||
PhabricatorLiskDAO $object,
|
PhabricatorLiskDAO $object,
|
||||||
array $xactions) {
|
PhabricatorApplicationTransaction $xaction) {
|
||||||
|
|
||||||
// TODO: This should use extractFilePHIDs() instead, but the way
|
switch ($xaction->getTransactionType()) {
|
||||||
// the transactions work right now makes pretty messy.
|
case PhabricatorPasteTransaction::TYPE_CREATE:
|
||||||
|
return array($xaction->getNewValue());
|
||||||
if ($this->pasteFile) {
|
|
||||||
$this->pasteFile->attachToObject(
|
|
||||||
$this->getActor(),
|
|
||||||
$object->getPHID());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return $xactions;
|
return parent::extractFilePHIDsFromCustomTransaction($object, $xaction);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
protected function shouldSendMail(
|
protected function shouldSendMail(
|
||||||
PhabricatorLiskDAO $object,
|
PhabricatorLiskDAO $object,
|
||||||
array $xactions) {
|
array $xactions) {
|
||||||
|
|
|
@ -33,31 +33,36 @@ final class PasteCreateMailReceiver
|
||||||
|
|
||||||
$title = $mail->getSubject();
|
$title = $mail->getSubject();
|
||||||
if (!$title) {
|
if (!$title) {
|
||||||
$title = pht('Pasted via email.');
|
$title = pht('Email Paste');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$file = PhabricatorPasteEditor::initializeFileForPaste(
|
||||||
|
$sender,
|
||||||
|
$title,
|
||||||
|
$mail->getCleanTextBody());
|
||||||
|
|
||||||
$xactions = array();
|
$xactions = array();
|
||||||
|
|
||||||
$xactions[] = id(new PhabricatorPasteTransaction())
|
$xactions[] = id(new PhabricatorPasteTransaction())
|
||||||
->setTransactionType(PhabricatorPasteTransaction::TYPE_CREATE)
|
->setTransactionType(PhabricatorPasteTransaction::TYPE_CREATE)
|
||||||
->setNewValue(array(
|
->setNewValue($file->getPHID());
|
||||||
'title' => $title,
|
|
||||||
'text' => $mail->getCleanTextBody()));
|
|
||||||
$xactions[] = id(new PhabricatorPasteTransaction())
|
$xactions[] = id(new PhabricatorPasteTransaction())
|
||||||
->setTransactionType(PhabricatorPasteTransaction::TYPE_TITLE)
|
->setTransactionType(PhabricatorPasteTransaction::TYPE_TITLE)
|
||||||
->setNewValue($title);
|
->setNewValue($title);
|
||||||
|
|
||||||
$xactions[] = id(new PhabricatorPasteTransaction())
|
$xactions[] = id(new PhabricatorPasteTransaction())
|
||||||
->setTransactionType(PhabricatorPasteTransaction::TYPE_LANGUAGE)
|
->setTransactionType(PhabricatorPasteTransaction::TYPE_LANGUAGE)
|
||||||
->setNewValue(''); // auto-detect
|
->setNewValue(''); // auto-detect
|
||||||
$xactions[] = id(new PhabricatorPasteTransaction())
|
|
||||||
->setTransactionType(PhabricatorTransactions::TYPE_VIEW_POLICY)
|
|
||||||
->setNewValue(PhabricatorPolicies::POLICY_USER);
|
|
||||||
|
|
||||||
$paste = id(new PhabricatorPaste())
|
$paste = PhabricatorPaste::initializeNewPaste($sender);
|
||||||
->setAuthorPHID($sender->getPHID());
|
|
||||||
$content_source = PhabricatorContentSource::newForSource(
|
$content_source = PhabricatorContentSource::newForSource(
|
||||||
PhabricatorContentSource::SOURCE_EMAIL,
|
PhabricatorContentSource::SOURCE_EMAIL,
|
||||||
array(
|
array(
|
||||||
'id' => $mail->getID(),
|
'id' => $mail->getID(),
|
||||||
));
|
));
|
||||||
|
|
||||||
$editor = id(new PhabricatorPasteEditor())
|
$editor = id(new PhabricatorPasteEditor())
|
||||||
->setActor($sender)
|
->setActor($sender)
|
||||||
->setContentSource($content_source)
|
->setContentSource($content_source)
|
||||||
|
|
Loading…
Reference in a new issue